naveenkr37
Newbie level 1
Hi,
I am having a problem in receiving continuous data from USART by using PIC18F4520. I am receiving single character or integer, but i am not receiving correctly strings or continuous integers
here is my code,
"USART TRANSMITTING PROGRAM"
"USART RECEIVING PROGRAM"
- - - Updated - - -
i need solution for above problem
I am having a problem in receiving continuous data from USART by using PIC18F4520. I am receiving single character or integer, but i am not receiving correctly strings or continuous integers
here is my code,
"USART TRANSMITTING PROGRAM"
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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 #include <p18f4520.h> #include "usart.h" #pragma config OSC = HS #pragma config WDT = OFF #pragma config LVP = OFF #pragma config DEBUG = ON #pragma config PBADEN = OFF #define TXEN TXSTAbits.TXEN #define BRGH TXSTAbits.BRGH #define SPEN RCSTAbits.SPEN #define CREN RCSTAbits.CREN #define BRG16 BAUDCONbits.BRG16 #define TXIF PIR1bits.TXIF //#define RCIF PIR1bits.RCIF int str[]={'0','0','0','0','0'}; int i=4,j=0; int a[]={1,2,3,4,5,6},c; void USARTInit(void); void USARTWriteByte(char ); void USARTWriteString( char* ); void USARTWriteInt(int ,unsigned char ); void USARTInit() { //Baud Rate = 9600 Bits per Second //*** Note: Valid On 20MHz Crystal ONLY *** //For other crystal freq calculate new values for SPBRG SPBRG=31; BRGH=0; //low speed BRG16=0; //for 8bit baud rate SPEN=1; // serial port enable TXEN=1; // transmit enable bit CREN=1; //Enable Receiver (RX) } void USARTWriteByte(char ch) { //Wait for TXREG Buffer to become available while(!TXIF); //Write data TXREG=ch; } void USARTWriteString( char *str) { while((*str)!='\0') { //Wait for TXREG Buffer to become available while(!TXIF); //Write data TXREG=(*str); //Next goto char str++; } } void USARTWriteInt(int val,unsigned char field_length) { if(val<0) { USARTWriteByte('-'); //Write '-' sign for negative numbers. val=(val*(-1)); //Make it positive. } //Convert Number To String and pump over Tx Channel. while(val) { str[i]=val%10; val=val/10; i--; } if(field_length>5) while(str[j]==0) j++; else j=5-field_length; for(i=j;i<5;i++) { USARTWriteByte('0'+str[i]); } } void main() { TRISCbits.TRISC7=1; //set RC7 to input TRISCbits.TRISC6=0; //set RC6 to output USARTInit(); while(1) { for(c=0;c<6;c++) { USARTWriteInt(a[c],1); } } }
"USART RECEIVING PROGRAM"
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 #include <p18f4520.h> #include<delays.h> #include "lcd.h" #include "usart.h" void LCDStart(void); #pragma config OSC = HS #pragma config WDT = OFF #pragma config LVP = OFF #pragma config DEBUG = ON #pragma config PBADEN = OFF #define TXEN TXSTAbits.TXEN #define BRGH TXSTAbits.BRGH #define SPEN RCSTAbits.SPEN #define CREN RCSTAbits.CREN #define BRG16 BAUDCONbits.BRG16 #define TXIF PIR1bits.TXIF #define RCIF PIR1bits.RCIF void USARTInit() { //Baud Rate = 57600 Bits per Second //*** Note: Valid On 20MHz Crystal ONLY *** //For other crystal freq calculate new values for SPBRG SPBRG=31; BRGH=0; BRG16=0; SPEN=1; CREN=1; //TXSTA REG TXEN=1; //RCSTA //Enable Receiver (RX) //BAUDCON //SPBRGH=0X02; //SPBRG=0X08; } #pragma idata section3 const unsigned char str[]="recieved data"; char a[6],i=0,c=0; #pragma code void main() { TRISCbits.TRISC7=1; TRISCbits.TRISC6=1; Wait(50); LCDInit(LS_BLINK); Wait(50); USARTInit(); Wait(50); while(1) { LCDWriteStringXY(4,0,str); while(!RCIF); c=RCREG; a[i]=c; i++; LCDStart(); } } void LCDStart() { Wait(50); LCDClear(); Wait(50); LCDGotoXY(6,1); while(a[i]!='\0') { LCDData(c); i++; } }
- - - Updated - - -
i need solution for above problem
Last edited by a moderator: