?? lcd20x4.c
字號:
/*=======================================================
20x4字符液晶主程序,編譯軟件(ICCAVR_6.31)
CPU內(nèi)部晶振8M
數(shù)據(jù)線B0~B7接PORTB, E=c2 RW=c1 RS=c0
=========================================================
接線圖如下:
_______________ ______________
| --1|GND |
| --2|+5V |
| --3|V0 |
| | |
Pc0|--------4|RS |
Pc1|--------5|RW |
Pc2|--------6|E |
CPU | | |
ATmmega16L PB0|--------7|D0 LCD20x4 |
PB1|--------8|D1 |
PB2|--------9|D2 |
PB3|-------10|D3 |
PB4|-------11|D4 |
PB5|-------12|D5 |
PB6|-------13|D6 |
PB7|-------14|D7 |
| | |
| --15|+LED |
| --16|-LED |
_______________| |______________|
========================================================*/
/*
#include<ioM16V.h>
//#include <LCD20x4.h>
//CPU_ATmmega16L
#define RS_H asm("sbi 0x15,0") //RS設(shè)為高電平
#define RS_L asm("cbi 0x15,0") //RS設(shè)為低電平
#define RW_H asm("sbi 0x15,1") //RW設(shè)為高電平
#define RW_L asm("cbi 0x15,1") //RW設(shè)為低電平
#define E_H asm("sbi 0x15,2") //E設(shè)為高電平
#define E_L asm("cbi 0x15,2") //E設(shè)為低電平
//#define NOP asm("NOP") //機(jī)器等待1個周期(1M=1uS,8M=0.125uS)
//#define SLEEP asm("SLEEP") //休眠
//=======================================================
//微秒級延時程序
void delay_us(int time)
{
do
{
time--;
}
while (time > 1);
}
//=======================================================
//毫秒級延時程序
void delay_ms(unsigned int time)
{
while(time != 0)
{
delay_us(1000);//延時1000us
time--;
}
}
//=======================================================
//讀取lcd是否內(nèi)部操作(忙碌)狀態(tài)
char Lcd_Busy()
{
char r;
DDRB = 0x00; //端口B設(shè)為輸入方式
E_L;RS_L;RW_H; //E=0(致能),RS=0(命令),RW=1(讀)
delay_us(10); //液晶延時子程序
E_H;
delay_us(10); //液晶延時子程序
r = PINB & 0x80; //讀取lcd_data第八位
E_L;
DDRB=0xff; //端口B設(shè)為輸出方式
return r; //讀取結(jié)果返回
}
//=======================================================
//向Lcd發(fā)送命令程序
void Lcd_Command(unsigned char Command)
{
while(Lcd_Busy()); //判斷l(xiāng)cd是否內(nèi)部操作狀態(tài)
E_L;RS_L;RW_L; //E=0(致能),RS=0(命令),RW=0(寫)
delay_us(10); //液晶延時子程序
E_H;
PORTB = Command; //向Lcd發(fā)送命令
delay_us(10); //液晶延時子程序
E_L;
}
//=======================================================
//向lcd寫入一個字符程序
void Lcd_Write(unsigned char Data)
{
while(Lcd_Busy()); //判斷l(xiāng)cd是否內(nèi)部操作狀態(tài)
E_L;RS_H;RW_L; //E=0(致能),RS=1(數(shù)據(jù)),RW=0(寫)
delay_us(10); //液晶延時子程序
E_H;
PORTB = Data; //向lcd寫入一個字符
delay_us(10); //液晶延時子程序
E_L;
}
/*=======================================================
LCD第1行顯示地址1~20(0x80~0x93)
LCD第2行顯示地址1~20(0xc0~0xd3)
LCD第3行顯示地址1~20(0x94~0xa7)
LCD第4行顯示地址1~20(0xd4~0xe7)
=======================================================*/
//初始化LCD_8位接口,4行x20字符的工作方式
/*
void Lcd_Init()
{
// DDRB = 0xff; //端口B設(shè)為輸出方式
// DDRC = 0xff; //端口D設(shè)為輸出方式
Lcd_Command(0x38); //
Lcd_Command(0x38); //
Lcd_Command(0x38); //
Lcd_Command(0x38); //
Lcd_Command(0x08); //令顯示器off
Lcd_Command(0x01); //清除顯示器
Lcd_Command(0x06); //令LCD每接收到1Byte數(shù)據(jù)后,AC自動加1
Lcd_Command(0x0C); //令光標(biāo),0x0c=不顯示,0x0d=顯示閃動.
/*
Lcd_Command(0x08); //令顯示器off 40uS
Lcd_Command(0x01); //清除顯示器1.64mS
Lcd_Command(0x06); //令LCD每接收到1Byte數(shù)據(jù)后,AC自動加1 0x04AC減1 40us
Lcd_Command(0x0C); //令光標(biāo),0x0c=不顯示,0x0d=顯示閃動.40uS
*/
//}
//=======================================================
//寫ASCII字符串程序
/*
void asc_tran(unsigned char *asc)
{
while((*asc) != 0) //判斷字是否結(jié)束
{
Lcd_Write(*asc); //向lcd寫入字符串
asc++; //移下一個字符
}
}
//=======================================================
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -