Vol. 09: Digital Clock

Programming PIC microcontrollers, part 3.

+ Downloads & Extras:


Up to now most of the parts we have been using with our microcontroller have been discreet components like resistors and potentiometers. Peripheral devices are integrated circuits (ICs) like our microcontroller that pack all of the discreet components into a small chip to perform particular function. These chips come in a variety of package styles. We are going to prototype on a breadboard so we ordered our chips in a PDIP package.

Looking at the Pin Description for this chip we see that pins 1,12 and 13 are used for data and will connect to I/O pins on the PIC. Pins 4 and 9 go to ground. Pin 18 is a resistor to set the peak segment current for the LEDs which is calculated based on the forward voltage of the LEDs you choose. This resistor can also be used to limit the brightness of the LEDs. Pin 19 connects to +5volts. Pin 24 is dataout which we are not using. The remaining pins connect to the LED display. This chip is for use with common-cathode type 7-segment displays or common-cathode-row 8x8 or smaller LED matrix displays. Pins labeled DIG 0-7 sink current from each digit's cathodes while pins labeled SEG A-G, and DP source current to the individual LEDs in the digit. So the SEG A pin on the 7221 connects to the SEG A pin of every digit. That is a lot of wires on your breadboard! Fortunately we are only using 5 digits in our display. Now that we have our PIC, the 7221 and the display wired up and powered we can write some code to see if it works.

From the MAX7219/MAX7221 datasheet:Table 1. Serial-Data Format (16 Bits) 
D15 D14 D13 D12 D11 D10 D9  D8  D7  D6  D5  D4  D3  D2  D1  D0  
 X | X | X | X |   ADDRESS     |             DATA              |	

The 7221

Table 1 of the data sheet shows us the serial data format for the 7221. From this table we learn that communication takes place in 16 bit chunks and that the first 4 bits do not matter. The next 4 bits say which register we want to send the data to and the last 8 bits are the data to send. All of the information the chip needs to operate are stored in its’ registers. We control the chip by changing the data stored in the registers. Table 2 shows the register address map. Take for instance the Shutdown Mode register. According to the datasheet the 7221 begins in Shutdown Mode. We need to Set this to off in order for our chip to run. We will initialize our display registers when our program first runs by first making the 7221's latch pin low, sending the 16 bits on the data line and then setting the latch pin high. The code looks like this:

low s7221 'make the latch pin low
shiftout dout, dclock, 1, [%00001100,%00000001 ]
'the first byte selects the shutdown mode register. the next sets it to normal operation.
high s7221 'make the latch pin high

Other registers we set in the top of our code are:Intensity register - set to maximum. Scan Limit - we only use 5 digits. Decode mode - set to on for the first 4 digits. Test mode - set "on" then "off" 3 times with a pause so we see that the communication is working.

After initializing the 7221, writing information to the display is a simple matter. Each digit or row of LEDs has its own register. So to write to the first digit, we first send the address for that digit and then the binary version of the number we want to display. %00000001 for the first digit and %00000010 to show the number "2". This would have a different effect on the fifth digit for which we are not using the decode mode. This digit is treated as a row of 8 LEDs so sending %00000101 and %00000010 would light the 2nd LED in that row of LEDs. This is how you would use a matrix of LEDs. Already we have a clock that is correct twice a day. Next we need to add a way to keep track of time.

Drawing a picture to an 8x8 matrix of LEDs:

row7 = %01111110
row6 = %10000001
row5 = %10100101
row4 = %10000001
row3 = %10100101
row2 = %10111101
row1 = %10000001
row0 = %01111110

The graphical equivalent of "Hello, World!"

Watch Crytal

The DS1305 Real Time Clock will keep track of the year, date, day of the week, hour and seconds. All we need to make it run is a 32.768kHz watch crystal. We can access the registers to read and write the time. We can set two alarms and even keep information on the chip.

First we connect the chip to our circuit. Running through the pin description in the datasheet:

Pin 1: ground as we have no rechargeable battery.
Pin 2: to connects to a +9volt battery
Pin 3 & 4: the two leads of our watch crystal.
Pin 5-7: we will not use
Pin 8: ground
Pin 9: connects to power to tell the chip to use SPI for communication.
Pin 10: the latch pin
Pin 11: the data communication clock
Pin 12: data in from the microcontroller
Pin 13: data out to the microcontroller
Pin 14: connects to power
Pin 15: not used
Pin 16: Power

Communicating with the DS1305 is similar to with the 7221. Send the address you want to talk too and then send the data. First we must initialize the chip by first sending $8F which is the hex value of the control register. $8F is the same as %10001111 which is the same as 143. The datasheet lists the values in hex format so we will use those too. Immediately after sending the address we send the data which is %00000111. It is important to set the first two bits to 0. The rest do not matter now. Later I may want to use alarms with the clock so I set the last three bits to 1.

Development Environment

Development Environment.
  1. Write the code in a plain text editor or an integrated development environment (IDC). An IDC puts all the software you need for programming in one package. We used Microcode Studio available here. MPLAB is another IDC which is available from www.microchip.com.
  2. Compile the code with the PicBasicPro (PBP) compiler from www.melabs.com. After installing PBP and telling the IDC where to find it we have only to press Compile in the IDC. Compiling creates a hex file from the code we wrote.
  3. Program the microcontroller by loading the hex file created by the compiler into the software for your programmer. We used the PICkit 1 programmer available from Microchip. The PICkit 1 has been replaced by the PICkit 2. This programmer can only program PICs with up to 14 pins. There are many PIC programmers on the market. www.melabs.com makes a variety of programmers capable of programming larger pics.

The PIC we are using is the 16F675 that came with the programmer. PICs are manufactured by www.microchip.com. They can be ordered from them or from another electronic components store such as digikey.com or jameco.com.

Join the conversation -- every MAKE article has an online page that includes a place for discussion. We've made these RSS and Atom feeds to help you watch the discussions: subscribe.