This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Loop back



#define SYSCTL_RCGC2_R (*((volatile unsigned long *)0x400FE108))
#define GPIO_PORTD_DATA_R (*((volatile unsigned long *)0x400073FC))
#define GPIO_PORTD_DIR_R (*((volatile unsigned long *)0x40007400))
#define GPIO_PORTD_AFSEL_R (*((volatile unsigned long *)0x40007420))
#define GPIO_PORTD_DEN_R (*((volatile unsigned long *)0x4000751C))
#define GPIO_PORTD_AMSEL_R (*((volatile unsigned long *)0x40007528))
#define GPIO_PORTD_PCTL_R (*((volatile unsigned long *)0x4000752C))



// Port B
#define GPIO_PORTB_DATA_R (*((volatile unsigned long *)0x400053FC))
#define GPIO_PORTB_DIR_R (*((volatile unsigned long *)0x40005400))
#define GPIO_PORTB_AFSEL_R (*((volatile unsigned long *)0x40005420))
#define GPIO_PORTB_DEN_R (*((volatile unsigned long *)0x4000551C))
#define GPIO_PORTB_CR_R (*((volatile unsigned long *)0x40005524))
#define GPIO_PORTB_AMSEL_R (*((volatile unsigned long *)0x40005528))
#define GPIO_PORTB_PCTL_R (*((volatile unsigned long *)0x4000552C))

unsigned long in,out, out2;
void ssd(int n);
void wait(void);
const char output[5] = {0x9B, // 4
0xB0, // 3
0xA4, // 2
0xF9, // 1
0xC0, // 0
};


int main(void){

unsigned long volatile delay;
unsigned long volatile delay1;

// Port D Initialization:
SYSCTL_RCGC2_R |= 0x08; // Port D clock
delay = SYSCTL_RCGC2_R; // wait 3-5 bus cycles
GPIO_PORTD_DIR_R |= 0x0A; // PD3, PD2 output
GPIO_PORTD_DIR_R &= ~0x01; // PD0 input
GPIO_PORTD_AFSEL_R &= ~0x09; // not alternative
GPIO_PORTD_AMSEL_R &= ~0x09; // no analog
GPIO_PORTD_PCTL_R &= ~0x0000F0FF; // bits for PD3, PD1, PD0
GPIO_PORTD_DEN_R |= 0x0B; // enable PD3, PD1, PD0


SYSCTL_RCGC2_R |= 0x02; // Port B clock
delay1 = SYSCTL_RCGC2_R; // wait 3-5 bus cycles
GPIO_PORTB_DIR_R |= 0xFF; // PB8-0 as output
GPIO_PORTB_AFSEL_R &= ~0xFF; // not alternative
GPIO_PORTB_AMSEL_R &= ~0xFF; // no analog
GPIO_PORTB_PCTL_R &= ~0xFFFFFFFF; // bits for PB8-0
GPIO_PORTB_DEN_R |= 0xFF; // enable PB8-0

int x = 2;
// Open loop:
while(x !=0 ){

in = (GPIO_PORTD_DATA_R&0x01); // in 0 if not pressed, 1 if pressed

if(in == 0x01){

out2 = (in^0x00)<<1;
out = (in^0x01)<<3;


// GPIO_PORTD_DATA_R = out;
//GPIO_PORTD_DATA_R = out2;
ssd(3);

}else if(in == 0x00){

out2 = (in^0x00)<<1;
out = (in^0x01)<<3;


// GPIO_PORTD_DATA_R = out2;
//GPIO_PORTD_DATA_R = out;

ssd(0);

}


}

} // End of design


// Method for delay:

void ssd(int n){

GPIO_PORTB_DATA_R = output[n];
}

I can't make it loop back when i wire up the circuit. It changes once and stop and does not loop back

It would be great if anyone would help me out.

  • Hi tony,

    What changes once and stops? Please provide more info.
    Also, either edit, or re-post the code now using the "insert code tool" ( looks like a </> in the top right of the editor) and...please index the code ( a line of code inside a "while" should be a TAB forward) . I am with new lenses and that just makes my eyes hurt (indexing the code is very important!) 

  • sorry guys,

    I am new to the platform didn't know about it.

    Question rephrase:

    When I wire up the circuit and press the input button, 1 changes to 0 in seven segment as expected,. then i let go of the button, the program must change back to 1, but the program completely quits.

    This is the updated code:

     // Port D:
    #define SYSCTL_RCGC2_R          (*((volatile unsigned long *)0x400FE108))
    #define GPIO_PORTD_DATA_R       (*((volatile unsigned long *)0x400073FC))
    #define GPIO_PORTD_DIR_R        (*((volatile unsigned long *)0x40007400))
    #define GPIO_PORTD_AFSEL_R      (*((volatile unsigned long *)0x40007420))
    #define GPIO_PORTD_DEN_R        (*((volatile unsigned long *)0x4000751C))
    #define GPIO_PORTD_AMSEL_R      (*((volatile unsigned long *)0x40007528))
    #define GPIO_PORTD_PCTL_R       (*((volatile unsigned long *)0x4000752C))
    
    // Port B:
    
    #define GPIO_PORTB_DATA_R       (*((volatile unsigned long *)0x400053FC))
    #define GPIO_PORTB_DIR_R        (*((volatile unsigned long *)0x40005400))
    #define GPIO_PORTB_AFSEL_R      (*((volatile unsigned long *)0x40005420))
    #define GPIO_PORTB_DEN_R        (*((volatile unsigned long *)0x4000551C))
    #define GPIO_PORTB_CR_R         (*((volatile unsigned long *)0x40005524))
    #define GPIO_PORTB_AMSEL_R      (*((volatile unsigned long *)0x40005528))
    #define GPIO_PORTB_PCTL_R       (*((volatile unsigned long *)0x4000552C))
    
    // Port A:
    
    #define GPIO_PORTA_DATA_R       (*((volatile unsigned long *)0x400043FC))
    #define GPIO_PORTA_DIR_R        (*((volatile unsigned long *)0x40004400))
    #define GPIO_PORTA_AFSEL_R      (*((volatile unsigned long *)0x40004420))
    #define GPIO_PORTA_DEN_R        (*((volatile unsigned long *)0x4000451C))
    #define GPIO_PORTA_CR_R         (*((volatile unsigned long *)0x40004524))
    #define GPIO_PORTA_AMSEL_R      (*((volatile unsigned long *)0x40004528))
    #define GPIO_PORTA_PCTL_R       (*((volatile unsigned long *)0x4000452C))
    	
    // Variables:
    
    unsigned long in1, out1, out1_2;
    unsigned long in2, out2, out2_2;
    unsigned long in3, out3, out3_2;
    unsigned long in4, out4, out4_2;
    
    // Port D initialization:
    
    void PortD_Init(void){
    	
    	unsigned long volatile delay;
    	SYSCTL_RCGC2_R |= 0x08;           // Port D clock
      delay = SYSCTL_RCGC2_R;           // wait 3-5 bus cycles
      GPIO_PORTD_DIR_R |= 0x0A;         // PD3, PD2 output
      GPIO_PORTD_DIR_R &= ~0x01;        // PD0 input 
      GPIO_PORTD_AFSEL_R &= ~0x09;      // not alternative
      GPIO_PORTD_AMSEL_R &= ~0x09;      // no analog
      GPIO_PORTD_PCTL_R &= ~0x0000F0FF; // bits for PD3, PD1, PD0
      GPIO_PORTD_DEN_R |= 0x0B;         // enable PD3, PD1, PD0
    }
    
    // Port B initialization:
    
    void PortB_Init(void){
    	
    	unsigned long volatile delay;
    	SYSCTL_RCGC2_R |= 0x02;           // Port B clock
      delay = SYSCTL_RCGC2_R;           // wait 3-5 bus cycles
      GPIO_PORTB_DIR_R |= 0xFF;         // PB8-0 as output
      GPIO_PORTB_AFSEL_R &= ~0xFF;      // not alternative
      GPIO_PORTB_AMSEL_R &= ~0xFF;      // no analog
      GPIO_PORTB_PCTL_R  = 0x00000000; // bits for PB8-0
      GPIO_PORTB_DEN_R  |= 0xFF;         // enable PB8-0
    }
    
    // Port A initialization:
    
    void PortA_Init(void){
    	
    	unsigned long volatile delay;
    	
    	SYSCTL_RCGC2_R |= 0x00;           // Port B clock
      delay = SYSCTL_RCGC2_R;           // wait 3-5 bus cycles
      GPIO_PORTA_DIR_R |= 0xFF;         // PB8-0 as output
      GPIO_PORTA_AFSEL_R &= ~0xFF;      // not alternative
      GPIO_PORTA_AMSEL_R &= ~0xFF;      // no analog
      GPIO_PORTA_PCTL_R  = 0x00000000; // bits for PB8-0
      GPIO_PORTA_DEN_R  |= 0xFF;         // enable PB8-0
    }
    // Seven segment look-up function:
    
    const char output[5] = {0x9B, // To display: 4
                            0xB0, // To display: 3
                            0xA4, // To display: 2
                            0xF9, // To display: 1
                            0xC0, // To display: 0
    	};
    
    // Data to Seven Segment display:
    	
    void sevenSegment_display(int n){
    	GPIO_PORTB_DATA_R = output[n];
    }
    
    // ParkingModule Counter dislay:
    
    void ParkingModule(int count){
    	
    			if(count == 4){
    				
    				sevenSegment_display(4);
    				
    			}else if(count == 3){
    				
    				sevenSegment_display(3);
    				
    			}else if(count == 3){
    				
    				sevenSegment_display(2);
    				
    			}else if(count == 1){
    				
    				sevenSegment_display(1);
    				
    			}else if(count == 0){
    				
    				sevenSegment_display(0);
    			}
    
    }
    
    int main(void){
    	
    	int count = 4;
    	int check1 = 0, check2 = 0, check3 = 0; // Counter Checking, whether to increment the counter or not.
    	
    	PortD_Init(); // Calling the subroutine of port D
    	PortB_Init(); // Calling the subroutine of port B
    	PortA_Init(); // Calling the subroutine of port A
    	
    	while(1){
    		
    		// Parking spot 1:
    		in1 = (GPIO_PORTD_DATA_R&0x01); // in 0 if not pressed, 1 if pressed
    		
    		if(in1 == 0x01){
        
    			out1_2 = (in1^0x00)<<1;
    			out1   = (in1^0x01)<<3;
    			
    			GPIO_PORTD_DATA_R = out1;
    			GPIO_PORTD_DATA_R = out1_2;
    			
    			check1 = 1;
    			
    			// Counter Incrementation loop:
    
    			if(count <= 0){
    				count = 0;
    			}else{
    				count = count - 1;
    			}
    			
    			ParkingModule(count);
    			
    		}else if(in1 == 0x00){
    			
    			out1_2 = (in1^0x00)<<1;
    			out1   = (in1^0x01)<<3;
    			
    			GPIO_PORTD_DATA_R = out1;
    			GPIO_PORTD_DATA_R = out1_2;
    			
    			// Counter Incrementation loop:
    			
    			if(count >= 4 && check1 == 1){
    				count = 4;
    				
    			}else if(count < 4 && check1 == 1){
    				check1 = 0;
    				count = count - 1;
    				
    			}else if(check1 == 0){
    				count = count;
    			}
    			
    			ParkingModule(count);
    		}
    		
    		// Praking spot 2:
    		
    		in2 = (GPIO_PORTA_DATA_R&0x00); // in 0 if not pressed, 1 if pressed
    		
    		if(in2 == 0x01){
        
    			out2_2 = (in2^0x00)<<1;
    			out2   = (in2^0x01)<<2;
    			
    			GPIO_PORTA_DATA_R = out2;
    			GPIO_PORTA_DATA_R = out2_2;
    			
    			check2 = 1;
    			
    			if(count == 0){
    				count = 0;
    			}else{
    				count = count - 1;
    			}
    			
    			ParkingModule(count);
    			
    		}else if(in2 == 0x00){
    			
    			out2_2 = (in2^0x00)<<1;
    			out2   = (in2^0x01)<<3;
    			
    			GPIO_PORTA_DATA_R = out2;
    			GPIO_PORTA_DATA_R = out2_2;
    			
    			// Counter Incrementation loop:
    			
    			if(count >= 4 && check2 == 1){
    				count = 4;
    				
    			}else if(count < 4 && check2 == 1){
    				check2 = 0;
    				count = count - 1;
    				
    			}else if(check2 == 0){
    				count = count;
    			}
    			
    			ParkingModule(count);
    		}
    		
    		// Parking Spot 3:
    		
    		in3 = (GPIO_PORTA_DATA_R&0x08); // in 0 if not pressed, 1 if pressed
    		
    		if(in3 == 0x01){
        
    			out3_2 = (in3^0x00)<<1;
    			out3   = (in3^0x01)<<2;
    			
    			GPIO_PORTA_DATA_R = out3;
    			GPIO_PORTA_DATA_R = out3_2;
    			
    			check3 = 1;
    			
    			if(count == 0){
    				count = 0;
    			}else{
    				count = count - 1;
    			}
    			
    			ParkingModule(count);
    			
    		}else if(in3 == 0x00){
    			
    			out3_2 = (in3^0x00)<<1;
    			out3   = (in3^0x01)<<3;
    			
    			GPIO_PORTA_DATA_R = out3;
    			GPIO_PORTA_DATA_R = out3_2;
    			
    			// Counter Incrementation loop:
    			
    			if(count >= 4 && check3 == 1){
    				count = 4;
    				
    			}else if(count < 4 && check3 == 1){
    				check3 = 0;
    				count = count - 1;
    				
    			}else if(check3 == 0){
    				count = count;
    			}
    			
    			ParkingModule(count);	
    		}
    		
      }// Infinite While Loop.
    }// End of design.
    

  • So my question is, since i have put infinite loop, why does not the program keep looping.

    I debugged the code and its was looping back, But not when i wire up the circuit,  it stops after one iteration.

  • Hello Tony,

    Where does the program quit to? Can you be more specific.

    Regards

    Amit

  • Why do you think it's not looping?

    Robert

  • when i construct the circuit on breadboard and and push the button, it the seven segment counts down as expected then, when i let go of the button, it just says the same, it is no counting up.

  • First, where do expect the program to count up?

    Second, I think your main function is way too complex and that's a big part of the reason you are having problems.

     

    Third this a good example of where Test Driven Design (TDD) would help prevent issues from reaching the micro.

     

    Robert

  • Hello Robert,

    I agree with you. It is way too complicated for a count up/down. And the lack of comments with the actual operations makes it more confusing.

    Regards

    Amit

  • Amit Ashara said:

    I agree with you. It is way too complicated for a count up/down. And the lack of comments with the actual operations makes it more confusing.

     No, it is not confusing is a typical  beginner coding where lack of structure and methods are in place, so forget coding and we open a contest:

     see here:

    void ParkingModule(int count){
        
                if(count == 4){
                    
                    sevenSegment_display(4);
                    
                }else if(count == 3){
                    
                    sevenSegment_display(3);
                    
                }else if(count == 3){
                    
                    sevenSegment_display(2);
                    
                }else if(count == 1){
                    
                    sevenSegment_display(1);
                    
                }else if(count == 0){
                    
                    sevenSegment_display(0);
                } 
    

     This is useless and sevenSegment_display(count); do the job, in other part resemble me some idea of VHDL or similar HDL, so this can come from FPGA design converted to software some way....

     Over the issue I BET over ISR fault!!

     Port D is used and other incriminated too, I don't see recommended action to prevent what is on sticky. To our poster we can gently say: <<please read all first introductory part of forum>>  and your loop maybe remain where is and don't go away. This is the usual... Hardware Bus error Fault.

     Why not split forum to best serve beginner? This way we can solve in one shot other than figure complex case coming from professional coders issues.

  • Hi Robert,

    I am a beginner. Just started using Texas Launchpad Tiva c series evaluation kit and also new to C programming language. I know that the method for pakingModule is useless, I could have just implemented using one method itself. The seven segment does display the numbers as intended. But it quits after one loop when I wire the circuit, by which I mean the program just stops entirely after I press the button. What do you think I could have done wrong?

    Any advice on how to make the code better?

    Thank you.

  • Hello Tony,

    Debug!!! When you stop the program, can you check in the IDE where is the Program Counter, On a CCS kind of IDE it will tell you where the program is looping in.

    Regards

    Amit

  • tony joseph said:
    Any advice on how to make the code better?

     First thing to make it better is style but style come with experience and time so now your code has to be converted to working than faulty.

     Amit suggested you debug, this is the first troubleshooter, if you single step code or pause when stop counting you can get the bug..

     Again I bet you find code looping on Fault ISR. Please read all top entry of this forum, you can find all you need to cure your trouble and avoid be gotcha again by. If you get still in trouble ask again but this is a really complex processor, is still plaguing me after long time I am using but avoiding common issue help be happy with! So please read all recommendation then do the long term job, get a good programming style and learn feature/trick and trap of programming.

     Better code is forever commented clean short and simple with function and procedure of non trivial task.

  • Tony, I'm just chock full of suggestions :)

    ParkingModule may not be doing much but I doubt it's causing you a problem.

    main is an issue and should be cleaned up.

    However, I think the first thing you should do is see if you can answer my question.  Where do expect the code to count up?

    I will respectfully disagree with Amit on the debug comment.  I think you will benefit much more from desk time then from bench time.

     

    So my advice more or less in order would be

    1. Sit down, slow down and think.  The most powerful tool at your disposal is your brain.  Don't neglect it by figuratively running all over the place.
    2. See if you can tell me where the code counts up
    3. Remove code unrelated to your problem and get your self down to a single button input. Simplify, Simplify, Simplify.  You may have a need for that code later but for now ruthlessy prune away what you do not need.
    4. Code review.  This is small enough to sit down with pen and paper and hand execute. Just walk through the execution paths and make sure what you expect to do is what you are doing.

    Longer term

    • Get PC-Lint and use it before compiling.  You have code segements here that literally do nothing.  There can be reasons to do that but they don't apply here.
    • Get and read "Test-Driven Development for Embedded C" by James Grenning.  Start moving in the direction of using it. It can save a lot of grief when dealing with things like your counter.  Algorithm and coding problems are a lot easier to find on a PC with a lot of disk space.

    Robert

  • Robert Adsett said:
    I will respectfully disagree with Amit on the debug comment

     Sorry Robert, I disagree with you too, Amit suggested to debug to see where code hang up. This is still a good way, the code on perspective of paper can be horrible but it can work, devil is under the hood this case so I recommend again our poster please read

    Diagnosing Common Development Problems and Tips for TM4C Devices

    search where your devil is, skip my contribution is not in your code, then also read  the following 2 post.

     If still problem arises we can help but a way to learn how to search for a solution and document issues is a must!

  • I don't think the code is hanging Roberto.

    Robert

  • Hello Robert and Roberto,

    This is the scenario where all of us are right in our own ways. The poster needs to go back to the drawing board (he already indicated he is a beginner), build the code top down/bottom's up on paper before entering the realm of the device.

    When on the device, debug the issue, see where the code is looping or faulting before coming back.

    Thanks to both of you we have a post where we can bring back ENG-101

    Regards

    Amit

  • Robert Adsett said:
    However, I think the first thing you should do is see if you can answer my question.  Where do expect the code to count up?

     Robert, really good question, I have to add other hint:

     When we use Brain and sit down to search for troubles we cannot find, another better way is to explain how program work to another person, two can see the worst or just it come out of the paper.

     When we sit in and read execute code we are slow, processor is FAASSSSSTTTTEERRRRRRRRRRRRRRRRRRRRRRRRRR and thing repeat at lighting speed. So think what happen in fast forward mode too.

    Check for all misinterpreted errors like A and 0 is forever zero, A Or 1 is forever 1. I seen some of these construct too.

  • Robert Adsett said:
    I don't think the code is hanging Roberto.

     Debug or not to debug? This dilemma got solved loading code to TIVA run pause and... OHHH

     Hi Robert, as teacher I got a fine print reading, as TIVA user I got  <<see what happen before diagnose>> so TADA I won, where can we meet for a good coffe cup? ;)