View previous topic :: View next topic |
Author |
Message |
muhilazhagan
Joined: 12 Oct 2015 Posts: 5 Location: chennai
|
Serial communication between two PICs |
Posted: Mon Oct 12, 2015 5:52 am |
|
|
I couldn't establish serial communication between two pic microcontrolllers.
This is my code for transmitter.
Code: |
#include <16f877a.h>
#fuses HS,NOLVP,NOWDT
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,errors)
void main()
{
int c=2;
delay_ms(100);
putc('c');
}
|
for receiver
Code: |
void main()
{
int c;
lcd_init();
while (1)
delay_ms(50);
{
c = getc();
printf(lcd_putc," date %02d",c);
}
|
Someone please help. |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Mon Oct 12, 2015 11:14 am |
|
|
Tell us what you can see.
Mike |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Oct 12, 2015 11:47 am |
|
|
Quote: | void main()
{
int c=2;
delay_ms(100);
putc('c');
while(TRUE);}
|
The int c=2 line does nothing. You are sending a lower-case c character.
But, the PIC will go to sleep before the character is sent. It will not be
sent. You need to add a while(TRUE); statement at the end of main()
to prevent the PIC from going into Sleep mode.
Quote: | void main()
{
int c;
lcd_init();
while (1)
delay_ms(50);
{
c = getc();
printf(lcd_putc," date %02d",c);
} |
The lines shown in bold are an endless while() loop. It will do
delay_ms(50) forever. The remaining code will not be executed.
You need to learn about using braces with a while() statement. |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Mon Oct 12, 2015 5:24 pm |
|
|
Divide and conquer helps in this situation.
Test the TX by getting it to talk to a PC.
Test the RX by getting a PC to talk to it.
(You may need a temporary MAX232 chip, or the like.)
THEN get TX and RX to talk to each other.
Mike |
|
|
muhilazhagan
Joined: 12 Oct 2015 Posts: 5 Location: chennai
|
|
Posted: Tue Oct 13, 2015 1:30 am |
|
|
thanks guys i modified the program still i couldnt get any output
#include <16f877a.h>
#fuses HS,NOLVP,NOWDT
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,errors)
//transmitter
void main()
{
delay_ms(100);
putc('c');
while(true);
}
receiver:
void main()
{
int c;
lcd_init();
delay_ms(50);
c = getc();
printf(lcd_putc," %02c",c); // this line is not executing
while (1);
}[/code]
even now i could see nothing in my lcd |
|
|
muhilazhagan
Joined: 12 Oct 2015 Posts: 5 Location: chennai
|
|
Posted: Tue Oct 13, 2015 1:30 am |
|
|
thanks guys i modified the program still i couldnt get any output
Code: |
#include <16f877a.h>
#fuses HS,NOLVP,NOWDT
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,errors)
//transmitter
void main()
{
delay_ms(100);
putc('c');
while(true);
}
receiver:
void main()
{
int c;
lcd_init();
delay_ms(50);
c = getc();
printf(lcd_putc," %02c",c); // this line is not executing
while (1);
} |
even now i could see nothing in my lcd.
i have also tried with this below code but nothing happens
Code: | void main()
while(1)
{
int c;
lcd_init();
delay_ms(50);
c = getc();
printf(lcd_putc," %02c",c); // this line is not executing
} |
++++++++++++++++++
Fixed code block.
- Forum Moderator
++++++++++++++++++
Last edited by muhilazhagan on Tue Oct 13, 2015 1:52 am; edited 2 times in total |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19505
|
|
Posted: Tue Oct 13, 2015 1:45 am |
|
|
Start with learning to properly use the code buttons.....
Then look through the forum. Simple thing. LCD's need _time_ to wake before they can be used. The original Hitachi displays needed 90mSec (in their data sheet), but this is from when they 'start to wake', which is a lot higher voltage than the PIC needs. This is why the driver has a short delay in it, _but_ most of the 'clone' displays need longer. Something like 0.5 seconds typically.
master:
Code: |
#include <16f877a.h>
#fuses HS,NOLVP,NOWDT
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,UART1,bits=8,errors)
//transmitter
void main(void)
{
delay_ms(1000); //give time for the slave to wake
while(TRUE)
{
putc('c');
delay_ms(1000);
}
}
//This way a character will be sent every second
|
receiver:
Code: |
//header as master - you don't show what you are using.
void main(void)
{
int c, ctr=0;
delay_ms(500); //allow time for the LCD to be ready
lcd_init(); //now initialise
while (TRUE);
{
c = getc();
printf(lcd_putc,"\f%c %d",c,ctr++);
}
}
|
Now it'll count each time the character is received.
"%c" prints a single character. Using a 'width' with this is a bit strange. |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Tue Oct 13, 2015 2:29 am |
|
|
Are your PICs running at all?
Can you do the simple LED flasher?
(At the correct speed!)
Is your TX actually transmitting?
Will the LCD do a "Hello World" test?
...........
...........
It's all basic debugging.
Mike |
|
|
muhilazhagan
Joined: 12 Oct 2015 Posts: 5 Location: chennai
|
|
Posted: Tue Oct 13, 2015 6:45 am |
|
|
all those things are done and my pic interfaced with rtc is displaying time in lcd so no problem with pic. |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Tue Oct 13, 2015 7:10 am |
|
|
How do you KNOW your TX is transmitting at the correct rate?
Mike
PS Have you tried my previous divide and conquer suggestion? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19505
|
|
Posted: Tue Oct 13, 2015 7:40 am |
|
|
and, how is the connection made between the PIC's?. |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Tue Oct 13, 2015 7:57 am |
|
|
muhilazhagan wrote: | all those things are done and my pic interfaced with rtc is displaying time in lcd so no problem with pic. |
Hi,
Please post a picture of your actual hardware. Until we see that this is not exclusively a Proteus project, we can't offer any more help!
Pardon my skepticism, but I find it rather unlikely that you've got a *real* LCD and a *real* RTC working! Please prove me wrong! _________________ John
If it's worth doing, it's worth doing in real hardware! |
|
|
muhilazhagan
Joined: 12 Oct 2015 Posts: 5 Location: chennai
|
|
Posted: Tue Oct 13, 2015 11:33 pm |
|
|
finally done thankyou so much for ur time.
i had the problem with my hardware
thanks once again |
|
|
muralid
Joined: 13 Oct 2015 Posts: 9
|
|
Posted: Tue Oct 13, 2015 11:47 pm |
|
|
guys i am new to this forum.
I want transfer the message from one pic to another pic. I mean not a single character but a string. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19505
|
|
Posted: Wed Oct 14, 2015 1:02 am |
|
|
Understand one thing. In C, there is actually no such thing as a string!.....
I'm being pedantic, but other languages have a 'string' as a type. In C, a 'string' is just an array of characters _terminated with a '\0' (NULL)_.
Now if you look in the library 'input.c', you will find the source code for a function 'get_string', which receives characters, and adds them to an array, until it sees a carriage return, or too many characters arrive.
Sending or receiving a 'string' is just a matter of repeatedly handling single characters. It's up to use to decide what to use to signal the 'end' in transmission (typically line feed or carriage return is commonly used).
printf("A line of text\r");
Sends the sequence of characters 'A line of text' and the carriage return to the serial.
//With this as a defined variable
char buffer[20];
get_string(buffer,20);
Will fetch a maximum of _19_ characters from the serial, returning when the carriage return is seen.
'Why 19'?.
Remember my comment about a C string having a terminating NULL. This means a 19 character string needs 20 characters of storage. The get_string function takes it's 'max' value as the maximum it is allowed to put into the array, so will get 19+terminator, if told to limit to 20 characters. Look at the code, right near the start you will see the line 'max-=2', which it to allow first for the arrays being zero referenced (-1), and then for the terminator (-1).
So after this call on a receiving device connected to the machine sending using the printf, the array 'buffer' will contain "A line of text\0". Note not the carriage return, but a terminator instead. |
|
|
|