Thread: Help! Embedded C GPIO Issue

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    3

    Help! Embedded C GPIO Issue

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

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    if all you need are the average values, why do you need to store the 1's and 0's in an array? why not just count the number of ones's present every 8 bits and use that to find your average?
    what do you know about the incoming signal frequencies? how do they compare to your "chip" frequency? if i am not mistaken your "chip" only supports edge detection so frequency comparison is critical,if comparison is good i would suggest using interrupts to sample the frequency

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help with some embedded C
    By Crazyffrey in forum C Programming
    Replies: 2
    Last Post: 08-31-2013, 03:27 AM
  2. Embedded Linux & Embedded Java
    By jdomm95 in forum Linux Programming
    Replies: 4
    Last Post: 10-02-2012, 09:01 AM
  3. bandwidth issue / network issue with wireless device communication
    By vlrk in forum Networking/Device Communication
    Replies: 0
    Last Post: 07-05-2010, 11:52 PM
  4. Help with Embedded SQL
    By cjohnman in forum C Programming
    Replies: 4
    Last Post: 04-10-2008, 02:06 PM
  5. embedded
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 08-12-2002, 05:12 AM