|
|
View previous topic :: View next topic |
Author |
Message |
arrow
Joined: 17 May 2005 Posts: 213
|
What is wrong with this code? |
Posted: Wed Nov 23, 2005 7:16 am |
|
|
Hi
Could some one please tell me what is wrong with this code?
Code: |
#include <16F84A.H>
#fuses XT,NOPROTECT,NOWDT
#use delay(clock=3000000)
#use rs232(baud=19200, xmit=PIN_A4, rcv=PIN_B7, ERRORS)
//==================================================
//==================================================
main() {
int minutes;
minutes = 0;
while(true){
minutes++;
delay_ms(1000);
printf("%x ", minutes);
if(kbhit()){
minutes = getc();
minutes = 23;
}
}
}
|
It does not reset the minutes variable when I send a character on the RS232 line.
Thank you in advance.
a. |
|
|
Ttelmah Guest
|
|
Posted: Wed Nov 23, 2005 8:33 am |
|
|
The problem is that it'll miss the incoming character. You chip does not have a hardware UART, so you are using the 'soft' UART implementation. To 'see' a character with this, the code has to either be waiting in 'kbhit' when the character comes, or arrive inside this code within a very few uSec of the start of the 'start' bit of the arriving character, or the chip will never even know that a character has arrived. Your code is spending 99.9%of it's time sitting in the delay_ms routine, and unless the character just happens to arrive at the right moment, it'll simply be lost.
You need to do the timing usiing an interrupt, and sit polling for the input character. Unfortunately, even doing this, you will have problems, since with an incoming rate of 19200bps, the start bit, is only 52uSec long, and you must get to the test routine, well inside the first half of this time (say less than 20uSec after the character starts). At 3MHz, your processor wll only execute one instruction every 1.33uSec, and even a well written interrupt handler is going to take longer than you have available...
Best Wishes |
|
|
neil
Joined: 08 Sep 2003 Posts: 128
|
|
Posted: Wed Nov 23, 2005 8:35 am |
|
|
Hi, the two lines minutes=getc(); and minutes=23; caught my eye. Doing this will set minutes to the received *character* and then immediately set minutes to 23. Is this what you want to do?
If the problem is that you are not receiving any RS232, it could be the baud rate and clock frequency that are at fault. 19200baud sounds a bit fast for a 3MHz clock using the software serial functions. Try slowing down to something like 2400 baud and see if it works at that speed first.
Also, where are you getting your 3MHz clock from? It seems like an odd frequency to come from a crystal or a canned oscillator!
Regards,
Neil. |
|
|
neil
Joined: 08 Sep 2003 Posts: 128
|
|
Posted: Wed Nov 23, 2005 8:37 am |
|
|
Oops! These replies arrived at the same time! Ttelmah is right by the way.... best ignore my last one!!!! |
|
|
|
|
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
|