新闻中心

EEPW首页>嵌入式系统>设计应用> PIC16F91x系列单片机驱动1602液晶屏程序

PIC16F91x系列单片机驱动1602液晶屏程序

作者: 时间:2016-11-29 来源:网络 收藏
IO口需要如下这样配置,不用段式LCD功能,也必须配置COMCON0和LCDCON,
/*-----------1602端口初始化-------------*/
void Port_1602_Init(void)
{
PORTA=0x00;
CMCON0=0xff;
ANSEL = 0x00;
TRISA=0X00;
PORTC=0x00;
TRISC=0x00;
LCDCON=0x00;
}
完整程序如下:
main.c如下:
#include
#include "Display.h"
#include "main.h"
#define uchar unsigned char
#define uint unsigned int
__CONFIG(WDTDIS & HS & PWRTDIS & BORDIS);//设置配置位
/************************定义显示字符*****************************************/
uchar Wel[] = {"Welcome To"};
uchar tab[] = {"Bei Jing"};
/*******************************************************************************
* 函 数 名: Delay_US(uint8 delay)
* 函数功能: 微秒延时--12us
* 入口参数: delay
* 返 回: 无
*******************************************************************************/
void Delay_US(uint delay)
{
for(;delay;)
{
delay--;
}
}
/*******************************************************************************
* 函 数 名: Delay_MS(uint16 delay)
* 函数功能: 毫秒延时--1ms
* 入口参数: delay
* 返 回: 无
*******************************************************************************/
void Delay_Ms(uint delay)
{
uint i;
for(i=0;i
Delay_US(83);
}
/*******************************************************************************
* 函 数 名: Delay_Sec(uint16 delay)
* 函数功能: 毫秒延时--1ms
* 入口参数: delay
* 返 回: 无
*******************************************************************************/
void Delay_Sec(uint delay)
{
uint i,j;
for(i=0;i<20*delay;i++)
{
for(j=0;j<4536;j++);
}
}
/******************************************************************************
* 函 数 名: main()
* 函数功能: LCD显示字符
* 入口参数: 无
* 返 回: 无
*******************************************************************************/
void main()
{
Port_1602_Init();
INIT_1602();
Display_1602_string(3,0,10,Wel);
Display_1602_string(4,1,8,tab);
while(1)
{
}
}
main.h如下:
#ifndef __MAIN_H__
#define __MAIN_H__
#define uchar unsigned char
#define uint unsigned int
#define LCD_RS RA0
#define LCD_RW RA1
#define LCD_EN RA2
void Delay_US(uint delay); //12微秒延时
void Delay_Ms(uint delay); //1毫秒延时
void Delay_Sec(uint delay); //1秒延时
#endif
Display.c如下:
#include
#include "Display.h"
#include "main.h"
/*******************************************************************************
* 函 数 名: uchar Chk_1602_busy(void)
* 函数功能: 读液晶忙通道数据
* 入口参数: 无
* 返 回: 无
*******************************************************************************/
uchar Chk_1602_busy(void)
{
uint gR_data;
uint gwait_time=0xff; //设置忙超时数
LCD_RS=0; //表示状态
LCD_RW=1; //选择读
LCD_EN=1;
TRISC = 0xFF; //接收口设为输入口
Delay_US(30);
gR_data=PORTC;
while(TESTBIT(gR_data,7)) //表示busy
{
--gwait_time;
if(!gwait_time)
{ LCD_EN=0;TRISC = 0x00; return 0; }
}
LCD_EN=0;
TRISC = 0x00; //恢复为输出口
return 1;
}
/******************************************************************************
* 函 数 名: void Write_1602_command(uchar gcmd,uchar gvalue)
* 函数功能: 写指令
* 入口参数: gcmd--指令 gvalue--是否查忙
* 返 回: 无
*******************************************************************************/
void Write_1602_command(uchar gcmd,uchar gvalue)
{
if(gvalue) //写命令时大部分情况下是在LCD空闲模式下写
{
if(Chk_1602_busy())
{
LCD_RS=0; //选择指令
LCD_RW=0; //选择写
PORTC=gcmd; //赋指令
LCD_EN=1; //使能
Delay_US(30);
LCD_EN=0;
}
}
else
{
LCD_RS=0; //选择指令
LCD_RW=0; //选择写
PORTC=gcmd; //赋指令
LCD_EN=1; //使能
Delay_US(30);
LCD_EN=0;
}
}

上一页 1 2 下一页

评论


技术专区

关闭