Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

transmitting and receiving data from usart by using PIC18F4520

Status
Not open for further replies.

naveenkr37

Newbie level 1
Joined
Jul 10, 2013
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
31
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"

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:

I would first recommend testing your transmission routines using a PC with terminal emulator software, like Putty, as the receiver.

You can then determine which transmission routines, if any, are not functioning as expected and then remedy the issues.

I would then test the reception routines using a PC with terminal emulator software as the transmitter.

You can then determine which reception routines, if any, are not functioning as expected and then remedy the issues.


Note: When interfacing a devices UART/USART to a PCs RS-232 port, an RS-232 transceiver, like a MAX232, must be utilized or the microcontroller maybe damaged.


Finally, perform a similar test to which you have previously posted, which tests the interaction between two devices using the appropriate routines.


I allows recommend testing individual routines, before testing their interaction between them.


BigDog
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top