// 功能:C8051F(330) LCD1602(4线驱动)
//**********************************************
#include
#include "delay.h"
//
//|---| |
//VSS VDD VL RS RW E
//
//连接:
//RS->P0.1
//RW->P0.2
//E->P0.3
//D4~D7->P1.4~P1.7
//VL根据不同型号,可能要接VSS或VDD
//B+/B-->+5/GND
//VSS/VDD->GND/+3.3
sbit lcd_rs=P0^1;
sbit lcd_rw=P0^2;
sbit lcd_e=P0^3;
#define LCD_DAT P1
//HD44780指令
#define LCD_CLEAR 0x01
#define LCD_HOME 0x02
#define LCD_SET_INPUT 0x06
#define LCD_DISPLAY_OFF 0x80
#define LCD_DISPLAY_ON 0x0c
#define LCD_SHIFT 0x14
#define LCD_MODE_8 0x38
#define LCD_MODE_4 0x28
//unsigned char lcd_add=0x00;
void lcd_write_cmd(unsigned char dat)
{
lcd_rs=0;
lcd_rw=0;
lcd_e=1;
LCD_DAT=dat;
delay_us(1);
lcd_e=0;
//delay_us(50);
delay_us(1);
lcd_e=1;
LCD_DAT=dat<<4;
delay_us(1);
lcd_e=0;
delay_us(50);
}
void lcd_sendchar(unsigned char dat)
{
lcd_rs=1;
lcd_rw=0;
lcd_e=1;
LCD_DAT=dat;
delay_us(1);
lcd_e=0;
//delay_us(50);
delay_us(1);
lcd_e=1;
LCD_DAT=dat<<4;
delay_us(1);
lcd_e=0;
delay_us(50);
}
//
//{
//lcd_rs=0;
//lcd_rw=1;
//lcd_e=1;
//while(LCD_DAT&0x80);
//lcd_e=0;
//delay_us(50);
//}
void lcd_init(void)
{
lcd_e=0;
delay_ms(15);
lcd_write_cmd(LCD_MODE_8);
delay_ms(5);
lcd_write_cmd(LCD_MODE_8);
delay_ms(5);
lcd_write_cmd(LCD_MODE_8);
lcd_write_cmd(LCD_MODE_4);
//delay_us(50);
lcd_write_cmd(LCD_DISPLAY_ON);
//delay_us(50);
lcd_write_cmd(LCD_CLEAR);
delay_ms(2);
lcd_write_cmd(LCD_SET_INPUT);
//delay_us(50);
}
void lcd_reset(void)
{
lcd_write_cmd(LCD_CLEAR);
delay_ms(2);
lcd_write_cmd(LCD_HOME);
delay_ms(2);
}
void lcd_setadd(unsigned char add)
{
if(add>0x0f)add+=0x30;
lcd_write_cmd(add|0x80);
}
void lcd_sendstr(unsigned char add,unsigned char *dat)
{
lcd_setadd(add);
while(*dat)
{
}
}
//-----------------------------------------------------------------
//----------------------lcd1602.h---------------------------------
#ifndef _LCD_1602
#define _LCD_1602
extern void lcd_write_cmd(unsigned char dat);
extern void lcd_sendchar(unsigned char dat);
extern void lcd_wait_busy(void);
extern void lcd_init(void);
extern void lcd_sendstr(unsigned char add,unsigned char *dat);
extern void lcd_setadd(unsigned char add);
extern void lcd_reset(void);
#endif
//-------------------------------------------------------------------
//----------------delay.c-------------------------------------------
//**********************************************
// 功能:C8051F(330)精确延时 24.5M
// 作者:July
// Blog: http://blog.sina.com.cn/mcuworld
// 最后更新:2008-12-31
// 尊重劳动成果,引用请勿删除本信息谢谢!
//**********************************************
#include
#include
//----------------------------------------------
//----------------------------
// 微秒级精确延时
// 仿真:(24.5M)
// delay_us(1);
// delay_us(10);
// delay_us(100); //102.74us
//----------------------------
void delay_us(unsigned char k)
{
while(k--)
{
}
}
//----------------------------------------------
// 毫秒级精确延时
// 仿真:(24.5M)
// delay_ms(1);
// delay_ms(10);
// delay_ms(100); //老机太慢出不来鸟~自己测吧:)
//----------------------------------------------
void delay_1ms()
{
}
void delay_ms(unsigned int i)
{
}
//------------------------------------------
//----------------delay.h-----------------
#ifndef _DELAY_H
#define _DELAY_H
extern void delay_us(unsigned char k);
extern void delay_ms(unsigned int i);
#endif
//---------------------------------------
//---------------------main.c--------------
//**********************************************
// 功能:C8051F(330)精确延时+UART0+LCD1602(4线)
// 作者:July
// Blog: http://blog.sina.com.cn/mcuworld
// 最后更新:2009-5-12
// 尊重劳动成果,引用请勿删除本信息谢谢!
//**********************************************
//
//C8051F/STC/MPC群:78614293
//Cortex-M3/M0(STM32)群:78612895
//PIC/PIC32群:78614030
//LabWindows/CVI群:20328398
//WinCE/C#群:74373749
//
#include
//#include
#include "uart0.h"
#include "delay.h"
#include "lcd1602.h"
void SYSCLK_Init(void);
void PORT_Init(void);
void main(void)
{
unsigned char address=0;
PCA0MD&=~0x40 ;
P0|=0x01;
lcd_init();
lcd_reset();
lcd_sendstr(0,"1234567890abcdef");
lcd_sendstr(16,"ghijklmnopqrstuv");
sendstr("Welcome to July's Hut! ");
}
//-----------------------------------------------------------------------------
// 端口初始化
//-----------------------------------------------------------------------------
//
// 配置交叉开关和通用IO
//
// P0.4 - UART TX0
// P0.5 - UART RX0
void PORT_Init(void)
{
P0MDOUT|=0x01;
}
//-----------------------------------------------------------------------------
// 系统时钟初始化
//-----------------------------------------------------------------------------
void SYSCLK_Init(void)
{
}
//----------------------------------------------------
//-------------main.h----------------------
#ifndef _MAIN_H
#define _MAIN_H
// 系统工作频率
#define SYSCLK 24500000
// 串口工作波特率
#define BAUDRATE 9600
#endif
//----------------------------------------
//----------uart0.c-----------------------
//**********************************************
// 功能:C8051F(330) UART0
// 作者:July
// Blog: http://blog.sina.com.cn/mcuworld
// 最后更新:2009-5-12
// 尊重劳动成果,引用请勿删除本信息谢谢!
//**********************************************
#include
#include "main.h"
unsigned char temp ;
void UART0_Init(void);
void Uart0_Transmit(unsigned char tmp);
void sendstr(unsigned char*tmp);
//-----------------------------------------------------------------------------
// 串口UART0初始化
//-----------------------------------------------------------------------------
void UART0_Init(void)
{
}
//-----------------------------------------------------------
// 串口UART0中断
//-----------------------------------------------------------
//中断向量0x0023
void UATR0_ISR(void)interrupt 4
{
}
//-----------------------------------------------------------
// 串口UART0发送
//-----------------------------------------------------------
void Uart0_Transmit(unsigned char tmp)
{
}
//-----------------------------------------------------------
// 发送字符串
//-----------------------------------------------------------
void sendstr(unsigned char*tmp)
{
}
//-----------------------------------
//-----------------------uart0.h-------------------
#ifndef _UART0_H
#define _UART0_H
extern void UART0_Init(void);
extern void Uart0_Transmit(unsigned char tmp);
extern void sendstr(unsigned char*tmp);
#endif
//-------------------------------------
上一篇:C8051F(330)精确延时程序(24.5M)
下一篇:LCD12864(ST7920)的C8051驱动(并行)
推荐阅读最新更新时间:2024-03-16 14:43