代作LED指示灯编程、讲解LED、辅导C报告
- 首页 >> C/C++编程#include <reg52.h>
#include <intrins.h>
#include <string.h>
#include "1602.h"
#include "delay.h"
sbit Key1 = P3^5;
sbit Key2 = P3^3;
sbit Key3 = P1^3;
sbit Led = P3^6; //LED指示灯
sbit Speak = P2^2;
unsigned int count;
unsigned char Flag;
unsigned char full;
int Angle_up=90; //??????
int Angle_down=20; //??????
int max,min;
unsigned int mb=45,counter,n;
unsigned int Oldcount,Newcount; //上一次脉冲的时间/当前脉冲的时间
unsigned char Sycount; //输液完时间计数
void KeyScan(void) //按键扫描
{
unsigned char temp[3];//定义显示区域临时存储数组
if(Key1 == 0) //调角度状态
{
DelayMs(10); //消抖
if(Key1 == 0)
{
Flag=1;
count++;
if(count==1)
{
LCD_Clear();
LCD_Write_Com(0x0c); //显示界面
temp[0]=Angle_up/100+'0';
temp[1]=Angle_up%100/10+'0';
temp[2]=Angle_up%10+'0';
LCD_Write_String(0,0,"Value_up :");
LCD_Write_String(13,0,temp);
temp[0]=Angle_down/100+'0';
temp[1]=Angle_down%100/10+'0';
temp[2]=Angle_down%10+'0';
LCD_Write_String(0,1,"Value_down: ");
LCD_Write_String(13,1,temp);
}
if(count>2)
{
LCD_Write_Com(0x0c);//关闭光标
count=0;
Flag=0;
LCD_Clear();
LCD_Write_String(0,0,"Speed: d/min");
LCD_Write_String(0,1,"Level: ");
}
}
while(Key1 == 0);//等待键松开
}
}
void Set_angle(unsigned char sel)
{
unsigned char temp[3];
max=360;
min=0;
if(sel==2) {max=200;min=0;LCD_cursor(15,1);} //angle_down
if(sel==1) {max=200;min=0;LCD_cursor(15,0);} //angle_up
if(Key3 == 0)
{
DelayMs(10);
if(Key3 == 0)
{
if(sel==1) //上限度数 加
{
Angle_up++;
if(Angle_up>max) Angle_up=min;//查看数值有效范围
if(Angle_up<min) Angle_up=max;
LCD_Write_Com(0x0c); //光标关闭,不关闭后边显示相应会显示光标
temp[0]=Angle_up/100+'0';
temp[1]=Angle_up%100/10+'0';
temp[2]=Angle_up%10+'0';
LCD_Write_String(13,0,temp);
}
if(sel==2) //下限角度 加
{
Angle_down++;
if(Angle_down>max) Angle_down=min;//查看数值有效范围
if(Angle_down<min) Angle_down=max;
LCD_Write_Com(0x0c); //光标关闭,不关闭后边显示相应会显示光标
temp[0]=Angle_down/100+'0';
temp[1]=Angle_down%100/10+'0';
temp[2]=Angle_down%10+'0';
LCD_Write_String(13,1,temp);
}
}
while(Key3 == 0);//等待键松开
}
if(Key2 == 0)
{
DelayMs(10);
if(Key2 == 0)
{
if(sel==1) //上限度数 减
{
Angle_up--;
if(Angle_up>max) Angle_up=min;//查看数值有效范围
if(Angle_up<min) Angle_up=max;
LCD_Write_Com(0x0c); //光标关闭,不关闭后边显示相应会显示光标
temp[0]=Angle_up/100+'0';
temp[1]=Angle_up%100/10+'0';
temp[2]=Angle_up%10+'0';
LCD_Write_String(13,0,temp);
}
if(sel==2) //下限角度减
{
Angle_down--;
if(Angle_down>max) Angle_down=min;//查看数值有效范围
if(Angle_down<min) Angle_down=max;
LCD_Write_Com(0x0c); //光标关闭,不关闭后边显示相应会显示光标
temp[0]=Angle_down/100+'0';
temp[1]=Angle_down%100/10+'0';
temp[2]=Angle_down%10+'0';
LCD_Write_String(13,1,temp);
}
}
while(Key2 == 0);//等待键松开
}
}
void Init(void)
{
//定时器0初始化
TMOD |= 0x01; //定时器T0工作于方式1
TH0 = 0x4C;
TL0 = 0x00; //T0定时时间为50ms
TR0 = 1; //开定时器T0
ET0 = 1; //关闭定时中断
//外部中断0初始化
IT0 = 0; //外部中断0为边沿触发方式
EX0 = 1;//使能外部中断
EA = 1; //开总中断
}
void main(void)
{
LCD_Init();
LCD_Clear();
Init();
LCD_Write_String(0,0,"Speed: d/min");
while(1)
{
KeyScan();
if(Flag==0)
{
//输液速度显示
LCD_Write_Char(7,0,mb/100+0x30);
LCD_Write_Char(8,0,mb%100/10+0x30);
LCD_Write_Char(9,0,mb%10+0x30);
//液位显示
LCD_Write_Char(14,1,full+0x30);
if((mb>Angle_down&&mb<Angle_up)&&full==0)
{
Speak=1;
}
else
{
Speak=0;
}
}
else
{
Speak=1;
Set_angle(count);
}
}
}
//**************************************************
//滴速:通过计数30s中脉冲个数*2换算到一分钟的滴数
void External0() interrupt 0//外部中断0程序
{
counter++;
}
//***************************************************
//定时中断服务程序
//用于数码管定时显示和30s计时
void Timer0() interrupt 1
{
TH0 = 0x4C;
TL0 = 0x00;
n++;
if(n==600) //30s
{
mb=2*counter; //换算到一分钟的速度
n=0; //参数清理,重新检测
counter=0;
}
}
#include "1602.h"
#include "delay.h"
sbit RS = P2^7; //定义端口
sbit RW = P2^6;
sbit EN = P2^5;
#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
/*短一点的延时*/
void delay1(int i)
{
int j,k;
for(j=0;j<i;j++)
for(k=0;k<10;k++);
}
/*------------------------------------------------
判忙函数
------------------------------------------------*/
void LCD_Check_Busy(void)
{
unsigned char temp=0;
DataPort= 0xFF;
RS_CLR;
RW_SET;
if(P0^7==1)
{
EN_SET;
EN_CLR;
}
}
/*------------------------------------------------
写入命令函数
------------------------------------------------*/
void LCD_Write_Com(unsigned char com)
{
LCD_Check_Busy();
RS_CLR;
delay1(10);
RW_CLR;
delay1(10);
DataPort= com;
delay1(10);
EN_SET;
delay1(10);
EN_CLR;
}
/*------------------------------------------------
写入数据函数
------------------------------------------------*/
void LCD_Write_Data(unsigned char Data)
{
LCD_Check_Busy();
RS_SET;
delay1(10);
RW_CLR;
delay1(10);
DataPort= Data;
EN_SET;
delay1(10);
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 LCD_cursor(unsigned char X,unsigned char Y)
{
if (Y == 0)
{
LCD_Write_Com(0x80 + X);
}
else
{
LCD_Write_Com(0xC0 + X);
}
LCD_Write_Com(0x0e);
}