有while(1)就这个程序不显示东西,而去掉while(1)就显示0;去掉的话显示原因我知道:单片机执行完主函数后又开始循环执行,相当于从头开始,但是为什么加上while(1)就不显示了啊?
#include<reg51.h>
sbit RS=P2^5;
//sbit OE=P3^6;
sbit RW=P2^6;
sbit E=P2^7;
void Lcd_disp(int a);
void write_to_lcd(int c,int d,char e);
char convert(int a);
void cmd_row(int c);
void cmd_column(int d);
void cmd_data(char e);
void
delay_us(unsigned int value);
void cmd(int d);
void delay_1ms(void); //误差 0us
void
delay_nms(unsigned int value);
void csh();
//#define smg_wm P2
//#define smg_dm P0
//#define key_board_port P1
sbit my74HC573_OE = P3^7;//(配置电路中要用的数据位写法,尽量标明器件名称)
sbit my74HC138_C = P2^7;
sbit my74HC138_B = P2^6;
sbit my74HC138_A = P2^5;
int i; //for timer0 of smg display
//unsigned char DM[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90};
//unsigned char WM[] = {0x00, 0x20, 0x40, 0x60, 0x80, 0xa0};
//void delay();
//int b[2];
int ARR_KEY[] = {
0X77, 0X7B, 0X7D, 0X7E,
0XB7, 0XBB, 0XBD, 0XBE,
0XD7, 0xff,0XDB, 0XDD, 0XDE,
0XE7, 0XEB, 0XED, 0XEE
};
int get_key();
void xs();
int research(int a);
void mw(int z);
int q=2;
void main()
{
csh();
while(1)
{
q=research(get_key());
Lcd_disp(q);
}
}
int get_key()
{
int a=0xf0,b=0x0f,c,d,e;
P1=a;
c=P1;
P1=b;
d=P1;
e=c|d;
return e;
}
/*
** 搜索出对应的下标,返回(下标+1)
*/
int research(int a)
{
int i=0;
for(;i<10;i++)
{
if(a==ARR_KEY[i]&&a!=0xff)
{
return i+1;
}
else if(a==0xff) //如果不按键返回0
return 0;
}
}
void Lcd_disp(int a)
{
int c=0x80,d=5;
char e;
e=convert(a);
write_to_lcd(c,d,e);
}
void write_to_lcd(int c,int d,char e)
{
cmd_row(c);
cmd_column(d);
cmd_data(e);
}
char convert(int a)
{
char b;
b=a+'0';
return (b);
}
void cmd_row(int c)
{
RS=0;
RW=0;
P0=c;
E=1;
delay_us(10);
E=0;
RW=1;
RS=1;
}
void cmd_column(int d)
{
RS=0;
RW=0;
P0=d;
E=1;
delay_us(10);
E=0;
RW=1;
RS=1;
}
void cmd_data(char e)
{
RS=1;
RW=0;
P0=e;
E=1;
delay_us(10);
E=0;
RW=1;
RS=0;
}
void cmd(int d)
{
RS=0;
RW=0;
P0=d;
E=1;
delay_us(10);
E=0;
RW=1;
RS=1;
}
void
delay_us(unsigned int value)
{
unsigned int i;
for (i = 0; i < value; i++) {
;
}
}
void delay_1ms(void) //误差 0us
{
unsigned char a,b,c;
for(c=1;c>0;c--)
for(b=142;b>0;b--)
for(a=2;a>0;a--);
}
void
delay_nms(unsigned int value)
{
unsigned int i;
for (i = 0; i < value; i++) {
delay_1ms();
}
}
void csh()
{
delay_nms(15);
cmd(0x38);
delay_nms(5);
cmd(0x38);
delay_nms(5);
cmd(0x38);
delay_nms(5);
cmd(0x38);
delay_nms(5);
cmd(0x08);
delay_nms(5);
cmd(0x01);
delay_nms(5);
cmd(0x06);
delay_nms(5);
cmd(0x0c);
} |
|