按键控制两位数码管加减

关于按键方法的原帖地址:http://www.amobbs.com/thread-4308630-1-1.html
//本文按键未经消抖处理,有了好方法再来上传。

#include "reg51.h"

sbit we = P2^7;
sbit du = P2^6;

typedef unsigned char uchar8;
typedef unsigned int uint16;

uchar8 code table[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77};//段码

uchar8 i = 0;
char buf[2] = {0};
bit SystemTime2ms = 0;//系统2MS时标
void Smg_Display(char* Smg_Cnt)
{
	buf[0] = *Smg_Cnt/10;
	buf[1] = *Smg_Cnt%10;
	if(SystemTime2ms == 1)//2MS定时时间到
	{
		SystemTime2ms = 0;//2MS标志清零
		P0 = ~(0x01 << i);
		we = 1;//打开位选
		we = 0;//关闭位选
		P0 = table[buf[i]];
		du = 1;//打开段选
		du = 0;//关闭段选
		if(++i > 1)
		{
			i = 0;
		}
	}
}

void T0_Init(void)//定时器初始化函数
{
	TMOD = 0X01;
	TH0 = (65536 - 2000)/256;//设置定时器初值
	TL0 = (65536 - 2000)%256;
	TR0 = 1;//打开定时器0
	ET0 = 1;//打开定时器0中断
	EA = 1;//开总中断
}
	
void Key_Read(uchar8 *Trg,uchar8 *Cont)
{
	uchar8 ReadData = ((P3 ^ 0xff)&0x0f);
	*Trg = ReadData & (ReadData ^ (*Cont));
	*Cont = ReadData;
}

void Key_Process(void)
{
  static uchar8 Trg = 0,Cont = 0;
  static char Smg_Cnt = 0;
  Key_Read(&Trg,&Cont);
  switch(Trg)
  {
  	case 0x01:
	{
		if(++Smg_Cnt > 99)
		{
		Smg_Cnt = 0;
		}
	}
	break;

	case 0x02:
	{
		if(--Smg_Cnt < 0)
		{
		Smg_Cnt = 99;
		}
	}
	break;
  }
  Smg_Display(&Smg_Cnt);
}


void main()
{
	T0_Init(void);
	while(1)
	{
	 Key_Process();
	}
}

void Time0(void)interrupt 1
{
	TH0 = (65536 - 2000)/256;//重装定时器初值
	TL0 = (65536 - 2000)%256;
	SystemTime2ms = 1;//系统2MS时标置1	
}
  • 5
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,那我先介绍一下如何使用STM32标准库来定义按键和数码管。 首先需要在代码中引入相应的头文件,如下所示: ```c #include "stm32f10x.h" #include "stm32f10x_gpio.h" #include "stm32f10x_rcc.h" ``` 接着需要定义按键和数码管的引脚,可以使用GPIO宏来定义,如下所示: ```c #define KEY1_PIN GPIO_Pin_0 #define KEY2_PIN GPIO_Pin_1 #define LED1_PIN GPIO_Pin_8 #define LED2_PIN GPIO_Pin_9 #define LED3_PIN GPIO_Pin_10 #define LED4_PIN GPIO_Pin_11 #define SEG_A_PIN GPIO_Pin_12 #define SEG_B_PIN GPIO_Pin_13 #define SEG_C_PIN GPIO_Pin_14 #define SEG_D_PIN GPIO_Pin_15 ``` 然后需要对引脚进行初始化,使用RCC和GPIO宏来初始化,如下所示: ```c RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = KEY1_PIN | KEY2_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = SEG_A_PIN | SEG_B_PIN | SEG_C_PIN | SEG_D_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOC, &GPIO_InitStructure); ``` 以上代码使用了GPIOA、GPIOB和GPIOC来初始化引脚,需要根据实际情况进行修改。 接着,需要编写一个循环,用来检测按键状态和控制数码管的显示。具体代码如下: ```c while(1) { if(GPIO_ReadInputDataBit(GPIOA, KEY1_PIN) == RESET) { // 按下KEY1,数码管加1 num++; if(num > 9999) num = 0; } if(GPIO_ReadInputDataBit(GPIOA, KEY2_PIN) == RESET) { // 按下KEY2,数码管减1 num--; if(num < 0) num = 9999; } // 显示num的四位数码 int temp = num % 10; GPIO_WriteBit(GPIOC, SEG_A_PIN, temp & 0x01); GPIO_WriteBit(GPIOC, SEG_B_PIN, (temp >> 1) & 0x01); GPIO_WriteBit(GPIOC, SEG_C_PIN, (temp >> 2) & 0x01); GPIO_WriteBit(GPIOC, SEG_D_PIN, (temp >> 3) & 0x01); temp = (num / 10) % 10; GPIO_WriteBit(GPIOB, LED1_PIN, temp & 0x01); GPIO_WriteBit(GPIOB, LED2_PIN, (temp >> 1) & 0x01); GPIO_WriteBit(GPIOB, LED3_PIN, (temp >> 2) & 0x01); GPIO_WriteBit(GPIOB, LED4_PIN, (temp >> 3) & 0x01); temp = (num / 100) % 10; GPIO_WriteBit(GPIOC, SEG_A_PIN, temp & 0x01); GPIO_WriteBit(GPIOC, SEG_B_PIN, (temp >> 1) & 0x01); GPIO_WriteBit(GPIOC, SEG_C_PIN, (temp >> 2) & 0x01); GPIO_WriteBit(GPIOC, SEG_D_PIN, (temp >> 3) & 0x01); temp = num / 1000; GPIO_WriteBit(GPIOC, SEG_A_PIN, temp & 0x01); GPIO_WriteBit(GPIOC, SEG_B_PIN, (temp >> 1) & 0x01); GPIO_WriteBit(GPIOC, SEG_C_PIN, (temp >> 2) & 0x01); GPIO_WriteBit(GPIOC, SEG_D_PIN, (temp >> 3) & 0x01); // 延时一段时间 for(int i=0; i<100000; i++); } ``` 以上代码中,使用了一个变量num来存储当前数码管显示的数字。当按下按键时,会改变num的值,然后根据num的值来控制数码管的显示。在代码的最后,使用一个for循环来进行延时,以便观察数码管的显示效果。 需要注意的是,以上代码仅为示例代码,具体实现方式需要根据实际情况进行修改。同时,还需要根据实际情况选择合适的GPIO引脚来进行初始化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值