📄 1602_main.c
字号:
#include <iom16v.h>
#include "1602.h"
//void delay_ms(unsigned int us);
//void delay_us(unsigned int uu);
unsigned char key_read(void);
unsigned char get_key(void);
unsigned int get_ad(void);
unsigned char lcd_process(void);
int key,disp_num1;
float disp_num2;
/*
void delay_us(unsigned int uu) //90us
{unsigned int ii;
for(ii=0;ii<uu;ii++)
;
}
void delay_ms(unsigned int us) //1ms
{unsigned int ii;
for(ii=0;ii<us;ii++)
delay_us(100);
}
*/
/*
键盘扫描函数
读取当前键盘的状态
有按键按下返回相应按键值
无按键按下返回"0x00"
*/
unsigned char key_read(void){
unsigned char k;
//非常经典的键盘控制例子
DDRD = 0x00;/*获取列地址*/
PORTD = 0x0F;
DDRD = 0xF0;
delay_ms(10);
k = PIND;
DDRD = 0x00;/*获取行地址*/
PORTD = 0xF0;
DDRD = 0x0F;
delay_ms(10);
k |= PIND;
DDRD = 0x00;/*输出复位*/
PORTD = 0xFF;
switch (k) {/*将按键码转换成键值*/
// case 0x00: return 0x00;
case 0xEE: return 1;
case 0xDE: return 2;
case 0xBE: return 3;
case 0x7E: return 4;
case 0xED: return 5;
case 0xDD: return 6;
case 0xBD: return 7;
case 0x7D: return 8;
case 0xEB: return 9;
case 0xDB: return 44;
case 0xBB: return 55;
case 0x7B: return 66;
case 0xE7: return 77;
case 0xD7: return 88;
case 0xB7: return 99;
case 0x77: return 100;
default : return 0x00;
}
}
/*
按键获取函数
获取按键信号,其中包含有状态记录及按键去颤抖。
有正确按键按下返回相应按键值
无正确按键按下返回"0x00"
*/
unsigned char get_key(void){
unsigned char i;
static unsigned char j;/*按键记录*/
i = key_read();
if (i == 0x00){/*无有效按键按下*/
j = 0x00;/*清除按键记录*/
return 0x00;/*程序退出*/
}
if (j == 0x00) {/*为新按键*/
j = i;/*保存本次采样结果*/
delay_ms(10);/*去按键颤抖*/
i = key_read();
if(i == j) {
return i;
}
}
return 0x00;
}
/*ADC采样函数,采样第7通道信号,采样分辨率256*/
unsigned int get_ad(void) { //3.69KHZ
unsigned char hh,ll;
unsigned int num=0;
// _CLI();
asm("cli");
DDRA = 0x00; /*方向输入*/
PORTA = 0x7F; /*打开上拉*/
// delay_us(1); //10us
ADMUX = 0x47; /*基准AVCC、10位、通道7*/
ADCSRA = 0xC0; /*使能、开启、4分频*/
while(!(ADCSRA & (1 << ADIF))); /*等待*/
ll=ADCL;
num = ADCH;
num=num*256+ll;
ADCSRA &= ~(1 << ADIF); /*清标志*/
ADCSRA &= ~(1 << ADEN); /*关闭转换*/
DDRA=0XFF;
PORTA=0X00;
//SEI();
asm("sei");
return num;
}
unsigned char lcd_process(void)
{
if( disp_num2<=3.3&&disp_num2>=1)
{
LCD_write_char(1,0,'U');
LCD_write_char(2,0,'=');
LCD_write_num(3,0,(disp_num2*100)/100);
LCD_write_char(4,0,'.');
LCD_write_num(5,0,(int)(disp_num2*10)%10);
LCD_write_num(6,0,(int)(disp_num2*100)%10);
LCD_write_char(7,0,'V');
}
else if( disp_num2<1&&disp_num2>=0.1)
{
LCD_write_char(1,0,'U');
LCD_write_char(2,0,'=');
LCD_write_num(3,0,0);
LCD_write_char(4,0,'.');
LCD_write_num(5,0,(disp_num2*100)/10);
LCD_write_num(6,0,(int)(disp_num2*100)%10);
LCD_write_char(7,0,'V');
}
else if( disp_num2<0.1)
{
LCD_write_char(1,0,'U');
LCD_write_char(2,0,'=');
LCD_write_num(3,0,0);
LCD_write_char(4,0,'.');
LCD_write_num(5,0,0);
LCD_write_num(6,0,(disp_num2*100)/10);
LCD_write_char(7,0,'V');
}
}
void main(void) {
unsigned char i;
unsigned char *p;
int time_s;
PORTB = 0xFF; /*电平设置*/
DDRB = 0xFF; /*方向输出*/
PORTA = 0xFF;
DDRA = 0xFF;
delay_ms(100);
LCD_init();
time_s=0;
delay_ms(500);
LCD_clear();
while (1) {
key=get_key();
switch(key)
{
case 1:
time_s=1;
LCD_clear();
LCD_write_str(6,6,"start...");
delay_ms(500);
LCD_clear();
disp_num1=get_ad();
disp_num2=disp_num1*0.00322;
lcd_process();
break;
case 2:
time_s=2;
LCD_clear();
LCD_write_str(4,4,"over...");
delay_ms(500);
LCD_clear();
break;
case 3:
time_s=3;
break;
case 4:
time_s=4;
break;
case 5:
time_s=5;
break;
case 6:
time_s=6;
break;
case 7:
time_s=7;
break;
case 8:
time_s=8;
break;
case 9:
time_s=9;
break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -