View previous topic :: View next topic |
Author |
Message |
tranz
Joined: 10 Feb 2007 Posts: 78
|
Wrong data on the software UART |
Posted: Tue Aug 12, 2008 8:50 am |
|
|
Hey guys,
I have interfaced 16F877A with 18F4620 and defined software UART pins to make them communicate with each other.
Code for 18f4620
Code: |
#use rs232(baud=9600, xmit=PIN_C1,rcv=PIN_C0, STREAM=HOSTPC)
void main()
{
while(1)
{
BYTE c='A';
fputc(c, HOSTPC);
delay_ms(1000);
c='B';
fputc(c, HOSTPC);
delay_ms(1000);
}}
|
Code for 16F877A
Code: |
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8, STREAM=HOSTPC)
#include <stdio.h>
#include "lcd2.c"
void main()
{
byte d,c,x,output;
lcd_init();
while(TRUE)
{
c=fgetc(HOSTPC);
printf(lcd_putc, "\fValue= %2X", c);
delay_ms(1000);
output_high(PIN_C1);//LED high
delay_ms(100);
output_low(PIN_C1);//LED low
}
}
|
I am not getting A and B on the LCD output, for 'A' I get "F0", for 'B' I get "F1".
Any Suggestions? |
|
|
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Tue Aug 12, 2008 10:18 am |
|
|
Just a thought, but on the 16F877A yoiu are using a REAL hardware usart pins.
That might be mixing it up.
Try the whole interupt and hardware usart firmware on the 16F877a
see C:\Program Files\PICC\Examples\EX_SISR.C |
|
|
tranz
Joined: 10 Feb 2007 Posts: 78
|
|
Posted: Tue Aug 12, 2008 12:27 pm |
|
|
Here is the new code with the suggested modification
Code: |
#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();
printf(lcd_putc,"\fValue= %2X",buffer[next_in]);
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();
enable_interrupts(int_rda);
enable_interrupts(global);
printf(lcd_putc,"\r\n\Running...\r\n");
// The program will delay for 10 seconds and then display
// any data that came in during the 10 second delay
do {
delay_ms(1000);
printf(lcd_putc,"\r\nBuffered data => ");
while(bkbhit)
putc( bgetc() );
output_high(PIN_C1);//LED high
delay_ms(100);
output_low(PIN_C1);//LED low
} while (TRUE);
}
|
But still its showing the same output as the previous one, one thing in common is "F" preceding a number, e.g. 'B' = F4; '2'= F1
I think the data is getting corrupted somehow, but cant point the finger where.
Suggestions? |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Aug 12, 2008 1:43 pm |
|
|
Your programs are not complete; missing the #fuses lines, etc.
What is your compiler version number?
For the PIC18F4620 the CCP2 module has priority over digital I/O on pin C2. Some compiler versions disable this module automatically, but to make sure it is best to add to start of your main the following instruction: Code: | setup_ccp2(CCP_OFF); |
|
|
|
tranz
Joined: 10 Feb 2007 Posts: 78
|
|
Posted: Tue Aug 12, 2008 1:55 pm |
|
|
Thanks ckielstra, but it did not help. I am still getting the same outputs as before. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Aug 12, 2008 3:26 pm |
|
|
For me to reproduce your problem you will have to answer the other two issues mentioned in my previous post:
1) What is your software version number?
2) Add the missing items from your example programs (#fuses, etc).
And coming to think of it a few more questions:
3) You are testing on real hardware or using a simulator program like Proteus?
4) If using real hardware, what voltages are both chips running at?
5) How is the physical connection between the two chips established?
- MAX232 or similar driver chips?
- Distance between the devices?
- etc. |
|
|
tranz
Joined: 10 Feb 2007 Posts: 78
|
|
Posted: Tue Aug 12, 2008 4:10 pm |
|
|
The version of the compiler being used is 4.073.
The fuses on 18F4620 are
Code: |
#include <18f4680.h>
#use delay(clock=40000000)
#fuses H4
#fuses NOFCMEN
#fuses NOXINST
#fuses NOIESO
#fuses NOPBADEN
#fuses MCLR
#fuses NOWDT
#fuses NOPROTECT
#fuses NODEBUG
#fuses NOSTVREN
#fuses PUT
#fuses NOLVP
#fuses NOBROWNOUT
#fuses NOCPD
#fuses WRT
#fuses NOWRTD
#fuses NOEBTR
#fuses NOCPB
#fuses NOEBTRB
#fuses WRTB
|
The fuses in 16F877A are
Code: |
#include <16F877A.h>
#fuses HS,NOLVP,NOWDT,PUT
#use delay(clock=20000000)
|
I am testing this with real hardware, and the pins above mentioned are connected directly i.e. TX to RX and vice versa.
Both these micros are running at 5V and the distance between them is about 3mm.
Do you think the difference in clock speeds can be a cause of this? if so how do you think I can rectify this?
Regards |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Aug 12, 2008 5:57 pm |
|
|
Quote: | Do you think the difference in clock speeds can be a cause of this? | This shouldn't be a problem. The compiler will warn you if a certain baudrate can not be achieved for your clock speed.
I don't know what is causing your problems. The fuses you have specified look OK and your compiler version v4.073 is a good one.
Code: | #include <18f4680.h>
#use delay(clock=40000000)
#fuses H4 | It's better to locate the '#use delay' instruction after the #fuses are specified. In this setup it's no problem but I just found out compiler v4.077 has a bug if you specify the internal oscillator (INTRC_IO).
Try to narrow down the problem. Figure out a test to proof which of the two systems is at fault. For example by measuring the serial signal with a scope.
A wild guess is that one of the two systems is running with a clock speed off by a factor 4. This is easy to test by blinking a led every second. Use a stopwatch to measure a 10 second period (don't count by head).
Another test worth trying is to run the PIC18F4680 on the internal oscillator without PLL: Code: | #include <18f4680.h>
#fuses INTRC_IO
... other fuses
#use delay(clock=8MHz) // Note: this line _after_ the fuses (compiler v4.077 bug). |
|
|
|
tranz
Joined: 10 Feb 2007 Posts: 78
|
|
Posted: Wed Aug 13, 2008 7:51 am |
|
|
Thanks Ckielstra,
The system is now working, the problem was with the baud rate, since 16f was running at 20MHz and 18F was running at 40Mhz, I calculated the available baud rate ranges on both these clock rates, 9600 was way to low.
So the solution was going for a higher baud rate. the 18F is now running at 19800 baud and the 16F at 38400 baud. With this, the data is being displayed perfectly.
Cheers |
|
|
drh
Joined: 12 Jul 2004 Posts: 192 Location: Hemet, California USA
|
|
Posted: Wed Aug 13, 2008 8:20 am |
|
|
Quote: | the 18F is now running at 19800 baud and the 16F at 38400 baud. With this, the data is being displayed perfectly.
|
You still have a problem. The "system" should work with the same baud rates. _________________ David |
|
|
tranz
Joined: 10 Feb 2007 Posts: 78
|
|
Posted: Wed Aug 13, 2008 8:52 am |
|
|
Well the data seems to come out fine. I have checked it with various inputs, and it displays the correct output.
I am also confused with the results, but it seems to work. |
|
|
drh
Joined: 12 Jul 2004 Posts: 192 Location: Hemet, California USA
|
|
Posted: Wed Aug 13, 2008 2:54 pm |
|
|
What is the frequency of the xtal used with the 18F4680 ? _________________ David |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Aug 13, 2008 5:41 pm |
|
|
It's not good when you have to select two different baud rates to make it work.
I checked the fuses generated by your v4.073 compiler and these are correct.
The code for setting the baudrate on the PIC16F877A to 9600 baud is also correct. Leaving the PIC18 with the software UART as the most likely error source.
Have you performed one of the tests I suggested?
Quote: | The fuses on 18F4620 are
Code:
#include <18f4680.h> | Oops.... you are mixing the configuration of two different processors here....
Which is the one you are using |
|
|
tranz
Joined: 10 Feb 2007 Posts: 78
|
|
Posted: Thu Aug 14, 2008 7:14 am |
|
|
18F4680 is the one being used in this case, sorry my bad it was a typo. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu Aug 14, 2008 2:54 pm |
|
|
Have you tried to narrow down the problem to one of the two PICs?
I suggested some tests... |
|
|
|