|
|
View previous topic :: View next topic |
Author |
Message |
tienchuan
Joined: 25 Aug 2009 Posts: 175
|
[Help] Soft UART PIC18F |
Posted: Sat Apr 06, 2013 2:40 am |
|
|
Hi all
I have a problem when using one hard UART and one soft UART of PIC18F4680.
The interrupts of receiver not work.
I used:
MCU: PIC18F4680
Complier: CCS C PCH 4.140
and here is code:
Code: |
#include <18LF4680.h>
#include <def_18f4680.h>
#fuses H4,NOPROTECT,NOLVP,NOWDT
#device *=16
#use delay(clock=40M)
#use rs232(FORCE_SW, baud=9600, xmit=PIN_A1,rcv=PIN_A0, STREAM=PIC) // giao tiep Pic18F second (Slave)
#int_ext
void ngat_rs232()
{
porte=0;
char c;
c=fgetc(PIC);
//fputc(c,COM);
chk_data=1;
}
void main(void)
{
set_tris_a(0X02);// SW UART 2: KET NOI PIC18F --TX:RA0, RX: RA1
set_tris_b(0);
set_tris_c(0x80);// HW UART 1: KET NOI PC --TX: RC6, RX: RC7
set_tris_d(0);
set_tris_e(0);
porte=0xff;
porta=0xff;
xoabuffer();
lcd_init();
lcd_gotoxy(1,1);
lcd_putc('\f');
printf(lcd_putc,"%s","12345678");
//------------------------------------------------------------
// SETUP INTERRUPTS
//------------------------------------------------------------
//enable_interrupts(int_rda);
enable_interrupts(int_ext);
ext_int_edge( H_TO_L );
//enable_interrupts(int_timer1);
enable_interrupts(global);
//fprintf(COM,"THIS IS SLAVE");
while(1)
{
porte=0xff;
if (chk_data==1)
{
lcd_gotoxy(1,2);
printf(lcd_putc,"%c",c);
chk_data=0;
}
}
|
Result, lcd not display the string i send from the pic master,
Please show me way to fix it.
Thanks all _________________ Begin Begin Begin !!! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Sat Apr 06, 2013 4:45 am |
|
|
Er.
Obvious reason.
INT_EXT, is on pin B0, not A1.
So it'll never trigger for data arriving on A1.....
Data sheet. |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
|
tienchuan
Joined: 25 Aug 2009 Posts: 175
|
|
Posted: Sun Apr 07, 2013 4:55 am |
|
|
Ttelmah wrote: | Er.
Obvious reason.
INT_EXT, is on pin B0, not A1.
So it'll never trigger for data arriving on A1.....
Data sheet. |
:( i can't find interrupt services routine for soft uart as #int_rda in hardware uart.
I need to get character is received, if i used pins configed (A! and A0), where i can get data from soft uart?
Please show me example code with sodf uart? I'm really neeed.
In addition, I want to know connect wire between 2 PIC to communication via soft uart? Need use max232 to convert data ?
Pls show me way to fix it
thanks. _________________ Begin Begin Begin !!! |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Sun Apr 07, 2013 5:38 am |
|
|
I don't use that PIC but downloaded the datasheet to read the specs...
Sorry , but you cannot use pins A0 or A1 for interrupts.The hardware is not capable of it.
Your choices are B0,B1,B2,B4,B5,B6 and B7.Those can have interrupts.
If you can only use A0 and A1,then you cannot implement a software UART with interrupt.
As suggested, a different PIC would be a better choice. Any that have 2 hardware UARTs.I use the PIC18F46K22 in a 40pin DIP package for all my projects.Along with 2 UARTS,these's lots of RAM,PROM and internal perhipherals.
Trying to decide which PIC to choose for a project can be hard.In my case it took me a week to read,re-read and decide the 46K22 was the best 'overall' PIC.While 'overkill' for many smaller projects,having a common, one-chip-does-anything, allows projects to be completed faster.
If you're stuck with what you have,use the software UART for the slowest speed device as you'll poll it very often to not lose data.
It's always best to use MAX232(or equal) which allows you to connect to a PC for testing,to verify your code works before you try PIC2PIC communications. CCS has example programs in the 'examples' flder as well as in the FAQ section of the help files(press F11 to access when your project is open).
hth
jay |
|
|
tienchuan
Joined: 25 Aug 2009 Posts: 175
|
|
Posted: Sun Apr 07, 2013 6:55 am |
|
|
temtronic wrote: | I don't use that PIC but downloaded the datasheet to read the specs...
Sorry , but you cannot use pins A0 or A1 for interrupts.The hardware is not capable of it.
Your choices are B0,B1,B2,B4,B5,B6 and B7.Those can have interrupts.
If you can only use A0 and A1,then you cannot implement a software UART with interrupt.
As suggested, a different PIC would be a better choice. Any that have 2 hardware UARTs.I use the PIC18F46K22 in a 40pin DIP package for all my projects.Along with 2 UARTS,these's lots of RAM,PROM and internal perhipherals.
Trying to decide which PIC to choose for a project can be hard.In my case it took me a week to read,re-read and decide the 46K22 was the best 'overall' PIC.While 'overkill' for many smaller projects,having a common, one-chip-does-anything, allows projects to be completed faster.
If you're stuck with what you have,use the software UART for the slowest speed device as you'll poll it very often to not lose data.
It's always best to use MAX232(or equal) which allows you to connect to a PC for testing,to verify your code works before you try PIC2PIC communications. CCS has example programs in the 'examples' flder as well as in the FAQ section of the help files(press F11 to access when your project is open).
hth
jay |
thanks temtronic
I'll test with your ideas.
In my project, can not used the Pic with 2 hard UART, because the computer is only ONE Com port and not allow change another PIC18F model.
I used 2 PIC18F4680 for my project, the first PIC18F(master) will connect directly with PC(Com Port) via hard UART,and trasfer data to second PIC18F(Slave) via soft UART.
My model circuit is:
PC <----Hard UART----> PIC18F (Master)----SOFT UART--->PIC18F(Slave)
p/s: PIC18F Slave only receiver data from PIC18F Master, not tranceiver.
I want to known that is needed Max232 between 2 PIC18F( Master and Slave), if not, how to wiring between them?
I'll change code and test with your method, thanks u _________________ Begin Begin Begin !!! |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Sun Apr 07, 2013 8:15 am |
|
|
Yes, always use MAX232 for UART communications. It allows you to use PC for testing code, making it easier to debug.
In your situation the software UART should work fine connected to the 'slave' as the 'master' PIC will be in control, 'polling' or 'asking' the slave do you have any new data?
hth
jay |
|
|
tienchuan
Joined: 25 Aug 2009 Posts: 175
|
|
Posted: Sun Apr 07, 2013 9:08 am |
|
|
temtronic wrote: | Yes, always use MAX232 for UART communications. It allows you to use PC for testing code, making it easier to debug.
In your situation the software UART should work fine connected to the 'slave' as the 'master' PIC will be in control, 'polling' or 'asking' the slave do you have any new data?
hth
jay |
Thanks temtronic.
I fixed it correct.
I'm not using MAX232 between 2 PIC18F4680.
I connected as :
TX(Master)->RX(Slave)
RX(Master)->TX(Slave)
mass-->mass
And i'll update code later!
THanks and regards. _________________ Begin Begin Begin !!! |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|