key.c
#include "key.h" //把led.h文件包含进来
#include "system.h" //把timer0.h文件包含进来
#include "smg.h"extern uint num;
char key_value; //有按键按下
char key_shourtflag;//没有按键按下uint KeyScan () //按键扫描函数
{static uint timer=0;char key_temp=0;if(k1==0){key_temp|=0x01;}if(k2==0){key_temp|=0x02;}if(k3==0){key_temp|=0x04;}if(k4==0){key_temp|=0x08;}if(key_temp!=0){timer++; if(timer==20){key_value=key_temp;key_shourtflag=1;} else if(timer==1000){timer=500;key_value=key_temp;key_shourtflag=1;} //否则长按不停加1 }else{key_shourtflag=0;timer=0; //定时清0}}void KEY_SHEZHI() //按键设置
{ if(key_shourtflag==0) //没有按键按下直接退出,不执行以下内容{return;}switch(key_value) {case(0):break;case(0x01):num++;break;case(0x02):num++; break;case(0x04):num--;break;case(0x08):num--;break;case(0x03): //双按键num=6666;break;case(0x5): //双按键num=7777;break; case(0x9): //双按键num=8888;break;case(0x7): //三按键num=9999;break; } key_value=0; }
key.h
#include "system.h" //把timer0.h文件包含进来
#include "smg.h"
sbit k1=P3^7;
sbit k2=P3^6;
sbit k3=P3^5;
sbit k4=P3^4;uint KeyScan ();
void KEY_SHEZHI();
smg.c
#include "smg.h"
sbit WEI0 = P2^7;
sbit WEI1 = P2^6;
sbit WEI2 = P2^5;
sbit WEI3 = P2^4;
sbit aa = P0^0;
sbit bb = P0^1;
sbit cc = P0^2;
sbit dd = P0^3;
sbit ee = P0^4;
sbit ff = P0^5;
sbit gg = P0^6;
sbit dp = P0^7;
uint num=0;uchar code smgduan[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//共阴
//uchar code smgduan[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//共阳//uchar DisplayData[3];void smg_DigDisplay() //数码管显示
{static uchar i=0;P0=0x00;//消隐switch(i) //位选,选择点亮的数码管,{case(0):WEI0=0;WEI1=1;WEI2=1;WEI3=1;smg_scan(num % 10000/ 1000); break;//显示第0位case(1):WEI0=1;WEI1=0;WEI2=1;WEI3=1;smg_scan(num % 1000 / 100);break;//显示第1位case(2):WEI0=1;WEI1=1;WEI2=0;WEI3=1;smg_scan(num % 100/10);break;//显示第2位case(3):WEI0=1;WEI1=1;WEI2=1;WEI3=0;smg_scan(num%10);break;//显示第3位} i++; if(i>3){i=0;}}//数码管 驱动
void smg_scan(unsigned char n)
{uchar temp;temp = smgduan[n];aa = temp & (0x01<<0);bb = temp & (0x01<<1);cc = temp & (0x01<<2);dd = temp & (0x01<<3);ee = temp & (0x01<<4);ff = temp & (0x01<<5);gg = temp & (0x01<<6);
}///******************
//数码管 驱动
//*********************/
//void smg_scan(unsigned char n)
//{
// uchar temp;
// temp = smgduan[n];
// if(temp&0x01)aa=1;
// else aa=0;
//
// if(temp&0x02)bb=1;
// else bb=0;
//
// if(temp&0x04)cc=1;
// else cc=0;
//
// if(temp&0x08)dd=1;
// else dd=0;
//
// if(temp&0x10)ee=1;
// else ee=0;
//
// if(temp&0x20)ff=1;
// else ff=0;
//
// if(temp&0x40)gg=1;
// else gg=0;
//
// if(temp&0x80)dp=1;
// else dp=0; //}void SMG_XS(void)
{static uint x=0;x++;if(x>=1000) //定时500毫秒{ num++;x=0;}} 在这里插入代码片
smg.h
#include "system.h"//sbit WEI0 = P2^7;
//sbit WEI1 = P2^6;
//sbit WEI2 = P2^5;
//sbit WEI3 = P2^4;void smg_DigDisplay(); //数码管位选
void smg_scan(unsigned char n);//数码管 驱动
void SMG_XS(void);在这里插入代码片
#include “system.c”
#include "system.h"uchar flag_1ms=0;
unsigned char count_0_1ms=0;
uint count_100us=0;
void Timer0_Init(void)
{TH0 = T1MS>>8; //赋初值TL0 = T1MS; //赋初值TMOD = 0x01; //设置定时器0用十六位定时器方式EA = 1; //打开总中断ET0 = 1; //打开定时器0中断TR0 = 1; //启动定时器0
}void Timer0() interrupt 1 //中断号1
{ TH0 = T1MS>>8; //赋初值TL0 = T1MS; //赋初值count_0_1ms++;if(count_0_1ms>=10){flag_1ms=1;count_0_1ms=0;}count_100us++;
}void Delay_us(uint xus)
{for(;xus>0;xus--);
}void Delay_ms(uint xms)
{for(;xms>0;xms--){Delay_us(1000);}
}
#include “system.h”
//#include "reg51.h"
#include "STC12C5630AD.H"
//#include "STC12C5A60S2.H"
#define uchar unsigned char //定义无符号字符
#define uint unsigned int //定义无符号整形#define u8 unsigned char //定义无符号字符
#define u16 unsigned int //定义无符号整形#define FOSC 12000000L //定义晶振频率
#define T1MS (65536-FOSC/12/10000) //定时10毫秒的时候,计数器要写入的值void Delay_us(uint xus); //us级延时
void Delay_ms(uint xms); //ms级延时
void Timer0_Init(void); //定时器初始化
main.c
#include "system.h"
#include "key.h"
#include "led.h"
#include "smg.h"extern uchar flag_1ms; //在其他模块中寻找其定义。另外,extern也可用来进行链接指定。void main(void) //程序的主函数
{
// P0M0 = 0x0;
// P0M1 = 0xff;
// P0M0 = 0x20;
// P0M1 = 0x20;Timer0_Init(); 定时器0函数调用while(1) //大括号里面的东西,被不停的循环执行{ //1ms进入一次if(flag_1ms==1) //加了我之后,我的大括号里面的东西,被定期的1ms执行一次,反复执行 {flag_1ms=0; smg_DigDisplay();//数码管位选Led_SHANSHUO();// LED 500ms闪烁KEY_SHEZHI(); //按键设置KeyScan ();//按键扫描函数// smg_XS(); //数码管显示}}
}