ATmega128(lcd1602)
//編譯環(huán)境 AVR Studio 4.17/AVR GCC
//系統(tǒng)時鐘7.3728MHZ,設置熔絲位為外部高頻石英晶體振蕩,啟動時間4.1ms
//***********************************************************************
//包含文件
//***********************************************************************
#include
#include
#define F_CPU7372800/* 單片機主頻為7.3728MHz,用于延時子程序 */
#include
#include <avr/io.h>
#include
//***********************************************************************
//定義變量區(qū)
//***********************************************************************
#define uchar unsigned char
#define uint unsigned int
#define delay_us(x) _delay_us(x) //AVR GCC延時函數(shù) x(us)
#define delay_ms(x) _delay_ms(x) //AVR GCC延時函數(shù) x(ms)
#define RS_CLRPORTF &= ~(1 << PF1) //RS置低
#define RS_SETPORTF |= (1 << PF1) //RS置高
#define RW_CLRPORTF &= ~(1 << PF2) //RW置低
#define RW_SETPORTF |= (1 << PF2) //RW置高
#define EN_CLRPORTF &= ~(1 << PF3) //E置低
#define EN_SETPORTF |= (1 << PF3) //E置高
#define Data_IO PORTA //液晶數(shù)據(jù)口
#define Data_DDR DDRA //數(shù)據(jù)口方向寄存器
#define D_LE0 PORTD &= ~(1 << PD4) //數(shù)碼管段控制位為0,鎖存端口數(shù)據(jù)
#define D_LE1 PORTD |= (1 << PD4) //數(shù)碼管段控制位為1,鎖存器輸出與端口一致
#define W_LE0 PORTD &= ~(1 << PD5) //數(shù)碼管位控制位為0
#define W_LE1 PORTD |= (1 << PD5) //數(shù)碼管位控制位為1
//*************************************************************************
//初始化子程序
//*************************************************************************
void system_init()
{
Data_IO = 0xFF; //電平設置
Data_DDR = 0xFF; //方向輸出
PORTF = 0xFF; //電平設置
DDRF = 0xFF; //方向輸出
PORTD=0xFF;
DDRD=0xFF;
D_LE1; //關掉數(shù)碼管,以免顯示亂碼
W_LE1;
Data_IO=0xFF; //關數(shù)碼管
W_LE0;
}
//***********************************************************************
//顯示屏命令寫入函數(shù)
//***********************************************************************
void LCD_write_com(unsigned char com)
{
RS_CLR;
RW_CLR;
EN_SET;
Data_IO = com;
delay_ms(5);
EN_CLR;
}
//***********************************************************************
//顯示屏數(shù)據(jù)寫入函數(shù)
//***********************************************************************
void LCD_write_data(unsigned char data)
{
RS_SET;
RW_CLR;
EN_SET;
Data_IO = data;
delay_ms(5);
EN_CLR;
}
//***********************************************************************
//顯示屏清空顯示
//***********************************************************************
void LCD_clear(void)
{
LCD_write_com(0x01);
delay_ms(5);
}
//***********************************************************************
//顯示屏字符串寫入函數(shù)
//***********************************************************************
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 ++;
}
}
//***********************************************************************
//顯示屏單字符寫入函數(shù)
//***********************************************************************
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);
}
//***********************************************************************
//顯示屏初始化函數(shù)
//***********************************************************************
void LCD_init(void)
{
DDRA = 0xFF; //I/O口方向設置
DDRA |= (1 << PA5) | (1 << PA6) | (1 << PA7);
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)
{
unsigned char i;
unsigned char *p;
system_init(); //系統(tǒng)初始化,設置IO口屬性
delay_ms(100); //延時100ms
LCD_init(); //液晶參數(shù)初始化設置
while (1)
{
i = 1;
p = "www.hcit.edu.com"; //字符串輸出顯示
LCD_clear();
LCD_write_str(0,0,"Welcome");
delay_ms(250);
while (*p) {
LCD_write_char(i,1,*p); //單個字符輸出顯示
i ++;
p ++;
delay_ms(250); //延時250ms
}
delay_ms(250);
}
}
評論