Thread: Beginner GPIO Question

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

    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. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    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++)
        {
            int one_or_zero = READ_YOUR_DEVICE();
    
            samples[n] <<= 1; // shift bits over one position
    
            if (one_or_zero == 1)
            {
                samples[n] |= 1;
            }
        }
    }
    
    int main(void)
    {
        unsigned char samples[4] = { 0 }; // all bits zeroed
        int i;
    
        // ...
    
        for (i = 0; i < 4; i++)
        {
            read8bits(samples, i);
        }
    
        // ...
    
        return 0;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    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. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This appears to be a re-worded duplicate of Help! Embedded C GPIO Issue

    *thread closed*
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help! Embedded C GPIO Issue
    By stumpyhuck29 in forum C Programming
    Replies: 1
    Last Post: 02-03-2014, 08:53 PM
  2. Beginner Question -- From Absolute Beginner's Guide to C
    By Dghelerter in forum C Programming
    Replies: 5
    Last Post: 12-26-2013, 01:30 PM
  3. Beginner question
    By johnmeikle33 in forum C Programming
    Replies: 16
    Last Post: 12-23-2008, 11:50 AM
  4. Beginner Question
    By pobri19 in forum C Programming
    Replies: 5
    Last Post: 05-03-2008, 04:04 AM
  5. beginner question
    By davidnj732 in forum C Programming
    Replies: 2
    Last Post: 02-17-2003, 09:45 PM