View previous topic :: View next topic |
Author |
Message |
kookee
Joined: 11 Feb 2016 Posts: 4 Location: Pakistan
|
Serial Communication between 2 PIC16F877a |
Posted: Thu Feb 11, 2016 10:21 am |
|
|
Hi friends,
I am a new in MCU programming and I use CCS C compiler. Friends I want to communicate between two PIC16F877A MCU using serial port with serial interrupt but I can’t do it.
Please help me in this regard. It’s more easy for me if share with Proteus circuit.
Thanks _________________ When we give cheerfully and accept gratefully, everyone is blessed |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Thu Feb 11, 2016 11:36 am |
|
|
Hi,
Please post the code that you can't get to work, and we'll help you get it running. This is easier for us, and is a much better way for you to learn than just giving you a piece of code! _________________ John
If it's worth doing, it's worth doing in real hardware! |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9246 Location: Greensville,Ontario
|
|
Posted: Thu Feb 11, 2016 12:04 pm |
|
|
Obvious place to start is with the ex_sisr that CCS supplies in the examples folder but HOW you connect to 2 PICs is very important !
You don't specify if you want full duplex, half duplex, single wire, or ??? it is possible to connect both PICs without any logic level translation( no MAX232) but we'd need more information from you.
Jay |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Fri Feb 12, 2016 2:44 am |
|
|
Like others have said, we need more info.
See the sticky about Proteus on this forum.
I'd begin by getting each device to talk to a PC, (will need a MAX232 which you can remove later if you don't need it).
Jay suggests using one of the CCS supplied examples as a good starting point.
Mike |
|
|
kookee
Joined: 11 Feb 2016 Posts: 4 Location: Pakistan
|
|
Posted: Fri Feb 12, 2016 11:04 am |
|
|
Thanks for reply
I connect
TX of PIC#1 to RX of PIC#2
RX of PIC#1 to TX of PIC#2
with 20MHz clock and RS232 setting is
Code: | #use rs232(baud=9600, xmit=PIN_C6,rcv=PIN_C7) |
My code for both PICs are under please. and i use CCS C compiler 4.114.
Code: |
/* USE FOR PIC # 1*/
#include <main.h>
#include "Flex_LCD420.C"
#define BUFFER_SIZE 32
BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
BYTE next_out = 0;
#int_rda
void serial_isr() {
int t;
buffer[next_in]=getc();
t=next_in;
next_in=(next_in+1) % BUFFER_SIZE;
if(next_in==next_out)
next_in=t; // Buffer full !!
}
#define bkbhit (next_in!=next_out)
BYTE bgetc() {
BYTE c;
while(!bkbhit) ;
c=buffer[next_out];
next_out=(next_out+1) % BUFFER_SIZE;
return(c);
}
void main()
{
lcd_init();
// setup_adc_ports( RA0_RA1_RA3_ANALOG ); // set AN0, AN1 & AN3 as a Analog
// setup_adc(ADC_CLOCK_INTERNAL);
lcd_gotoxy(1,1);
printf(lcd_putc,"ASDF");
printf("CAN I READ IT");
while(true)
{
output_toggle(LED);
delay_ms(DELAY);
printf("H");
}
} |
Code: |
/* USE FOR PIC # 2 */
#include <main.h> //PIC2
#include "Flex_LCD420.C"
#define BUFFER_SIZE 32
BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
BYTE next_out = 0;
#int_rda
void serial_isr()
{
int t;
buffer[next_in]=getc();
t=next_in;
next_in=(next_in+1) % BUFFER_SIZE;
if(next_in==next_out)
next_in=t; // Buffer full !!
//lcd_gotoxy(3,3);
//printf(lcd_putc,"RS232 Read %d",buffer);
}
#define bkbhit (next_in!=next_out)
BYTE bgetc()
{
BYTE c;
while(!bkbhit) ;
c=buffer[next_out];
next_out=(next_out+1) % BUFFER_SIZE;
return(c);
}
void main()
{
lcd_init();
// setup_adc_ports( RA0_RA1_RA3_ANALOG ); // set AN0, AN1 & AN3 as a Analog
// setup_adc(ADC_CLOCK_INTERNAL);
lcd_gotoxy(1,1);
printf(lcd_putc,"ASDF");
delay_ms(100);
//printf("CAN I READ IT");
while(true)
{
//lcd_gotoxy(2,2);
//printf(lcd_putc,"RS232 Read");
output_toggle(LED);
delay_ms(DELAY);
lcd_gotoxy(1,1);
printf(lcd_putc,"1232456"); //%c",bgetc());
}
} |
_________________ When we give cheerfully and accept gratefully, everyone is blessed |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Fri Feb 12, 2016 12:22 pm |
|
|
Please use the code button, it preserves the formatting and makes code easier to read.
A simple guide as to what's supposed to happen would help.
Also you're not telling us what you can/cannot see.
Like I said before it's simpler to start by getting each processor to talk/listen to a PC via a MAX232 chip.
That way you get a better feel for what's going on.
You can send known signals to the PIC and see what it does with them, and monitor what's coming back, (assuming it's all in ASCII).
Mike |
|
|
kookee
Joined: 11 Feb 2016 Posts: 4 Location: Pakistan
|
|
Posted: Fri Feb 12, 2016 1:17 pm |
|
|
Mike Walne wrote: | Please use the code button, it preserves the formatting and makes code easier to read.
A simple guide as to what's supposed to happen would help.
Also you're not telling us what you can/cannot see.
Like I said before it's simpler to start by getting each processor to talk/listen to a PC via a MAX232 chip.
That way you get a better feel for what's going on.
You can send known signals to the PIC and see what it does with them, and monitor what's coming back, (assuming it's all in ASCII).
Mike |
thanks
i test my code in Proteus.
in it PIC 1 send data but PIC 2 not show it on LCD _________________ When we give cheerfully and accept gratefully, everyone is blessed |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9246 Location: Greensville,Ontario
|
|
Posted: Fri Feb 12, 2016 1:33 pm |
|
|
Proteus is BUSTED !!!
get real hardware, wire it up,test, report back
No one here wants to FIX busted Proteus
Jay |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Feb 12, 2016 8:46 pm |
|
|
This schematic shows the serial pin connections between the two PICs:
https://electrosome.com/wp-content/uploads/2014/09/PIC-to-PIC-
Communication-UART-Circuit-Diagram.jpg
Tx on the first PIC goes to Rx on the 2nd PIC, etc. The DIP switch on
the first PIC is read by a program and the value is sent by serial to the
2nd PIC. The 2nd PIC displays the DIP switch setting on the LEDs.
There are no MAX232A chips in the circuit, so this schematic is only for
short range - basically, two boards on your workbench. |
|
|
|