51单片机控制并口1602显示单字母实验

2019年10月14日08:46:55 评论

本程序基于51单片机开发板<传送门>

本程序开发板原理图<传送门>,提取码: 2bkj

#include"reg52.h"
#define uint unsigned int
#define uchar unsigned char

#define LCD1602_DB P0 //data bus 数据总线
sbit LCD1602_RS = P2^6;
sbit LCD1602_RW = P2^5;
sbit LCD1602_EN = P2^7;

void Read_Busy();
void Lcd1602_Write_Cmd(unsigned char cmd);
void Lcd1602_Write_Data(unsigned char dat);
void InitLcd1602();

void main()
{
    P1=0x0f0;
    InitLcd1602();
    Lcd1602_Write_Cmd(0x80);
    Lcd1602_Write_Data('A');
    while(1);
}

void Read_Busy() //忙检测函数,判断bit7是0,允许执行;1禁止
{
    unsigned char sta; //
    LCD1602_DB = 0xff;
    LCD1602_RS = 0;
    LCD1602_RW = 1;
    do
    {
         LCD1602_EN = 1;
         sta = LCD1602_DB;
         LCD1602_EN = 0; //使能,用完就拉低,释放总线
    }while(sta & 0x80);
}

void Lcd1602_Write_Cmd(unsigned char cmd) //写命令
{
    Read_Busy();
    LCD1602_RS = 0;
    LCD1602_RW = 0;
    LCD1602_DB = cmd;
    LCD1602_EN = 1;
    LCD1602_EN = 0;
}

void Lcd1602_Write_Data(unsigned char dat) //写数据
{
     Read_Busy();
     LCD1602_RS = 1;
     LCD1602_RW = 0;
     LCD1602_DB = dat;
     LCD1602_EN = 1;
     LCD1602_EN = 0;
}

void InitLcd1602() //1602初始化
{
      Lcd1602_Write_Cmd(0x38); //打开,5*8,8位数据
      Lcd1602_Write_Cmd(0x0c);
      Lcd1602_Write_Cmd(0x06);
      Lcd1602_Write_Cmd(0x01); //清屏
}

发表评论

您必须才能发表评论!