#include "lm3sxxxx.h" #define LCD_1602_DATA GPIO_PORTB_BASE #define LCD_1602_PORT GPIO_PORTF_BASE #define LCD_1602_EN GPIO_PIN_0 #define LCD_1602_RW GPIO_PIN_1 #define LCD_1602_RS GPIO_PIN_2 void delay(unsigned long ulVal) { ulVal=SysCtlClockGet()/4000; while(--ulVal!=0); } unsigned char LCD_check_busy(void) { unsigned char result; GPIOPinWrite(LCD_1602_PORT,LCD_1602_RS,0x00);//RS=0 GPIOPinWrite(LCD_1602_PORT,LCD_1602_RW,0x02);//RW=1 GPIOPinWrite(LCD_1602_PORT,LCD_1602_EN,0x04);//EN=1 GPIOPinTypeGPIOInput(LCD_1602_DATA,0XFF);//设置数据口为输入模式 result=(GPIOPinRead(LCD_1602_DATA,0x08)&0x08); GPIOPinTypeGPIOOutput(LCD_1602_DATA,0XFF);//从新设置数据口为输出模式 GPIOPinWrite(LCD_1602_PORT,LCD_1602_EN,0x00);//EN=0 return result; } void lcd_write(unsigned char cd,unsigned char temp) { while(LCD_check_busy()); if(cd) //当写数据使RS=1,当写指令使RS=0; GPIOPinWrite(LCD_1602_PORT,LCD_1602_RS,0x01);//RS=1 else GPIOPinWrite(LCD_1602_PORT,LCD_1602_RS,0x00);//RS=0 GPIOPinWrite(LCD_1602_PORT,LCD_1602_RW,0x00);//RW=0 GPIOPinWrite(LCD_1602_PORT,LCD_1602_EN,0x00);//EN=0 delay(1); GPIOPinWrite(LCD_1602_DATA,0XFF,temp); //写数据到LCD GPIOPinWrite(LCD_1602_PORT,LCD_1602_EN,0x04);//EN=1 delay(2); GPIOPinWrite(LCD_1602_PORT,LCD_1602_EN,0x00);//EN=0 } //定位置 void lcd_pos(unsigned char x,unsigned char y) { if(x<16) { if(y==0) x=0x80+x; else x=0xc0+x; lcd_write(0,x); } } //LCD初始化 void lcd_init(void) { SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);//使能数据口 SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);//使能控制口 GPIOPinTypeGPIOOutput(LCD_1602_PORT,LCD_1602_RS|LCD_1602_RW|LCD_1602_EN);//设置控制口为输出模式 GPIOPinTypeGPIOOutput(LCD_1602_DATA,0XFF);//设置数据口为输出模式 lcd_write(0,0x38); delay(1); lcd_write(0,0x38); delay(1); lcd_write(0,0x38); delay(1); lcd_write(0,0x0C); delay(1); lcd_write(0,0x06); delay(1); // lcd_write(0,0x18); // delay(1); lcd_write(0,0x01); // delay(1); } //写字符串到LCD void lcd_strwdat(unsigned char x,unsigned char y,unsigned char *str) { lcd_pos(x,y); while(*str!='\0') { lcd_write(1,*str); str++; } } int main() { //clockInit();//初始化晶振 lcd_init(); lcd_pos(0,0); lcd_strwdat(0,0,"hello"); lcd_strwdat(0,1,"world"); while(1); }