此程序是基于51hei单片机开发板上面写的,如需要移植到自己的电路上,修改相应的端口即可 ***********************************************************************
* @file main.c
* @author xr
* @date 2014年5月8日22:11:33 -- 2014年5月9日12:03:49
* @version V1.2.3
* @brief LCD1602液晶跑表 单片机STC89C52RC MCU 晶振 11.0592MHZ
***********************************************************************
*/
#include
/* 系统时钟 */
#define SYS_XTAL (11059200UL/12)
/* 定时器T0重载值 */
unsigned char thr0, tlr0;
unsigned char thr1, tlr1;
/* 跑表计数 */
unsigned char timer[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0}; //分别表示跑表的各个位上的数字
bit flag10ms = 0;
extern bit stopflag;//跑表走停标志位
extern void InitalLCD1602();
extern void LcdShowStr(unsigned char x, unsigned char y, unsigned char * str);
extern void KeyDriver();
extern void KeyScan();
void DisplayTimer();
void ConfigTimer0(unsigned int xms);
void ConfigTimer1(unsigned int xms);
/* 主函数main() */
void main(void)
{
ConfigTimer0(10); //定时10ms
ConfigTimer1(1);
InitalLCD1602();
LcdShowStr(0, 0, "stopwatch");
LcdShowStr(2, 1, "0000000.00s"); //液晶初始化显示
LcdShowStr(10, 0, "stop!");
while (1)
{
KeyDriver();
DisplayTimer();
if ((flag10ms == 1) && (stopflag == 1))
{
flag10ms = 0;
timer[0]++;
if (timer[0] > 9)
{
timer[0] = 0;
timer[1]++;
if (timer[1] > 9)
{
timer[1] = 0;
timer[2]++;
if (timer[2] > 9)
{
timer[2] = 0;
timer[3]++;
if (timer[3] > 9)
{
timer[3] = 0;
timer[4]++;
if (timer[4] > 9)
{
timer[4] = 0;
timer[5]++;
if (timer[5] > 9)
{
timer[5] = 0;
timer[6]++;
if (timer[6] > 9)
{
timer[6] = 0;
timer[7]++;
if (timer[7] > 9)
{
timer[7] = 0;
timer[8]++;
if (timer[8] > 9)
{
timer[8] = 0;
}
}
}
}
}
}
}
}
}
}
}
}
/* 将跑表时间显示到液晶上 */
void DisplayTimer()
{
unsigned char str[20];
/* 分解timer */
str[0] = timer[8] + '0';
str[1] = timer[7] + '0';
str[2] = timer[6] + '0';
str[3] = timer[5] + '0';
str[4] = timer[4] + '0';
str[5] = timer[3] + '0';
str[6] = timer[2] + '0';
str[7] = '.';
str[8] = timer[1] + '0';
str[9] = timer[0] + '0';
str[10] = '\0';
LcdShowStr(2, 1, str);
}
/* 定时器T0配置 */
void ConfigTimer0(unsigned int xms)
{
unsigned long tmp;
tmp = (SYS_XTAL * xms) / 1000;
tmp = 65536-tmp + 18;
thr0 = (unsigned char)(tmp >> 8) ;
tlr0 = (unsigned char)tmp;
TMOD &= 0xF0; //清零T0控制位
TMOD |= 0x01; //定时器方式1
TH0 = thr0;
TL0 = tlr0;
TR0 = 1; //开启timer0
ET0 = 1; //开启T0中断
EA = 1; //开启总中断
}
/* 配置定时器T1 */
void ConfigTimer1(unsigned int xms)
{
unsigned long tmp;
tmp = (SYS_XTAL * xms) / 1000;
tmp = 65536 - tmp + 18;
thr1 = (unsigned char)(tmp >> 8);
tlr1 = (unsigned char)tmp;
TMOD &= 0x0F;
TMOD |= 0x10;
TH1 = thr1;
TL1 = tlr1;
TR1 = 1;
ET1 = 1;
EA = 1;
}
/* 定时器T0中断服务 */
void Timer0_ISP() interrupt 1
{
TH0 = thr0;
TL0 = tlr0;
flag10ms = 1; //定时10ms
}
/* 定时器T1中断服务 */
void Timer1_ISP() interrupt 3
{
TH1 = thr1;
TL1 = tlr1; //定时1ms
KeyScan();
}
/**
***********************************************************************
* @file Lcd1602.c
* @author xr
* @date 2014年5月7日13:33:17
* @version V1.2.3
* @brief LCD1602液晶底层驱动
***********************************************************************
*/
#include
//LCD1602_IO
sbit LCD1602_RS = P1^0;
sbit LCD1602_RW = P1^1;
sbit LCD1602_EN = P1^5;
#define LCD1602_DB P0
/* 液晶忙碌等待 */
void LCD1602Wait()
{
unsigned char sta;
LCD1602_DB = 0xFF;//总线拉高,检测液晶状态字
LCD1602_RS = 0;
LCD1602_RW = 1;
do
{
LCD1602_EN = 1;
sta = LCD1602_DB;
LCD1602_EN = 0;//避免液晶输出数据
} while (sta & 0x80);//状态字最高位STA7 == 0空闲,1忙碌
}
/* 液晶写命令 */
void LCD1602WriteCmd(unsigned char cmd)
{
LCD1602Wait();
LCD1602_RS = 0;
LCD1602_RW = 0;
LCD1602_EN = 0;
LCD1602_DB = cmd;
LCD1602_EN = 1;
LCD1602_EN = 0;
}
/* 液晶写数据 */
void LCD1602WriteData(unsigned char dat)
{
LCD1602Wait();
LCD1602_RS = 1;
LCD1602_RW = 0;
LCD1602_EN = 0;
LCD1602_DB = dat;
LCD1602_EN = 1;
LCD1602_EN = 0;
}
/* 液晶初始化 */
void InitalLCD1602()
{
LCD1602WriteCmd(0x38);
LCD1602WriteCmd(0x0C);
LCD1602WriteCmd(0x06);
LCD1602WriteCmd(0x01);//清屏
}
/* 写数据到液晶上,字符串str,坐标(x, y),地址addr */
void LcdShowStr(unsigned char x, unsigned char y, unsigned char * str)
{
unsigned char addr;
if (y == 0)
{
addr = 0x00 + x;
}
else
{
addr = 0x40 + x;
}
LCD1602WriteCmd(addr | 0x80);
while (*str != '\0')
{
LCD1602WriteData(*str++);
}
}
/**
***********************************************************************
* @file keyboard.c
* @author xr
* @date 2014年5月8日22:11:33 -- 2014年5月9日12:03:49
* @version V1.2.3
* @brief 按键驱动 单片机STC89C52RC MCU 晶振 11.0592MHZ
***********************************************************************
*/
#include
/* 按键输出输入端口定义 */
sbit KEY_IN1 = P2^4;
sbit KEY_IN2 = P2^5;
sbit KEY_IN3 = P2^6;
sbit KEY_IN4 = P2^7;
sbit KEY_OUT1 = P2^3;
sbit KEY_OUT2 = P2^2;
sbit KEY_OUT3 = P2^1;
sbit KEY_OUT4 = P2^0;
extern unsigned char timer[9]; //分别表示跑表的各个位上的数字
/* 按键当前状态 */
unsigned char volatile keySta[4][4] = {{1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}};
/* 按键对应标准PC键盘编码 */
const unsigned char code keyCodeMap[4][4] = {
{'1', '2', '3', 0x26}, /* 数字键 1, 2, 3 和 向上键 */
{'4', '5', '6', 0x25}, /* 数字键 4, 5, 6 和 向左键 */
{'7', '8', '9', 0x28}, /* 数字键 7, 8, 9 和 向下键 */
{'0', 0x1B, 0x0D, 0x27} /* 数字键 0 和 向右键 */
};
bit stopflag = 0;//跑表走停标志位 0 停止,1运行
void KeyAction(unsigned char keycode);
void LcdShowStr(unsigned char x, unsigned char y, unsigned char * str);
/* 按键驱动函数 */
void KeyDriver()
{
/* 上一次按键的备份值 */
static unsigned char keybackup[4][4] = {{1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}};
unsigned char i, j;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
if (keySta[i][j] != keybackup[i][j]) //当前按键状态和上一次的按键状态不同
{ //按键有动作
if (keybackup[i][j] != 0) //上一次按键是弹起
{
KeyAction(keyCodeMap[i][j]); //当前按键是想、按下
}
keybackup[i][j] = keySta[i][j]; //备份当前按键值
}
}
}
}
/* 按键扫描函数 */
void KeyScan()
{
unsigned char i = 0;
static unsigned char keyout = 0;//按键行索引
static unsigned char keybuf[4][4] = {{0xFF, 0xFF, 0xFF, 0xFF}, {0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF}, {0xFF, 0xFF, 0xFF, 0xFF}
};
/* 按键消抖 */
keybuf[keyout][0] = (keybuf[keyout][0] << 1) | KEY_IN1;
keybuf[keyout][1] = (keybuf[keyout][1] << 1) | KEY_IN2;
keybuf[keyout][2] = (keybuf[keyout][2] << 1) | KEY_IN3;
keybuf[keyout][3] = (keybuf[keyout][3] << 1) | KEY_IN4;
/* 更新按键的值 */
for (i = 0; i < 4; i++)
{
if ((keybuf[keyout][i] & 0x1F) == 0x1F)
{
//五次检测按键的值都是1
keySta[keyout][i] = 1;
}
else if ((keybuf[keyout][i] & 0x1F) == 0x00)
{
//五次检测的按键值都是0
keySta[keyout][i] = 0;
}
}
/* 按键行索引++ */
keyout++;
keyout &= 0x03;//到4归零
/* 根据按键索引选择行按键进行扫描 */
switch (keyout)
{
case 0: KEY_OUT1 = 0; KEY_OUT4 = 1;//选择第一行按键
case 1: KEY_OUT2 = 0; KEY_OUT1 = 1;
case 2: KEY_OUT3 = 0; KEY_OUT2 = 1;
case 3: KEY_OUT4 = 0; KEY_OUT3 = 1;
default: break;
}
}
/* 按键动作函数 */
void KeyAction(unsigned char keycode)
{
unsigned char i = 0;
if (keycode == 0x1B) //ESC
{
/* 跑表复位 */
stopflag = 0;
for (i = 0; i < 9; i++)
{
timer[i] = 0;
}
LcdShowStr(2, 1, "0000000.00s");
LcdShowStr(10, 0, "reset!");
}
else if (keycode == 0x0D) //回车键 跑表走停
{
if (stopflag == 0)
{
stopflag = 1;
LcdShowStr(10, 0, "start!");
}
else
{
stopflag = 0;
LcdShowStr(10, 0, "stop! "); //多写入一个空格
}
}
}