📄 st7920 c语言驱动程序.txt
字号:
/*=====================================================================
File name: ST7920.c
Description:This is a test programm to diver ST7920.The interface between
MCU and LCM is in parallel mode(Only write dat is available).
Pin assignment :
1--VSS;2--VDD;3--V0;4--R/S(CS)--P7.0;5--R/W(SID)--P7.1;
6--E(SCLK)--P7.2;7-14--DB0-DB7--P6;15--PSB--P7.3;
16--NC--P7.4;17--/RST--P7.5;18--VEE;19--LEDA;20--LEDK
RS RW Description
0 0 MPU write instruction to IR(ST7920)
0 1 MPU read BF and AC(address counter)
1 0 MPU write data to DR(data register)
1 1 MPU read dat from DR
Version: 1.0
date : 5,10 2005 (M-D-Y)
========================================================================*/
#include <C8051F040.H>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
//define connection between MCU and ST7920
sbit ST7920RW=P7^1; //Pin 5
sbit ST7920RS=P7^0; //Pin 4
sbit ST7920CS=P7^6; //Pin 6
//=================== prototype list =============================
void ST7920_Delay1ms(uchar n); //to delay 1 ms about
void ST7920_SendCmd(uchar cmd); //write command to ST7920
void ST7920_SendData(uchar dat); //write dat to ST7920
void ST7920_Configuration(void);
void ST7920_Init(void); //Initialize ST7920
//================================================================
void main(void)
{
uchar *chara="我爱你!";
uchar *date="5-11-2005";
uchar i;
ST7920_Configuration();
ST7920_Init();
ST7920_SendCmd(0x80);
ST7920_SendData('I');
ST7920_SendCmd(0x81);
ST7920_SendData('l');
ST7920_SendData('o');
ST7920_SendData('v');
ST7920_SendData('e');
ST7920_SendData('!');
ST7920_SendCmd(0x90);
for(i=0;i<4;i++)
{
ST7920_SendData(*chara++);
ST7920_SendData(*chara++);
}
ST7920_SendCmd(0x88);
/*while(*date!='\0')
{
ST7920_SendData(*date);
date++;
} */
for(;*date!='\0';date++)
ST7920_SendData(*date);
while(1);
}
//================================================================
void ST7920_Delay1ms(uchar n)
{
uchar i;
uchar j;
for(j=0;j<n;j++)
for(i=0;i<255;i++);
}
//================================================================
void ST7920_SendCmd(unsigned char cmd)
{
EA=0;
SFRPAGE=0x0f;
P6=cmd;
ST7920RS=0;
ST7920RW=0;
ST7920CS=1;
_nop_();
ST7920CS=0;
_nop_();
ST7920CS=1;
_nop_();
ST7920RW=1;
P6=0xff;
ST7920RS=0;
while(P6&0x80); //make sure ST7920 is not in busy state by reading BF
ST7920CS=0;
ST7920CS=1;
EA=1;
// if(cmd)
// ST7920_Delay1ms(3);
}
//================================================================
void ST7920_SendData(unsigned char dat)
{
EA=0;
SFRPAGE=0x0f;
P6=dat;
ST7920RS=1;
ST7920RW=0;
ST7920CS=1;
ST7920CS=0;
ST7920CS=1;
ST7920RW=1;
P6=0xff;
ST7920RS=0;
while(P6&0x80); //if BF(busy flag) is '1',wait
ST7920CS=0;
ST7920CS=1;
EA=1;
}
//================================================================
//must be excuted ST7920_Init() firstly when reset the system
void ST7920_Init(void)
{
ST7920_Delay1ms(200); //waiting
ST7920_SendCmd(0x30); //function set :8-bit interface
ST7920_Delay1ms(10);
ST7920_SendCmd(0x30); //basic instruction
ST7920_Delay1ms(10);
ST7920_SendCmd(0x0c); //display on,cursor off,blink off
ST7920_Delay1ms(0x100);
ST7920_SendCmd(0x01); //display clear
ST7920_Delay1ms(100);
ST7920_SendCmd(0x06); //cursor move right AC increased by 1,display not shift
ST7920_Delay1ms(100);
}
//==================================================================
void ST7920_Configuration(void)
{
WDTCN=0x07;
WDTCN=0xde;
WDTCN=0xad; //disable watchdog
P6MDOUT=0x00; // P6 is configured in Open-Drain mode
P7MDOUT=0x0f;
SFRPAGE=0x0f;
CLKSEL=0x00; // select internal oscillator
OSCXCN=0x00; // disable external oscillator
OSCICN=0x80; // system clock derived from internal oscillator(24.5MHZ) devided by 8
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -