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

RS-232 and PIC16F876: Buffer problem?

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



Joined: 21 Oct 2007
Posts: 2

View user's profile Send private message

RS-232 and PIC16F876: Buffer problem?
PostPosted: Sun Oct 21, 2007 6:44 pm     Reply with quote

Hello!

I'm trying to developed a system using PIC16F876 and the RS-232 protocol. My main goal is to receive information from the ADC0, send it to the PC through the serial port, and receive data from the PC to set the PIC's PWM. I'm currently using the default I/O ports TX and RX from the PIC (ports 17 and 18) and MAX232 to implement the RS-232 protocol.

Everything seems to be going well, I receive and transmits data for some time, until the PIC suddenly stops responding. I don't know what is happening since my PIC code is extremely simple and I can't seem to find any hardware error. The only thing I can think of is the receiving buffers since I'm sending and receiving data constantly, so I would like to know more about that.

Any help would be welcomed. For a better analysis my code is presented bellow.

Code:

#if defined(__PCM__)
#include <16F876A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=10000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, BRGH1OK)

#elif defined(__PCH__)
#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=10000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, BRGH1OK)
#endif

#include <stdlib.h>

void main ()
{
   int adc;
   int pwm1;
   int pwm2;
   char value = '\0';
   int valueI = 0;
   
   setup_ccp1 (CCP_PWM);
   setup_ccp2 (CCP_PWM);

   setup_port_a(ALL_ANALOG);
   setup_adc(adc_clock_internal);
   set_adc_channel( 0 );

   setup_timer_2(T2_DIV_BY_1, 255, 1);

   while(TRUE)
   {
      adc=read_adc();
      printf("%c",adc);
      delay_ms (250);
      
      if(kbhit())
      {
         value = getc();
         valueI = (int)value;
      }

      if (valueI <128)
      {
         set_pwm2_duty(0);
         pwm1 = (valueI*2);
         set_pwm1_duty(pwm1);
      }
      else
      {
         set_pwm1_duty(0);
         pwm2 = (valueI - 128);
         pwm2 = (pwm2 * 2);
         set_pwm2_duty(pwm2);
      }         
   }
}


Thanks in advance for your time.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Oct 21, 2007 8:02 pm     Reply with quote

Quote:

The only thing I can think of is the receiving buffers.

while(TRUE)
{
adc=read_adc();
printf("%c",adc);
delay_ms (250);
if(kbhit())
{
value = getc();
valueI = (int)value;
}

The 250 ms delay will prevent you from reading the hardware UART's
receive buffer during that time. The UART only has a 2-byte receive fifo.
If you send more characters than that, it will get an overrun error and
lock up. You can tell the compiler to automatically clear the lockup
condition by adding the ERRORS directive, as shown in bold:
Quote:
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, BRGH1OK, ERRORS)

This will cure the lockups, but you will still lose characters.

A better solution would be to add an interrupt-driven software receive fifo.
See the CCS example file, Ex_Sisr.c, for an example of how to do this.
It's in this directory:
Quote:
c:\Program Files\Picc\Examples
isabele_cda



Joined: 21 Oct 2007
Posts: 2

View user's profile Send private message

PostPosted: Sun Oct 21, 2007 8:47 pm     Reply with quote

The delay was used at first as a kind of debug (print the result on Windows hyperterminal in a frequency we could see - we were using 1s). I guess we forgot to ajust it later. Thanks for pointing it out! Smile

I'll try this modification later this week and let you know the results.
Thank you very much!
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