View previous topic :: View next topic |
Author |
Message |
Replika
Joined: 15 Apr 2008 Posts: 4
|
PIC & PC - Request to send |
Posted: Tue Apr 22, 2008 10:00 pm |
|
|
I'm trying to communicate between PC and PIC16F877A
Here is my snippet:
Code: | #include <16F877A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=2400,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,stop=1, errors)
(...)
#INT_RDA
void serial_interrupt()
{
tmp_byte = getc();
write_program_eeprom(pos, tmp_byte);
pos++;
}
(...)
|
I can send data from PC to PIC, just put the data to Serial port
But how to get data from PIC when PC have a demand. I mean in my program in PC (written in VC#.NET), when I do a thing, PIC send its data to serial port, that the program can get it?
Thanks in advance. |
|
|
sohailkhanonline
Joined: 06 Mar 2008 Posts: 35 Location: pakistan
|
|
Posted: Tue Apr 22, 2008 11:52 pm |
|
|
use
read_program_eeprom(address);
keep track of pos variable and use it to retrieve data from eeprom then send the result to PC via rs232 using printf function
look both
read_program_eeprom(address) and printf
in help directory of CCS _________________ sohail khan |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Wed Apr 23, 2008 7:30 am |
|
|
Quote: |
But how to get data from PIC when PC have a demand. I mean in my program in PC (written in VC#.NET), when I do a thing, PIC send its data to serial port, that the program can get it?
|
The way to do what you want is:
1) Store the incoming PC commands in a temporary buffer (RAM space, NOT in EEPROM as you point).
2) In main() you should look for a flag that tell you that a new command was arrieved and stored in a buffer.
3) Use a parser to access the buffer and to interpret/decode the received command.
4) Make an action (Ex: repply to PC) related to such event.
http://www.ccsinfo.com/forum/viewtopic.php?t=31144&highlight=parser
http://www.ccsinfo.com/forum/viewtopic.php?t=25425&highlight=parser
Humberto |
|
|
Replika
Joined: 15 Apr 2008 Posts: 4
|
|
Posted: Wed Apr 23, 2008 9:37 am |
|
|
Thanks you two.
I know in PIC, we have to received first, but I'm still confused about using interrupt for sending data to rs232 port. I have INT_RDA for receiving, and INT_TBE for sending? Could you please make an example that we can send bytes (base on my code in first post)
Code: |
#INT_RDA
void serial_interrupt()
{
tmp_byte = getc();
if (tmp_byte == DEMAND)
{
post = START;
enable_interrupts(INT_TBE);
return;
}
write_program_eeprom(pos, tmp_byte);
pos++;
}
#INT_TBE
function send()
{
putc (read_program_eeprom[pos]);
pos++;
if (pos > MAX)
disable_interrupts(INT_TBE);
}
|
|
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Wed Apr 23, 2008 9:52 am |
|
|
Either 1) the PIC can send data when it is ready, and the PC has to worry about buffering it until the PC software is ready for it. The PC probably has a hardware buffer to take care of this, but I don't program PCs so I am not sure. Or 2) the PC sends an inquiry message to the PIC asking it to send data. Often the inquiry is just a single character such as '#' or a null. The PIC can't read the PC's "mind" to know when to send data.
Does this answer the right question? _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
Ttelmah Guest
|
|
Posted: Wed Apr 23, 2008 10:16 am |
|
|
However - key thing - read Humberto's reply.
_Don't_ write to the EEPROM, inside the INT_RDA.
Make your decisions in the main code (outside the interrupt). Write to the EEPROM out here as well.
Reading the EEPROM, inside the transmit interrupt is probably OK (this is almost instantaneous), but writing to the program EEPROM, _completely stops code from executing_, while the write takes place.
Do some searches here. There are dozens (hundreds...) of examples about using the INT_RDA, and slightly fewer about using INT_TBE.
Best Wishes |
|
|
Replika
Joined: 15 Apr 2008 Posts: 4
|
|
Posted: Wed Apr 23, 2008 11:02 am |
|
|
But I have to store large amout of bytes, so I can not store them in ram.
Any advice?
I've tested. My program works well (write_program_eeprom) with baud rate 2400. (Still not check the INT_TBE yet)
Thanks all, again |
|
|
|