how to join the low byte nd high byte
Discussion in "PIC Microcontroller Discussion" started by norms Mar 8, 2008.
Sat Mar 08 2008, 10:28 am
im using vb program in reading the data send by my pic16f877 and im getting results alternately.. is it because of the low byt and high byte that is being write in my usart? please help???
for my vb prog:
how am i going to read the low and high byte since im only using sData as input
unsigned int adc_val; unsigned char low_byte,hi_byte; void main() { USART_Init(9600); ADCON0 = 0; ADCON0.ADON = 1; TRISA = 0xFF; TRISB = 0; PORTB = 0; while (1) { ADCON0.GO = 1 adc_val = ADC_Read(0); low_byte = adc_val; hi_byte = adc_val > > 8; USART_Write(low_byte); USART_Write(hi_byte); low_byte =USART_Read(); high_byte =USART_Read() if(adc_val==511) //if ADC value read in is 511 {PORTB = 0xFF;} //display some LED's on PORTB .... .... . } }
for my vb prog:
Private Sub CmdExit_Click() If MSComm1.PortOpen = True Then MSComm1.PortOpen = False End If End End Sub Private Sub Form_Load() MSComm1.InputLen = 0 MSComm1.CommPort = 2 MSComm1.Settings = "9600,N,8,1" End Sub Private Sub Timer1_Timer() Dim BytesToRead As Integer Dim sData As Variant MSComm1.PortOpen = True BytesToRead = 1 Do DoEvents Loop Until MSComm1.InBufferCount = BytesToRead sData= MSComm1.Input Text1.Text = (((Asc(sData) * 0.00489) & " Volts DC" MSComm1.PortOpen = False End Sub
how am i going to read the low and high byte since im only using sData as input
[ Edited Sat Mar 08 2008, 11:20 am ]
Sat Mar 08 2008, 11:24 am
in C you can combine something like this..
int combined; char adcHI, adcLOW; combined = adcHI; combined = (combined << 8) | adcLOW; //now combined is 16-bit value.
Sat Mar 08 2008, 07:45 pm
thanks ajay... does this dont mess up with the readings ill be getting in my vb program? ill be using this code as this
unsigned int adc_val;
int combined
char adcHI, adcLOW;;
void main()
{
USART_Init(9600);
ADCON0 = 0;
ADCON0.ADON = 1;
TRISA = 0xFF;
TRISB = 0;
PORTB = 0;
while (1)
{
ADCON0.GO = 1
adc_val = ADC_Read(0);
adcLOW = adc_val;
adcHigh= adc_val >> 8;
combined = adcHI;
combined = (combined <<8) | adcLOW
USART_Write(combined)
combined = USART_Read();
do i need to delete the low_byte and high_byte thing?
unsigned int adc_val;
int combined
char adcHI, adcLOW;;
void main()
{
USART_Init(9600);
ADCON0 = 0;
ADCON0.ADON = 1;
TRISA = 0xFF;
TRISB = 0;
PORTB = 0;
while (1)
{
ADCON0.GO = 1
adc_val = ADC_Read(0);
adcLOW = adc_val;
adcHigh= adc_val >> 8;
combined = adcHI;
combined = (combined <<8) | adcLOW
USART_Write(combined)
combined = USART_Read();
do i need to delete the low_byte and high_byte thing?
[ Edited Sat Mar 08 2008, 07:53 pm ]
Sat Mar 08 2008, 11:30 pm
Hi.....
First of all you need to understand what you are doing.Let me explain.....
You are reading the adc value and storing it in adc_val which is an int type ie 2-bytes.
When you send data to the PC through the PIC's USART,you can send only 1 byte at a time.Therefore you cannot send adc_val through the USART at once(since it's 2-bytes).So what you do is split the adc_val into two separate bytes ie. hi_byte and low_byte.Now send one byte first(say lo_byte) through USART,then the next(hi_byte).
Now in your VB program you receive these 2 bytes and then recombine them into the original integer adc_val,using the logic depicted above by Ajay(you have to do this in your VB program and not PIC)..
In your PIC program what is this for? Can you explain?
First of all you need to understand what you are doing.Let me explain.....
You are reading the adc value and storing it in adc_val which is an int type ie 2-bytes.
When you send data to the PC through the PIC's USART,you can send only 1 byte at a time.Therefore you cannot send adc_val through the USART at once(since it's 2-bytes).So what you do is split the adc_val into two separate bytes ie. hi_byte and low_byte.Now send one byte first(say lo_byte) through USART,then the next(hi_byte).
Now in your VB program you receive these 2 bytes and then recombine them into the original integer adc_val,using the logic depicted above by Ajay(you have to do this in your VB program and not PIC)..
low_byte =USART_Read();
high_byte =USART_Read()
In your PIC program what is this for? Can you explain?
Sun Mar 09 2008, 11:39 am
ah ok.thanks... can u give me some info on how i am going to combine this? i've tried but not that succesfull.
Sun Mar 09 2008, 07:37 pm
You cannot send variable combine to UART directly, coz its a 16-bit variable and UART is capable of sending 8-bit.
in your first post you are doing right.. i means sending first byte and then second byte. Now you need to do the combining thing in VB.. you can do it something like this
in your first post you are doing right.. i means sending first byte and then second byte. Now you need to do the combining thing in VB.. you can do it something like this
Dim Value As Integer = (CInt(FirstByte) << 8) Or SecondByte
Mon Mar 10 2008, 10:36 am
where i am going to insert the code uve given?
this is my code:
this is my code:
Private Sub CmdExit_Click() If MSComm1.PortOpen = True Then MSComm1.PortOpen = False End If End End Sub Private Sub Form_Load() MSComm1.InputLen = 0 MSComm1.CommPort = 2 MSComm1.Settings = "9600,N,8,1" End Sub Private Sub Timer1_Timer() Dim BytesToRead As Integer Dim sData as Variant MSComm1.PortOpen = True BytesToRead = 1 Do DoEvents Loop Until MSComm1.InBufferCount = BytesToRead sData= MSComm1.Input Text1.Text = (((Asc(sData) * 0.00489) & " Volts DC" MSComm1.PortOpen = False End Sub
[ Edited Mon Mar 10 2008, 06:17 pm ]
Mon Mar 10 2008, 06:32 pm
i donno much about VB coding and stuff.. only thing i can tell you is.. you need to read two bytes as sent by controller.
Lets say you are sending high byte first and then low byte..
so you need to read them separately one by one.. and store them in two variables and then combine both of them as mentioned above.
e.g.
Lets say you are sending high byte first and then low byte..
so you need to read them separately one by one.. and store them in two variables and then combine both of them as mentioned above.
e.g.
FirstByte= MSComm1.Input SecondByte = MSComm1.Input Combined = (CInt(FirstByte) << 8) Or SecondByte
[ Edited Mon Mar 10 2008, 06:33 pm ]
Tue Mar 11 2008, 12:36 pm
Ajay, thank you very much for the help, i've tried using ur code but when im in the Combine = (CInt(FirstByte)<<8) Or Secondbyte the "<<" gives an error, i can only use one <, isn't it a c code "<<"? ... and when i tried checking the value send by the microcontroller the reading was at 255 and changes to 239, is there something wrong with my code?
Powered by e107 Forum System