Discussion in "PIC Microcontroller Discussion" started by    norms    Mar 8, 2008.
Sat Mar 08 2008, 10:28 am
#1
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???

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
#2
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
#3
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?



[ Edited Sat Mar 08 2008, 07:53 pm ]
Sat Mar 08 2008, 11:30 pm
#4
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)..

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
#5
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
#6
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

Dim Value As Integer = (CInt(FirstByte) << 8) Or SecondByte 
Mon Mar 10 2008, 10:36 am
#7
where i am going to insert the code uve given?
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
#8
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.
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
#9
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?
Tue Mar 11 2008, 10:33 pm
#10
Try this.....
Combined = CInt(Firstbyte * (2^8)) OR Secondbyte

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

Darrellciz
Thu Apr 18 2024, 11:07 am
Charlessber
Thu Apr 18 2024, 09:29 am
BartonSem
Thu Apr 18 2024, 04:56 am
DonaldKnown
Thu Apr 18 2024, 12:24 am
utaletxcyw
Wed Apr 17 2024, 10:21 am
Anthonyvab
Wed Apr 17 2024, 08:48 am
RobertCix
Wed Apr 17 2024, 06:46 am
Astorne
Tue Apr 16 2024, 08:52 pm