我給你來個程序試試
- #include <STC15.h>
- #include<intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define DataPort P0 // 數據端口
- #define Busy 0x80
- sbit RS = P2^1; //LCD控制引腳定義
- sbit RW = P2^2;
- sbit E = P2^3;
- code char exampl[]="Hello Every Body";
- code char examp2[]={0x32,0x30,0x31,0x36,0x00,0x31,0x01,0x32,0x36,0x02};
- code char Hzzimo[]={0x08,0x0F,0x12,0x0F,0x0A,0x1F,0x02,0x00, //“年”
- 0x0F,0x09,0x0F,0x09,0x0F,0x09,0x11,0x00, //“月”
- 0x0F,0x09,0x09,0x0F,0x09,0x09,0x0F,0x00};//“日”
- /******************************** 1Ms延時函數 ********************************/
- void Delayms(){ //1Ms延時 @12MHzx
- uchar i, j;
- i = 12; j = 169;
- do{
- while (--j);
- } while (--i);
- }
- /*************************** Ms延時函數 *****************************/
- void Delay(uchar t){
- while(--t) Delayms();
- }
- /******************************** 400Ms延時函數 ********************************/
- void Delay400Ms(void){
- unsigned char i, j, k;
- _nop_();_nop_();i = 19;
- j = 62;k = 43;
- do{
- do{
- while (--k);
- } while (--j);
- } while (--i);}
- /**************************** 等待允許函數 ******************************/
- void WaitForEnable( void ) {
- DataPort = 0xff;
- RS =0; RW = 1; _nop_();
- Delayms();
- E = 1; _nop_(); _nop_();
- Delayms();
- while( DataPort & Busy );
- E = 0;
- }
- /**************************** 寫命令函數 ******************************/
- void LcdWriteCommand( uchar CMD,uchar AttribC ) {
- if (AttribC) WaitForEnable(); // 檢測忙信號?
- RS = 0; RW = 0; _nop_();
- DataPort = CMD; _nop_(); // 送控制字子程序
- E = 1;_nop_();_nop_();E = 0; // 操作允許脈沖信號
- }
-
- /*************************** 寫數據函數 ***************************/
- void LcdWriteData( char dataW ) {
- WaitForEnable(); // 檢測忙信號
- RS = 1; RW = 0; _nop_();
- DataPort = dataW; _nop_();
- E = 1; _nop_(); _nop_(); E = 0; // 操作允許脈沖信號
- }
- /**************************** 顯示光標定位函數 ****************************/
- void LocateXY( char posx,char posy) {
- uchar temp;
- temp = posx & 0xf;
- posy &= 0x1;
- if ( posy )temp |= 0x40;
- temp |= 0x80;
- LcdWriteCommand(temp,0);
- }
- /**************************** 單字符顯示函數 ******************************/
- void DispOneChar(uchar x,uchar y,uchar Wdata) {
- LocateXY( x, y ); // 定位顯示字符的x,y位置
- LcdWriteData( Wdata ); // 寫字符
- }
- /***************************** 顯示字符串函數 *****************************/
- void ePutstr(uchar x,uchar y,uchar j, uchar code *ptr){
- uchar i;
- for (i=0;i<j;i++) {
- DispOneChar(x++,y,ptr[i]);
- if ( x == 16 ){
- x = 0; y ^= 1;
- }
- }
- }
- /***************************** LCD初始化函數 ********************************/
- void LcdReset( void ) {
- LcdWriteCommand( 0x38, 0); // 顯示模式設置(不檢測忙信號)
- Delay(5);
- LcdWriteCommand( 0x38, 0); // 共三次
- Delay(5);
- LcdWriteCommand( 0x38, 0);
- Delay(5);
- LcdWriteCommand( 0x38, 1); // 顯示模式設置(以后均檢測忙信號)
- LcdWriteCommand( 0x08, 1); // 顯示關閉
- LcdWriteCommand( 0x01, 1); // 顯示清屏
- LcdWriteCommand( 0x06, 1); // 顯示光標移動設置
- LcdWriteCommand( 0x0c, 1); // 顯示開及光標設置
- }
- /************************** 自定義漢字字符函數 ***************************/
- void Hz(){
- uchar i;
- LcdWriteCommand( 0x40,1 );
- for (i=0;i<24;i++){
- LcdWriteData(Hzzimo[i]);
- }
- }
- /******************************* 主函數 **********************************/
- void main(void){
- LcdReset();
- Delay400Ms();
- Hz();
- ePutstr(0,0,16,exampl); // 第一行從第0位開始顯示Hello Every Body
- ePutstr(4,1,10,examp2); // 第二行從第4位開始顯示2014年5月26日
- while(1);
- }
復制代碼 |