13.07.2015 Views

Nuts & Volts

Nuts & Volts

Nuts & Volts

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

SOFTWAREThe code shown in Listing 1 is not that long orcomplicated once you break it down. Remember, we aredoing two advanced functions here: using the Timer1 andusing interrupts.HOW IT WORKSThe program starts off with the specific DEFINEsrequired. This defines the bootloader self programmingsetup that the Ultimate OEM module has built in. I know afew readers are using bootloader modules so this is therefor them. Since most readers are programming it into ablank PIC using a PIC programmer, you don’t need this lineso I commented it out by putting an apostrophe in front ofit. PICBasic Pro will treat it as a comment line and ignore itduring compile time.‘ DEFINE LOADER_USED 1 ‘ This command line for Ultimate‘ OEM onlyThe next DEFINE establishes the oscillator frequency.PICBasic Pro defaults to 4 MHz. Therefore, we must adjustthe time-based commands for the higher frequency.PICBasic Pro automatically adjusts for the higher speedwhen we add this DEFINE:DEFINE OSC 20We establish only one variable for this simple program.It’s called “counter.” It will store how many times theprogram interrupted so we can see when we have reached10 interrupts (one second).counter var byte‘Establish a byte size variableThe program has to initialize Timer1 to 3035 decimal,and we do that by writing directly to the Timer1 registers,TMR1H and TMR1L. You could use decimal numbers forthis, but that would be confusing because TMR1H wouldhave to be set to 11 and TMR1L to 219, and it’s not obviousthat these combine to form the word value 3035. You coulduse binary, setting all eight 1s or 0s in their proper order,but that’s a lot of typing. This is where hexadecimalnumbers are handy. You can use the Windows scientificcalculator to easily convert 3035 decimal to $0BDBhexadecimal and then make TMR1H equal to the first twodigits and TMR1L equal to the second two. That’s what I didhere. The dollar sign tells PICBasic Pro that the number isa hex value.TMR1H = $0B ‘Preset Timer 1 to 3035TMR1L = $DB‘ using $0BDB hexLISTING 1‘ DEFINE LOADER_USED 1 ‘ This command line for Ultimate OEM onlyDEFINE OSC 20‘Set oscillator to 20 MHzcounter var byte‘Establish a byte size variableTMR1H = $0B ‘Preset Timer 1 to 3035TMR1L = $DB‘ using $0BDB hexT1CON = %00110001‘Timer1 on with 1:8 prescalerPIE1 = %00000001‘Enable Timer1 InterruptINTCON = %11000000‘Enable interruptsON INTERRUPT GOTO mytimer ‘Define interrupt handlerhigh 2‘Initialize B2 LED to oncounter =0‘Initialize counter to zeromainif counter = 10 then‘Test for 10 interruptstoggle 2‘10 interrupts occurred so flip LED statecounter = 0‘Reset counter variableendif‘End the If-Then commandgoto main‘Loop back to the Beginning‘*** This is where we go on and interrupt ***Next, we enter the special registersetup commands. As mentioned,the PIC automatically divides the20 MHz clock by four, but we needto set up the divide-by-eightprescaler. We do that by setting theproper bits in the T1CON register.Setting the fifth and sixth bits to “1”establishes the prescaler as 1:8. Thefirst bit (bit 0 in the data sheet)turns the timer on (set to “1”) or off(set to “0”). We turn it on here. Asyou can see, in this case, I usedbinary rather than decimal or hex(the “%” symbol indicates it’s a binarynumber). Using binary makes iteasy to check which bits are set andwhich are cleared. Each numbersystem has its proper place inprogramming.T1CON = %00110001‘Timer1 on with‘ 1:8 prescalerdisablemytimer:TMR1H = $0BTMR1L = $DBcounter = counter +1PIR1.0 = 0resume16 May 2006‘Prevent interrupts from occurring‘Interrupt handler routine label‘Preset Timer 1 to 3035 decimal‘ using $0BDB hex‘Increment the timer overflow count‘Clear Timer1 overflow interrupt flag‘This is how we exit an interruptAnother register that needs tobe set up is the PIE1 register. Itcontrols peripherals such as Timer1and Timer2. The first bit (bit 0) is theTimer1 interrupt enable bit. We needto set this to “1” to allow or enablethe Timer1 overflow to cause an

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!