问题描述
我有一个项目,其中一个 8x2 字符的 lcd 由 atmega8 微控制器控制。
液晶显示器是一个 dips082-hnled:https://eu.mouser.com/datasheet/2/127/dips082e-274871.pdf
因为它很容易控制,所以我编写了自己的函数来初始化它并向其写入一些文本。 写一些文字是我真正需要的,对于这个项目......
问题如下:
虽然液晶显示器的第一行与我的程序完美配合,但我根本无法在第二行显示任何内容。
我查看了 lcds 数据表和 lcd 使用的驱动芯片数据表,但我无法弄清楚问题是什么。
我还检查了第二个已知良好的相同类型的液晶显示器,我遇到了同样的问题,所以它也不能是液晶显示器有问题。
#include <xc.h>
#include "pinmapping.h"
#include "main.h"
#include <util/delay.h>
//LCD:
//RS: HIGH=Screen Data,LOW=Command
//RW: HIGH=Read,LOW=Write
//E: FALLING EDGE=Execute Command/Write Data
//D0...D7: PORTD,8-bit data lines
void toggleEnable(){
//set pin low
PORTB = PORTB & (~LCD_E);
//wait for 3 ms so the lcd has time to execute the instruction
_delay_ms(3);
//set pin high again
PORTB = PORTB | LCD_E;
//allow for a minimal high time
_delay_ms(3);
}
void initLCD(){
//Wait until internal LCD init complete
_delay_ms(25);
//Set RS and RW low
PORTB = PORTB & (~(LCD_RS | LCD_RW));
//Function Set (8-bit data,2 lines,5x8 font)
PORTD = 0b00110000;
toggleEnable();
//display ON/OFF (display on,Cursor visible,Cursor blink)
PORTD = 0b00001100;
toggleEnable();
//Clear display,Cursor Home
PORTD = 0b00000001;
toggleEnable();
//Entry Mode set (Cursor auto-increment)
PORTD = 0b00000110;
toggleEnable();
//Set RS and RW high again
PORTB = PORTB | (LCD_RS | LCD_RW);
}
void testLCD(){
char testchars[] = "ABCDEFGHIJKLMnopQRSTUVWXYZabcdefghijklmnopqrstuvxyz0123456789!?-+,. :;<=>*/()%&#$";
//Set RS low and RW low
PORTB = PORTB & (~LCD_RS);
PORTB = PORTB & (~LCD_RW);
//Clear display,Cursor Home
PORTD = 0b00000001;
toggleEnable();
//Set RS high and RW low
PORTB = PORTB | LCD_RS;
PORTB = PORTB & (~LCD_RW);
//Write the array contents to the lcd - this should completely fill the DDRAM
for(int i = 0; i < 80; i++){
PORTD = testchars[i];
toggleEnable();
}
}
void writeLCD(char text[16]){
//Set RS low and RW low
PORTB = PORTB & (~LCD_RS);
PORTB = PORTB & (~LCD_RW);
//Clear display,Cursor Home
PORTD = 0b00000001;
toggleEnable();
//Set RS high and RW low
PORTB = PORTB | LCD_RS;
PORTB = PORTB & (~LCD_RW);
//loop through string send it to lcd
for(int i = 0; i < 16; i++){
//We have a 16 char lcd so we need to jump to line 2 after 8 chars
if(i == 8){
//Set RS low and RW low
PORTB = PORTB & (~LCD_RS);
PORTB = PORTB & (~LCD_RW);
//Set DD-RAM Address to 0x40 - the beginning of line 2
PORTD = 0xC0;
//and execute command
toggleEnable();
//Set RS high and RW low
PORTB = PORTB | LCD_RS;
PORTB = PORTB & (~LCD_RW);
}
//put the current char to data bus
PORTD = text[i];
//then toggle the enable pin to actually write it to the lcd
toggleEnable();
}
//Set RS and RW low
PORTB = PORTB & (~(LCD_RS | LCD_RW));
//reset cursor to start
PORTD = 0b00000010;
toggleEnable();
//Set RS and RW high again
PORTB = PORTB | (LCD_RS | LCD_RW);
}
void commandLCD(char command){
//Set RS and RW low
PORTB = PORTB & (~(LCD_RS | LCD_RW));
PORTD = command;
toggleEnable();
//Set RS and RW high again
PORTB = PORTB | (LCD_RS | LCD_RW);
}
如您所见,我编写了一个测试函数,该函数应该填充 lcd 的整个 80 字节显示内存。
我使用的是 xc8 编译器。
initLCD 函数在 io-port 初始化后立即在 main 的开头被调用。
pinmapping.h 文件为使用的不同引脚定义位掩码。 (例如:LCD_RS)
lcd 的 8 条数据线连接到 atmega 的端口 D,匹配它们的位(例如:lcd D0 到 avr D0,lcd D1 到 avr D1 等)。
也许我忽略了一些东西或者我做错了什么,但我无法弄清楚是什么导致了问题......
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)