📄 volt.c
字号:
#include "reg52.h"
//#include "Port_define.h"
#include"DRIVER_1602.h"
#define uchar unsigned char
#define uint unsigned int
#define KT 30 //按键消抖参数
sbit K1U=P0^5;
sbit K1D=P0^4;
sbit K2U=P0^3;
sbit K2D=P0^2;
sbit K3U=P0^0;
sbit K3D=P0^1;
sbit cs=P1^7;
sbit Data=P1^4;
sbit clk=P1^3;
sbit clr=P1^6;
sbit ld=P1^5;
uchar key1=0,key2=0,key3=0,temp=0;
uint volt;
/**************************DAC7611驱动*********************************************/
void DAC7611(uint x)
{ uchar i;
cs=0;
for(i=0;i<=11;i++)
{clk=0;
if((x&0x800)==0) Data=0;
else Data=1;
x<<=1;
clk=1;}
cs=1; }
void out(uint x)
{
ld=0;
clr=1;
DAC7611(x);
}
/***********************************************************************/
/***********************************按键扫描**************************************/
void key(void)
{uint voltemp;
voltemp=volt;
key1=(voltemp%10000)/1000;
key2=(voltemp%1000)/100;
key3=(voltemp%100)/10;
if(K1U==0)
{temp++;
if(temp>KT)
{temp=0;
key1++;}
}
if(K1D==0)
{temp++;
if(temp>KT)
{temp=0;
key1--;}
}
if(K2U==0)
{temp++;
if(temp>KT)
{temp=0;
key2++;}
}
if(K2D==0)
{temp++;
if(temp>KT)
{temp=0;
key2--;
if(key2>9)key2=0;}
}
if(K3U==0)
{temp++;
if(temp>KT)
{temp=0;
key3++;}
}
if(K3D==0)
{temp++;
if(temp>KT)
{temp=0;
key3--;
if(key3>9)key3=0;}
}
volt=key1*1000+key2*100+key3*10;
if(volt>=4095){volt=0;}
//if(volt>4095){volt=0;}
}
/*********************************************************************************/
/*****************************LCD应用层驱动*****************************************/
void disp(unsigned int x)
{
LCD_set_xy(5,0);
LCD_en_dat((x%10000)/1000+48);
LCD_en_dat('.');
LCD_en_dat((x%1000)/100+48);
LCD_en_dat((x%100)/10+48);
LCD_en_dat(x%10+48);
}
void disp1(uchar x)
{
LCD_set_xy(0,1);
LCD_en_dat((x%1000)/100+48);
LCD_en_dat((x%100)/10+48);
LCD_en_dat(x%10+48);
}
void disp2(uchar x)
{
LCD_set_xy(4,1);
LCD_en_dat((x%1000)/100+48);
LCD_en_dat((x%100)/10+48);
LCD_en_dat(x%10+48);
}
void disp3(uchar x)
{
LCD_set_xy(9,1);
LCD_en_dat((x%1000)/100+48);
LCD_en_dat((x%100)/10+48);
LCD_en_dat(x%10+48);
}
/*********************************************************************************/
void main(void)
{ uint dvolt,voltreal;
P0=0xFF;
volt=0;
LCD_init();
ld=0;
clr=1;
while(1)
{dvolt=voltreal;
key();
/*
LCD_cls();//清屏
delay_nms(2);
LCD_write_string(0,0,"1602 test");
LCD_write_string(5,1,"awakening");
delay_nms(200);
LCD_cls();
delay_nms(2);
LCD_write_string(0,0,"test 1602");
LCD_write_string(0,1,"1234567890");
LCD_write_char(0,1,'a');
delay_nms(200);
*/
LCD_write_string(0,0,"Volt:");
LCD_write_string(11,0,"V");
disp(volt);
disp1(key1);
disp2(key2);
disp3(key3);
// LCD_write_string(0,1,1234);
/*****************精度校正************************/
if((volt>0)&&(volt<=816))
{voltreal=volt-1;}
if((volt>816)&&(volt<=2000))
{voltreal=volt-2;}
if((volt>2000)&&(volt<=3200))
{voltreal=volt-3;}
if((volt>3200)&&(volt<=3900))
{voltreal=volt-4;}
if((volt>3900)&&(volt<=4095))
{voltreal=volt-5;}
/************************************************/
if(dvolt!=voltreal)
{DAC7611(voltreal);
dvolt=voltreal;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -