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

PIC12F683 and RS232 RX problem

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



Joined: 11 Dec 2011
Posts: 4

View user's profile Send private message

PIC12F683 and RS232 RX problem
PostPosted: Sun Dec 11, 2011 3:25 pm     Reply with quote

I can't receive any byte on my PIC12F683. TX is working correctly.
Is kbhit() working on this uP ? Shoud I declare rx buffer or something else ?


Code:

#include <12F683.h>

#FUSES WDT                      //Watch Dog Timer
#FUSES INTRC_IO                 //Internal RC Osc, no CLKOUT
#FUSES PUT                      //Power Up Timer
#FUSES NOMCLR                   //Master Clear pin used for I/O
#FUSES NOBROWNOUT               //No brownout reset
#define MOSFET PIN_A2

#use delay(int=4000000,RESTART_WDT)
#use rs232(baud=9600,parity=N,xmit=PIN_A1,rcv=PIN_A5,bits=8,stream=PORT1,restart_wdt)

unsigned char high, cycle, i, j,ch;


void main()
{
   output_low(MOSFET);
   setup_adc_ports(NO_ANALOGS);
   setup_comparator(NC_NC);
   setup_vref(FALSE);

   printf("start\n\r");
   high=9;
   cycle=60;
     
   while(1)
   {
      output_high(MOSFET);
      delay_ms(high);
      output_low(MOSFET);
      delay_ms(cycle-high);

     
      if(kbhit())
      {
         ch = getc();
         printf("ch: %c\n\r", ch);
         
         switch(ch)
         {
            case 'q':   high++;
                        if(high>=HIGH_MAX)
                           high=HIGH_MAX;
                        printf("h: %d\n\r", high);   
                        break;
            case 'a':   if(high>=1)
                           high--;
                        printf("h: %d\n\r", high);   
                        break;   
            case 'x':   cycle++;
                        if(cycle>=CYCLE_MAX)
                           cycle=CYCLE_MAX;
                        printf("c: %d\n\r", cycle);   
                        break;
            case 'z':   if(cycle>=CYCLE_MIN)
                           cycle--;
                        printf("c: %d\n\r", cycle);   
                        break;                           
            default:
                        break;
         }
      }
   }
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19537

View user's profile Send private message

PostPosted: Sun Dec 11, 2011 3:41 pm     Reply with quote

Your problem is time.
This chip does not have a hardware UART. Only software.
With a software UART, you _will_ miss characters, unless you poll the RS232 input at _least_ twice per bit time (3* is really necessary). So for 9600bps, your loop _must_ take no longer than 1/(9600*2) seconds to execute. 52uSec. You have 9mSec delay in the loop looking for the character. In this time, over 8 characters can be missed.....

Solutions:
1) Change to a chip with a hardware UART - easiest, and best solution.
2) Change how your delays are done. Poll the interrupt in a fast loop in main, and use a count in a hardware timer to operate the mosfet lines. Relatively easy, and cheap.
3) Use an edge triggered interrupt to call the serial code. Can be made to work, cheap, more likely to be reliable, but quite a lot of code involved.

Best Wishes
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Sun Dec 11, 2011 6:20 pm     Reply with quote

Another thing...You have specified a STREAM=PORT1 your print needs to reflect that also. Look at fprintf in the CCS help
temtronic



Joined: 01 Jul 2010
Posts: 9241
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Dec 11, 2011 7:08 pm     Reply with quote

and.. you should always add 'errors' to the use rs232(...) options !
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Dec 11, 2011 7:28 pm     Reply with quote

The ERRORS parameter doesn't do anything for a software UART (which is
the only type that is available on the 12F683). The compiler ignores it.
It's only functional on Hardware UARTs.
temtronic



Joined: 01 Jul 2010
Posts: 9241
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Dec 11, 2011 7:42 pm     Reply with quote

man, I gotta get better bifocals...
I thought it said 16f688 not 16f683...

getting old is 'fun'...
ckielstra



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

View user's profile Send private message

PostPosted: Mon Dec 12, 2011 4:27 am     Reply with quote

temtronic wrote:
I thought it said 16f688 not 16f683...
Even worse, it says 12F683
Better make work of those bifocals. Razz
omcdr



Joined: 11 Dec 2011
Posts: 4

View user's profile Send private message

PostPosted: Tue Dec 13, 2011 2:45 am     Reply with quote

Thanks for replies, I will change to uP with hw RS.
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