View previous topic :: View next topic |
Author |
Message |
Sparky
Joined: 02 Sep 2005 Posts: 7
|
Strange Problem with delay_ms |
Posted: Fri Sep 02, 2005 10:55 am |
|
|
Hello everyone.
I've come across a frustrating problem in a very simple program. I'm trying to get a 16f871 to send an 8 bit number to a second 16f871 which then displays it on leds.
The transmitting chip gets the number from a pot and works perfectly. I've check the output with the scope. The problem is with the recieving chip.
Here is the program
Code: | #include <16F871.h>
#device adc=8
#use delay(clock=20000000)
#fuses HS,NOWDT,PUT,NOBROWNOUT,NOLVP
#use rs232(baud=2400,xmit=PIN_C6,rcv=PIN_C7,bits=8,parity=e)
void display (int8 data);
void main() {
int8 x,i;
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_counters(RTCC_INTERNAL,RTCC_DIV_2);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
set_tris_b(0);
i=1;
do
{
do
{
}
while (!kbhit());
x=getc();
display(x);
}
while (i=1);
}
void display (int8 data)
{
output_b(data);
delay_us(10);
}
|
This code works but if delay_us(10) is replaced with delay_ms(10) in the display function it displays the wrong number and doesn't change if I turn the pot on the transmit chip.
Any ideas? Am I using the kbhit() function wrong. |
|
|
newguy
Joined: 24 Jun 2004 Posts: 1909
|
|
Posted: Fri Sep 02, 2005 11:03 am |
|
|
One thing that comes to mind is that if the delay is changed to 10 ms, then the receiving pic's USART could be locking up.
If the USART receives characters during the 10 ms delay, but those characters aren't "extracted" from its receive buffer, then the USART will lock up. If you add "ERRORS" to your #use rs232 line, then these receive overrun errors will automatically be cleared, and the USART won't lock up. |
|
|
Sparky
Joined: 02 Sep 2005 Posts: 7
|
|
Posted: Fri Sep 02, 2005 2:44 pm |
|
|
Thanks newguy. Added "ERRORS" to the #use rs232 line.
Now the output flickers between the correct number and some random number before freezing on the wrong number after about 10 seconds.
Still works fine if the delay is short.
Is there some other way of resetting USART before reading from it. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Sep 02, 2005 3:42 pm |
|
|
At what rate are you sending characters to the receiver ?
You're only checking the receiver's USART once every 10ms.
So I'd suggest that you put a delay in your transmitter PIC
so that it only sends a character every 15 ms. Then you'll
never miss it on the receiver side. |
|
|
|