STM32 simple four arithmetic calculator, no operating system required

It can realize simple four arithmetic operations, addition, subtraction, multiplication and division, without operating system, and can be solved with five buttons and an LCD screen.

1. Button initialization

void KEY_Init(void) //IO初始化
{ 
 	GPIO_InitTypeDef GPIO_InitStructure;
 
 	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);//ʹÄÜPORTA,PORTEʱÖÓ

    //KEY0~KEY4
    GPIO_InitStructure.GPIO_Pin =GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入
 	GPIO_Init(GPIOE, &GPIO_InitStructure);//初始化IO口

}

 Because the pull-up input is set, it is grounded.

2. Button processing function

//按键处理函数
//返回按键值
//mode:0,不支持连按´;1,支持连按;
u8 KEY_Scan(u8 mode)
{	 
	static u8 key_up=1;//按键松开标志
	if(mode)key_up=1;  //支持连按		  
	if(key_up&&(KEY0==0||KEY1==0||KEY2==0||KEY3==0||KEY4==0))
	{
		delay_ms(10);//去抖动
		key_up=0;
		if(KEY0==0)return KEY0_PRES;
		else if(KEY1==0)return KEY1_PRES;
		else if(KEY2==0)return KEY2_PRES;
		else if(KEY3==0)return KEY3_PRES;
		else if(KEY4==0)return KEY4_PRES;
	}else if(KEY0==1&&KEY1==1&&KEY2==1&&KEY3==1&&KEY4==1)key_up=1; 	    
 	return 0;// 无按键按下
}

3. Main function

 int main(void)
 {	 
 	u16 x,y=0;
    u16 Z,flag,num=0;
	vu8 key=0;
	delay_init();	    	 //延时函数初始化

   //设置NVIC中断分组2,2位抢占优先级,2位响应优先级
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	uart_init(115200);	 	//´串口初始化为115200
	LCD_Init();  //LCD屏幕初始化
    KEY_Init();  //按键初始化
	POINT_COLOR=RED;  //设置字体为红色
  	while(1) 
	{
		
		key=KEY_Scan(0);	//等到键值
	   	if(key)
		{						   
			switch(key)
			{			
        case KEY2_PRES:		
					flag=1;
        break;				
				case KEY0_PRES:
					if (flag==1)x--;
				  else   x++;
					break; 
				case KEY1_PRES:
					if (flag==1)y--;
					else  y++;
					break;
				case KEY3_PRES:
       {u8 i;
		     i++;
					
				if(i>4)i=0;
					switch(i)
					{case 1:
						LCD_ShowString(55,150,10,10,24,(u8*)"+");
						Z=1;
						break;						
  					case 2: 
						LCD_ShowString(55,150,10,10,24,(u8*)"-");
						Z=2;
						break;
						case 3: 
						LCD_ShowString(55,150,10,10,24,(u8*)"*");
						Z=3;
						break;
						case 4: 
						LCD_ShowString(55,150,10,10,24,(u8*)"/");
						Z=4;
						break;
						}
				};break;
			 
				case KEY4_PRES:	
         flag=0;					
				if(Z==1)num=x+y;
				if(Z==2)num=x-y;
				if(Z==3)num=x*y;
				if(Z==4)num=x/y;
        break;	
				
			}
		}else delay_ms(10); 
	
		LCD_ShowNum(30,120,x,4,24);
		LCD_ShowNum(30,180,y,4,24);
		LCD_ShowString(55,210,10,10,24,(u8*)"="); 	
		LCD_ShowNum(30,240,num,5,24);
	}
		
}

KEY2 button. After pressing it down, the flag bit will change. When it is determined that the flag becomes 1 and KEY0 and KEY1 are pressed, the values ​​of x and y will decrease, otherwise they will increase. Increase or decrease by 1 each time. By pressing KEY3, use the switch statement to obtain the current operation symbol.

Experimental results

 

It is not easy to make, and complete engineering documents are required, please like and add to the comment area and leave an email

Guess you like

Origin blog.csdn.net/m0_63171897/article/details/127366034