free 8051 Microcontroller Projects AVR PIC Microcontroller Projects Tutorials Ebooks Libraries, interfacing tutorials, lcd tutorial, stepper motor, dc motor 8051 assembly language programming electronics and communication ECE CSE pdf ebooks library BE final year project ideas Embedded systems

 
8051 microcontroller 8051 microcontroller
Forums

Moderators: Ajay, Junied , abbas1707, Arun Kumar V, pdi33, Shailesh NAYAK, ۞ TPS ۞, shyam, sashijoseph
Author Post
norms
Fri Mar 07 2008, 08:58PM
 User Offline
Registered Member #6079
Joined: Sat Feb 16 2008, 12:34AM

Posts: 28
Thanked 0 times in 0 posts
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???

CODE:
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:

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


how am i going to read the low and high byte since im only using sData as input



[ Edited Fri Mar 07 2008, 09:50PM ]
Back to top


Ajay
Fri Mar 07 2008, 09:54PM
Rickey's World Admin

 User Offline

Registered Member #1
Joined: Fri Feb 24 2006, 04:56AM

Posts: 3753
Thanked 696 times in 655 posts
in C you can combine something like this..
CODE:

int combined;
char adcHI, adcLOW;

combined = adcHI;
combined = (combined << 8) | adcLOW;
//now combined is 16-bit value.
 


www.rickeyworld.info
If you feel satisfied with the user's forum reply please click on the thank button.

Obey forum rules!
Respect others!
Back to top


norms
Sat Mar 08 2008, 06:15AM
 User Offline
Registered Member #6079
Joined: Sat Feb 16 2008, 12:34AM

Posts: 28
Thanked 0 times in 0 posts
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, 06:23AM ]
Back to top


sashijoseph
Sat Mar 08 2008, 10:00AM

 User Offline
Registered Member #5870
Joined: Mon Feb 04 2008, 06:26PM

Posts: 554
Thanked 124 times in 117 posts
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?

Let there be music........
Back to top


norms
Sat Mar 08 2008, 10:09PM
 User Offline
Registered Member #6079
Joined: Sat Feb 16 2008, 12:34AM

Posts: 28
Thanked 0 times in 0 posts
ah ok.thanks... can u give me some info on how i am going to combine this? i've tried but not that succesfull.
Back to top


Ajay
Sun Mar 09 2008, 07:07AM
Rickey's World Admin

 User Offline

Registered Member #1
Joined: Fri Feb 24 2006, 04:56AM

Posts: 3753
Thanked 696 times in 655 posts
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

CODE:
Dim Value As Integer = (CInt(FirstByte) << 8) Or SecondByte
 


www.rickeyworld.info
If you feel satisfied with the user's forum reply please click on the thank button.

Obey forum rules!
Respect others!
Back to top


norms
Sun Mar 09 2008, 10:06PM
 User Offline
Registered Member #6079
Joined: Sat Feb 16 2008, 12:34AM

Posts: 28
Thanked 0 times in 0 posts
where i am going to insert the code uve given?
this is my code:
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, 05:47AM ]
Back to top


Ajay
Mon Mar 10 2008, 06:02AM
Rickey's World Admin

 User Offline

Registered Member #1
Joined: Fri Feb 24 2006, 04:56AM

Posts: 3753
Thanked 696 times in 655 posts
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.
CODE:

FirstByte= MSComm1.Input
SecondByte = MSComm1.Input

Combined = (CInt(FirstByte) << 8) Or SecondByte
 


[ Edited Mon Mar 10 2008, 06:03AM ]

www.rickeyworld.info
If you feel satisfied with the user's forum reply please click on the thank button.

Obey forum rules!
Respect others!
Back to top


norms
Tue Mar 11 2008, 12:06AM
 User Offline
Registered Member #6079
Joined: Sat Feb 16 2008, 12:34AM

Posts: 28
Thanked 0 times in 0 posts
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?
Back to top


sashijoseph
Tue Mar 11 2008, 10:03AM

 User Offline
Registered Member #5870
Joined: Mon Feb 04 2008, 06:26PM

Posts: 554
Thanked 124 times in 117 posts
Try this.....
Combined = CInt(Firstbyte * (2^8)) OR Secondbyte

Let there be music........
Back to top


 

Jump:     Back to top

Syndicate this thread: rss 0.92 Syndicate this thread: rss 2.0 Syndicate this thread: RDF
Powered by e107 Forum System

8051 Microcontroller Projects 8051 AVR tutorials PIC microcontroller, 8051 assembly language programming electronics and communication ECE CSE pdf ebooks library BE final year project ideas Embedded systems