I have this project I am doing and I am a beginner. 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 number 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 either start of accomplish this in C code?
Here is the code I have so far. Please keep in mind that I have no idea what I am doing with these arrays:
Code:
#define one_or_zero GP4 //GP4 is the PIC's digital input expecting
//a pulse train of 1's and 0's at 50% duty cycle
int sampleA_result; //These are used to store the bits in different
//"locations" of the array
int sampleB_result;
int sampleC_result;
int sampleD_result;
char sampleA0[8]; //Declaring these arrays to be 8 "bits" long.
char sampleA1[8]; //Both A0 and A1 are 8 long because it is possible
//in my application to be
//getting nothing but 0's or nothing but 1's.
char sampleB0[8];
char sampleB1[8];
char sampleC0[8];
char sampleC1[8];
char sampleD0[8];
char sampleD1[8];
int sample_result[32]; //This is the average for all 32 bits
//that have been sampled either 1 or 0
int i;
main()
{
while(1)
{
//initializations,etc. go here
sleep(); //I don't really know how to slow the pulse
//train down to easily sample it at my sampling frequency
getONEorZERO();
average();
}
}
/////////////////////////////////////////////////
void sleep(void)
{
//something goes here
}
/////////////////////////////////////////////////
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;
}
}
///////////////////////////////////////////////////////
void average(void)
{
sample_result=(sampleA_result+sampleB_result+sampleC_result+sampleD_result)>>3;
//taking 32 bits that have been accumulated
//and finding an average
//of how many are 1's. The bit-shift should be equal to 32/4.
if(sample_result<0x06) //if I get less than seven 1's in my averaged result something happens
{
//something happens
}
}