依然是使用标准模板写数码管显示,这里写一个简单的数码管显示。
基本思路
smg.c
#include "smg.h"
uchar wei[8]={
0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
uchar duan[11]={
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf};
uchar zhi[8]={
0};//存显示内容的数组
void chuli(uchar d0,d1,d2,d3,d4,d5,d6,d7)//将显示的内容存入数组
{
zhi[0]=d0;
zhi[1]=d1;
zhi[2]=d2;
zhi[3]=d3;
zhi[4]=d4;
zhi[5]=d5;
zhi[6]=d6;
zhi[7]=d7;
}
void display()//显示
{
static uchar i=0;
Y7;P0=0xff;//消隐
Y6;P0=wei[i];
Y0;P0=0xff;//防止最后一位高亮
Y7;P0=duan[zhi[i]];
i++;
if(i==8)i=0;
}
smg.h
#ifndef _SMG_H_
#define _SMG_H_
#include "common.h"
void chuli(uchar d0,d1,d2,d3,d4,d5,d6,d7);
void display(void);
#endif
ds.c
#include "ds.h"
extern uchar timer_flag;
void InitTimer0()//定时器初始化
{
TMOD=0x01;
TH0=(65535-1000)/256;
TL0=(65535-1000)%256;
ET0=1;
TR0=1;
EA=1;
}
void ServiceTimer0() interrupt 1//定时器0服务函数
{
TH0=(65535-1000)/256;
TL0=(65535-1000)%256;
display();//显示函数,每1ms刷新一次
timer_flag=1;
}
ds.h
#ifndef _DS_H_
#define _DS_H_
#include "common.h"
#include "smg.h"
void InitTimer0(void);
#endif
main.c
#include "main.h"
uchar timer_flag=0;
void main()
{
cls_buzz();
InitTimer0();
while(1)
{
if(timer_flag==1)
{
//chuli(1,2,10,3,4,10,5,6);//显示12-34-56
chuli(1,10,2,10,3,10,4,10);//将需要显示的内容在这里写入显示1-2-3-4-,duan[10]数组里第10个存的是是-
timer_flag=1;
}
}
}
main.h
#ifndef _MAIN_H_
#define _MAIN_H_
#include "common.h"
#include "ds.h"
#include "smg.h"
#endif
common.c
#include "common.h"
void cls_buzz()//关闭蜂鸣器、灯光
{
Y4;P0=0xff;
Y5;P0=0xa0&(P0|0x5f);
}
common.h
#ifndef _COMMON_H_
#define _COMMON_H_
#include "STC15F2K60S2.h"
#include
#define uchar unsigned char
#define uint unsigned int
#define Y4 P2=0x9f&(P2|0xe0);
#define Y5 P2=0xbf&(P2|0xe0);
#define Y6 P2=0xdf&(P2|0xe0);
#define Y7 P2=0xff&(P2|0xe0);
#define Y0 P2=0x1f&(P2|0xe0);
void cls_buzz(void);
#endif
这样写的好处: