?? lcd12864.c
字號:
/*
LCD12864液晶屏驅動模塊
1、可直接嵌入到項目中使用
2、晶振頻率:1M
3、如晶振提高低層驅動延時要作相應修改
AVR_AFA
www.iccavr.com
*/
#include <iom128v.h>
#include "font.h"
#define RS_CLR PORTC &= ~(1 << PC2) /*命令或數據選擇*/
#define RS_SET PORTC |= (1 << PC2) /*RS = 1命令,RS = 0數據*/
#define RW_CLR PORTC &= ~(1 << PC3) /*讀取或寫入選擇*/
#define RW_SET PORTC |= (1 << PC3) /*RW = 1讀,RW = 0寫*/
#define EN_CLR PORTC &= ~(1 << PC4) /*讀寫使能信號*/
#define EN_SET PORTC |= (1 << PC4) /*下降沿有效*/
#define RST_CLR PORTC &= ~(1 << PC5) /*芯片復位腳*/
#define RST_SET PORTC |= (1 << PC5) /*高電平復位*/
#define CSA_CLR PORTC &= ~(1 << PC7) /*左半屏片選*/
#define CSA_SET PORTC |= (1 << PC7) /*高電平選中*/
#define CSB_CLR PORTC &= ~(1 << PC6) /*右半屏片選*/
#define CSB_SET PORTC |= (1 << PC6) /*高電平選中*/
/*延時函數*/
void delay_us(unsigned int n) {
if (n == 0) {
return ;
}
while (--n);
}
/*延時函數*/
void delay_ms(unsigned char i) {
unsigned char a, b;
for (a = 1; a < i; a++) {
for (b = 1; b; b++) {
;
}
}
}
/*顯示屏命令寫入函數前半屏*/
void LCD0_write_com(unsigned char com) {
RS_CLR;
RW_CLR;
CSA_SET;
CSB_CLR;
EN_SET;
PORTA = com;
delay_us(1);
EN_CLR;
}
/*顯示屏命令寫入函數后半屏*/
void LCD1_write_com(unsigned char com) {
RS_CLR;
RW_CLR;
CSA_CLR;
CSB_SET;
EN_SET;
PORTA = com;
delay_us(1);
EN_CLR;
}
/*顯示屏命令寫入函數*/
void LCD0_write_data(unsigned char data) {
RS_SET;
RW_CLR;
CSA_SET;
CSB_CLR;
EN_SET;
PORTA = data;
delay_us(1);
EN_CLR;
}
/*顯示屏命令寫入函數*/
void LCD1_write_data(unsigned char data) {
RS_SET;
RW_CLR;
CSA_CLR;
CSB_SET;
EN_SET;
PORTA = data;
delay_us(1);
EN_CLR;
}
/*顯示屏清空顯示*/
void disp_clear(unsigned char x0, unsigned char x1, unsigned char y) {
unsigned char x;
/*清除高位*/
x = x0;
if (x < 63) { /*地址在左半屏范圍內*/
LCD0_write_com(y | 0xB8);
LCD0_write_com(x | 0x40);
}
while (x < x1) {
LCD0_write_data(0x00);
x ++;
if (x > 63) { /*判斷地址是否越界*/
break;
}
}
if (x < x1) { /*地址進入左半屏范圍*/
LCD1_write_com(y | 0xB8);
LCD1_write_com(x | 0x40);
}
while (x < x1) {
LCD1_write_data(0x00);
x ++;
}
/*清除低位*/
x = x0;
if (x < 63) { /*地址在左半屏范圍內*/
LCD0_write_com((y+1) | 0xB8);
LCD0_write_com(x | 0x40);
}
while (x < x1) {
LCD0_write_data(0x00);
x ++;
if (x > 63) { /*判斷地址是否越界*/
break;
}
}
if (x < x1) { /*地址進入左半屏范圍*/
LCD1_write_com((y+1) | 0xB8);
LCD1_write_com(x | 0x40);
}
while (x < x1) {
LCD1_write_data(0x00);
x ++;
}
}
/*在指定位置顯示一個ASCII 字符*/
void disp_char(unsigned char x, unsigned char y, unsigned char ascii) {
unsigned char i = 0x00;
const unsigned char *q; /*取字庫指針*/
ascii -= 0x20;
q = &ENGLISH_FONT[ascii * 16]; /*定位指針地址*/
/*顯示高8位*/
i = 0x00;
if (x < 64) { /*地址在左半屏范圍內*/
LCD0_write_com(y | 0xB8);
LCD0_write_com(x | 0x40);
while (i < 8) {
LCD0_write_data(*q);
q ++;
x ++;
i ++;
if (x > 63) { /*判斷地址是否越界*/
break;
}
}
}
if (i < 8) { /*地址進入左半屏范圍*/
LCD1_write_com(y | 0xB8);
LCD1_write_com(x | 0x40);
while (i < 8) {
LCD1_write_data(*q);
q ++;
x ++;
i ++;
}
}
/*顯示低8位*/
i = 0x00;
x -= 8;
if (x < 64) { /*地址在左半屏范圍內*/
LCD0_write_com((y+1) | 0xB8);
LCD0_write_com(x | 0x40);
while (i < 8) {
LCD0_write_data(*q);
q ++;
x ++;
i ++;
if (x > 63) { /*判斷地址是否越界*/
break;
}
}
}
if (i < 8) { /*地址進入左半屏范圍*/
LCD1_write_com((y+1) | 0xB8);
LCD1_write_com(x | 0x40);
while (i < 8) {
LCD1_write_data(*q);
q ++;
x ++;
i ++;
}
}
CSA_CLR;
CSB_CLR;
}
/*在指定位置顯示一個漢字*/
void disp_word(unsigned char x, unsigned char y, unsigned char word) {
unsigned char i = 0x00;
const unsigned char *q; /*取字庫指針*/
q = &CHINESE_FONT[word * 32]; /*定位指針地址*/
/*顯示高16位*/
i = 0x00;
if (x < 64) { /*地址在左半屏范圍內*/
LCD0_write_com(y | 0xB8);
LCD0_write_com(x | 0x40);
while (i < 16) {
LCD0_write_data(*q);
q ++;
x ++;
i ++;
if (x > 63) { /*判斷地址是否越界*/
break;
}
}
}
if (i < 16) { /*地址進入左半屏范圍*/
LCD1_write_com(y | 0xB8);
LCD1_write_com(x | 0x40);
while (i < 16) {
LCD1_write_data(*q);
q ++;
x ++;
i ++;
}
}
/*顯示低16位*/
i = 0x00;
x -= 16;
if (x < 64) { /*地址在左半屏范圍內*/
LCD0_write_com((y+1) | 0xB8);
LCD0_write_com(x | 0x40);
while (i < 16) {
LCD0_write_data(*q);
q ++;
x ++;
i ++;
if (x > 63) { /*判斷地址是否越界*/
break;
}
}
}
if (i < 16) { /*地址進入左半屏范圍*/
LCD1_write_com((y+1) | 0xB8);
LCD1_write_com(x | 0x40);
while (i < 16) {
LCD1_write_data(*q);
q ++;
x ++;
i ++;
}
}
CSA_CLR;
CSB_CLR;
}
/*顯示一串字符*/
void disp_char_str(unsigned char x, unsigned char y, unsigned char *str) {
while (*str != 0) {
disp_char(x, y, *str);
x += 8;
str ++;
}
}
/*顯示屏初始化函數*/
void disp_init(void) {
DDRA = 0xFF; /*I/O口方向設置*/
DDRC = 0xFF;
RST_CLR;
delay_ms(10);
RST_SET;
LCD0_write_com(0xC0); /*顯示起行設置*/
LCD1_write_com(0xC0);
LCD0_write_com(0x3F); /*開顯示設置*/
LCD1_write_com(0x3F);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -