?? 液晶方向.txt
字號:
#include<iom16v.h> //包含定義ATmega16寄存器的頭文件
#include<macros.h> //包含一些常用的宏定義
#define LCD_EN_PORT PORTA
#define LCD_RW_PORT PORTA
#define LCD_RS_PORT PORTA
#define LCD_DATA_PORT PORTA
#define LCD_DATA_DDR DDRA
#define LCD_DATA_PIN PINA
#define LCD_EN (1<<PA2) //portc5 out
#define LCD_RW (1<<PA1) //portd3 out/in
#define LCD_RS (1<<PA0) //portc4 out
#define LCD_DATA ((1<<PA4)|(1<<PA5)|(1<<PA6)|(1<<PA7)) //portd4/5/6/7 out
unsigned char data[8]={0x04,0x0e,0x15,0x04,0x04,0x04,0x04,0x00};//↑
unsigned char data1[8]={0x04,0x04,0x04,0x04,0x15,0x0e,0x04,0x00};//↓
void delay_1us()
{
asm("nop");
}
/////////////////////////
void delay_nus(unsigned int n) //N us延時函數
{
unsigned int i=0;
for (i=0;i<n;i++)
asm("nop");
}
//////////////////////////
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();
}
////////////////////////////////////////
void LCD_en_write(void) //EN端產生一個高電平脈沖,寫LCD
{
LCD_EN_PORT|=LCD_EN;
delay_nus(1);
LCD_EN_PORT &= ~LCD_EN;
}
/////////////////////////////////////
void LCD_wait_Ready(void) //等待LCD空閑
{
LCD_DATA_DDR &= ~0x80; //PD7 I/O口方向設置為輸入
LCD_RW_PORT|=LCD_RW; //RW=1
LCD_RS_PORT&=~LCD_RS; //RS=0
LCD_EN_PORT |= LCD_EN; //EN=1
while (!( LCD_DATA_PIN&0x80 ) == 0); //RW=1,讀PD7,為0表示空閑?
LCD_EN_PORT &= ~LCD_EN; //EN=0
LCD_DATA_DDR |= 0xf0; //設置為輸出porta4/5/6/7
}
////////////////////////////////////
void LCD_write_char(unsigned char command,unsigned char data)
{ unsigned char command_temp,data_temp;
command_temp = command;
data_temp = data;
LCD_wait_Ready();
LCD_RW_PORT &= ~LCD_RW; //RW=0
if (command == 0)
{LCD_RS_PORT |= LCD_RS; //RS=1
LCD_DATA_PORT &= 0X0F;
LCD_DATA_PORT |= data_temp&0xf0; //send high 4bit
}
else
{
LCD_RS_PORT &= ~LCD_RS; //RS=0
LCD_DATA_PORT &= 0X0F;
LCD_DATA_PORT |= command_temp&0xf0;//send high 4bit
}
LCD_en_write();
command_temp=command_temp<< 4; //send low 4bit
data_temp=data_temp<<4;
LCD_DATA_PORT&=0X0F;
if (command==0)LCD_DATA_PORT|=data_temp&0xf0;
else
LCD_DATA_PORT|=command_temp&0xf0;
LCD_en_write();
LCD_RW_PORT|=LCD_RW;
LCD_RS_PORT^=LCD_RS;
delay_nus(1);
}
/////////////////////////////////////////////////////
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_write_string(unsigned char X,unsigned char Y,unsigned char *s)
{
LCD_set_xy( X, Y );
while (*s) {
LCD_write_char( 0, *s);
//delay_nus(70);
s ++;
}
}
/////////////////////////////////
void LCD_write_string1(unsigned char X,unsigned char Y,unsigned char dat)
{
LCD_set_xy( X, Y );
LCD_write_char(0,dat);
}
////////////////////////////////////////////////////////////////////////////////
void LCD_write_char2(unsigned char X,unsigned char Y,unsigned char dat) //列x=0~15,行y=0,1
{
LCD_set_xy( X, Y ); //寫地址
LCD_write_char(0,dat);
}
void my_signs(unsigned char ascii,unsigned char *z)//0x00
{
unsigned char address=0,i,temp=0; //ascii為與要定義的圖案對應的ascii碼值(0x00到
temp=(ascii&0x07)<<3; //0x007間任選)
for(i=0;i<8;i++)
{ address = 0x40 +temp+ i;
LCD_write_char( address,0);
delay_nus(1);
LCD_write_char(0,*(z++));
delay_nus(1);
}
}
//建立圖案
void LCD_init(void) //液晶初始化
{
DDRA|=LCD_DATA|LCD_RW;
DDRA|=LCD_RS|LCD_EN;
LCD_write_char(0x28,0); //4bit test
LCD_write_char(0x0c,0); //顯示開
my_signs(0x00,data);//↑ //建立圖案
my_signs(0x01,data1);//↓
LCD_write_char(0x01,0); //清屏
}
/////////////////////
void main(void)
{
LCD_init();
LCD_write_char(0x01,0); //顯示清屏
LCD_write_string(0,0," Freq: ");
LCD_write_string(0,1," Speed: . ");
LCD_write_char2(8,0,0x00);//↑
LCD_write_char2(0x0A,1,0x01);//↓
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -