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;
}
}