View previous topic :: View next topic |
Author |
Message |
newbie1 Guest
|
Receiving RS232 for control |
Posted: Sun Jun 28, 2009 1:22 pm |
|
|
Hi all,
I am trying to write a program that can read data from RS232 sent from a PC or another pic. The chip should be able to do different functions depending on which key I press on the computer. So far I am trying to use something like this:
Code: |
if (kbhit()){
keypressed=getc();
printf("key pressed is: %c /r/n",keypressed);
if(keypressed=="a") {
printf("I can do something do to with this key being hit");
output_high(PIN_B2);
}
|
that is just a general idea of what I'm trying to do, I know this won't compile. I don't know how to really define the if(keypressed=="a") part.
I figured I needed to use the input.c file for this application. I saw a sample step.c in the CCS files, and it included input.c. The problem with input.c is that I can't seem to compile my code when it is included. I am using MPLAB, and I added it to the "other files", and I added the ctype.h header to the folder under "headers". But it just won't compile with it! It has an error that says undefined identifier getc(), and others that are similar. I have not modified it in any way I think.
Anyways, do you guys this that I Am on the right track with this?
Thanks for the help!
Newbie1. |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Jun 28, 2009 1:31 pm |
|
|
See this example program:
http://www.ccsinfo.com/forum/viewtopic.php?t=39145&start=1
This program receives a single character from the PC, then converts
it to upper case, and then uses a switch-case statement to select the
correct code to execute, based upon the character that was received. |
|
![](templates/subSilver/images/spacer.gif) |
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Sun Jun 28, 2009 6:33 pm |
|
|
Note you need a single quote pair around a constant character. This always catches me.
Code: | case 'B': // Read switch on Slave PIC
or
if(keypressed=='a') { |
_________________ The search for better is endless. Instead simply find very good and get the job done. |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Sun Jun 28, 2009 11:35 pm |
|
|
aha! Thank you all soo much. Got it!
So is this the same method that a wireless remote would work through say an xbee, or even infrared type device. Can we use RS232 on a pic to pic using the xbee or fm radio transmitter and check to see if 'a' or something else was pressed?
Also, any suggestions on the problems I'm having with the input.c file?
Thanks all!
I really appreciate it.
Newbie1. |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Jun 28, 2009 11:51 pm |
|
|
You need to invoke the rs232 library. It has the getc() function in it.
This is done with a #use rs232() statement. See the example below:
Code: |
#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PI7N_C7, ERRORS)
#include <input.c>
//======================================
void main()
{
while(1);
} |
|
|
![](templates/subSilver/images/spacer.gif) |
|