怎样才能实现数据加减,要有上下限,超过之后蜂鸣器会报警,各位前辈帮我看看要怎么改。
用OLED显示两个屏,第一个屏显示个人信息,第二个屏显示电机状态、速度和温度
#include <reg51.h>
#include "delay.h"
#include "oled12864.h"
#include "key.h"
#include "motor.h"
void screen1();
void screen2();
void key();
void motor();
void display();
void alarm();
unsigned char flag=0,speed=50,num=10,banji=1901,key_mode=0,wendu=30,key_num=0;
unsigned char code ch1[]={0,1,'#'};//名字
unsigned char code ch2[]={2,3,4,'#'};//
unsigned char code ch3[]={5,6,'#'};//班级
unsigned char code ch4[]={7,8,'#'};//学号
sbit bee=P3^6;
void main()
{
OLED_Init();
DC_Motor_Init();
// OLED_Clear();//显示清屏
while(1)
{
display();
// OLED_Clear();
key();
motor();
}
}
void display()
{
screen1();
screen2();
}
void screen1()
{
OLED_Show_stringChinese(0,0,ch1);//ch1姓名
OLED_Show_Char(0,32,':');
OLED_Show_stringChinese(0,37,ch2);//ch2
OLED_Show_stringChinese(2,0,ch4);//ch6学号
OLED_Show_Char(2,32,':');
OLED_Show_Num(2,36,num,4);
}
void screen2()
{
OLED_Show_String(0,0,"status:");
OLED_Show_String(2,0,"speed:");
OLED_Show_String(4,0,"wendu:");
OLED_Show_Num(2,8*8,speed,2);
OLED_Show_Num(4,8*8,wendu,2);
motor();
}
void key()
{
if(key1_scan()!=0)//key1_scan切换
{
key_mode++;
key_mode%=2;
// key_num=0;
OLED_Clear();
}
switch(key2_scan())
{
case 0:break;
case 1:OLED_Show_String(0,7*8,"runing ");break;
case 2:OLED_Show_String(0,7*8,"stop");break;
}
switch(key3_scan())//按键
{
case 0:break;
case 1:switch(key_mode)
{
case 0:switch(key_num)
{
case 0:break;
case 1:speed+=1;break;
case 2:speed+=10;break;
}break;
case 1:switch(key_num)
{
case 0:break;
case 1:wendu+=1;break;
case 2:wendu+=10;break;
}break;
}break;
}
switch(key4_scan())//按键
{
case 0:break;
case 1:switch(key_mode)
{
case 0:switch(key_num)
{
case 0:break;
case 1:speed-=1;break;
case 2:speed-=10;break;
}break;
case 1:switch(key_num)
{
case 0:break;
case 1:wendu-=1;break;
case 2:wendu-=10;break;
}break;
}break;
}
}
void motor()
{
// flag=0,1,2;
switch(flag)
{
case 0:ENA=0;TR0=0;break;
case 1:TR0=1;DC_Motor_Run(1,speed);break;
}
}
void alarm()
{
if(wendu>50)
bee=1;
else if(wendu<20)
bee=1;
else bee=0;
}
|