?? 1602._c
字號:
#include <iom16v.h>
#include <macros.h>
#define LCD_EN_PORT PORTB
#define LCD_RW_PORT PORTB
#define LCD_RS_PORT PORTB
#define LCD_DATA_PORT PORTB
#define LCD_DATA_DDR DDRB
#define LCD_DATA_PIN PINB
#define LCD_EN 0x80 //portd7 out
#define LCD_RS 0x40 //portc6 out
#define LCD_DATA 0xf0 //portb4/5/6/7 out
/*--------------------------------------------------------------------------------------------------
Public function prototypes
--------------------------------------------------------------------------------------------------*/
void LCD_init (void);
void LCD_en_write (void);
void LCD_write_char (unsigned command,unsigned data);
void LCD_set_xy (unsigned char x, unsigned char y);
void LCD_write_string (unsigned char X,unsigned char Y,unsigned char *s);
void delay_nus (unsigned int n);
void delay_nms (unsigned int n);
void LCD_init(void) //液晶初始化
{
DDRB|=LCD_DATA; // 數據為輸出
DDRB|=LCD_RS|LCD_EN; //置位RS.EN
delay_nms(15);
LCD_write_char(0x28,0); //4位顯示
LCD_write_char(0x0c,0); //顯示開
LCD_write_char(0x01,0); //清屏
}
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s)
{
LCD_set_xy( X, Y ); //寫地址
while (*s) // 寫顯示字符
{
LCD_write_char( 0, *s );
s ++;
}
}
void LCD_set_xy( unsigned char x, unsigned char y ) //寫地址函數
{
unsigned char address;
if (y == 0) address = 0x80 + x;
else
address = 0xc0 + x;
LCD_write_char( address, 0 );
}
void LCD_en_write(void) //液晶使能
{
LCD_EN_PORT|=LCD_EN;
delay_nus(1);
LCD_EN_PORT&=~LCD_EN;
}
void LCD_write_char(unsigned command,unsigned data) // 寫數據
{
unsigned command_temp,data_temp;
command_temp=command;
data_temp=data;
delay_nus(16);
if(command==0)
{
LCD_RS_PORT|=LCD_RS; //RS=1
LCD_DATA_PORT&=0X0f;
LCD_DATA_PORT|=data_temp&0xf0; //寫高四位
LCD_en_write();
data_temp=data_temp<<4;
LCD_DATA_PORT&=0X0f;
LCD_DATA_PORT|=data_temp&0xf0; //寫低四位
LCD_en_write();
}
else
{
LCD_RS_PORT&=~LCD_RS; //RS=0
LCD_DATA_PORT&=0X0f;
LCD_DATA_PORT|=command_temp&0xf0; //寫高四位
LCD_en_write();
command_temp=command_temp<<4;
LCD_DATA_PORT&=0x0f;
LCD_DATA_PORT|=command_temp&0xf0; //寫低四位
LCD_en_write();
}
}
/*void main(void)
{
DDRA|=LCD_DATA; // 數據為輸出
DDRC|=LCD_RS|LCD_EN; //置位RS.EN
LCD_init();
for(;;) //for循環
{
LCD_write_char(0x01,0);
delay_nms(2);
LCD_write_string(0,0,"Wellcome");
LCD_write_string(0,1,"www.OURAVR.com!");
delay_nms(5000);
LCD_write_string(0,0,"happy new year!");
LCD_write_string(0,1,"amork:everfriend");
delay_nms(5000);
}
}*/
/*-----------------------------------------------------------------------
延時函數
系統時鐘:8M
-----------------------------------------------------------------------*/
void delay_1us(void) //1us延時函數
{
asm("nop");
}
void delay_nus(unsigned int n) //N us延時函數
{
unsigned int i=0;
for (i=0;i<n;i++)
delay_1us();
}
void delay_1ms(void) //1ms延時函數
{
unsigned int i;
for (i=0;i<1140;i++);
}
void delay_nms(unsigned int n) //N ms延時函數
{
unsigned int i=0;
for (i=0;i<n;i++)
delay_1ms();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -