普中51单片机,配的LCD9648的屏。直接下载的例程,但无法正常显示。
请那位高手帮忙看下代码有什么问题。
main.c
- #include <reg52.h>
- #include <intrins.h>
- sbit CS0 = P0^0; //????
- sbit RST = P0^1; //?′??
- sbit RS = P2^7; //êy?Y?üá???????
- sbit SCL = P2^6; //SPIê±?ó??
- sbit SDA = P2^5; //SPIêy?Y??
- sbit KEY_ADD=P3^3; //按键
- sbit KEY_DEC=P3^4;
- unsigned char curr,currold;//全局变量,当前箭头位置
- unsigned char code user16x16[]={
- 0x00,0x00,0x20,0x00,0x30,0x00,0x38,0x00,0x3C,0x00,0x3E,0x00,0x3F,0x00,0x3F,0x80,
- 0x3F,0xC0,0x3F,0x80,0x3F,0x00,0x3E,0x00,0x3C,0x00,0x38,0x00,0x30,0x00,0x20,0x00,
- };
- unsigned char code *MainMenu[]=
- {
- {" 1.设置1"},
- {" 2.设置2"},
- {" 3.设置3"},
- {" 4.设置4"},
- {" 5.设置5"},
- {" 6.设置6"},
- {" 7.设置7"},
- {" 8.设置8"},
- {" 9.设置9"},
- {" 0.设置0"},
- };
- void Delay10ms(unsigned int c) //?ó2? 0us
- {
- unsigned char a,b;
- for(;c>0;c--)
- for(b=38;b>0;b--)
- for(a=130;a>0;a--);
- }
- void SendDataSPI(unsigned char dat)
- {
- unsigned char i;
-
- for(i=0; i<8; i++)
- {
- if( (dat&0x80)!=0 ) SDA = 1;
- else SDA = 0;
- dat <<= 1;
- SCL = 0;
- SCL = 1;
- }
- }
- void WriteComm(unsigned int i)
- {
- CS0 = 0;
- RS = 0;
- SendDataSPI(i);
- CS0 = 1;
- }
- void WriteData(unsigned char i)
- {
- CS0 = 0;
- RS = 1;
- SendDataSPI(i);
- CS0 = 1;
- }
- void LCD_Init(void)
- {
- unsigned int i;
- //CS0=0;
-
- RST=1;
- for(i = 0; i < 1000; i++);
-
- RST=0;
- for(i = 0; i < 1000; i++);
- RST=1;
- for(i = 0; i < 1000; i++);
- WriteComm(0xe2); //èí?t?′??
- WriteComm(0xc8); //0xC8??í¨·??ò????????·′?ò£?0xC0?a?y3£·??ò
- WriteComm(0xa0); //0xA0??·??ò?????y3£·??ò£¨0xA1?a·′·??ò
- WriteComm(0x2f);
- WriteComm(0x26);
- WriteComm(0x81); //±3?°1a??±è?è
- WriteComm(0x10);
- WriteComm(0xaf); //?a????ê?
- }
- void Clear()
- {
- unsigned char i,j;
-
- for(i=0; i < 9; i++)
- {
- WriteComm(0x40);
- WriteComm(0xb0+i);
- WriteComm(0x10);
- WriteComm(0x00);
-
- for(j = 0; j < 96; j++)
- {
- WriteData(0x00);
- }
- }
- }
- /***********************************************
- 用户自定义字符
- ***********************************************/
- void CGRAM()
- {
- int i;
- // WriteComm(0x30);
-
- WriteComm(0x40);
- for(i=0;i<16;i++)
- {
- WriteData(user16x16[i*2]);
- WriteData(user16x16[i*2+1]);
- }
- }
- /***********************************************
- 显示用户自定义字符
- ***********************************************/
- void DisplayCGRAM(unsigned char x,unsigned char y)
- {
- switch(y)
- {
- case 1: WriteComm(0xb0+x);break;
- case 2: WriteComm(0xb1+x);break;
- case 3: WriteComm(0xb2+x);break;
- // case 4: WriteComm(0x98+x);break;
- default:break;
- }
- WriteData(00);
- WriteData(00);
- }
- /***********************************************
- 显示字符串
- x:横坐标值,范围0~8
- y:纵坐标值,范围1~4
- ***********************************************/
- void LCD_PutString(unsigned char x,unsigned char y,unsigned char code *s)
- {
- switch(y)
- {
- case 1: WriteComm(0xb0+x);break;
- case 2: WriteComm(0xb1+x);break;
- case 3: WriteComm(0xb2+x);break;
- // case 4: WriteComm(0x98+x);break;
- default:break;
- }
- while(*s>0)
- {
- WriteData(*s);
- s++;
- Delay10ms(50);
- }
- }
- /***********************************************
- 清屏
- ***********************************************/
- void ClrScreen()
- {
- WriteComm(0x01);
- Delay10ms(15);
- }
-
- /***********************************************
- 调用显示更新
- ***********************************************/
- void DisplayUpdata(void)
- {
- unsigned char num;
- ClrScreen();
- num=sizeof(MainMenu)/sizeof(MainMenu[0]);//判断数组中数值个数
- if((0+(curr/4)*4)<num)
- LCD_PutString(0,1,MainMenu[0+(curr/4)*4]);
- else //如果超出数组最大元素,则写空信息,不判断此信息可能会出现乱码
- LCD_PutString(0,1,"");
- if((1+(curr/4)*4)<num)
- LCD_PutString(0,2,MainMenu[1+(curr/4)*4]);
- else
- LCD_PutString(0,2,"");
- if((2+(curr/4)*4)<num)
- LCD_PutString(0,3,MainMenu[2+(curr/4)*4]);
- else
- LCD_PutString(0,3,"");
- if((3+(curr/4)*4)<num)
- LCD_PutString(0,4,MainMenu[3+(curr/4)*4]);
- else
- LCD_PutString(0,4,"");
- DisplayCGRAM(0,curr%4+1);
- }
- /***********************************************
- MAIN
- ***********************************************/
- void main()
- {
- LCD_Init();
- CGRAM(); //写入自定义字符
- DisplayUpdata();
- while(1)
- {
- if(curr!=currold) //光标位置变化,则更新显示
- {
- DisplayUpdata();
- currold=curr;
- }
-
- if(!KEY_ADD)
- {
- Delay10ms(10);
- if(!KEY_ADD)
- {
- while(!KEY_ADD);
- {
- if(curr<sizeof(MainMenu)/sizeof(MainMenu[0])-1)
- //判断数组中数值个数
- { curr++; }
- }
- }
- }
- if(!KEY_DEC)
- {
- Delay10ms(10);
- if(!KEY_DEC)
- {
- while(!KEY_DEC);
- {
- if(curr>0)
- { curr--; }
- }
- }
- }
- }
- }
复制代码
|