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 CCS Technical Support

problem with UART on 16f688

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
rklan
Guest







problem with UART on 16f688
PostPosted: Sat May 31, 2008 3:00 am     Reply with quote

Hello,
I like to use the hardware uart in my 16f688.
It doesn't work yet.
When I use software uart it works fine. But i like to use interrupt so I have to use hardware uart.
I tried this:
Code:

while (TRUE)
{

#use rs232(baud=9600,bits=8,xmit=PIN_C1,rcv=PIN_C2,ERRORS) //use hardware uart
c=getchar();
putchar('@');
putchar(c);
 
}

This works with C1 and C2.... but when I use C4 and C5 (this must be the hardware UART, it does not receive......
Can anyone help??
Ttelmah
Guest







PostPosted: Sat May 31, 2008 4:26 am     Reply with quote

First, put the #USE_RS232 directive outside the program (or certainly outside the _while_ loop). This is a _preprocessor_ directive, setting up the port. It does cause the addition of configuration code for the UART, and if placed outside the code, this is automatically put at the start of the program. Including it inside the while loop, could result in this code being included in the loop, and this might well cause problems with the hardware UART....
Second, add the line to disable the comparator module in front of this code. The RC4 pin, is multiplexed with the comparator 2 output, and this could also cause problems (setup_comparator(NC_NC_NC_NC);).
Then, if it still doesn't work, post the compiler version. There was a fault on some versions of the compiler, not correctly initialising some peripherals like the comparator, and we will probably know if yours was a version with this problem.

Best Wishes
rklan
Guest







problem with UART on 16f688
PostPosted: Sat May 31, 2008 5:23 am     Reply with quote

Thanks!!
But it doesn't work yet....
I placed the #use rs232 directive in the loop, because I like to use a 2nd uart, later..
I like to receive from one and transfer to another.

But, the setup comparator(nc_nc_nc_nc) didn't help...
My compiler version:3.203

Thanks for your help!
Ttelmah
Guest







PostPosted: Sat May 31, 2008 8:22 am     Reply with quote

You still don't want to put the #use directive inside the loop. Do a search here on how to use two setups. They don't work as if they are a normal part of the code. The last one 'passed' in _linear_ routing (ignoring any loops), will be the one that applies. Really, it'd be very much easier to use streams, and these were introduced before your compiler version (about 3.180), so are the way to go. Look in the 'readme.txt' for your compiler which describes these.
Ouch. 3.203, is old. I'd be very suspicious this is not working right. Though I have older compilers, I have nothing comparable in the early 3.2xx versions, since I threw these away, as 'non usable', till about 3.221...
Do a search here, PCM programmer, has posted manual initialisation routines, for some of the early compiler problems in the past, that should get the basic serial working.

Best Wishes
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Sat May 31, 2008 8:42 am     Reply with quote

Quote:
This works with C1 and C2.... but when I use C4 and C5 (this must be the hardware UART, it does not receive......


#use rs232 is a pre-processor directive used at compilation time to configure the MCU,
hence -as Ttelmah said- it should be at the beginning.

To receive a char using the hardware UART you need:
1) enable the INT_RDA
2) enable the INT_GLOBAL
3) put the code inside the interrupt service routine.

PCM Version 3.203 was released in Jul/2004. I suggest you not to use it for a comercial product.


Humberto
rklan
Guest







PostPosted: Sat May 31, 2008 9:32 am     Reply with quote

It still does not work with the hardware uart...
I'll get a new compiler first.... that would be a nice idea....

Thanks for the tips everyone!

Code:
#include <16f688.h>
#fuses INTRC,NOWDT,NOPROTECT, NOMCLR
#use delay(clock=8000000)
#use rs232(baud=9600,bits=8,xmit=PIN_C4,rcv=PIN_C5,ERRORS) //use hardware uart

char c;

#int_rda
void Rx_isr() {
      
   printf("/r/ntest");
}


void main() {

enable_interrupts(global);
enable_interrupts(int_rda);
setup_comparator(NC_NC_NC_NC);
while (TRUE)
{
delay_ms (200);  //wait
putchar('@');

 
}


}
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Sat May 31, 2008 1:00 pm     Reply with quote

Try this:

Code:

#include <16f688.h>
#fuses PUT,INTRC,NOWDT,NOPROTECT, NOMCLR
#use delay(clock=8000000)
#use rs232(baud=9600,bits=8,xmit=PIN_C4,rcv=PIN_C5,ERRORS) //use hardware uart

char c;

#int_rda
void Rx_isr() {
    c=getchar();
    putchar(c);
}

void main()
{
    setup_oscillator(OSC_8MHZ);
    enable_interrupts(global);
    enable_interrupts(int_rda);
    setup_comparator(NC_NC_NC_NC);
    printf("/r/nTest");

while (TRUE)
  {
    output_low(PIN_Bx);  // Connect a LED in Bx to "see" that it is doing something   
    delay_ms(200);
    output_high(PIN_Bx);
    delay_ms(200);   
  }

}



Humberto
rklan
Guest







16f688 UART Problem
PostPosted: Mon Jun 02, 2008 11:40 am     Reply with quote

Hi Folks,

I bought myself a new compiler today.... and it's working!
So the bug was in my, very old, compiler.
Thanks for al the good code and tips.!!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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