int cd_tx_btc; // Bit Count
int16 cd_tx_buf; // Serial data buffer
int cd_rx_btc; // Bit count
int16 cd_rx_buf; // Serial data buffer
#INT_TIMER1
void timer1_isr() {
// Timer 1; 16 Bit, clocked at 9,830,400 Hz for RS232 Receive timing.
// 1024 clocks = 1 bit time @ 9600 Baud.
// 0000 - 0x200 = 0xfc00
l = get_timer1();
set_timer1(0xfc30);
if(cd_rx_btc)
// Presently processing a character
if(cd_rx_btc == 10 && input_state(PIN_C2))
// Abort if centre of start bit |= 0
cd_rx_btc=0;
else if(cd_rx_btc) {
cd_rx_buf >>= 1;
if(input_state(PIN_C2))
cd_rx_buf |= 0x100;
cd_rx_btc--;
if(!cd_rx_btc) {
if(cd_rx_buf & 0x100)
// Stop bit present, Start bit shifted out.
cd_rput(cd_rx_buf & 0xff);
}
}
}
#INT_CCP1
void ccp1_isr() {
// Triggered on C2 (cd_rxd) falling edge; possible start bit
// Already in receive?
if(cd_rx_btc)
return;
// New character, setup..
cd_rx_btc=10;
cd_rx_buf=0;
set_timer1(0xfe30);
}
#INT_RTCC
void rtc_isr() {
// Prescaler /4, 8 bit counter, Int every 1024 clocks
// 9.8304MHz / 1024 = 9600Hz
//INT Routine timing
//output_high(pin_d0);
// Software RS232 Routines
// Transmit
if(cd_tx_btc) {
if(cd_tx_buf & 1)
output_high(PIN_A5);
else
output_low(PIN_A5);
cd_tx_buf >>= 1;
cd_tx_btc--;
}
else
output_high(PIN_A5);
}
// In main() initialisation section:
setup_timer_0(RTCC_INTERNAL | RTCC_DIV_4 | RTCC_8_BIT);
setup_timer_1(T1_INTERNAL | T1_DIV_BY_1);
setup_ccp1(CCP_CAPTURE_FE);
enable_interrupts(INT_RTCC);
enable_interrupts(INT_TIMER1);
enable_interrupts(INT_CCP1);
enable_interrupts(GLOBAL);