#include <p18f4550.h>
#include "p18f4550_hw_config.h" // configures for 20MHz crystal and Fcy of 12MHz
#include "p18f4550_LCD.h" // control a 2x16 character LCD display
#define LED LATCbits.LATC7 // an LED with 470 ohm Rs attached to port C bit 7
void wait_ms(unsigned int time) // function for delay in ms
{ // an instruction cycle takes 83.3 ns, so 12,000 cycles will create a 1 ms delay
int i = 0;
const int cCycles_Per_ms = 921; // This variable will need to be adjusted to calibrate the function for 1 ms delay
while(time){
for( i = 0; i < cCycles_Per_ms ; i ++){
}
time--; // decrement the time unit, repeat till time = 0
}
}
void main(void)
{
// Hardware Configuration
// LED Config
TRISCbits.TRISC7 = 0; // RC7 pin 26 set for output
LED = 0; // led off
// LCD Display
LCD_init();
// check the wait_ms() function for accuracy
// setup timer 0 and turn it on
// ON 8B Src Edg PSA PS2 PS1 PS0
T0CON = 0x88; // 1 0 0 0 1 0 0 0 -> 16 bit, PS = 1
TMR0H = 0x00; TMR0L = 0x00; // count up from 0
wait_ms(3); // expect 3 ms delay
T0CON = 0x08; // Stop the timer
LCD_byte(TMR0L); // display TMRO count low byte (must be read first)
LCD_cmd(0xC0); // move to line 2 of the LCD
LCD_byte(TMR0H); // display TMRO count high byte
while(1){ // infinite loop to blink an led
LED = 0; // led off
wait_ms(800);
LED = 1; // led on
wait_ms(200);
} // verify that the LED blinks at a 1 sec rate
}