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

16f88 strange UART behavior

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



Joined: 30 Jun 2007
Posts: 12

View user's profile Send private message

16f88 strange UART behavior
PostPosted: Thu Jul 14, 2011 1:10 am     Reply with quote

Greetings,
The following code will echo, but will not work with the case statement. Also kbhit() with a single getc() call will just loop, even though there are no characters being input.

I expect I have done something stupid, any insight would be appreciated. I have had some consistent UART problems with this chip.
Thanks in advance....

Code:

#include "main.h"
#include "flex_lcd.c"

#define rs485_cntrl pin_B0
void blink(void)
{
   output_high(pin_B1);
   delay_ms(1000);
   output_low(pin_B1);
   delay_ms(1000);

}

int16 ohms =      102;
int16 minutes =      20;
int fuel_percent =   73;
int1 on_off = 0;
char perc = "%";
char t = 'a';
char s = 'a';

void main()
{
   setup_oscillator(OSC_4MHZ|OSC_TIMER1);
   setup_ccp1(CCP_PWM);
   set_pwm1_duty(55);
   setup_comparator(NC_NC_NC_NC); 
   setup_timer_2(T2_DIV_BY_1,250,1);      //200 us overflow, 200 us interrupt
   lcd_init();
   delay_ms(10);
   output_low(RS485_cntrl);//enable rs485 chip to recieve
   setup_uart(TRUE);
//    output_high(RS485_cntrl);//enable rs485 chip to transmit

while (1)
 {
   output_low(RS485_cntrl);//enable rs485 chip to recieve
   delay_ms(1);

   if(kbhit(PORT1)){
   s = getc(); 
   s = getc(); 
   output_high(RS485_cntrl);//enable rs485 chip to transmit
     delay_ms(1);
    putc(s);
   delay_ms(1);
   output_low(RS485_cntrl);//enable rs485 chip to recieve
   delay_ms(1);
   }
switch(s){
   case 'a':
   printf(lcd_putc,"Fuel %c %u  Time %Lu\n",perc, fuel_percent, minutes);
   if (on_off)printf(lcd_putc,")hms= %Lu Pump = ON\n", ohms);
   if (!on_off)printf(lcd_putc,"Ohms %Lu Pump = OFF\n", ohms);
   delay_ms(400);
   break;

   case 'b':
   printf(lcd_putc,"Fuel %c %u  Time %Lu\n",perc, 12, 123);
   if (on_off)printf(lcd_putc,")hms= %Lu Pump = ON\n", ohms);
   if (!on_off)printf(lcd_putc,"Ohms %Lu Pump = OFF\n", ohms);
   delay_ms(400);
   break;

   case 'c':
   printf(lcd_putc,"Fuel %c %u  Time %Lu\n",perc, 37, 501);
   if (on_off)printf(lcd_putc,")hms= %Lu Pump = ON\n", ohms);
   if (!on_off)printf(lcd_putc,"Ohms %Lu Pump = OFF\n", ohms);
   delay_ms(400);
   break;
   default:
   printf(lcd_putc,"Fuel %c %u  Time %Lu\n",perc, 99, 999);
   if (on_off)printf(lcd_putc,")hms= %Lu Pump = ON\n", ohms);
   if (!on_off)printf(lcd_putc,"Ohms %Lu Pump = OFF\n", ohms);
   delay_ms(400);
   break;
   }
 }

Header file>>>>>>>>>>>>>>>>>>>>

#include <16f88.h>

#device adc=16

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES INTRC_IO                 //Internal RC Osc, no CLKOUT
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES CCPB3
#use delay(int=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_B5,rcv=PIN_B2,bits=8,stream=PORT1,errors)

Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Thu Jul 14, 2011 2:19 am     Reply with quote

I'd be suspicious of your RS4385 hardware, rather than the UART.

1) Have you got the RS485 bus _biased_ so when nothing is transmitting, receiving devices 'see' a high on their RX line?. This is required on RS485 - a couple of manufacturers do receivers that automatically do this. The 'A' line needs to be fractionally above the B line on the bus.
2) Is your bus terminated?. Again ties to the above, with it being possible to make the terminator also provide the bias - there are terminator chips that do this.
3) Is your buffer chip setup, so that when you are transmitting, the RX line to the PIC is again 'high'. You need to turn off the receive part at the same time you turn on the TX part, and have a pull-up resistor, so when the receive part is disabled, the RO line floats high (normally it goes high impedance when the receiver is disabled).

Unless all of the above are 'true', the UART _will_ be receiving unexpected characters, which will probably explain the problems...

Best Wishes
cogen



Joined: 30 Jun 2007
Posts: 12

View user's profile Send private message

PostPosted: Thu Jul 14, 2011 4:21 pm     Reply with quote

Ttelmah,
Thank you for your response. I will check the RS485 hardware this evening, (my other job).

I just wanted to thank you in person, (or as close as I can get). I have seen many, many thoughtful posts by you on this forum. I for one, am really appreciative of your knowledge and the effort you put into supporting this forum. Thank you!

I will post my findings on the hardware at my first opportunity.

Mike
cogen



Joined: 30 Jun 2007
Posts: 12

View user's profile Send private message

PostPosted: Thu Jul 14, 2011 7:52 pm     Reply with quote

I have built the hardware as Ttelmah has described with the exception of pulling the rx pin on the pic high. Was not sure that was what was meant. Rs485 chip is a sn65hvd1780.
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Fri Jul 15, 2011 2:08 am     Reply with quote

The point is when you turn on the transmitter, and turn off the receiver, the receive output on the chip _floats_. If you look at the data sheet, when RE is high. there is between -1, and 1uA of current flow on the output pin (so indeterminate direction). If this is connected to the receive input of the PIC, which has no inherent pullup, the line can float anywhere (limited only by the clamp diodes in the chips). Certainly _not_ guaranteed to give a nice 'idle' level, instead you may well receive garbage characters (depending on electrical noise, and the actual float current.....
You have to ensure that when you turn the receiver off, the R line floats _high_.

Best Wishes
cogen



Joined: 30 Jun 2007
Posts: 12

View user's profile Send private message

PostPosted: Sun Jul 17, 2011 10:52 am     Reply with quote

Well, I pulled the rx pin high, and did not solve the problem. I am doing PIC to PIC communication however, and the problem seems to clear up when I cut the PC out of the picture. I had tried a couple of different terminal programs in case there were extraneous characters being introduced. No help.

I do seem to remember having a similar problem with the 16f88 a while ago with just a rs232 connection....Thank you for the help!
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