标题:
stm32上驱动1602液晶的代码(keil平台)
[打印本页]
作者:
panfeng881112
时间:
2010-12-13 19:17
标题:
stm32上驱动1602液晶的代码(keil平台)
本帖最后由 panfeng881112 于 2010-12-13 19:18 编辑
#define SET_LE GPIO_SetBits(GPIOD, GPIO_Pin_8)
#define CLE_LE GPIO_ResetBits(GPIOD, GPIO_Pin_8)
#define LCD_Read GPIO_SetBits(GPIOD, GPIO_Pin_9)
#define LCD_Write GPIO_ResetBits(GPIOD, GPIO_Pin_9)
#define SET_SC GPIO_SetBits(GPIOD, GPIO_Pin_10)
#define CLE_SC GPIO_ResetBits(GPIOD, GPIO_Pin_10)
/******************************************************/
void lcd_init( void ) //初始化
{
SysTickDelay(15);
Lcd_Write_Command( 0x38,0); //
SysTickDelay(5);
Lcd_Write_Command( 0x38,0); //
SysTickDelay(5);
Lcd_Write_Command( 0x38,0); //
Lcd_Write_Command( 0x38,1); //
Lcd_Write_Command( 0x08,1);
Lcd_Write_Command( 0x01,1); //
Lcd_Write_Command( 0x06,1); //
Lcd_Write_Command( 0x0d,1);
}
/******************************************************/
void Lcd_Write_Command(unsigned char x,char bz) //向液晶里面写入指令
{
if(bz) lcd_bz();
CLE_SC;
LCD_Write;
Delay(40); // delay>30us
SET_LE;
Delay(40); // delay>30us
Gpio_data(x);
Delay(20); // delay 20us
CLE_LE;
Delay(300); // delay>400us
}
/******************************************************/
void Lcd_Write_Data( char x) //向液晶里面写入数据
{
lcd_bz();
SET_SC;
LCD_Write;
Delay(40); // DELAY>30US
SET_LE;
Delay(40); // DELAY>40US
Gpio_data(x);
Delay(20); // 20US
CLE_LE;
Delay(300); //400US
}
/******************************************************/
void lcd_bz()//判断忙
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7; // DB0~DB7
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //50M时钟速度
GPIO_Init(GPIOD, &GPIO_InitStructure);
CLE_SC;
LCD_Read;
CLE_LE;
Delay(40); // delay>30us
SET_LE;
Delay(100); //delay>100us
while(GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_0));
CLE_LE;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7; // DB0~DB7
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //50M时钟速度
GPIO_Init(GPIOD, &GPIO_InitStructure);
}
/******************************************************/
void Gpio_data(unsigned char x)
{
GPIO_ResetBits(GPIOD, GPIO_Pin_7); //DB0
GPIO_ResetBits(GPIOD, GPIO_Pin_6); //DB1
GPIO_ResetBits(GPIOD, GPIO_Pin_5); //DB2
GPIO_ResetBits(GPIOD, GPIO_Pin_4); //DB3
GPIO_ResetBits(GPIOD, GPIO_Pin_3); //DB4
GPIO_ResetBits(GPIOD, GPIO_Pin_2); //DB5
GPIO_ResetBits(GPIOD, GPIO_Pin_1); //DB6
GPIO_ResetBits(GPIOD, GPIO_Pin_0); //DB7
if(x&0X01)GPIO_SetBits(GPIOD, GPIO_Pin_7);//DB0
if(x&0X02)GPIO_SetBits(GPIOD, GPIO_Pin_6);//DB1
if(x&0X04)GPIO_SetBits(GPIOD, GPIO_Pin_5);//DB2
if(x&0X08)GPIO_SetBits(GPIOD, GPIO_Pin_4);//DB3
if(x&0X10)GPIO_SetBits(GPIOD, GPIO_Pin_3);//DB4
if(x&0X20)GPIO_SetBits(GPIOD, GPIO_Pin_2);//DB5
if(x&0X40)GPIO_SetBits(GPIOD, GPIO_Pin_1);//DB6
if(x&0X80)GPIO_SetBits(GPIOD, GPIO_Pin_0);//DB7
}
/******************************************************/
void set_position(unsigned char x,unsigned char y) //字符初始位置设定,x表示列,y表示行
{
unsigned char addr;
if(y==0)
addr=0x80+x;
else if(y==1)
addr=0xC0+x;
Lcd_Write_Command(addr,1) ;
}
/******************************************************/
void write_string(unsigned char x,unsigned char y, unsigned char *string) //向1602写一个字符串
{
int i=0;
set_position(x,y);
while(string
!='\0')
{
Lcd_Write_Data(string
);
i++;
}
}
/******************************************************/
char wel1[]="
www.hymcu.com
";
int main(void)
{
ChipHalInit(); //片内硬件初始化
ChipOutHalInit(); //片外硬件初始化
lcd_init();
write_string(2,0,wel1);
write_string(3,1,wel1);
}
图片附件:
DSCN2966.JPG
(2010-12-13 19:17, 49.14 KB) / 下载次数 247
http://bbs.hymcu.com/attachment.php?aid=672&k=1fe4268b39ed9735eed90ecfaa8a205e&t=1732441738&sid=783R3l
作者:
th1019
时间:
2011-3-10 22:20
define那里没怎么看明白。大侠赐教
欢迎光临 珩源工控论坛 (http://bbs.hymcu.com/)
Powered by Discuz! 7.2