anhphong208
Joined: 23 Jun 2014 Posts: 29 Location: viet nam
|
|
Posted: Thu Jul 03, 2014 7:44 am |
|
|
ezflyr wrote: | Hi,
First, learn to use the 'Code' buttons when posting your code. This will preserve the formatting, and make the code much easier to read.
The next thing I would do is to figure out a 'protocol' for the commands/data that you will send from your PC (application) to the PIC. You can make you life easy or hard with the design of the protocol. To make things easy, start every transmission with a unique character, and end every transmission with a carriage return <CR>. For example, you could define something like this:
This would set motor #1 to a 90% duty cycle. Hopefully, you get the point!
Here is an interrupt handler that you could use to receive the commands:
Code: |
//-----< RS232 Packet definitions >-----
int1 RX_Command_Ready; // TRUE If a receive packet is ready
#define RX_SIZE 10 // RS232 buffer for serial reception
char RxBuffer[RX_SIZE]; // RS232 serial RX buffer
int8 Index = 0; // RS232 RX data IN index
#INT_RDA // Interrupt driven RS232 routine
void rs232_isr(void)
{
char temp; // local data storage
temp = fgetc(PC); // get rx data
// If we got a '#', then the command is beginning.
if (temp == '#')
{
Index = 0;
RxBuffer[Index] = temp;
Index++;
return;
}
// If we got a CR, then the command is completed.
if (temp == CR)
{
RxBuffer[Index] = temp;
RX_Command_Ready = TRUE;
return;
}
// Save the character to the receive buffer.
RxBuffer[Index]=temp;
// Check for buffer overflow.
if ( Index >= (RX_SIZE - 1) )
Index = 0;
else
Index++;
}
|
This is a 'linear' buffering arrangement that is probably well suited to your needs. Inside your Main() routine, you would have a check for Rx_Command_Ready, and when true, you would know a valid command has been received. Do something simple with this at first like toggling an LED via a command sent from your PC.
Get rid of all the TRIS stuff in your code. Without a Fast_IO directive it's not doing anything anyway. The compiler will handle the TRIS for you, so just delete it.
John |
...........................thanks ezflyr _________________ thanks |
|