找回密码
 立即注册
查看: 284|回复: 0

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

[复制链接]
  • 打卡等级:以坛为家I
  • 打卡总天数:224
  • 最近打卡:2025-05-07 08:02:46

79

主题

631

回帖

1148

积分

金牌会员

积分
1148
发表于 2024-6-6 01:22:49 | 显示全部楼层 |阅读模式
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 )
    {
    }
}



回复

使用道具 举报 送花

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|深圳国芯人工智能有限公司 ( 粤ICP备2022108929号-2 )

GMT+8, 2025-5-7 11:58 , Processed in 0.146867 second(s), 47 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表