Downloading
Links Mentioned
Microchip 18F252 Page

Links
Home
AWC

Microchip
PicList


Home
Introducing the PIC
A Programmer
Selecting a PIC
Hardware Tools
Software Tools
Downloading
Examining the Code



 

Here is the program you entered in the last frame. Click on a part of the code to find out more about it.

Main:
	movlw 0x85
	movwf T0CON
	BCF TRISC,3
top:
	btg PORTC,3
	clrf TMR0L
	clrf TMR0H  ; order is important!
	bcf INTCON,TMR0IF
waitl:
	btfss INTCON,TMR0IF
	bra waitl
        bra top
	

This program blinks the LED on the board (which is attached to PORTC pin 3). To make the blinking visible, it employs one of the PIC's special timer registers. Moving 0x85 (hex 85) into T0CON turns on the 16-bit counter TMR0 and causes it to advance by one count every 64 clock cycles.

The PIC uses a particular clock frequency depending on the crystal or ceramic resonator you use (in this case, 20MHz). It internally divides this frequency by 4, so effectively that's 5MHz or 200nS per clock pulse. The counter only advances every 64 "ticks" so that's 12.8uS per count. What's more, the program will allow the 16-bit counter to overflow, and that will occur after 65536 counts or about 839mS. You could make things faster by selecting another prescaler value (change 0x85 to 0x84, for example).

How do you find these magic registers and what they mean? You need to read the PIC datasheet for the part in question. You can find a link on the left side of this page.

Each I/O port on the PIC has a data register (like PORTC) and a direction register (TRISC, in this case). The direction register has a one in it for each port bit that is an input (the default). If you set a TRISC bit to zero, you make the corresponding bit in PORTC an output instead of an input.

The BTG instruction toggles the output of PORTC pin 3 so the LED flashes. In fact, when modifying a port register, it is better to manipulate the latch register (LATC, in this case). This register is strictly for output and doesn't get confused with the current input state of the pin. Try replacing PORTC with LATC, recompile, download, and verify that the program operates in the same way.

The PIC operates very quickly, so if you didn't put a time delay in, the LED would blink so fast that you'd never be able to see it with the naked eye.  To do the delay the program takes the following steps:

1. Clear both TMR0L and TMR0H (the two bytes that make up the 16-bit counter).

2. Clear the Timer overflow bit (TMR0IF).

3. Wait for the overflow bit to go high before proceeding.

It is important to clear the timer registers in the order shown. Suppose the timer had the value 0x03FF and you cleared the top byte (making the timer 0x00FF). Before the next instruction executes, the timer increments and therefore becomes 0x100. Now clearing the low byte results in a timer value of 0x0100! Clearing the low byte first avoids this situation (although for other reasons, it is unlikely to actually matter in this program).

More to come....

Back