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.

Serial communication using USART between pic18f4550 and laptop giving error

Status
Not open for further replies.

sadpony

Junior Member level 2
Joined
Jul 14, 2013
Messages
22
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
227
I am doing a project wherein I have to identify a key pressed and send the information to Bray's terminal using serial communication. It is continuously display 00 irrespective of the value written in TXREG. I think there is some error in the code. Please help me solve it.
The key reading part is correct as I have verified it by displaying on the LCD.




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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include <p18f4550.h>
#include <stdio.h>
#include"usart.h"
 
 
#pragma config PBADEN   =   OFF // PORTB<4:0> pins are configured as digital I/O on Reset)
#pragma config WDT      =   OFF // watch dog timer off
//#pragma config LVP        =   OFF // Low voltage program off
 
 
/*The following lines of code perform interrupt vector relocation to work with the USB bootloader. These must be
used with every application program to run as a USB application.*/
extern void _startup (void);
#pragma code _RESET_INTERRUPT_VECTOR = 0x1000
 
 
void _reset (void)
{
        _asm goto _startup _endasm
}
 
#pragma code
#pragma code _HIGH_INTERRUPT_VECTOR = 0x1008
void high_ISR (void)
{
}
 
#pragma code
#pragma code _LOW_INTERRUPT_VECTOR = 0x1018
void low_ISR (void)
{
}
#pragma code
 
void myMsDelay (unsigned int time)//time in milliseconds
{
        unsigned int i, j;
        for (i = 0; i < time; i++)
                for (j = 0; j < 710; j++);
}
 
 
void PORTs_init(void) 
{   
            
          INTCON2bits.RBPU=0; //To enable the internal pullup
          TRISB = 0x0F; //PORTB lower byte as input(columns) and higher byte as output(rows)
          
          ADCON1 = 0x0E; //PORTE as digital input
          
          OSCCON = 0x72; //8MHz internal clock
          SPBRG = 12;    //9600 baud rate
          TRISCbits.TRISC6 = 0;
          TXSTA = 0b00100010;
          RCSTAbits.SPEN = 0;
                            
          myMsDelay(100);
          TXREG =0x09;
          myMsDelay(100);
          TXREG =0x0A;
        
}
 
 
void main()
{
        PORTs_init();  
        
        while(1)
        {
                //Row0 made 0 and columns checked for keypress     
                PORTB = 0xE0;
                myMsDelay(100);//100 milliseconds
                
                
                
           switch(PORTB & 0x0F)
                {
                        case 0x0E:
                                //Delay to take into account debouncing time
                                myMsDelay (1000);
                                if((PORTB & 0x0F) == 0x0E){
                                    while(!TXSTAbits.TRMT);
                                    TXREG=1;
                                    }
                                break;
 
                        case 0x0D:
                                myMsDelay (1000);
                                if((PORTB & 0x0F) == 0x0D){
                                    while(!TXSTAbits.TRMT);
                                    TXREG=2;
                                    }                           
                                break;
 
                        case 0x0B:
                                myMsDelay (1000);
                                if((PORTB & 0x0F) == 0x0B){
                                    while(!TXSTAbits.TRMT);
                                    TXREG=3;
                                    }
                        
                                break;
 
                        case 0x07:
                                myMsDelay (1000);
                                if((PORTB & 0x0F) == 0x07){
                                    while(!TXSTAbits.TRMT);
                                    TXREG=4;
                                    }
                                break;
                }
 
                //Row1 made 0 and columns checked for keypress
                PORTB = 0xD0;
                myMsDelay(1000);
                switch(PORTB & 0x0F)
                {
                        case 0x0E:
                                myMsDelay (1000);
                                if((PORTB & 0x0F) == 0x0E){
                                    while(!TXSTAbits.TRMT);
                                    TXREG=5;
                                    }
                                    
                                break;
 
                        case 0x0D:
                                myMsDelay (1000);
                                if((PORTB & 0x0F) == 0x0D){
                                while(!TXSTAbits.TRMT);
                                    TXREG=6;
                                    }
                                break;
 
                        case 0x0B:
                                myMsDelay (1000);
                                if((PORTB & 0x0F) == 0x0B){
                                    while(!TXSTAbits.TRMT);
                                    TXREG=7;
                                    }
                                break;
 
                        case 0x07:
                                myMsDelay (1000);
                                if((PORTB & 0x0F) == 0x07){
                                    while(!TXSTAbits.TRMT);
                                    TXREG=8;
                                    }
                               
                                break;
                }
                //Row2 made 0 and columns checked for keypress
                PORTB = 0xB0;
                myMsDelay(1000);
                switch(PORTB & 0x0F)
                {
                        case 0x0E:
                                myMsDelay (1000);
                                if((PORTB & 0x0F) == 0x0E){
                                    while(!TXSTAbits.TRMT);
                                    TXREG=9;
                                    }
                                break;
 
                        case 0x0D:
                                myMsDelay (1000);
                                if((PORTB & 0x0F) == 0x0D){
                                    while(!TXSTAbits.TRMT);
                                    TXREG=10;
                                    }
                                break;
 
                        case 0x0B:
                                myMsDelay (1000);
                                if((PORTB & 0x0F) == 0x0B){
                                    while(!TXSTAbits.TRMT);
                                    TXREG=11;
                                    }
                                break;
 
                        case 0x07:
                                myMsDelay (1000);
                                if((PORTB & 0x0F) == 0x07){
                                    while(!TXSTAbits.TRMT);
                                    TXREG=12;
                                    }
                                break;
                }
 
                //Row3 made 0 and columns checked for keypress
                PORTB = 0x70;
                myMsDelay(1000);
                switch(PORTB & 0x0F)
                {
                        case 0x0E:
                                myMsDelay (1000);
                                if((PORTB & 0x0F) == 0x0E){
                                    while(!TXSTAbits.TRMT);
                                    TXREG=13;
                                    }
                                break;
 
                        case 0x0D:
                                myMsDelay (1000);
                                if((PORTB & 0x0F) == 0x0D){
                                    while(!TXSTAbits.TRMT);
                                    TXREG=14;
                                    }
                                break;
 
                        case 0x0B:
                                myMsDelay (1000);
                                if((PORTB & 0x0F) == 0x0B){
                                    while(!TXSTAbits.TRMT);
                                    TXREG=15;
                                    }
                                break;
 
                        case 0x07:
                                myMsDelay (1000);
                                if((PORTB & 0x0F) == 0x07){
                                    while(!TXSTAbits.TRMT);
                                    TXREG=16;
                                    }   
                                break;                  
   
            }
        
        }
   
}



Thanks in advance
 

1>check what baud rate u r setting 9600 or 4800 bps
2>i think may be the garbage value is loaded to your buffer reg(txreg).check that
3> bray's terminal supports ascii characters so if u r sending anything it will display ascii characters eg(31h=1 etc)
4> check the voltage level of ur max232 ic .it will vary from 3.4 t0 4.8v



**************************************************************
There are only two ways to live your life. One is as though nothing is a miracle. The other is as though everything is a miracle.
****************************************************************
 
Thanks Tanmay for the help but I have few queries

I want the baud rate to be 9600. I configured the OSCCON register for 8MHZ internal clock, then using the formula
Baud rate=Clock frequency/(64(X+1)) , I calculated X and substituted in SPBRG. Is there any erro in this.

Also how should I check the TXREG contents.

Bray's terminal has an option of displaying HEX, so I selected that.

I gave 4.9V to max232. I will vary the voltage and see if there is any change.

Please do answer this post.
Thanks in advance.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top