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.

Troubles with UART

Status
Not open for further replies.

bobo99

New Member
Hi there my name is Bojan and I'm trying to establish PIC to PC communication for a school project. Yet when I ask my teacher for help i get nothing. So I'm on my own and I've tried to figure this out for about two months now. I'm following a tutorial written by Chuck Hellebuyck "Getting Started with PICs."... i have my circuit breadboarded as seen in the attached file. I attempted using picbasic pro as was outlined in the tutorial but absolutely nothing happened. My teacher then told me to use mikrobasic.. So i tried reading the help files and this is what code i came up with :

program soft_uart_test
dim received_byte, er as byte
dim y as word
y = 56
main:
Soft_Uart_Init(PORTC, 1, 2, 9600, 0) ' Initialize soft UART
while true
do
Soft_Uart_Write(y)
received_byte = Soft_Uart_Read(er) ' Read received data
loop until er = 0
PortB = received_byte
wend
end.

Again nothing really works... i used a logic probe and at the input into my chip i notice that I am receiving a pulse from the computer and it gets to the chip... another problem that i ran into was that when i compiled using mikrobasic... it says Warning:Generated Baud Rate 10000 (error 4.17%).... this project has been killing me... any help would be greatly appreciated... thanks


bojan
 

Attachments

  • untitled.JPG
    untitled.JPG
    82.5 KB · Views: 394
How are you communicating with this code? If you are using Hyperterminal then you should have it set to 9600,N,8,1 and flow control set to NONE.

Your using the software UART even though the 876 has a hardware one. Microelectronica supply the following example of using the hardware module,
Code:
program Usart_lib
dim i as byte

main:
   Usart_init(2400)                ' Initialize USART module (8 bit, 2400 baud rate, no parity bit...
   while true
     if Usart_Data_Ready = 1 then  ' If data is received:
       i = Usart_Read              ' Read the received data
       Usart_Write(i)              ' Send data via USART
     end if
   wend
end.

Maybe you could try this to make sure your circuit is correct. You can tell if this works as Hyperterminal should display what you type. You would of course set Hyperterminal to 2400 for the above example to work.

Mike.
 
P.S. the other thing that could be wrong is the cable. You may have a null modem cable instead of a straight through cable. If this is the case then the circuit can be slightly modified to get it working.
 
I think that in the data sheet i found that RB7 is receive and RB6 is transmit, I think:S so I hooked everything up according to that but I still cant get it to work:S. Just a random question in the schematic for my breadboard why are the pins 1,6,and 4 on the serial cable supposed to be connected together. And another weird thing is that whenver i open hyperterminal it connects to COM1, but it wont let me type anything...so I always resort to using the USART terminal that is provided with mikrobasic. I Also tried using your code and I still cant manage to get it to work.

again thanks in advance

bojan
 
Last edited:
bobo99 said:
I think that in the data sheet i found that RB7 is receive and RB6 is transmit, I think:S so I hooked everything up according to that but I still cant get it to work:S. Just a random question in the schematic for my breadboard why are the pins 1,6,and 4 on the serial cable supposed to be connected together. And another weird thing is that whenver i open hyperterminal it connects to COM1, but it wont let me type anything...so I always resort to using the USART terminal that is provided with mikrobasic. I Also tried using your code and I still cant manage to get it to work.

again thanks in advance

bojan

Do you have a MAX232 line driver, or equivalent, connected between the PIC pins 6 & 7 and the PC' serial port??

The reason why some pins are linked on the 9way 'D' is because the PC program is testing the state of these pins, to see if it needs to have TX or RX communication with the remote device.

The PC program can be written so it dosn't test the pins, then you just require
TXD/RXD and ground wires.

>> I think that in the data sheet i found that RB7 is receive and RB6 is transmit, I think

This is referring to when you use the PIC's internal UART, I can't advise on your code, I use assembler.
 
Last edited:
SUCESS!!! I send a 3 to my pic! and i get back a 3, i send a 4 i get back a 4! horray!:D:D thanks Pommie for the code!!! As to why your code worked and mine didn't.... i don't know:( thanks! Now i want to be able to make this more interactive, how could i make my pic make send out a sentence such as...."Enter Which LED to light".... i tried creating a variable....such as dim y as word.... and then said y = "Enter Which LED TO LIGHT" but it wouldnt want to compile that... or i guess before i start making this more difficult i have to get my basic project working. I set TRISB = 0 (set it as output) and then later in my code i tell it to bounce back what i typed in but i also want the pic to light up the corresponding light. Using PORTB.i to turn on the corresponding pin number, but this doesnt seem to work..

i = Usart_Read ' Read the received data
Usart_Write(i)
PORTB.i = 1


bojan
 
Last edited:
Ok so slightly confused, I really have no idea how to program, but i'm getting somwhere through trial and error:p
program datatransmit
dim i as byte
dim y as char [40]
dim j as char [40]
y = "Please Choose a Number Between one and seven"
j = "Thank you for choosing"
TRISB = 0
main:
Usart_init(9600)
Usart_Write_Text (y)
loop:
if Usart_Data_Ready = 0 then
goto loop
Usart_Data_Ready = 1 then ' If data is received:
i = Usart_Read ' Read the received data
Usart_Write_Text (j)
Usart_Write (i)
PORTB = i
delay_ms (3000)
goto main ' Send data via USART
end if
wend
end.
my main code is this , i'm telling the pic to ask me what number i want to enter the 'y' variable, i then want it to wait untill i actually send a number (not sure if i went at it the best way), but when i try to compile this...it says Identifier '=' was not declared for the part that i highlighted...:'( why:S.
(btw sorry if i'm posting this in the wrong forum)

bojan
 
You have a few things wrong,
You have a wend without a while.
You have an if without endif. (might be wrong here)

I would guess (I don't use basic on pics) it should be something like,
Code:
main
Usart_init(9600)
Usart_Write_Text (y)
loop:
if Usart_Data_Ready = 0 then
   ' do nothing
else
  i = Usart_Read ' Read the received data
  Usart_Write_Text (j)
  Usart_Write (i)
  PORTB = i
  delay_ms (3000)
end if
goto loop

Mike.
 
I just had a look at the microelectronica site and found **broken link removed**. I also just tried the example in their compiler and it worked. I like to have "main" first in my code and so moved it to the top of the code - this resulted in a barrage of (non sensical) errors. It seems that the compiler is only single pass!!!!

Personally, I'd find a different compiler. MicroBasic appears to be a pile of pooh.

Mike.
 
Pommie said:
I just had a look at the microelectronica site and found **broken link removed**. I also just tried the example in their compiler and it worked. I like to have "main" first in my code and so moved it to the top of the code - this resulted in a barrage of (non sensical) errors. It seems that the compiler is only single pass!!!!

Personally, I'd find a different compiler. MicroBasic appears to be a pile of pooh.

To be honest it sounds more like you're doing something wrong? - and good programming practice (like Pascal forces on you - which is why it used to be the language of choice at University) means the main program should be at the end.
 
Nigel Goodwin said:
To be honest it sounds more like you're doing something wrong?

If I take the example code on **broken link removed**. I can copy it into MicroBasic and it compiles fine - just warns about the baud rate error. If I now add a constant so the code becomes,

Code:
dim txt, delim as char[20]
[COLOR="Red"]const MSG = "Hello"[/COLOR]

' Reads chars from usart until delimiter sequence is detected.

It now doesn't compile. It gives the same warning but nothing else - no error message. I rem the line - it compiles. Put it back - no compile.

BTW, the constant was chosen because that is the exact example in the help file.

Hence my conclusion - pile of pooh.

Mike.
 
I don't use BASIC, but that declaration seems wrong. It should be something like this: const MSG as string[10] = "Hello"

EDIT: no... the type is optional (pag 61) :eek:
 
Last edited:
i have this code working :

Usart_init(9600)
main:
Usart_Write_Text (y)
if
Usart_Data_Ready = 1
then
i = Usart_Read
Usart_Write_Text (j)
Usart_Write (i)
PORTB=i
end if
delay_ms (5000)
goto main
end.

Weird thing is it seems to skip my Usart_Data_Ready = 1 step. The output on my screen is "Please choose a number...." every 5 seconds, and when i try to send a number it doesnt accept it. The "then" part of the code doesnt seem to work:S

bojan
 
The resetting is probably caused by the Watchdog Timer. Try turning it of in the config (edit project).

Your other problem is probably caused by splitting up the if statement. Try moving it onto one line so it reads,
Code:
if Usart_Data_Ready = 1 then

Mike.
 
weird:S turned off watchdog timer( it was already off) put the if statement all in one line... it still repeats Usart_Write_Text (y) every 5 seconds, and when I send a number it stops sending out the greeting msg, but doesnt turn on the light or send back then number:S its still skipping the whole "then" statement:S

bojan

btw thanks so much for all the help
 
Just noticed that you loop back to main after a delay of 5 seconds, so it's doing what it's supposed to do. Try changing it to,
Code:
Usart_init(9600)
main:
Usart_Write_Text (y)
Loop:
if Usart_Data_Ready = 1 then
	i = Usart_Read
	Usart_Write_Text (j)
	Usart_Write (i)
	PORTB=i
end if
delay_ms (5000)
goto Loop
end.

As to why it isn't echoing back, have you declared i as global and it's being used in Usart_Write_Text. Try changing the variable name to something else.

Mike.
 
thank you, you are a god, this is amazing!! it finally works:D:D:D thank you ! again :D...lol i have a random 7 segment display, it'll be interesting as to weather i'll be able to code that,

thanks again mike!


bojan
 
Status
Not open for further replies.

Latest threads

Back
Top