# include "bsp.h"
# define S 5.0f
uint8_t JumpTime = 0; /* 外部中断0边沿跳变次数统计 */
uint8_t MeasureOK = 0; /* 测量完成标志 */
uint32_t Time = 0; /* 用于计算两次边沿跳变的时间 */
uint8_t str[20] = {0}; /* LCD显示缓存区 */
sbit key = P3^2; /* 外部中断引脚 */
int main(void)
{
bsp_Config(); /* 板级外设初始化 */
EX0 = 1; /* 开启外部中断0 */
bsp_lcd_ShowString(1, 0, "Speed Measuring");
while(1)
{
}
}
void EX0_IRQ(void) interrupt 0
{
float speed = 0.0f;
JumpTime++; /* 统计中断次数 */
while(key == 0); /* 等待按键被释放 */
if(JumpTime >= 2 && MeasureOK == 0) /* 按键已经按了两次,计算速度 */
{
JumpTime = 0; /* 清除跳变量 */
MeasureOK = 1; /* 已经完成测量了 */
speed = (float)((S /Time) * 1000.0);
Time = 0;
sprintf(str,"%f m/s", speed);
bsp_lcd_ShowString(0, 1, str);
}
}
#include "bsp.h"
void bsp_Config(void)
{
bsp_lcd_Config();
bsp_tim_Config();
}
void bsp_DelayMs(uint8_t ui_nMs)
{
uint8_t a = 0, b = 0;
for(; ui_nMs > 0; ui_nMs --)
{
for(a = 199; a > 0; a --)
{
for(b = 1; b > 0; b --);
}
}
}
# include "bsp.h"
# include "app.h"
# define TIMS (65536 - FOSC/1000)
void bsp_tim_Config(void)
{
TMOD = 0x01;
TL0 = TIMS;
TH0 = TIMS >> 8;
TR0 = 1;
ET0 = 1;
EA = 1;
}
static count = 0;
extern uint8_t JumpTime;
extern uint32_t Time;
void timer0_IRQ(void) interrupt 1 using 1
{
TL0 = TIMS; /* 需要重装初值 */
TH0 = TIMS >> 8;
if(JumpTime == 1) /* 只有在第一次按下后才开始计数,直到第二次按下 */
{
Time ++;
}
}
static void bsp_lcd_WriteCmd(uint8_t cmd)
{
LCD_EN = 0; /* 使能LCD1602 */
LCD_RD = 0; /* 选择发送命令 */
LCD_RW = 0; /* 选择写入 */
LCD_DATA = cmd; /* 写入命令 */
bsp_DelayMs(5); /* 等待数据稳定延时5ms */
LCD_EN = 1; /* 写入时序 */
bsp_DelayMs(10);
LCD_EN = 0;
}
static void bsp_lcd_WriteData(uint8_t dat)
{
LCD_EN = 0;
LCD_RD = 1;
LCD_RW = 0;
LCD_DATA = dat; //将数据放到LCD里
bsp_DelayMs(1);
LCD_EN = 1;
bsp_DelayMs(5);
LCD_EN = 0;
}
void bsp_lcd_Config(void)
{
bsp_lcd_WriteCmd(0x38); /* 开显示 */
bsp_lcd_WriteCmd(0x0c); /* 不显示光标 */
bsp_lcd_WriteCmd(0x06); /* 写一个指针加1 */
bsp_lcd_WriteCmd(0x01); /* 清屏 */
bsp_lcd_WriteCmd(0x80); /* 指针复位 */
}
static void bsp_lcd_SetPos(uint8_t x, uint8_t y)
{
/* 坐标超出范围了 */
if(x > 15) return ;
if(y > 1) return ;
if(y == 0) bsp_lcd_WriteCmd(x + 0x80);
else if(y == 1) bsp_lcd_WriteCmd(x + 0xc0);
}
void bsp_lcd_ShowNum(uint8_t x, uint8_t y, uint32_t num, uint8_t xbit)
{
uint8_t temp = 0;
while(xbit)
{
temp = num % 10; /* 取得要显示的个位 */
/* 从最后一位开始显示 */
bsp_lcd_SetPos(x + xbit - 1, y);
bsp_lcd_WriteData(temp + 0x30);
num/=10; /* 已经显示完个位了,把个位扔掉 */
xbit --; /* 由于是从后往前开始显示,所以x坐标轴要减小 */
}
}
void bsp_lcd_ShowChar(uint8_t x, uint8_t y, uint8_t c)
{
bsp_lcd_SetPos(x, y);
bsp_lcd_WriteData(c);
}
void bsp_lcd_ShowString(uint8_t x, uint8_t y, uint8_t *string)
{
uint8_t i = 0;
while(*(string + i)) /* 在字符串没有结束就一直显示 */
{
bsp_lcd_ShowChar(x + i, y, *(string + i));
i++;
}
}
|