CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Wrong data on the software UART
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
tranz



Joined: 10 Feb 2007
Posts: 78

View user's profile Send private message Send e-mail Yahoo Messenger MSN Messenger

Wrong data on the software UART
PostPosted: Tue Aug 12, 2008 8:50 am     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Tue Aug 12, 2008 10:18 am     Reply with quote

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

View user's profile Send private message Send e-mail Yahoo Messenger MSN Messenger

PostPosted: Tue Aug 12, 2008 12:27 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Aug 12, 2008 1:43 pm     Reply with quote

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

View user's profile Send private message Send e-mail Yahoo Messenger MSN Messenger

PostPosted: Tue Aug 12, 2008 1:55 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Aug 12, 2008 3:26 pm     Reply with quote

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

View user's profile Send private message Send e-mail Yahoo Messenger MSN Messenger

PostPosted: Tue Aug 12, 2008 4:10 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Aug 12, 2008 5:57 pm     Reply with quote

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

View user's profile Send private message Send e-mail Yahoo Messenger MSN Messenger

PostPosted: Wed Aug 13, 2008 7:51 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Aug 13, 2008 8:20 am     Reply with quote

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

View user's profile Send private message Send e-mail Yahoo Messenger MSN Messenger

PostPosted: Wed Aug 13, 2008 8:52 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Aug 13, 2008 2:54 pm     Reply with quote

What is the frequency of the xtal used with the 18F4680 ?
_________________
David
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Aug 13, 2008 5:41 pm     Reply with quote

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

View user's profile Send private message Send e-mail Yahoo Messenger MSN Messenger

PostPosted: Thu Aug 14, 2008 7:14 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Aug 14, 2008 2:54 pm     Reply with quote

Have you tried to narrow down the problem to one of the two PICs?
I suggested some tests...
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
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