View previous topic :: View next topic |
Author |
Message |
omcdr
Joined: 11 Dec 2011 Posts: 4
|
PIC12F683 and RS232 RX problem |
Posted: Sun Dec 11, 2011 3:25 pm |
|
|
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
|
|
Posted: Sun Dec 11, 2011 3:41 pm |
|
|
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
|
|
Posted: Sun Dec 11, 2011 6:20 pm |
|
|
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
|
|
Posted: Sun Dec 11, 2011 7:08 pm |
|
|
and.. you should always add 'errors' to the use rs232(...) options ! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Dec 11, 2011 7:28 pm |
|
|
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
|
|
Posted: Sun Dec 11, 2011 7:42 pm |
|
|
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
|
|
Posted: Mon Dec 12, 2011 4:27 am |
|
|
temtronic wrote: | I thought it said 16f688 not 16f683... | Even worse, it says 12F683
Better make work of those bifocals. |
|
|
omcdr
Joined: 11 Dec 2011 Posts: 4
|
|
Posted: Tue Dec 13, 2011 2:45 am |
|
|
Thanks for replies, I will change to uP with hw RS. |
|
|
|