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:52 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. |
|
|
Eugeneo
Joined: 30 Aug 2005 Posts: 155 Location: Calgary, AB
|
|
Posted: Mon Sep 05, 2005 1:54 am |
|
|
Why do you have a delay there? |
|
|
Sparky
Joined: 02 Sep 2005 Posts: 7
|
|
Posted: Mon Sep 05, 2005 7:59 am |
|
|
This program is just me begining to experiment with uarts and rs232.
The program is supposed to get data from the uart and then pass it to a function which will use it in this case to control the position of some servos.
When the data I was recieving didn't seem correct I reduced the program down to as simple a form as possible which is just recieve data, a function with a delay and recieve new data.
Basicaly I don't know as much about uart as I should, but from some of the posts I've read I'm begining to get the idea. |
|
|
Guest
|
|
Posted: Mon Sep 05, 2005 2:30 pm |
|
|
That code should work ok with a 10 us delay. Since you're using the hardware usart the kbhit function you will not have more than 4ms (1ps/2400bps/10b) or (20 000 000/5 * .004) instructions until you need to return to the kbhit function otherwise your data may be corrupt. That's why your 10 ms delay was causing problems. If you consider using the interrupt the data will be refreshed but it would be pointless with the 10 ms delay.
I hope this helps. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Sep 05, 2005 2:38 pm |
|
|
Quote: |
Since you're using the hardware usart the kbhit function you will not have more than 4ms (1ps/2400bps/10b) or (20 000 000/5 * .004) instructions
until you need to return to the kbhit function otherwise your data may be
corrupt. That's why your 10 ms delay was causing problems.
| Hi Guest,
He was told that same thing in his earlier thread from 2-3 days ago: http://www.ccsinfo.com/forum/viewtopic.php?t=24264
I don't know why he didn't accept the answers. |
|
|
|