Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

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) {
  

   if (UART1_Data_Ready()) {      // If data is received,
     uart_rd = UART1_Read();      //   read the received data,
     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.


received:7
sent:7
received:7
sent:7
received:7
sent:7
received:7
sent:7
received:7
sent:7
received:7
sent:7
received:7
sent:7
received:7
sent:7
received:7
sent:7
received:4
sent:6
received:4
sent:6
received:4
sent:6
received:4
sent:6
received:4
sent:6
received:4
sent:6
received:4
sent:6
received:4
sent:6
received:4
sent:6
received:eek:
sent:eek:
received:eek:
sent:eek:
received:eek:
sent:eek:
received:eek:
sent:eek:
received:eek:
sent:eek:
received:eek:
sent:eek:
received:eek:
sent:eek:
received:eek:
sent:eek:
received:eek:
sent:eek:
received:eek:
sent:eek:
received:eek:
sent:eek:
received:eek:
sent:eek:
received:eek:
sent:eek:
received:eek:
sent:eek:
received:eek:
sent:eek:
received:eek:
sent:eek:
received:eek:
sent:eek:
received:eek:
sent:eek:
received:q
sent:p
received:q
sent:p
received:q
sent:p
received:q
sent:p
received:q
sent:p
received:q
sent:p
received:q
sent:p
received:h
sent:~
received:h
sent:~
received:h
sent:~
received:h
sent:~
received:h
sent:~
received:h
sent:~
received:h
sent:~
received:h
sent:~
received:h
sent:~
received:h
sent:~
received:44
sent:66
received:44
sent:66
received:44
sent:66
received:44
sent:66
received:44
sent:66
received:44
sent:66
received:44
sent:66
received:44
sent:66
received:44
sent:66
received:44
sent:66
received:44
sent:66
received:44
sent:66
received:44
sent:66
received:44
sent:66
received:44
sent:66
received:44
sent:66
received:7
sent:7
received:777
sent:777
received:777
sent:777
received:7
sent:7
received:777
sent:777
received:777
sent:777
received:2
sent:2
received:2
sent:2
received:h
sent:n
received:h
sent:n
received:h
sent:n
received:h
sent:n
received:h
sent:n
received:h
sent:n
received:h
sent:n
received:h
sent:n
received:h
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.

received:4
sent:6
received:4
sent:6
received:4
sent:6
received:4
sent:6
received:4
sent:6
received:4
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
received:7
sent:7
received:7
sent:7
received:7
sent:7
received:77
sent:77
received:77
sent:77
received: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 .
 
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.
 
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.
 
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.
 
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.
 
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.
 
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.

Latest threads

New Articles From Microcontroller Tips

Back
Top