/* 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 * * 8-Bit LCD Interface * Transmitting PIC - transmits packets to the recieving PIC * */ //-------------------------------------------------------------- // libraries //-------------------------------------------------------------- #include #include "sci.h" #include "delay.h" #include "../constants.h" //-------------------------------------------------------------- // pin mapping //-------------------------------------------------------------- #define EB_INPUT RB0 //-------------------------------------------------------------- // global variables //-------------------------------------------------------------- unsigned alive_tx_cnt = 0; unsigned data_tx_cnt = 0; bit alive_tx_flag = 0; bit data_tx_flag = 0; //-------------------------------------------------------------- // function declarations //-------------------------------------------------------------- void interrupt timer_isr(void); void eb_isr(void); void alive_tx_isr(void); void data_tx_isr(void); void led_isr(void); //-------------------------------------------------------------- // main //-------------------------------------------------------------- main(void) { int x = 0; // configure processor // ----------------------------------------------------------- PORTA = 0xff; // set output data latches to default value CMCON = 0x07; // disable comparators to allow I/O TRISA = 0xff; // all bits output TRISB = 0xff; // 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 // ----------------------------------------------------------- // main code // ----------------------------------------------------------- for(;;) { if( data_tx_flag == 1){ if( x % 2 ){ // send NO Packet (000) // ------------------------- RB2 = 1; TRISB = 0xFB; // all inputs except RB2 GIE = 0; RB2 = 0; DelayUs(250); DelayUs(55); RB2 = 0; DelayUs(250); DelayUs(55); RB2 = 0; DelayUs(250); DelayUs(55); RB2 = 0; DelayUs(250); DelayUs(55); RB2 = 1; TRISB = 0xFF; // all inputs GIE = 1; data_tx_cnt = 0; } else{ //send YES packet (010) //-------------------------- RB2 = 1; GIE = 0; TRISB = 0xFB; // all inputs except RB2 RB2 = 0; DelayUs(250); DelayUs(55); RB2 = 0; DelayUs(250); DelayUs(55); RB2 = 1; DelayUs(250); DelayUs(55); RB2 = 0; DelayUs(250); DelayUs(55); RB2 = 1; TRISB = 0xFF; // all inputs GIE = 1; data_tx_cnt = 0; } x++; data_tx_flag = 0; } if( alive_tx_flag == 1){ // send alive packet // ------------------------------------ RB2 = 1; TRISB = 0xFB; // all inputs except RB2 RB2 = 0; DelayUs(250); DelayUs(55); TRISB = 0xFF; // all inputs // reset alive flag // ------------------------------------- alive_tx_cnt = 0; alive_tx_flag = 0; } } } //-------------------------------------------------------------- // interrupt service routine //-------------------------------------------------------------- void interrupt timer_isr(void) { T0IE = 0; // disable timer0 interrupt GIE = 0; // disables all interrupts alive_tx_isr(); data_tx_isr(); TMR0 = PRELOAD; // preload timer T0IF = 0; // clear timer overflow flag T0IE = 1; // enable timer0 interrupt GIE = 1; // enable all interrupts } //-------------------------------------------------------------- // alive tx process (isr called every 10 ms) //-------------------------------------------------------------- void alive_tx_isr(void) { alive_tx_cnt++; if( alive_tx_cnt == 100 ) { alive_tx_flag = 1; alive_tx_cnt = 0; } } //-------------------------------------------------------------- // data tx process (isr called every 10 ms) //-------------------------------------------------------------- void data_tx_isr(void) { data_tx_cnt++; if( data_tx_cnt == 500 ) { data_tx_flag = 1; data_tx_cnt = 0; } }