1. ## Beginner GPIO Question

I am very new at programming and I am trying to read a square pulse train coming into one of my digital GPIO pins of my PIC12 microcontroller. I have to "sample" and then store the incoming signal so that I have a bunch of 1's and 0's saved (possibly in an array). I have to save the amount of 1's and 0's I get(which are sampled in packets of 8, 4 times over) and calculate the number of 1's or 0's into an average. Anyone know how I can accomplish this in C code?

Here is the code I have so far:

Code:
```#define one_or_zero GP4       //My I/O variable assignment for this                                           //pin
void getONEorZERO(void)	//This function gets 4 samples(A                                                 //through D)
//, all 8 bits long from the General                                               // Purpose input.
{
char A=1;	//Flag for SampleA0,A1 performed first.
char B=0;	//Flag for SampleB0,B1 performed second.
char C=0;	//Flag for SampleC0,C1 performed third.
char D=0;	//Flag for SampleD0,D1 performed last.
if(A=1)
{
for(i=0;i<8;i++)
{
if(one_or_zero==0)
{
sampleA_result|=(sampleA[i])<<(7-i);
//here I am hoping to get the bit,
//then place it into a specific location of the                                 //array.
//This entire function can definitely be
//simplified by using pointers, but I don't                                       //really know how.
}
if(one_or_zero==1)
{
sampleA_result|=(sampleA[i])<<(7-i);
}
}
A=0;
B=1;
}
if(B=1)
{
for(i=0;i<8;i++)
{
if(one_or_zero==0)
{
sampleB_result|=(sampleB[i])<<(7-i);
}
if(one_or_zero==1)
{
sampleB_result|=(sampleB[i])<<(7-i);
}
}
B=0;
C=1;
}
if(C=1)
{
for(i=0;i<8;i++)
{
if(one_or_zero==0)
{
sampleC_result|=(sampleC[i])<<(7-i);
}
if(one_or_zero==1)
{
sampleC_result|=(sampleC[i])<<(7-i);
}
}
C=0;
D=1;
}
if(D=1)
{
for(i=0;i<8;i++)
{
if(one_or_zero==0)
{
sampleD_result|=(sampleD[i])<<(7-i);
}
if(one_or_zero==1)
{
sampleD_result|=(sampleD[i])<<(7-i);
}
}
D=0;
A=1;
}
}``` 2. Firstly, remember that the equality operator is ==, not just =. You need to change this in your if conditions.

Even with this change it's difficult to understand what your code is trying to do. Where are your variables declared? Do you mean getONEorZERO to be called multiple times, and each time it should execute a different section of code (guarded by your A, B, C, D variables) ? You would need to make A,B,C,D static and use else if's to do that, but it doesn't seem necessary.

Perhaps something more like this, although obviously a lot is still missing.
Code:
```void read8bits(unsigned char samples[], int n)
{
int i;
for (i = 0; i < 8; i++)
{

samples[n] <<= 1; // shift bits over one position

if (one_or_zero == 1)
{
samples[n] |= 1;
}
}
}

int main(void)
{
unsigned char samples = { 0 }; // all bits zeroed
int i;

// ...

for (i = 0; i < 4; i++)
{
}

// ...

return 0;
}``` 3. Assuming the program is trying to read a serial stream of digital data, you need some type of clocking / timing ability. Assuming there's a start bit for each transmitted code, then the program polls in a tight loop waiting for the start bit transition, then "waits" 1 1/2 data rate cycles to get into the middle of a transmission bit cycle, then reads a data bit, waits 1 data rate cycle, reads the next data bit, ... repeating this cycle until all data bits and optionally a "stop" bit are read. 4. This appears to be a re-worded duplicate of Help! Embedded C GPIO Issue Popular pages Recent additions 