到目前为止,我一直在研究微控制器,并且我已经了解了它是如何在8位模式下工作的,但有一点我不明白是如何在4位模式下使用它以及我尝试使用的代码我无法做到这一点,我几乎搜索了互联网上的所有网站来解释这个话题,但仍然没有得到它所以我非常感谢如果有人可以花一些时间向我解释这一点,我要提前感谢
答案 0 :(得分:1)
@ user3674628:到目前为止,你一定知道如何在4位模式下进行接口。虽然你没有指定你在这里使用的编译器,但我一直在使用带有xC8编译器的mplabx。它工作得很好。不要忘记调整mplabx上的配置位。成功了!
// PIC18F4550 Configuration Bit Settings
// 'C' source line config statements
#include <xc.h>
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIG1L
#pragma config PLLDIV = 5
#pragma config CPUDIV = OSC1_PLL2
#pragma config USBDIV = 2
// CONFIG1H
#pragma config FOSC = INTOSCIO_EC //this uses internal oscillator
#pragma config FCMEN = OFF
#pragma config IESO = OFF
// CONFIG2L
#pragma config PWRT = OFF
#pragma config BOR = OFF
#pragma config BORV = 3
#pragma config VREGEN = OFF
// CONFIG2H
#pragma config WDT = OFF
#pragma config WDTPS = 32768
// CONFIG3H
#pragma config CCP2MX = ON
#pragma config PBADEN = OFF
#pragma config LPT1OSC = OFF
#pragma config MCLRE = OFF
// CONFIG4L
#pragma config STVREN = ON
#pragma config LVP = OFF
#include <p18f4550.h>
#include <stdio.h>
#include <stdlib.h>
#include <plib/xlcd.h>
/* DATA_PORT defines the port to which the LCD data lines are connected */
#define DATA_PORT PORTB //for 4-bit mode connect D4-D7 of lcd to B0-B3 of lcd
#define TRIS_DATA_PORT TRISB
/* This defines the port where the control lines are connected.
* These are just samples, change to match your application.
* you may omit this part because its in y the compiler.
*/
#define RW_PIN LATBbits.LATB6
#define TRIS_RW TRISBbits.TRISB6
#define RS_PIN LATBbits.LATB5
#define TRIS_RS TRISBbits.TRISB5
#define E_PIN LATBbits.LATB4
#define TRIS_E TRISBbits.TRISB4
#define _XTAL_FREQ 4000000 // SET THIS TO SUIT YOUR FREQUENCY
void Wait(unsigned int delay)//delay for 100ms
{
for(;delay;delay--)
__delay_us(100);
}
//initialise the lcd
void LCDInit(){
OpenXLCD(FOUR_BIT & LINES_5X7 & CURSOR_ON & BLINK_OFF);
while(BusyXLCD());
WriteCmdXLCD(SHIFT_DISP_LEFT);
while(BusyXLCD());
}
void main(void)
{
ADCON1 = 0xF; // No analog, all digital i/o
TRISB = 0x0;
LCDInit()//initialise lcd
putrsXLCD( " Thanks God" );//display on the lcd
while(BusyXLCD());
WriteCmdXLCD(0xC0);// go to the next line on the lcd
while(BusyXLCD());
putrsXLCD( " I made it" );//display on the lcd
while(BusyXLCD());
while(1);
}
void DelayFor18TCY(void){
Delay10TCYx(20);
}
void DelayPORXLCD(void){
Delay1KTCYx(30);
}
void DelayXLCD(void){
Delay1KTCYx(10);
}
答案 1 :(得分:0)
第一个是非常有用的信息,第二个是示例源代码。
答案 2 :(得分:0)
对于4位接口数据,仅使用四条总线(DB4至DB7)进行传输。总线DB0到DB3 被禁用。 HD44780U和MPU之间的数据传输在4位数据之后完成 已被转移两次。至于数据传输的顺序,四个高位(对于8位 操作,DB4到DB7)在四个低位之前传输(对于8位操作,DB0为 DB3)。 在传输4位数据两次后,必须检查忙标志(一条指令)。二 更多的4位操作然后传输忙标志和地址计数器数据。
对于8位接口数据,使用所有8条总线(DB0至DB7)。
以下是有关其工作原理的示例:
#include <msp430g2553.h>
#define DR P1OUT = P1OUT | BIT4 // define RS high
#define CWR P1OUT = P1OUT & (~BIT4) // define RS low
#define READ P1OUT = P1OUT | BIT5 // define Read signal R/W = 1 for reading
#define WRITE P1OUT = P1OUT & (~BIT5) // define Write signal R/W = 0 for writing
#define ENABLE_HIGH P1OUT = P1OUT | BIT6 // define Enable high signal
#define ENABLE_LOW P1OUT = P1OUT & (~BIT6) // define Enable Low signal
void data_write(void)
{
ENABLE_HIGH;
delay(2);
ENABLE_LOW;
}
void data_read(void)
{
ENABLE_LOW;
delay(2);
ENABLE_HIGH;
}
void check_busy(void)
{
P1DIR &= ~(BIT3); // make P1.3 as input
while((P1IN&BIT3)==1)
{
data_read();
}
P1DIR |= BIT3; // make P1.3 as output
}
void send_command(unsigned char cmd)
{
check_busy();
WRITE;
CWR;
P1OUT = (P1OUT & 0xF0)|((cmd>>4) & 0x0F); // send higher nibble
data_write(); // give enable trigger
P1OUT = (P1OUT & 0xF0)|(cmd & 0x0F); // send lower nibble
data_write(); // give enable trigger
}
void send_data(unsigned char data)
{
check_busy();
WRITE;
DR;
P1OUT = (P1OUT & 0xF0)|((data>>4) & 0x0F); // send higher nibble
data_write(); // give enable trigger
P1OUT = (P1OUT & 0xF0)|(data & 0x0F); // send lower nibble
data_write(); // give enable trigger
}
void send_string(char s)
{
while(s)
{
send_data(*s);
s++;
}
}
void lcd_init(void)
{
P1DIR |= 0xFF;
P1OUT &= 0x00;
send_command(0x33);
send_command(0x32);
send_command(0x28); // 4 bit mode
send_command(0x0E); // clear the screen
send_command(0x01); // display on cursor on
send_command(0x06); // increment cursor
send_command(0x80); // row 1 column 1
}
#include “lcd.h”
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // stop watchdog timer
lcd_init();
send_string(“Hello”);
send_command(0xC0);
send_string(“World”);
while(1){}
}