|
- #include "MSP430F5529.h"
- #define uchar unsigned char
- #define uint unsigned int
- #define CPU_F ((double)8000000) //外部高频晶振8MHZ
- //#define CPU_F ((double)32768) //外部低频晶振32.768KHZ
- #define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0))
- #define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0))
- //12864/1602液晶控制管脚
- #define RS_CLR P6OUT &= ~BIT1 //RS置低
- #define RS_SET P6OUT |= BIT1 //RS置高
- #define RW_CLR P6OUT &= ~BIT2 //RW置低
- #define RW_SET P6OUT |= BIT2 //RW置高
- #define EN_CLR P6OUT &= ~BIT3 //E置低
- #define EN_SET P6OUT |= BIT3 //E置高
- //彩屏/12864液晶/1602液晶的数据口,三液晶共用
- #define DataDIR P3DIR //数据口方向
- #define DataPort P3OUT //P4口为数据口
- void clock_init()
- {
- UCSCTL3 = SELREF_2; // Set DCO FLL reference = REFO
- UCSCTL4 |= SELA_2; // Set ACLK = REFO
- UCSCTL0 = 0x0000; // Set lowest possible DCOx, MODx
- // Loop until XT1,XT2 & DCO stabilizes - In this case only DCO has to stabilize
- do
- {
- UCSCTL7 &= ~(XT2OFFG + XT1LFOFFG + DCOFFG);
- // Clear XT2,XT1,DCO fault flags
- SFRIFG1 &= ~OFIFG; // Clear fault flags
- }while (SFRIFG1&OFIFG); // Test oscillator fault flag
-
- __bis_SR_register(SCG0); // Disable the FLL control loop
- UCSCTL1 = DCORSEL_5; // Select DCO range 16MHz operation
- UCSCTL2 |= 249; // Set DCO Multiplier for 8MHz
- // (N + 1) * FLLRef = Fdco
- // (249 + 1) * 32768 = 8MHz
- __bic_SR_register(SCG0); // Enable the FLL control loop
- // Worst-case settling time for the DCO when the DCO range bits have been
- // changed is n x 32 x 32 x f_MCLK / f_FLL_reference. See UCS chapter in 5xx
- // UG for optimization.
- // 32 x 32 x 8 MHz / 32,768 Hz = 250000 = MCLK cycles for DCO to settle
- __delay_cycles(250000);
- }
- //*************************************************************************
- // 初始化IO口子程序
- //*************************************************************************
- void Port_init()
- {
- P1DIR|=BIT0;
- P1OUT|=BIT0;
- P3SEL = 0x00;
- P3DIR = 0xFF; //数据口输出模式
- P6SEL = 0x00;
- P6DIR|= BIT1 + BIT2 + BIT3; //控制口设置为输出模式
- }
- //***********************************************************************
- // 显示屏命令写入函数
- //***********************************************************************
- void LCD_write_com(unsigned char com)
- {
- RS_CLR;
- RW_CLR;
- EN_SET;
- DataPort = com; //命令写入端口
- delay_ms(5);
- EN_CLR;
- }
- //***********************************************************************
- // 显示屏数据写入函数
- //***********************************************************************
- void LCD_write_data(unsigned char data)
- {
- RS_SET;
- RW_CLR;
- EN_SET;
- DataPort = data; //数据写入端口
- delay_ms(5);
- EN_CLR;
- }
- //***********************************************************************
- // 显示屏清空显示
- //***********************************************************************
- void LCD_clear(void)
- {
- LCD_write_com(0x01); //清屏幕显示
- delay_ms(5);
- }
- //***********************************************************************
- // 显示屏字符串写入函数
- //***********************************************************************
- void LCD_write_str(unsigned char x,unsigned char y,unsigned char *s)
- {
-
- if (y == 0)
- {
- LCD_write_com(0x80 + x); //第一行显示
- }
- else
- {
- LCD_write_com(0xC0 + x); //第二行显示
- }
-
- while (*s)
- {
- LCD_write_data( *s);
- s ++;
- }
- }
- //***********************************************************************
- // 显示屏单字符写入函数
- //***********************************************************************
- void LCD_write_char(unsigned char x,unsigned char y,unsigned char data)
- {
-
- if (y == 0)
- {
- LCD_write_com(0x80 + x); //第一行显示
- }
- else
- {
- LCD_write_com(0xC0 + x); //第二行显示
- }
-
- LCD_write_data( data);
- }
- //***********************************************************************
- // 显示屏初始化函数
- //***********************************************************************
- void LCD_init(void)
- {
- LCD_write_com(0x38); //显示模式设置
- delay_ms(5);
- LCD_write_com(0x08); //显示关闭
- delay_ms(5);
- LCD_write_com(0x01); //显示清屏
- delay_ms(5);
- LCD_write_com(0x06); //显示光标移动设置
- delay_ms(5);
- LCD_write_com(0x0C); //显示开及光标设置
- delay_ms(5);
- }
- void main( void )
- {
- // Stop watchdog timer to prevent time out reset
- WDTCTL = WDTPW + WDTHOLD;
- clock_init();
- uchar i,*p;
- Port_init(); //系统初始化,设置IO口属性
- delay_ms(100); //延时100ms
- LCD_init(); //液晶参数初始化设置
- LCD_clear(); //清屏
- while (1)
- {
- i = 1;
- p = "xxxxxxxx"; //字符串输出显示
- LCD_clear();
- LCD_write_str(0,0,"Welcome.DM430-L");
- delay_ms(250);
-
- while (*p) {
- LCD_write_char(i,1,*p); //单个字符输出显示
- i ++;
- p ++;
- delay_ms(100); //延时250ms
- }
- delay_ms(250);
- P1OUT^=BIT0;
- }
- }
复制代码
|
-
-
1602.zip
423.5 KB, 下载次数: 28, 下载积分: 黑币 -5
评分
-
查看全部评分
|