Using C to program a PIC to read a serial input

Discussion in 'Homework Help' started by tbark, May 6, 2008.

  1. tbark

    Thread Starter New Member

    May 6, 2008
    3
    0
    I have to come up with a programme in C that reads an input signal of 4 bits. But before that 4 bits is a stretch of 8 bits which verify that that is the correct 4 bits. What i need to find out is how to read the stretch of data one bit at a time. I'm not well versed in C. The frequency of the input data is 100hz. I plan to test the bits one at a time and after receiving the initial 8 bits, i will proceed to store the following 4 bits.
     
  2. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    You may want to consider writing your code in the form of a finite state machine.

    Google will take you to a number of tutorials on the web.

    hgmjr
     
  3. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    The minimum you can read in C is one byte. After you have the byte, you can use the & and | operators to get at individual bits.
     
  4. tbark

    Thread Starter New Member

    May 6, 2008
    3
    0
    You say i can receive minimum of 1 bit at a time but in my case, if i'm just testing the bits one by one should i place a delay between each test so that i dont overlap and test the same bit twice? The thing is, i am only using 1 input pin and my data can only be received one bit at a time.
     
  5. Caveman

    Active Member

    Apr 15, 2008
    471
    0
    Let's say that you are reading on a rising clock edge (located on bit 0 of PORTA), while the input data is on bit 0 of PORTB). The code would be like this:
    Code ( (Unknown Language)):
    1.  
    2. unsigned char  data_in = 0;  // Initialize to 0.
    3.  
    4. // Read 8 bits
    5. for (count=0;count<8;count++)
    6. {
    7.    data_in <<= 1;   // Shift the data from last time.
    8.    while (PORTA & 0x01);      // Spin while PORTA.0 is high.
    9.    while (!(PORTA & 0x01));  // Spin while PORTA.0 is low.
    10.    // Rising edge has occurred, so read the port.
    11.    if (PORTB & 0x01)  // If data is high
    12.       data_in |= 0x01;  // Put the data in the LSB.
    13. }
    14.  
    15. // We've now read 8 bits.
    16. // Do the same for the next 4 bits.
    17.  
    If you want to read in MSB first, then you will do this:
    Code ( (Unknown Language)):
    1.  
    2. unsigned char data_in = 0;  // Initialize to 0.
    3.  
    4. // Read 8 bits
    5. for (count=0;count<8;count++)
    6. {
    7.    data_in >>= 1;   // Shift the data from last time.
    8.    while (PORTA & 0x01);      // Spin while PORTA.0 is high.
    9.    while (!(PORTA & 0x01));  // Spin while PORTA.0 is low.
    10.    // Rising edge has occurred, so read the port.
    11.    if (PORTB & 0x01)  // If high
    12.       data_in |= 0x80;  // Put the data in the MSB.
    13. }
    14.  
    15. // We've now read 8 bits.
    16. // Do the same for the next 4 bits.
    17.  
    Got it?
     
  6. tbark

    Thread Starter New Member

    May 6, 2008
    3
    0
    Thanks alot man. sorry but could u explain what 'spin' means? and the line:
    " data_in |= 0x01 "
     
  7. Caveman

    Active Member

    Apr 15, 2008
    471
    0
    The semicolon at the end of the while loop means that nothing else is being done in the loop, so it just will wait for the statement to become false. It is spinning there.

    data_in |= 0x01; is the same as
    data_in = data_in | 0x01;

    This will set bit 0 of data_in. You want this to happen because you are shifting in a one here. If the input port is a 0, you don't have to do anything because it is already a zero (due to the shift left).
     
Loading...