#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uint f,speed;//初始定义测速的F为记录的脉冲,speed为速度,
time为定时次数,
uchar time;
//1602定义//
unsigned char code digit[10]={"0123456789"}; //定义字符数组显示数字
sbit RS = P1^0; //定义端口
sbit RW = P1^1;
sbit EN = P2^5;
sbit DU = P2^0;
sbit WE = P2^1;
#define RS_CLR RS=0
#define RS_SET RS=1
#define RW_CLR RW=0
#define RW_SET RW=1
#define EN_CLR EN=0
#define EN_SET EN=1
#define DataPort P0
//计时器0//
void timer0 ()
{
TMOD=0x51;//设置定时器0.方式1
TH0=(65536-5000)/256;//定时50ms
TL0=(65536-5000)%256;
EA=1;
ET0=1;
TR0=1;
}
//计数器1,用来记录光电测速的脉冲//
//定时器0中断分别执行调速和测速//
void time0 (void) interrupt 1
{
TH0=(65536-5000)/256;//定时1ms
TL0=(65536-5000)%256;
//测速//
time=time++;
}
//显示模块//
void cmg88()//关数码管,点阵函数
{
DU=1;
P0=0X00;
DU=0;
}
/*------------------------------------------------
uS延时函数,含有输入参数 unsigned char t,无返回值
unsigned char 是定义无符号字符变量,其值的范围是
0~255 这里使用晶振12M,精确延时请使用汇编,大致延时
长度如下 T=tx2+5 uS
------------------------------------------------*/
void DelayUs2x(unsigned char t)
{
while(--t);
}
/*------------------------------------------------
mS延时函数,含有输入参数 unsigned char t,无返回值
unsigned char 是定义无符号字符变量,其值的范围是
0~255 这里使用晶振12M,精确延时请使用汇编
------------------------------------------------*/
void DelayMs(unsigned char t)
{
while(t--)
{
//大致延时1mS
DelayUs2x(245);
DelayUs2x(245);
}
}
/*------------------------------------------------
判忙函数
------------------------------------------------*/
bit LCD_Check_Busy(void)
{
DataPort= 0xFF;
RS_CLR;
RW_SET;
EN_CLR;
_nop_();
EN_SET;
return (bit)(DataPort & 0x80);
}
/*------------------------------------------------
写入命令函数
------------------------------------------------*/
void LCD_Write_Com(unsigned char com)
{
while(LCD_Check_Busy()); //忙则等待
RS_CLR;
RW_CLR;
EN_SET;
DataPort= com;
_nop_();
EN_CLR;
}
/*------------------------------------------------
写入数据函数
------------------------------------------------*/
void LCD_Write_Data(unsigned char Data)
{
while(LCD_Check_Busy()); //忙则等待
RS_SET;
RW_CLR;
EN_SET;
DataPort= Data;
_nop_();
EN_CLR;
}
/*------------------------------------------------
清屏函数
------------------------------------------------*/
void LCD_Clear(void)
{
LCD_Write_Com(0x01);
DelayMs(5);
}
/*------------------------------------------------
写入字符串函数
------------------------------------------------*/
void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s)
{
if (y == 0)
{
LCD_Write_Com(0x80 + x); //表示第一行
}
else
{
LCD_Write_Com(0xC0 + x); //表示第二行
}
while (*s)
{
LCD_Write_Data( *s);
s ++;
}
}
/*------------------------------------------------
写入字符函数
------------------------------------------------*/
void LCD_Write_Char(unsigned char x,unsigned char y,unsigned char Data)
{
if (y == 0)
{
LCD_Write_Com(0x80 + x);
}
else
{
LCD_Write_Com(0xC0 + x);
}
LCD_Write_Data( Data);
}
/*------------------------------------------------
初始化函数
------------------------------------------------*/
void LCD_Init(void)
{
LCD_Write_Com(0x38); /*显示模式设置*/
DelayMs(5);
LCD_Write_Com(0x38);
DelayMs(5);
LCD_Write_Com(0x38);
DelayMs(5);
LCD_Write_Com(0x38);
LCD_Write_Com(0x08); /*显示关闭*/
LCD_Write_Com(0x01); /*显示清屏*/
LCD_Write_Com(0x06); /*显示光标移动设置*/
DelayMs(5);
LCD_Write_Com(0x0C); /*显示开及光标设置*/
}
//将速度数据送到显示屏//
void dectobit(uint dec)
{
unsigned char ge,shi,bai,qian;
ge=dec%10;
shi=dec%100/10;
bai=dec%1000/100;
qian=dec/1000;
LCD_Write_Char(4,0,digit[ge]);
LCD_Write_Char(3,0,digit[shi]);
LCD_Write_Char(2,0,digit[bai]);
LCD_Write_Char(1,0,digit[qian]);
LCD_Write_String(1,1,"zhuan su");
}
//主函数//
void main()
{
cmg88();//关数码管,点阵函数
LCD_Init();
LCD_Clear();//清屏
timer0();
TH1=0x00;
TL1=0x00;
ET1=1;
TR1=1;
while(1)
{
if(time==20)//判断时间是否到1S了,50ms*20=1S
{
TR0=0;
TR1=0;//到了时间,停止计数器1
time=0;//清零
f=TH1;
f=f<<8;
f=f+TL1;
dectobit(f);
TH1=0x00;
TL0=0x00;
TR0=1;
TR1=1;
}
}
}
程序是这个,但是我试了,电机匀速时,它只能不断累积脉冲数,就是一直增加,直到溢出重新累加,显示就如0000,0030,0060,....就是不能显示出速度,而且当电机停止后,显示不清零的
一周热门 更多>