/* main.c * * Copyright (c) 2003-2004 Susan Cotterell. * Permission to copy is granted provided that this header remains * intact. This software is provided with no warranties. * * Version : 1.0 * * Recieving PIC * PIC recieves various packets, and displays packet contents * */ //-------------------------------------------------------------- // libraries //-------------------------------------------------------------- #include #include "sci.h" #include "io.h" #include "../constants.h" //-------------------------------------------------------------- // pin mappings //-------------------------------------------------------------- #define EB_OUTPUT RB0 //-------------------------------------------------------------- // global variables //-------------------------------------------------------------- unsigned char data_val = ERROR; unsigned rx_cnt = 0; bit rx_error = 0; bit rx_flag = 0; //-------------------------------------------------------------- // function declarations //-------------------------------------------------------------- void interrupt timer_isr(void); void rx_isr(void); void led_isr(void); //-------------------------------------------------------------- // main //-------------------------------------------------------------- main(void) { // configure processor // ----------------------------------------------------------- PORTA = 0xff; // set output data latches to default value CMCON = 0x07; // disable comparators to allow I/O TRISA = 0x00; // all bits output TRISB = 0x02; // all bits output, bit 1 input // Timer0 setup // ----------------------------------------------------------- asm("CLRWDT"); // turn off watch dog timer OPTION = 0x47; // setup prescaler TMR0 = PRELOAD; // preload timer INTCON = 0xA0; // setup interrupt control // USART setup // ----------------------------------------------------------- sci_Init(); // setup USART protocol InitIO(); // initialzie LCD ClearScreen(); // main code // ----------------------------------------------------------- PrintString("hello"); for(;;) { if( rx_flag == 1 ){ if( rx_error == 0){ // rx trigger by input data_val = sci_GetByte(); } else{ // rx trigger by isr, it timed out data_val = ERROR; } rx_cnt = 0; rx_error = 0; rx_flag = 0; if( data_val == YES ){ ClearScreen(); PrintString("YES"); } else if( data_val == NO){ ClearScreen(); PrintString("NO"); } else if(data_val == 0xFF){ //alive packet ClearScreen(); PrintString("ALIVE"); } else{ ClearScreen(); PrintString("ERROR"); PutChar(data_val/100 + '0'); PutChar(data_val%100/10 + '0'); PutChar(data_val%10 + '0'); } } } } //-------------------------------------------------------------- // interrupt service routine //-------------------------------------------------------------- void interrupt timer_isr(void) { T0IE = 0; // disable timer0 interrupt GIE = 0; // disables all interrupts if( RCIF ){ // interrupt triggered by USART rx_flag = 1; } rx_isr(); TMR0 = PRELOAD; // preload timer T0IF = 0; // clear timer overflow flag T0IE = 1; // enable timer0 interrupt GIE = 1; // enable all interrupts } //-------------------------------------------------------------- // rx process (isr called every 10 ms) //-------------------------------------------------------------- void rx_isr(void) { rx_cnt++; if( rx_cnt == 100 ) { rx_flag = 1; rx_error = 1; } }