ไม่ทราบว่าผมเขียนในการสั่งออก LCD ผิดตรงไหนป่าว
คือมันไม่แสดงข้อความตามนั้นเลย
/***************************************/
/* LCD */
/* LCD Routines for "ET-ARM7 KIT V1.0" */
/* Character 16x2 4-Bit Mode Interface */
/***************************************/
#include <LPC213x.H>
#include <stdio.h>
/*************************/
/* Define LCD PinIO Mask */
/*************************/
#define LCD_D4 (1<<28) // P1.28
#define LCD_D5 (1<<29) // P1.29
#define LCD_D6 (1<<30) // P1.30
#define LCD_D7 (1<<31) // P1.31
#define LCD_EN (1<<27) // P1.27
#define LCD_RS (1<<25) // P1.25
#define LCD_DATA (LCD_D4|LCD_D5|LCD_D6|LCD_D7)
#define LCD_IOALL (LCD_D4|LCD_D5|LCD_D6|LCD_D7|LCD_EN|LCD_RS)
#define lcd_rs_set() IOSET1 |= LCD_RS // RS = 1 (Select Instruction)
#define lcd_rs_clr() IOCLR1 |= LCD_RS // RS = 0 (Select Data)
#define lcd_en_set() IOSET1 |= LCD_EN // EN = 1 (Enable)
#define lcd_en_clr() IOCLR1 |= LCD_EN // EN = 0 (Disable)
#define lcd_clear() lcd_write_control(0x01) // Clear Display
#define lcd_cursor_home() lcd_write_control(0x02) // Set Cursor = 0
#define lcd_display_on() lcd_write_control(0x0E) // LCD Display Enable
#define lcd_display_off() lcd_write_control(0x08) // LCD Display Disable
#define lcd_cursor_blink() lcd_write_control(0x0F) // Set Cursor = Blink
#define lcd_cursor_on() lcd_write_control(0x0E) // Enable LCD Cursor
#define lcd_cursor_off() lcd_write_control(0x0C) // Disable LCD Cursor
#define lcd_cursor_left() lcd_write_control(0x10) // Shift Left Cursor
#define lcd_cursor_right() lcd_write_control(0x14) // Shift Right Cursor
#define lcd_display_sleft() lcd_write_control(0x18) // Shift Left Display
#define lcd_display_sright() lcd_write_control(0x1C) // Shift Right Display
/********************/
/* Declare Function */
/********************/
void lcd_init(); // Initial LCD
void lcd_wait(); // Wait Busy LCD Complete
void lcd_out_data4(unsigned char); // Strobe 4-Bit Data to LCD
void lcd_write_byte(unsigned char); // Write 1 Byte Data to LCD
void lcd_write_control(unsigned char); // Write Instruction
void lcd_write_ascii(unsigned char); // Write LCD Display(ASCII)
void goto_cursor(unsigned char); // Set Position Cursor LCD
void lcd_print(unsigned char*); // Print Display to LCD
void delay(unsigned long int); // Delay Function
/*****************/
/* Main Function */
/*****************/
int main(void)
{
PINSEL2 = 0x00000000;
IODIR1 |= 0xFA000000; // P1.25 - P1.31 = Output
lcd_init();
while(1)
{
goto_cursor(0x00); // ROW 1
//0123456789ABCDEF
lcd_print("** My Project **");
goto_cursor(0x40); // ROW 2
//0123456789ABCDEF
lcd_print("** TEST LCD **"); }
}
/**************************/
/* Wait Bysy LCD Complete */
/**************************/
void lcd_wait()
{
int loop=500; // Busy Delay Time
while(loop--); // Busy Loop
}
/****************************/
/* Strobe 4-Bit Data to LCD */
/****************************/
void lcd_out_data4(unsigned char val)
{
IOCLR1 |= (LCD_DATA); // Reset 4-Bit Pin Data
IOSET1 |= (val<<28); // DDDD EN,0,RS,0 0000 0000 0000 0000 0000 0000
}
/****************************/
/* Write Data 1 Byte to LCD */
/****************************/
void lcd_write_byte(unsigned char val)
{
lcd_out_data4((val>>4)&0x0F); // Strobe 4-Bit High-Nibble to LCD
lcd_en_set(); // EN = 1 = Strobe Signal
lcd_en_clr(); // EN = 0
lcd_wait(); // Wait LCD Execute Complete
lcd_out_data4((val)&0x0F); // Strobe 4-Bit Low-Nibble to LCD
lcd_en_set(); // EN = 1 = Strobe Signal
lcd_en_clr(); // EN = 0
lcd_wait(); // Wait LCD Execute Complete
}
/****************************/
/* Write Instruction to LCD */
/****************************/
void lcd_write_control(unsigned char val)
{
lcd_rs_clr(); // RS = 0 = Instruction Select
lcd_write_byte(val); // Strobe Command Byte
delay(50000); // Approx. 2mS Delay
}
/****************************/
/* Write Data(ASCII) to LCD */
/****************************/
void lcd_write_ascii(unsigned char c)
{
lcd_rs_set(); // RS = 1 = Data Select
lcd_write_byte(c); // Strobe 1 Byte to LCD
}
/*******************************/
/* Initial 4-Bit LCD Interface */
/*******************************/
void lcd_init()
{
lcd_rs_clr(); // RS = 0 = Instruction Select
lcd_en_clr(); // EN = 0
delay(50000); // wait VDD raise > 4.5V
lcd_write_control(0x33); // Initial (Set DL=1 3 Time, Reset DL=0 1 Time)
lcd_write_control(0x32);
lcd_write_control(0x28); // Function Set (DL=0 4-Bit,N=1 2 Line,F=0 5X7)
lcd_write_control(0x0C); // Display on/off Control (Entry Display,Cursor off,Cursor not Blink)
lcd_write_control(0x06); // Entry Mode Set (I/D=1 Increment,S=0 Cursor Shift)
lcd_write_control(0x01); // Clear Display (Clear Display,Set DD RAM Address=0)
}
/***************************/
/* Set LCD Position Cursor */
/***************************/
void goto_cursor(unsigned char i)
{
i |= 0x80; // Set DD-RAM Address Command
lcd_write_control(i);
}
/************************************/
/* Print Display Data(ASCII) to LCD */
/************************************/
void lcd_print(unsigned char* str)
{
int i;
for (i=0;i<16 && str[i]!=0;i++) // 16 Character Print
{
lcd_write_ascii(str[i]); // Print Byte to LCD
}
}
/***********************/
/* Delay Time Function */
/* 1-4294967296 */
/***********************/
void delay(unsigned long int count1)
{
while(count1 > 0) {count1--;} // Loop Decrease Counter
}