2022-11-07 08:11 PM
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
the proteus desgin we are testing on is mentioned below
//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
Solved! Go to Solution.
2022-11-08 01:04 PM
There are many issues with that code, but one of them is very important.
if(GPIOB->IDR&=0x3)
This code is not only reading and comparing the IDR value, but also writing it back the result. First, it's not what you intended. Second, IDR is a read-only register. Also take a note that each read of that register can potentially return a different value if external signals change. It's better to read it once into a variable and then use the variable for comparisons etc.
2022-11-07 08:13 PM
2022-11-07 08:58 PM
Not looking to get involved in homework assignments.
Your masking/testing of GPIOB->IDR looks wrong, focus on that.
2022-11-07 09:49 PM
Okay sir !
Thanks for the response.
2022-11-08 01:04 PM
There are many issues with that code, but one of them is very important.
if(GPIOB->IDR&=0x3)
This code is not only reading and comparing the IDR value, but also writing it back the result. First, it's not what you intended. Second, IDR is a read-only register. Also take a note that each read of that register can potentially return a different value if external signals change. It's better to read it once into a variable and then use the variable for comparisons etc.