#include #define CP0 0x1010 #define CP1 0x2020 #define DATA_EEMEM_PROTECT_DISABLE 0x0100 #define XT_OSC 0x0001 #define DISABLE_DEBUG 0x0800 /* CP0,CP1 code protect off*/ /* other bits zero which means WDT disabled Low Voltage prm disabled, brownout disabled power up timer enabled */ __CONFIG((CP1 | CP0) | DATA_EEMEM_PROTECT_DISABLE | XT_OSC | DISABLE_DEBUG); #define bitset(var,bitno) ((var) |= (1 << (bitno))) #define bitclr(var,bitno) ((var) &= ~(1 << (bitno))) #define bittst(var,bitno) (var & (1 << (bitno))) /* read character from serial port, increment, echo it back */ /* return 8 bit char from Recieve port */ unsigned char getch () { unsigned char c; /* while (!RCIF) */ while (!bittst(PIR1,5)); /* check RCIF bit */ c = RCREG; return(c); } /* send 8 bit char to Recieve port */ void putch (c) unsigned char c; { /* wait until transmit reg empty */ /* while (!TXIF) */ while (!bittst(PIR1,4)); /* check RCIF bit */ TXREG = c; } main(void) { unsigned char c; /* setup Async communication */ bitclr(TXSTA, 6); /* 8-bit transmit */ bitset(TXSTA, 5); /* transmit enable*/ bitclr(TXSTA, 4); /* async mode */ bitset(TXSTA, 2); /* high speed mode */ /* set baud rate */ // SPBRG = 25; /* 9600 in high baud rate mode, crystal = 4.00 Mhz */ SPBRG = 95; /* 9600 in high baud rate mode, crystal = 14.7456 Mhz */ bitset(TRISC, 7); /* serial port enable */ bitset(TRISC, 6); /* serial port enable */ bitset(RCSTA, 7); /* serial port enable */ bitclr(RCSTA, 6); /* 8-bit reception */ bitset(RCSTA, 4); /* enable recieve */ for(;;) { c = getch (); c++; /* increment Char */ putch (c); /* send the char */ } }