# anonymous problem in usart program

Status
Not open for further replies.

#### einton

##### New Member
HI i m using this usart program in mikroc pro using pic18f2580 @ 8 Mhz,the program has to return back the same data which sent to it
Code:
char uart_rd;

void main() {

ADCON1 |= 0x0F;                 // Configure AN pins as digital

UART1_Init(9600);               // Initialize UART module at 9600 bps
Delay_ms(100);                  // Wait for UART module to stabilize

while (1) {

UART1_Write(uart_rd);

}
}
}
the code is written to send back the same data which it received.
can any one tell why this sort of data is been there in response of above program.

sent:7
sent:7
sent:7
sent:7
sent:7
sent:7
sent:7
sent:7
sent:7
sent:6
sent:6
sent:6
sent:6
sent:6
sent:6
sent:6
sent:6
sent:6
sent
sent
sent
sent
sent
sent
sent
sent
sent
sent
sent
sent
sent
sent
sent
sent
sent
sent
sent
sent
sent
sent
sent
sent
sent
sent:~
sent:~
sent:~
sent:~
sent:~
sent:~
sent:~
sent:~
sent:~
sent:~
sent:66
sent:66
sent:66
sent:66
sent:66
sent:66
sent:66
sent:66
sent:66
sent:66
sent:66
sent:66
sent:66
sent:66
sent:66
sent:66
sent:7
sent:777
sent:777
sent:7
sent:777
sent:777
sent:2
sent:2
sent:n
sent:n
sent:n
sent:n
sent:n
sent:n
sent:n
sent:n
sent:n

if u look at the sequence u can see that what ever the data u are sending same no of digits are to be received
but the program as described earlier has to send same data as it is send. some time it send the same data it receive no matter how many times u send the same digits but some times it send some different data for a digit
for example if i sent it digit 4 it will send 6 in response of it no matter how many times u sent digit 4.

sent:6
sent:6
sent:6
sent:6
sent:6
sent:6
on the another hand if i m specifically sending 7 it also send 7 in response of it as no matter how many time u send data as shown here
sent:7
sent:7
sent:7
sent:77
sent:77
sent:77

so i mean to say that i m involved in some hardware problem which some time send accurate data but some time send incorrect data but always send the same ascii code for specified digit. please tell what gonna happening i m using max232n with 10uF caps .

#### Mr RB

##### Well-Known Member
That sounds like a baudrate mismatch. How accurate is your baudrate? Is the PIC using 8MHz xtal or internal osc?

You could try setting up the USART brg manually, test different brg values until you find the limits where the baudrate is too fast and too slow then pick a value in the middle.

#### einton

##### New Member
generated baud rate has .17% error but it is acceptable could u please tell how can baud rate be chosen manually , baud rate has specific values and we have to chose out of them.

#### Mr RB

##### Well-Known Member
Well if you cant "trim" the baudrate it's hard to tell if you have a baudrate problem. You could have a cable problem or an oscillator problem or anything.

If you want to trim the baudrate; check the USART chapter in the PIC 18F2580 datasheet, and see the pages on the brg. Then instead of using the "UART1_Init(9600)" just setup the USART manually by writing the values into the USART registers. The datasheet explains exactly how to do that. Then you can manually try different values of the brg and see where it works.

#### Pommie

##### Well-Known Member
I'm guessing that this is a software usart. Try increasing the number of stop bits to two and this may solve your problem.

Mike.

#### einton

##### New Member
HI everyone especially Mr RB AND POMMY, YES THE PROBLEM SOLVED VERY MUCH AND I M RECEIVING RIGHT DATA ON MY MIKROC PRO USART PROBLEM THIS TIME WHEN I SET THE BAUD RATE OF MY
USART TERMINAL ON 38400 BAUD , WHILE PROGRAM COMPILED WITH 9600 AND ASTONISHLY I M RECEIVING DATA CORRECTLY I THINK IT LOOKS LIKE MY XTAL PROBLEM ANY THOUGHT OF WORD U CAN SAY. THE PROGRAM IS WORKING WELL ENOUGH, AND ONE THING MORE CAN U TELL WHY DOES PIC RESET CONSTANTLY SOME TIME EVEN WHEN ALL CONNECTION ARE RIGHT.

#### Pommie

##### Well-Known Member
If your pic is reseting constantly then it could be one of two things. Mclr is floating and resting the chip - connect Mclr to Vdd via a 10k resistor. The watchdog timer is reseting the chip - turn off the watchdog timer in the config settings.

Mike.

Status
Not open for further replies.

Replies
15
Views
3K
Replies
9
Views
3K
Replies
4
Views
1K
Replies
10
Views
3K
Replies
18
Views
2K