最近在学习LCD1602,8线驱动。利用按键扫描改变数值,可是发现行不通。LCD值显示初始值,不能更新数据。搞了好长时间,也没有搞定,希望大侠出手相助。
define uchar unsigned char
#define uint unsigned int
#define RS 6
#define RW 7
#define EN 7
void s_ms(uint ms)
{
for(;ms>1;ms--);
}
//查忙
void busy(void)
{
uchar temp;
//temp=0X00;
s_ms(500);
PORTC&=~(1<<RS); //RS=0
s_ms(500);
PORTC|=(1<<RW); //RW=1
s_ms(500);
while(temp)
{
PORTB|=(1<<EN); //EN=1
s_ms(500);
DDRD=0x00; //A口变输入
PORTD=0xff; //上拉使能
s_ms(500);
temp = PIND&0x80; //读取D口
s_ms(500);
DDRD=0xff;
PORTD=0xff; //A口变输出
s_ms(500);
PORTB&=~(1<<EN); //EN=0
s_ms(500);
}
}
//写指令
void writecom(uchar com)
{
busy();
s_ms(1000);
PORTC&=~(1<<RS); //RS=0
s_ms(500);
PORTC&=~(1<<RW); //RW=0
s_ms(500);
PORTB|=(1<<EN); //EN=1
s_ms(500);
PORTD = com; //输出指令
s_ms(500);
PORTB&=~(1<<EN); //EN=0
s_ms(500);
}
//1602初始化
void LcdInit(void)
{
writecom(0x38);
s_ms(1000);
writecom(0x01);
s_ms(10000);
s_ms(1000);
s_ms(1000);
s_ms(1000);
s_ms(1000);
s_ms(1000);
s_ms(1000);
writecom(0x02);
s_ms(1000);
writecom(0x06);
s_ms(1000);
writecom(0x0c);
s_ms(1000);
writecom(0x38);
s_ms(1000);
}
//写数据
void writedata(uchar data)
{
busy();
s_ms(1000);
PORTC|=(1<<RS); //RS=1
s_ms(500);
PORTC&=~(1<<RW); //RW=0
s_ms(500);
PORTB|=(1<<EN); //EN=1
s_ms(500);
PORTA = data; //输出数据
s_ms(500);
PORTB&=~(1<<EN); //EN=0
s_ms(500);
}
void WriteChar(uchar row,uchar col,uint num,uchar *pBuffer)
{
uchar i,t;
if (row == 1) row = 0x80 + col;
else row = 0xC0 + col;
writecom(row);
PORTC|=(1<<RS);
s_ms(50);
PORTC&=~(1<<RW);
s_ms(50);
for(i=num;i!=0;i--)
{
t = *pBuffer;
s_ms(50);
PORTD = t;
s_ms(50);
PORTB|=(1<<EN);
s_ms(50);
PORTB&=~(1<<EN);
s_ms(50);
pBuffer++;
}
}
void WriteNum(uchar row,uchar col,uchar num)
{
if (row == 1) row = 0x80 + col;
else row = 0xC0 + col;
writecom(row);
PORTC|=(1<<RS);
s_ms(50);
PORTC&=~(1<<RW);
s_ms(50);
PORTD = num;
s_ms(50);
PORTB|=(1<<EN);
s_ms(50);
PORTB&=~(1<<EN);
s_ms(50);
}
#include<iom16v.h>
#include <macros.h>
#include"1602.h"
#define uchar unsigned char
#define uint unsigned int
uchar fun=1,n=0,b=0,d=0,TL,TH;
uint m=0,t1_out,t1,volt_out;
//float fr,vo;
uint fr,vo;
uchar adc_0[]={"ADC_3: . V"};
uchar adc_1[]={"ADC_4: . HZ"};
#pragma data:code
//const table[]={0,0.5,1,1.5,2.0,2.5,3.0,3.5,4.0,4.5,5.5};
//const time[]={0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0};
const table[]={0,5,10,15,20,25,30,35,40,45,55};
const time[]={1,2,3,4,5,6,7,8,9,10};
void key3(void);//波形按键选择键
void key4(void);//LCD开闭
void key5(void);//增加频率
void key6(void);//减少频率
void key7(void);//增加电压
void key8(void);//减少电压
void delay(uint ms)//延时程序
{
uint i,j;
for(i=0;i<ms;i++)
{
for(j=0;j<1141;j++);
}
}
uchar key_press()
{
uchar j;
DDRB|=0X7D;//有按键的管脚配置为输出
PORTB|=0X7D;//有按键的管脚配置为高电平
//DDRB&=(~BIT(6))&(~BIT(5));
DDRB&=0X7D;
j=PINB;
j=j&0X7D;
if(j==0X7D)
{
return 0;
}
else
{
return 1;
}
}
void key_scan()
{
uchar key;
delay(10);
if(key_press)
{
key=PINB;
key&=0X7D;
switch(key)
{
case 0X7C:
key3();
//DDRC=0XFF;
//PORTC=BIT(0);
break;
case 0X79:
key4();
// DDRC=0XFF;
//PORTC=BIT(1);
break;
case 0X75:
key5();
break;
case 0X6D:
key6();
break;
case 0X5D:
key7();
break;
case 0X3D:
key8();
break;
default:
//DDRC=0XFF;
// PORTC=BIT(2);
break;
}
while(key_press());
}
}
void main(void)
{
uint i,k,volt_1,volt_2,fre_1,fre_2;//temp_v3,temp_f3,j;
// float temp_v1,temp_v2,temp_f1,temp_f2;
fr=1;
vo=30;
DDRD=0xFF;
DDRC|=BIT(6)|BIT(7);
DDRB|=BIT(7);
LcdInit();
WriteChar(1,2,10,adc_0);
WriteChar(2,2,11,adc_1);
while(1)
{
//temp_v1=vo+0.00001;
//temp_v2=temp_v1*10;
//temp_v3=(uint)temp_v2;
//volt_1=temp_v3/10;
//volt_2=temp_v3%10;
volt_1=vo/10;
volt_2=vo%10;
volt_1+=0X30;
volt_2+=0X30;
//temp_f1=fr+0.00001;
//temp_f2= temp_f1*10;
//temp_f3=(uint)temp_f2;
//fre_1=temp_f3/10;
//fre_2=temp_f3%10;
fre_1=fr/10;
fre_2=fr%10;
fre_1+=0X30;
fre_2+=0X30;
WriteNum(1,8,(volt_1));
WriteNum(1,10,(volt_2));
WriteNum(2,8,(fre_1));
WriteNum(2,10,(fre_2));
i=key_press();
if(i)
{
key_scan();
}
// writecom(0x01);
}
}
void key3(void)//1键选择发波类型,1为正弦波,2为方波
{
fun++;
if(fun==3)
{
fun=1;
}
}
void key4(void) //4键显示频率
{
DDRD^=0XFF;
}
void key5(void)//2键加大频率,减小周期
{
if(fr>=0.0&&fr<=0.9)
{
//t1_out=t1_out-1;
m++;
// t1_out=time[m];
fr=time[m];
}
}
void key6(void) //3键减小频率,增大周期
{
if(fr>=0.1&&fr<=1.0)
{
//t1_out=t1_out+1;
m--;
// t1_out=time[m];
fr=time[m];
}
}
void key7(void)//增加电压
{
if(vo>=0&&vo<=4.5)
{
n++;
// volt_out=table[n];
vo=table[n];
}
}
void key8(void)//减少电压
{
if(vo>=0.5&&vo<=5)
{
n--;
// volt_out=table[n];
vo=table[n];
}
} |