|
|
View previous topic :: View next topic |
Author |
Message |
stefsun
Joined: 23 May 2007 Posts: 22
|
RS232 send data problems |
Posted: Sat Feb 02, 2008 9:48 pm |
|
|
the code is modified from EX_TGETC.C
When I use the following programme sent to the PC,when the MCU receives 05, It can sent to PC correctly.
Code: |
#include <18F4523.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include <input.c>
#define KEYHIT_DELAY 500 // in milliseconds
char timed_getc() {
long timeout;
char retval;
timeout=0;
while(!kbhit() && (++timeout< (KEYHIT_DELAY*100)))
delay_us(10);
if(kbhit())
retval = getc();
else
retval = 0;
return(retval);
}
void main()
{
int status;
char value;
while(TRUE)
{
status=1;
while(status==1)
{
value=timed_getc();
if(value==5)
{
status=0;
putc(0x06);
putc(0x03);
putc(0x55);
putc(0x56);
putc(0x59);
putc(0x95);
}
else
{
status=1;
putc(0x95);
}
}
}
}
|
But when I do not use endless loop, when the MCU receives 05, the MCU sends PC : 06 03 55 56 59 95
Now PC receives: 06 03 55 56 00, tthe code is:
Code: |
#include <18F4523.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include <input.c>
#define KEYHIT_DELAY 500 // in milliseconds
char timed_getc() {
long timeout;
char retval;
timeout=0;
while(!kbhit() && (++timeout< (KEYHIT_DELAY*100)))
delay_us(10);
if(kbhit())
retval = getc();
else
retval = 0;
return(retval);
}
void main()
{
int status;
char value;
status=1;
while(status==1)
{
value=timed_getc();
if(value==5)
{
status=0;
putc(0x06);
putc(0x03);
putc(0x55);
putc(0x56);
putc(0x59);
putc(0x95);
}
else
{
status=1;
putc(0x95);
}
}
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Feb 02, 2008 10:19 pm |
|
|
Quote: | But when I do not use endless loop |
Look at the .LST file for your project. Look at the instruction that the
compiler puts at the end of main(). If you set in motion a hardware
process that runs off the PIC's oscillator, such as the UART, and if the
UART has a 1-deep fifo, as well as the output shift register, what happens
to that hardware process when the PIC hits that instruction ? |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Mon Feb 04, 2008 3:06 am |
|
|
The simple answer is when the pic recieves 0x05 it sets status to 0 and outputs the values but before the UART has had time to send the last 2 your code drops out the bottom and the PIC goes to sleep. The UART stops sending when the PIC is asleep.
With the constant loop around the lot it does not go to sleep.
Basically what PCM Programmer said, but saved you the hassle of working it out! |
|
|
stefsun
Joined: 23 May 2007 Posts: 22
|
|
Posted: Tue Feb 19, 2008 8:39 am |
|
|
The code should run in accordance with the order of it, the first run
Putc (0x06);
Putc (0x03);
Putc (0x55);
Putc (0x56);
Putc (0x59);
Putc (0x95);
Then run while (status == 1) this statement, now status == 1, then sleep it |
|
|
|
|
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
|