在
proteus中用
ARM7LPC2131
仿真时,用LCD显示好好地,可只要将矩阵键盘接入ARM7的P0.16到P0.22端口时,LCD的显示就会受到影响这是为什么?下面是我写的代码
#include"config.h"
#define BUSY (1<<7)
#define RS (1<<8)
#define RW (1<<9)
#define E (1<<10)
unsigned int k;
uint8 str[] = {"Enter password:"};
void delay1ms(void)
{
unsigned int i,j;
for(i=0;i<4;i++)
for(j=0;j<38;j++);
}
void delaynms(unsigned int n)
{
unsigned int i;
for(i=0;i<n;i++)
delay1ms();
}
//LCD查忙
unsigned int lcd_busy(void)
{
unsigned int busy;
IO0DIR = 0XFFF;
IO0CLR = RS;
IO0SET = RW;
IO0SET = E;
for(k=1;k>0;k--);
busy = (IO0PIN & (1<<7));
IO0CLR = E;
return busy;
}
//LCD模式设置
void lcd_modset(unsigned char mod)
{
while(lcd_busy() != 0);
IO0CLR = RS;
IO0CLR = RW;
IO0CLR = E;
for(k=1;k>0;k--);
IO0CLR = 0XFF;
IO0SET = mod;
for(k=1;k>0;k--);
IO0SET = E;
for(k=1;k>0;k--);
IO0CLR = E;
}
//指定字符显示的实际地址
void lcd_waddr(unsigned char addr)
{
lcd_modset(addr|0x80);
}
//将数据字符的标准ASCII码写入液晶模块
void lcd_wdata(unsigned char data)
{
while(lcd_busy() != 0);
IO0SET = RS;
IO0CLR = RW;
IO0CLR = E;
IO0CLR = 0XFF;
IO0SET = data;
for(k=1;k>0;k--);
IO0SET = E;
for(k=1;k>0;k--)
IO0CLR = E;
}
//将字符串写入液晶模块
void lcd_wstr(uint8 *str)
{
unsigned int i=0;
for(;i<15;i++)
{
lcd_wdata(str[i]);
delaynms(50);
}
}
//LCD初始化
void lcd_init(void)
{
delaynms(15);
lcd_modset(0x38);
delaynms(10);
lcd_modset(0x38);
delaynms(10);
lcd_modset(0x38);
delaynms(10);
lcd_modset(0x0f);
delaynms(10);
lcd_modset(0x06);
delaynms(10);
lcd_modset(0x01);
delaynms(10);
}
//KEYPAD_PHONE键盘扫描函数
void key_s(void)
{
IO0CLR = 0X7<<16;
IO0DIR = IO0DIR | 0X7F<<16;
IO0SET = 0XF<<19;
IO0CLR = 1<<19;
delay1ms();
IO0SET = 1<<19;
IO0CLR = 1<<20;
delay1ms();
IO0SET = 1<<20;
IO0CLR = 1<<21;
delay1ms();
IO0SET = 1<<21;
IO0CLR = 1<<22;
delay1ms();
IO0SET = 1<<22;
}
//键盘键值读取函数
unsigned int key_r(void)
{
int row,col;
row = 0;
switch(IO0PIN & (7<<16))
{
case 0x3: col = 3;break;
case 0x5: col = 2;break;
case 0x6: col = 1;break;
default:break;
}
switch(IO0PIN & (0xf<<19))
{
case 0xe: row = 1;break;
case 0xc: row = 2;break;
case 0xa: row = 3;break;
case 0x7: row = 4;break;
default:break;
}
if(row != 0)
row = (row-1)*3 + col;
else
row = 0;
return(row);
}
//初始化
void init(void)
{
PINSEL0 = 0X00000000;
PINSEL1 = 0X00000000;
lcd_init();
lcd_waddr(0x00);
lcd_wstr(str);
delaynms(500);
//lcd_waddr(0x40);
//lcd_wdata('E');
//delaynms(500);
lcd_waddr(0x40+15);
lcd_wdata('s');
}
int main(void)
{
init();
while(1)
{
key_s();
switch(key_r())
{
case 0: break;
case 1: lcd_waddr(0x40);lcd_wdata('1');delaynms(500);break;
}
}
}
-
-
额。。。。,不好意思,刚看到,现在还需要吗?
一周热门 更多>