View previous topic :: View next topic |
Author |
Message |
casandralam
Joined: 26 Jan 2008 Posts: 15
|
need help in rs232 data send frm PC |
Posted: Fri Feb 22, 2008 10:03 pm |
|
|
hi,i'm using PIC16f877 and using 8LEDs to indicate the value i get from PC is correct...but the problem now is i just know to write the receive data from integer 30-40 only.the target i wish to get is from 0-100.it will be too long if i write the receive data as in my code below:
//rs232 data receive sample
#include <16f877a.h>
#device adc=10
#use delay(clock=4915200)
#fuses XT,NOWDT,NOPROTECT,NOLVP
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)
#byte portb=6
#byte portc=7
void main()
{
byte mystat=0;
int16 mydata;
set_tris_b(0x00);//all port B is output
set_tris_c(0x80);//C7 is input
do
{
if(kbhit())
{mydata=getch();
mystat=1;}
if (mystat==1)
{
if (mydata==30)
{portb=0xaa;} //1010 1010
else if(mydata==31)
{portb=0x00;} //0000 0000
else if(mydata==32)
{portb=0x01;} //0000 0001
else if(mydata==33)
{portb=0x02;} //0000 0010
else if(mydata==34)
{portb=0x03;} //0000 0011
else if(mydata==35)
{portb=0x04;} //0000 0100
else if(mydata==36)
{portb=0x05;} //0000 0101
else if(mydata==37)
{portb=0x06;} //0000 0110
else if(mydata==38)
{portb=0x07;} //0000 0111
else if(mydata==39)
{portb=0x08;} //0000 1000
else if(mydata==40)
{portb=0xff;} //1111 1111
mystat=0;
}
}
while(TRUE);
}
Do anyone can help me in this?
Any short way to can replace my way as above?
thanks for your help:-) |
|
|
stefsun
Joined: 23 May 2007 Posts: 22
|
|
Posted: Fri Feb 22, 2008 11:07 pm |
|
|
mydata=getch();
output_b(mydata);
try it |
|
|
meereck
Joined: 09 Nov 2006 Posts: 173
|
|
Posted: Sat Feb 23, 2008 10:36 am |
|
|
as for "30-40 only"
Check the ASCII table and you will see why you are getting values between 30h-40h.
These values are ascii values of characters 0,1...to 9.
If you want to get a numerical representation, simply subtract 0x30 (48 in decimal) from the value you receive from the PC.
Anyway, since you want to have values from 0-100, you can either send just one byte from the PC and put them directly to the output port, or you can send the value as a string (understand as '100', with length of 3 characters). However, you will have to buffer these character in the pic because you can receive just 1 character at a time. You can use atoi() function to get a numerical representation of the buffer. |
|
|
|