Using C to program a PIC to read a serial input

Thread Starter

tbark

Joined May 6, 2008
3
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.
 

hgmjr

Joined Jan 28, 2005
9,027
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
 

Mark44

Joined Nov 26, 2007
628
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.
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.
 

Thread Starter

tbark

Joined May 6, 2008
3
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.
 

Caveman

Joined Apr 15, 2008
471
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:
Rich (BB code):
unsigned char  data_in = 0;  // Initialize to 0.

// Read 8 bits
for (count=0;count<8;count++)
{
   data_in <<= 1;   // Shift the data from last time.
   while (PORTA & 0x01);      // Spin while PORTA.0 is high.
   while (!(PORTA & 0x01));  // Spin while PORTA.0 is low.
   // Rising edge has occurred, so read the port.
   if (PORTB & 0x01)  // If data is high
      data_in |= 0x01;  // Put the data in the LSB.
}

// We've now read 8 bits.
// Do the same for the next 4 bits.
If you want to read in MSB first, then you will do this:
Rich (BB code):
unsigned char data_in = 0;  // Initialize to 0.

// Read 8 bits
for (count=0;count<8;count++)
{
   data_in >>= 1;   // Shift the data from last time.
   while (PORTA & 0x01);      // Spin while PORTA.0 is high.
   while (!(PORTA & 0x01));  // Spin while PORTA.0 is low.
   // Rising edge has occurred, so read the port.
   if (PORTB & 0x01)  // If high
      data_in |= 0x80;  // Put the data in the MSB.
}

// We've now read 8 bits.
// Do the same for the next 4 bits.
Got it?
 

Caveman

Joined Apr 15, 2008
471
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).
 
Top