作為小白,總結了一些例程,希望對新手有用
main.c主程序
- unsigned char *s1="welcome!";這句話顯示有警告,不知道是什么問題,不過也可以執行。
- void main(void)
- {
- uchar i;
- WDTCTL=WDT_ADLY_250;
- LcdReset();
- Dispstr(04,0,s1);
- LocateXY(0,9);
- LcdWriteCommand(0x07,1); //0x07表示n=1,s=1整屏左移。
- for(i=12;i>0;i--)
- {
- LcdWriteData(0x20);
- IFG1 &=~ WDTIFG;
- while(!(IFG1 & WDTIFG));
- IFG1 &=~ WDTIFG;
- }
- while (1)
- {
- LcdWriteCommand(0x05,1); //0x05表示n=0,s=1整屏右移。
- for(i=24;i>0;i--)
- {
- LcdWriteData(0x20);
- IFG1&=~WDTIFG;
- while(!(IFG1&WDTIFG));
- IFG1&=~WDTIFG;
- }
- LcdWriteCommand(0x07,1);
- for(i=24;i>0;i--)
- {
- LcdWriteData(0x20);
- IFG1&=~WDTIFG;
- while(!(IFG1&WDTIFG));
- IFG1&=~WDTIFG;
- }
- }
- }
復制代碼
cry1602.c子程序
- #include "msp430g2553.h"
- #include "cry1602.h"
- #define datadir P1DIR
- #define dataport P1OUT
- #define busy 0x80
- #define ctr1dir P2DIR
- #define CLR_RS P2OUT&=~BIT0;
- #define SET_RS P2OUT|=BIT0;
- #define CLR_RW P2OUT&=~BIT1;
- #define SET_RW P2OUT|=BIT1;
- #define CLR_EN P2OUT&=~BIT2;
- #define SET_EN P2OUT|=BIT2;
- void DispStr(uchar x,uchar y,uchar *ptr)
- {
- uchar *temp;
- uchar i,n=0;
- temp=ptr;
- while(*ptr++!='\0')
- n++;
- for(i=0;i<n;i++)
- {Disp1Char(x++,y,temp[i]);
- if(x==0x0f)
- {
- x=0;
- y^=1;
- }
- }
- }
- void DispNChar(uchar x,uchar y,uchar n,uchar *ptr)
- {uchar i;
- for(i=0;i<n;i++)
- {
- Disp1Char(x++,y,ptr[i]);
- if(x==0x0f)
- {x=0;
- y^=1;
- }}}
- void LocateXY(uchar x,uchar y)
- {
- uchar temp;
- temp=x&0x0f;
- y&=0x01;
- if(y)
- temp|=0x40;
- temp|=0x80;
- LcdWriteCommand(temp,1);
- }
- void Disp1Char(uchar x,uchar y,uchar data)
- {LocateXY(x,y);
- LcdWriteData(data);
- }
- void LcdReset(void)
- {ctr1dir|=0x07;
- datadir=0xff;
- LcdWriteCommand(0x38,0);
- Delay5ms();
- LcdWriteCommand(0x38,0);
- Delay5ms();
- LcdWriteCommand(0x38,0);
- Delay5ms();
- LcdWriteCommand(0x38,1);
- LcdWriteCommand(0x08,1);
- LcdWriteCommand(0x01,1);
- LcdWriteCommand(0x06,1);
- LcdWriteCommand(0x0c,1);
- }
- void LcdWriteCommand(uchar cmd,uchar chk)
- {
- if(chk)
- WaitForEnable();
- CLR_RS;
- CLR_RW;
- _NOP();
- dataport=cmd;
- _NOP();
- SET_EN;
- _NOP();
- _NOP();
- CLR_EN;
- }
- void LcdWriteData(uchar data)
- {
- WaitForEnable();
- SET_RS;
- CLR_RW;
- _NOP();
- dataport=data;
- _NOP();
- SET_EN;
- _NOP();
- _NOP();
- CLR_EN;
- }
- void WaitForEnable(void)
- {
- P1DIR&=0X00;
- CLR_RS;
- SET_RW
- _NOP();
- SET_EN;
- _NOP();
- _NOP();
- while((P1IN&busy)!=0);
- CLR_EN;
- P1DIR|=0xff;
- }
- void Delay5ms(void)
- {
- uint i=40000;
- while(i!=0)
- {i--;}
- }
復制代碼
cry1602.h 子程序頭文件
- #ifndef _CRY1602_H
- #define _CRY1602_H
- #define uchar unsigned char
- #define uint unsigned int
- void DispStr(uchar x,uchar y,uchar *ptr);
- void DispNChar(uchar x,uchar y,uchar n,uchar *ptr);
- void LocateXY(uchar x,uchar y);
- void Disp1Char(uchar x,uchar y,uchar data);
- void LcdReset(void);
- void LcdWriteCommand(uchar cmd,uchar chk);
- void LcdWriteData(uchar data);
- void WaitForEnable(void);
- void Delay5ms(void);
- #endif
復制代碼
以上資料51hei下載地址:
MSP430G2553和LCD1602顯示的正確程序.doc
(35 KB, 下載次數: 23)
2020-3-19 10:26 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|