durongze 发表于 2024-6-6 01:22:49

分享一个89C51驱动st7920的例子,亲测可用。

lcd12864.h 文件;

#ifndef __LCD12864_H__
#define __LCD12864_H__

#include <reg52.h>
#include <intrins.h>
sbit RS = P0 ^ 0;
sbit RW = P0 ^ 1;
sbit EN = P0 ^ 2;
sbit PSB = P3 ^ 0;
sbit RST = P3 ^ 1;
#define _NOP() _nop_()
/* 12864 ?????? */
#define RS_CLR RS = 0 /* RS ?? */
#define RS_SET RS = 1 /* RS ?? */
#define RW_CLR RW = 0 /* RW ?? */
#define RW_SET RW = 1 /* RW ?? */
#define EN_CLR EN = 0 /* E ?? */
#define EN_SET EN = 1 /* E ?? */
#define PSB_CLR PSB = 0 /* PSB ??,???? */
#define PSB_SET PSB = 1 /* PSB ??,???? */
#define RST_CLR RST = 0 /* RST ?? , ??? */
#define RST_SET RST = 1 /* RST ?? */
/* 12864 ?????? */
#define DataPort P2 /*??? */
#define LcdData P2
void Delay_1ms( void );
void Delay_Nms( unsigned int n );
void LCD_write_com( unsigned char cmd );
void LCD_write_data( unsigned char dat );
void Ini_Lcd( void );
void Lcd_WriteStr( unsigned char x, unsigned char y, unsigned char *Str );
unsigned char Lcd_CheckBusy( void );
unsigned char Lcd_ReadData( void );

#endif





lcd12864.c 文件;

#include "LCD12864.h"

void delay_us( unsigned int t ) /* @11.0592MHz */
{
      while ( t-- ) {
                _nop_();
                _nop_();
                _nop_();
      }
}
void delay_ms( unsigned int t ) /* @11.0592MHz */
{
      unsigned char i, j;
      while ( t-- ) {
                _nop_();
                _nop_();
                _nop_();
                i = 11;
                j = 190;
                do {
                        while ( --j )
                              ;
                } while ( --i );
      }
}
void Delay_1ms( void )
{
      delay_ms( 1 );
}
/*
* ***********************************************************************
*?????????
* ***********************************************************************
*/
void LCD_write_com( unsigned char cmd )
{
      while ( Lcd_CheckBusy() ) {}


      RS_CLR;
      RW_CLR;
      EN_SET;
      DataPort = cmd;
      EN_CLR;
}
/*
* ***********************************************************************
*?????????
* ***********************************************************************
*/
void LCD_write_data( unsigned char dat )
{
      while ( Lcd_CheckBusy() )
                ;
      RS_SET;
      RW_CLR;
      EN_SET;
      DataPort = dat;
      /* delay_us( 1 ); */
      EN_CLR;
}
/*
* *************************************************************************
* ??? IO ????
* *************************************************************************
*/
void Port_init_12864( void )
{
      delay_ms( 50 );
      PSB_SET; /*?????? */
      RST_CLR;
      delay_ms( 100 );
      RST_SET;
}
/*******************************************
*????: Ini_Lcd
*? ?:???????
*? ?:?
*??? :?
********************************************/
void Ini_Lcd( void )
{
      Port_init_12864(); /*??????????? */
      LCD_write_com( 0x30 ); /*????? */
      Delay_1ms();
      LCD_write_com( 0x02 ); /*???? */
      Delay_1ms();
      LCD_write_com( 0x0c ); /*?????? , ???? */
      Delay_1ms();
      LCD_write_com( 0x01 ); /*???? */
      Delay_1ms();
      LCD_write_com( 0x06 ); /*???? */
      Delay_1ms();
      LCD_write_com( 0x80 ); /*????????? */
}
/*
* ***********************************************************************
*??????????????
* ***********************************************************************
*/
void Lcd_WriteStr( unsigned char x, unsigned char y, unsigned char *Str )
{
      if ( (y > 3) || (x > 7) )
                return; /*???????? */
      if ( y == 0 ) {
                LCD_write_com( 0x80 + x ); /*????? */
      }
      if ( y == 1 ) {
                LCD_write_com( 0x90 + x ); /*????? */
      }
      if ( y == 2 ) {
                LCD_write_com( 0x88 + x ); /*????? */
      }
      if ( y == 3 ) {
                LCD_write_com( 0x98 + x ); /*????? */
      }
      delay_us( 1 );
      while ( *Str > 0 ) {
                LCD_write_data( *Str );
                Str++;
                delay_us( 1 );
      }
}
unsigned char Lcd_CheckBusy( void )
{
      unsigned char Busy;
      RS_CLR;
      RW_SET;
      EN_SET;
      delay_us( 5 );
      Busy = LcdData & 0x80;
      EN_CLR;
      return(Busy);
}
/***********************************
*? LCD ?????
************************************/
unsigned char Lcd_ReadData( void )
{
      unsigned char Temp;
      while ( Lcd_CheckBusy() )
                ;
      P0 = 0XFF;
      RS_SET;
      RW_SET;
      EN_SET;
      delay_us( 10 );
      Temp = LcdData;
      EN_CLR;
      return(Temp);
}



main.c 文件
#include "reg52.h"
#include <intrins.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "LCD12864.h"
void main( void )
{
    Ini_Lcd();
    Lcd_WriteStr(0,0,"12345678");
    while ( 1 )
    {
    }
}



页: [1]
查看完整版本: 分享一个89C51驱动st7920的例子,亲测可用。