[ARM] increment and decrement of 7 segment(0-99 count) using 2 buttons

Status
Not open for further replies.

CuriousSoul

Newbie
Joined
Nov 7, 2022
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
3
Question: Interface two seven-segment LED displays to Port C of STM32F4 MCU that mustincrement and show the rolling of two digits from 00 onwards, to a max of 99, when aswitch SW1 (PB0) is pressed. And must decrement when the user is pressing SW2(PB1).



the issues we are facing is there is increment and decrement happening but then increment is not happening after it is decrementing till 0

the other issues which are being faced are mentioned below

1) the system is operating even when both the both the switches are off to the topmost mentioned conditional IF loop let it be increment or decrement

The conditions which are to be satisfied for us are

1) both on nothing should be done
2) both off nothing should be done
3) xor operation it should do increment/decrement(one OFF one ON combination)
4) after 99 in increment it should halt and not go to 0
5) after 0 in decrement it should stop

C:
//program starts from here

#include<stm32f401xe.h>
unsigned char Digit[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7c,0x07,0x7f,0x6f};
void MSdelay(unsigned int );
void MSdelay(unsigned int times)
{
    unsigned int i=0,j=0;
    for(j=0;j<times;j++)
    {
        for(i=0;i<1400;i++);
    }        
}



int main(void)
{
       unsigned int count1=0;//intialising count variable
       RCC->AHB1ENR|=6;//initialising clock for port B and port C
                GPIOB->MODER&=~(0XF);//clearing and setting port B as input
         GPIOC->MODER&=~(0X0FFFFFFF);//resetting port C MODER register
         GPIOC->MODER|=(0X05555555);//Setting all PC0-PC13 as outputs of port C
         GPIOB->PUPDR&=~(0XF);//clearing pull-up/pull-down configuration of switch B
         GPIOB->PUPDR|=(0X05);//setting them as pull up
       while(1)
         {
                    if(GPIOB->IDR&=0x3)
                     {
                         GPIOC->ODR=(Digit[0]<<7)|(Digit[0]);
                     }
        
                      if(GPIOB->IDR&=0X2)//If PB0 is ON
            {
                         GPIOC->ODR=(Digit[count1/10]<<7)|Digit[count1%10];
                         count1++;
                         if(count1>=100)
                         {
                             count1=99;
                             GPIOC->ODR=(Digit[count1/10]<<7)|Digit[count1%10];
                         }     
                         MSdelay(170);    
              }
                        else if(GPIOB->IDR&=0x1)//IF PB1 is ON
            {
                        count1--;
                        GPIOC->ODR=(Digit[count1/10]<<7)|Digit[count1%10];
                         if(count1<1)
                         {
                             GPIOC->ODR=(Digit[0]<<7)|(Digit[0]);                                                     
                         }
                         MSdelay(170);
                                                 //INCREMENT(count1);
            }
            }
            
}

// program ends here

the proteus desgin we are testing on is mentioned below
 

Suggestions:

* Display messages onscreen telling you what it's doing every step of the way.

* Break up your IF...ELSE tree into separate steps:
1) Examine state of switches.
2) Based on switch conditions, set a variable to 1 or -1 or zero.
3) Add that variable to your count.
4) Examine count value. (If it's 100 then set it to 99. If it's -1 then set it to zero.)
5) Display count value.
 

... and to make your code easier to follow and debug, use #define statements to give meaningful names to the inputs and outputs.

For example:
"else if(GPIOB->IDR&=0x1)//IF PB1 is ON"
would be easier to understand as "else if(input == decrementSwitch)" or words to that effect.

Brian.
 
some really quick observations here.

Your two conditions for the switches should be: (A and not B); (B and not A).
Use elseif instead of else if.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…