[英]Initialising LCD display with PIC16F1829 in C?
我正在尝试使用PIC16F1829初始化LCD屏幕。 我遵循了LCD给出的流程图,但是没有任何效果。 请在下面查看我的代码,如果您知道我要去哪里了,请告诉我。 我将稍后使用send_command()函数等对其进行优化。 。 。 目前,我只想确保它将初始化。
//config bits that are part-specific for the PIC16F1829
__CONFIG(FOSC_INTOSC & WDTE_OFF & PWRTE_OFF & MCLRE_OFF & CP_OFF & CPD_OFF & BOREN_ON & CLKOUTEN_OFF & IESO_OFF & FCMEN_OFF);
__CONFIG(WRT_OFF & PLLEN_OFF & STVREN_OFF & LVP_OFF);
/*Function Prototypes*/
void init_disp(void);
/*Define outputs*/
#define _XTAL_FREQ 500000
#define RS LATCbits.LATC0
#define EN LATCbits.LATC1
#define RW LATCbits.LATC2
#define DB1 LATCbits.LATC4
#define DB2 LATCbits.LATC3
#define DB3 LATCbits.LATC6
#define DB4 LATCbits.LATC7
#define TRIG ((EN=1),(EN=0))
void main(void) {
TRISC=0; //set PORTC to output
EN = 0;
__delay_ms(50);
init_disp();
}
void init_disp(void){
//function set 1
RW = 0;
RS = 0;
DB1 = 1; //Hello LCD
DB2 = 1;
DB3 = 0;
DB4 = 0;
TRIG;
//function set 2
__delay_ms(5);
TRIG;
//function set 3
__delay_ms(2);
TRIG;
// 4bit mode
__delay_ms(2);
DB1 = 0;
DB2 = 1;
DB3 = 0;
DB4 = 0;
TRIG;
//Display lines and font MS Nibble
__delay_ms(2);
DB1 = 0;
DB2 = 1;
DB3 = 0;
DB4 = 0;
TRIG;
//Display lines and font LS Nibble
DB1 = 0;
DB2 = 0;
DB3 = 1;
DB4 = 1;
TRIG;
//Display off MS Nibble
__delay_ms(1);
DB1 = 0;
DB2 = 0;
DB3 = 0;
DB4 = 0;
TRIG;
//Display off LS Nibble
DB1 = 0;
DB2 = 0;
DB3 = 0;
DB4 = 1;
TRIG;
//Display Clear MS Nibble
__delay_ms(5);
DB1 = 0;
DB2 = 0;
DB3 = 0;
DB4 = 0;
TRIG;
//display clear LS Nibble
DB1 = 1;
DB2 = 0;
DB3 = 0;
DB4 = 0;
TRIG;
//Entry Mode MS Nibble
__delay_ms(1);
DB1 = 0;
DB2 = 0;
DB3 = 0;
DB4 = 0;
TRIG;
//entry mode LS Nibble
DB1 = 0;
DB2 = 1;
DB3 = 1;
DB4 = 0;
TRIG;
}
默认情况下, PIC
所有引脚均为模拟输入。 在这里,您将PORTC
用作数字输出。 因此,您需要如下初始化PORTC
PORTC = 0x00; // Init PORTC
LATC = 0x00; // Data Latch
ANSELC = 0x00; // This will enable PORTC as digital
TRISC = 0x00; // Output Pins
希望这对您有用。 您需要参考PIC16F1829的数据表 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.