The original example code works fine, but the loop only sends garbage, instead of 0x30 and 0x31.
So,
How do I sync cpu and serial?
A good book/reference for pic & uart?
Is there a book, guide or anything to pic architecture and patterns?
- Code: Select all
//removed headers
// I/O definitions for the Explorer16
#define CTS _RF12 // Clear To Send, input, HW handshake
#define RTS _RF13 // Request To Send, output, HW handshake
#define TRTS TRISFbits.TRISF13 // tris control for RTS pin
// timing and baud rate calculations
#define BRATE 34 // 115200 baud (BREGH=1)
#define U_ENABLE 0x8008 // enable the UART peripheral (BREGH=1)
#define U_TX 0x0400 // enable transmission
// initialize the UART2 serial port
void initU2 ( void){
U2BRG = BRATE;
U2MODE = U_ENABLE;
U2STA = U_TX;
TRTS = 0; // make RTS output
RTS = 1; // set RTS default status
} // initU2
// send a character to the UART2 serial port
int putU2 ( int c){
while ( CTS); // wait for !CTS, clear to send
while ( U2STAbits.UTXBF); // wait while Tx buffer full
U2TXREG = c;
return c;
} // putU2
// wait for a new character to arrive to the UART2 serial port
char getU2 ( void){
RTS = 0; // assert Request To Send !RTS
while ( !U2STAbits.URXDA); // wait for a new character to arrive
RTS = 1;
return U2RXREG; // read the character from receive buffer
}// getU2
main(){
char c;
char s[] = {"0"......"1"} //1000 random 1 and 0
int sLength = 1000;
int i =0;
// 0. init the PPS configuration //
init_PPS (0);
// 1. init the UART2 serial port
initU2();
// 2. prompt
putU2( '>');
// 3. main loop
while ( 1) { // 3.1 wait for a character
//original code
// c = getU2();
// 3.2 echo the character
// putU2( c);
for(i=0;i<sLength;i++)
{
putU2(s[0]);
};
} // main loop
}// main