CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Serial Comm

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Guest








Serial Comm
PostPosted: Thu Jul 06, 2006 3:05 pm     Reply with quote

I need help:
I will appreciate anybody who can help me

I write a VB GUI to test the port of a PIC microcontroller
The GUI will send data from serial port to turn on or turn off a pin of the microcontroller (let say use portb of PIC)

Here is my PIC code:


Code:
#include <18F452>
#fuses XT,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) 



void main()
{
   
char PinNumber,PinState;

   while(1)
     {
         PinNumber = getc();
      PinState = getc();
      output_b(PinNumber);
      output_b(PinState);
      
   

   }

}



and here is my VB6 code

Code:
Private Sub Command1_Click()
Dim PinNumber As Long
Dim PinState As Long

'  Get Pin Number
PinNumber = cboPinNumber.ListIndex

'  Get Pin State
If OptState(0).Value = True Then
    PinState = 0
Else
    PinState = 1
End If

' Send Out Data
MSComm1.Output = Chr$(255) & Chr$(PinNumber) & Chr$(PinState)

End Sub

Private Sub Form_Load()
    Dim Pins As Long

'  Add the pin numbers 0 to 7 to cboPinNumber
'  PortD RD0 - RD7
    For Pins = 0 To 7
    cboPinNumber.AddItem CStr(Pins)
    Next Pins

    ' Default to Pin 0 being selected
    cboPinNumber.ListIndex = 0

    ' Default to optState(0) being selected
    OptState(0).Value = True

    '  Use COM1
    MSComm1.CommPort = 1

    ' 2400 baud, no parity, 8 data bits, 1 stop bit
    MSComm1.Settings = "9600,N,8,1"

    ' Disable DTR
    MSComm1.DTREnable = False

    ' Open the port
    MSComm1.PortOpen = True

End Sub

Private Sub Form_Unload(Cancel As Integer)
MSComm1.PortOpen = False
End Sub

Ttelmah
Guest







PostPosted: Fri Jul 07, 2006 4:25 am     Reply with quote

'Output_b', puts the _byte_ out on the whole port. As written, your VB program will send three bytes '255', then the Pin number, then the Pin state. Your receive code, will only look for two bytes (so will take '255', as the first value, then the pin number as the second), and will output first one, then the other byte to the port. You will never see the first, since it will only be present for one-millionth of a second, and the port will then end with the second byte (the pin number), present as a bit pattern on the output pins. The code will then loop back, and retrieve the 'value', but will then sit waiting, because nothing else arrives (until the next button event in the VB code).
You need something to turn the numbers into the required bit patterns.
Code:

#include <18F452>
#fuses XT,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) 

void main() {
    char PinNumber,PinState;
    int8 bit_pattern=0;
    int8 mask;
    while(1) {
        //Wait for the '255' header character
        while (getc()!=255) ;
       
        //Now retrieve the pin number and 'state'
        PinNumber = getc();
        PinState = getc();
        mask=1<<PinNumber;       
        //Now you have a 'mask', with a '1' in the bit corresponding
        //to the required pin number.
        if (PinState=1) bit_pattern |= mask;
        //Set the required bit if the 'state' is 1
        else bit_pattern &= ~(mask);
        //otherwise turn off the specified bit.
        output_b(bit_pattern);
   }
}


Best Wishes
Guest








PostPosted: Wed Jul 26, 2006 2:09 pm     Reply with quote

Can you explain how is this statement work?

I am really new into this
Code:
mask=1<<PinNumber;
newguy



Joined: 24 Jun 2004
Posts: 1907

View user's profile Send private message

PostPosted: Wed Jul 26, 2006 3:56 pm     Reply with quote

Anonymous wrote:
Can you explain how is this statement work?

I am really new into this
Code:
mask=1<<PinNumber;


Let's say PinNumber = 3.

mask = 1 << 3 means shift 0x01 left 3 times (in other words, multiply 1 by 2 three times), then assign this value to "mask". After all this is done, mask will be 0x08. Same thing as saying 1 * (2^3) = 8.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group