rushi53
Member level 2
MplabX & XC8 - PIC18F4550- USART Problem with 9600 baud rate & extrenal 20MHZ crystal
I am interfacing PIC18F4550 with a touchscreen. My touchscreen gives serial output with 9600 baud rate.
Below is the format of touchscreen *output data at 9600baud rate.
0x0A X:0065 Y:0089 Z:0029 0x0D
I am using 20MHZ external crystal for PIC18F4550. I am using MPLAB-X and xc8 compiler.
With my code I am reading touchscreen and sending the received X, Y, Z co-ordinates back to PC via serial transmit.
My code run only once and then it gets hanged.
It seems like PIC is not receiving data continuously or there might be problem with my config bits.
Please help me in this.
Below is my code:
I am interfacing PIC18F4550 with a touchscreen. My touchscreen gives serial output with 9600 baud rate.
Below is the format of touchscreen *output data at 9600baud rate.
0x0A X:0065 Y:0089 Z:0029 0x0D
I am using 20MHZ external crystal for PIC18F4550. I am using MPLAB-X and xc8 compiler.
With my code I am reading touchscreen and sending the received X, Y, Z co-ordinates back to PC via serial transmit.
My code run only once and then it gets hanged.
It seems like PIC is not receiving data continuously or there might be problem with my config bits.
Please help me in this.
Below is my code:
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 #include <pic18f4550.h> #include <xc.h> #pragma config FOSC = HS #pragma config WDT = OFF #pragma config LVP = OFF #pragma config PBADEN = OFF void tx_data(unsigned char); unsigned char rx_data(void); unsigned char serial_data = ' '; unsigned int i=0; unsigned char serial_data1, serial_data2, serial_data3, serial_data4, serial_data5, serial_data6, serial_data7, serial_data8, serial_data9, serial_data10, serial_data11, serial_data12, serial_data13, serial_data14, serial_data15, serial_data16, serial_data17, serial_data18, serial_data19; #define FREQ 20000000 // Frequency = 12MHz #define baud 9600 #define spbrg_value (((FREQ/64)/baud)-1) // Refer to the formula for Baud rate calculation in Description tab void DelayMs(int count); void main() { int j; SPBRG=spbrg_value; // Fill the SPBRG register to set the Baud Rate RCSTAbits.SPEN=1; // To activate Serial port (TX and RX pins) TXSTAbits.TXEN=1; // To enable transmission RCSTAbits.CREN=1; // To enable continuous reception while(1) { while(serial_data != 'X') // wait till X character is received { serial_data=rx_data(); // Receive data from PC } serial_data1=rx_data(); // Receive data from PC serial_data2=rx_data(); // Receive data from PC serial_data3=rx_data(); // Receive data from PC serial_data4=rx_data(); // Receive data from PC serial_data5=rx_data(); // Receive data from PC serial_data6=rx_data(); // Receive data from PC serial_data7=rx_data(); // Receive data from PC serial_data8=rx_data(); // Receive data from PC serial_data9=rx_data(); // Receive data from PC serial_data10=rx_data(); // Receive data from PC serial_data11=rx_data(); // Receive data from PC serial_data12=rx_data(); // Receive data from PC serial_data13=rx_data(); // Receive data from PC serial_data14=rx_data(); // Receive data from PC serial_data15=rx_data(); // Receive data from PC serial_data16=rx_data(); // Receive data from PC serial_data17=rx_data(); // Receive data from PC serial_data18=rx_data(); // Receive data from PC serial_data19=rx_data(); // Receive data from PC tx_data('X'); // Transmit the same data back to PC tx_data('='); // Transmit the same data back to PC tx_data(serial_data2); // Transmit the same data back to PC tx_data(serial_data3); // Transmit the same data back to PC tx_data(serial_data4); // Transmit the same data back to PC tx_data(serial_data5); // Transmit the same data back to PC tx_data(0x0D); // Transmit the same data back to PC tx_data(0x0A); // Transmit the same data back to PC tx_data('Y'); // Transmit the same data back to PC tx_data('='); // Transmit the same data back to PC tx_data(serial_data9); // Transmit the same data back to PC tx_data(serial_data10); // Transmit the same data back to PC tx_data(serial_data11); // Transmit the same data back to PC tx_data(serial_data12); // Transmit the same data back to PC tx_data(0x0D); // Transmit the same data back to PC tx_data(0x0A); // Transmit the same data back to PC tx_data('Z'); // Transmit the same data back to PC tx_data('='); // Transmit the same data back to PC tx_data(serial_data16); // Transmit the same data back to PC tx_data(serial_data17); // Transmit the same data back to PC tx_data(serial_data18); // Transmit the same data back to PC tx_data(serial_data19); // Transmit the same data back to PC tx_data(0x0D); // Transmit the same data back to PC tx_data(0x0A); // Transmit the same data back to PC DelayMs(1000); } } void tx_data(unsigned char data1) { TXREG=data1; // Store data in Transmit register while(PIR1bits.TXIF==0); // Wait until TXIF gets low } unsigned char rx_data(void) { while(PIR1bits.RCIF==0); // Wait until RCIF gets low return RCREG; // Retrieve data from reception register } void DelayMs(int count) { int Di,Dj; for(Di = 0 ; Di < count ; Di++) { for(Dj = 0 ; Dj < 100 ; Dj++); } }