Chương trình gửi ký tự ra 2x16 LCD dùng CCS C

Chương trình gửi ký tự ra 2x16 LCD dùng CCS C

#include "16F877A.h" // PIC16F877A header file

#use delay(clock=4000000) // for 4Mhz crystal

#fuses XT, NOWDT, NOPROTECT, NOLVP // for debug mode

#define WRITE_DATA 0

#define WRITE_COMMAND 1

#define NCHAR_PER_LINE 16 // max char numbers per line

#define MS10 10 // 10 milliseconds

#define US400 400 // 400 microseconds

#define LCD_RS PIN_A1

#define LCD_RW PIN_A2

#define LCD_E PIN_A3

///////////////////////////////////////////////////////////

//

/* private */ void lcd_write(byte dat, int1 option) {

delay_us(US400);

if (option == WRITE_DATA)

output_high(LCD_RS);

else // option == WRITE_COMMAND

output_low(LCD_RS);

output_low(LCD_RW);

output_b(dat);

output_high(LCD_E);

delay_us(US400);

output_low(LCD_E);

}

///////////////////////////////////////////////////////////

//

void lcd_init(void) {

output_low(LCD_E); // Let LCD E line low

lcd_write(0x38, WRITE_COMMAND); // Set LCD 16x2, 5x7, 8bits data

delay_ms(15);

lcd_write(0x01, WRITE_COMMAND); // Clear LCD display

delay_ms(MS10);

lcd_write(0x0f, WRITE_COMMAND); // Open display & current

delay_ms(MS10);

lcd_write(0x06, WRITE_COMMAND); // Window fixed (Character Entry Mode?)

delay_ms(MS10);

}

///////////////////////////////////////////////////////////

//

void lcd_display_char(int8 line, int8 pos, char ch) {

line = (line == 0) ? 0 : 1;

pos = (pos > NCHAR_PER_LINE) ? NCHAR_PER_LINE : pos;

lcd_write(0x80 + 0x40 * line + pos, WRITE_COMMAND);

lcd_write(ch, WRITE_DATA);

}

///////////////////////////////////////////////////////////

void lcd_display_str(int8 line, char str[], int8 nchars) {

int8 i;

for (i = 0; i < nchars; i++)

lcd_display_char(line, i, str[i]);

}

///////////////////////////////////////////////////////////

/**

* Display characters to a 2x16 LCD

*

* (1) LCD1 to GND

* (2) LCD2 to VDD 5 volts

* (3) LCD4 (RS) - LCD5 (RW) - LCD6 (E) to A1, A2, A3

* (4) LCD7-LCD14 to B0-B7 (bus data)

*

* Ref: http://pic16.com/bbs/dispbbs.asp?boa...ID=5879&page=1

*/

void main(void) {

int8 i;

char LINE1[] = { "SGN Tech" };

char LINE2[] = { "Xin chao" };

lcd_init();

// use of lcd_display_char()

for (i = 0; i < 8; i++)

lcd_display_char(0, i, LINE1[i]);

// use of lcd_display_str

lcd_display_str(1, LINE2, 8);

}

Bạn đang đọc truyện trên: AzTruyen.Top

Tags: