//PD15-PD8 对应 LCD1602的D7-D0
//PC0 RS
//PC1 WR
//PC2 E
// GPIOD--- GPIOA
// GPIOE--- GPIOB
#include\"stm32f10x.h\"
//LCD1602 的命令 状态
#define Set_RS() GPIO_SetBits(GPIOC,GPIO_Pin_0); // 数据
#define Reset_RS() GPIO_ResetBits(GPIOC,GPIO_Pin_0); // 命令 状态
#define Set_RW() GPIO_SetBits(GPIOC,GPIO_Pin_1); // 读
#define Reset_RW() GPIO_ResetBits(GPIOC,GPIO_Pin_1);// 写
#define Set_E() GPIO_SetBits(GPIOC,GPIO_Pin_2); // 使能
#define Reset_E() GPIO_ResetBits(GPIOC,GPIO_Pin_2);// 失能
uint8_t Buffer1[]={\"FGasdkkk\
uint8_t Buffer2[]={\"CDABabcdefghijkl\
uint8_t Buffer[];
GPIO_InitTypeDef GPIO_InitStructure;
void RCC_Config(void);
void GPIO_Config(void);
void Busy_Wait(void);
void Write_Cmd(uint8_t Cmd);
void Write_Data(uint8_t Data);
void Write_String(uint8_t cmd,uint8_t* p);
void LCD1602_Init(void);
void Delay(uint32_t t);
//uint8_t Read_Data(void);
int main(void)
{
RCC_Config();
GPIO_Config();
LCD1602_Init();
Write_String(0x80,Buffer1);
Write_String(0xc0,Buffer2);
while(1);
}
void RCC_Config(void)
{
SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO,ENABLE);
}
void GPIO_Config(void)
{
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_All;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOD,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2;
GPIO_Init(GPIOC,&GPIO_InitStructure);
}
void Busy_Wait(void)
{
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_All;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOD,&GPIO_InitStructure);
Reset_RS();
Set_RW();
Reset_E();
//Delay(5);
Set_E();
// Delay(25);
while(GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_15)==1);
Reset_E();
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_All;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOD,&GPIO_InitStructure);
}
void Write_Cmd(uint8_t Cmd)
{
Busy_Wait();
Reset_RS();
Reset_RW();
Reset_E();
// Delay(5);
Set_E();
GPIO_Write(GPIOD,(0xff00&(Cmd<<8)));
/*
GPIO_WriteBit(GPIOE,GPIO_Pin_10,(BitAction)((Cmd&0x80)>>7));//D7
GPIO_WriteBit(GPIOE,GPIO_Pin_9,(BitAction)((Cmd&0x40)>>6)); //D6
GPIO_WriteBit(GPIOE,GPIO_Pin_8,(BitAction)((Cmd&0x20)>>5)); //D5
GPIO_WriteBit(GPIOE,GPIO_Pin_7,(BitAction)((Cmd&0x10)>>4)); //D4
GPIO_WriteBit(GPIOD,GPIO_Pin_1,(BitAction)((Cmd&0x08)>>3)); //D3
GPIO_WriteBit(GPIOD,GPIO_Pin_0,(BitAction)((Cmd&0x04)>>2)); //D2
GPIO_WriteBit(GPIOD,GPIO_Pin_15,(BitAction)((Cmd&0x02)>>1)); //D1
GPIO_WriteBit(GPIOD,GPIO_Pin_14,(BitAction)((Cmd&0x01))); //D0
*/
//Delay(25);
Reset_E();
}
void Write_Data(uint8_t Data)
{
Busy_Wait();
Set_RS();
Reset_RW();
Reset_E();
// Delay(5);
Set_E();
GPIO_Write(GPIOD,(0xff00&(Data<<8)));
/*
GPIO_WriteBit(GPIOE,GPIO_Pin_10,(BitAction)((Data&0x80)>>7));//D7
GPIO_WriteBit(GPIOE,GPIO_Pin_9,(BitAction)((Data&0x40)>>6)); //D6
GPIO_WriteBit(GPIOE,GPIO_Pin_8,(BitAction)((Data&0x20)>>5)); //D5
GPIO_WriteBit(GPIOE,GPIO_Pin_7,(BitAction)((Data&0x10)>>4)); //D4
GPIO_WriteBit(GPIOD,GPIO_Pin_1,(BitAction)((Data&0x08)>>3)); //D3
GPIO_WriteBit(GPIOD,GPIO_Pin_0,(BitAction)((Data&0x04)>>2)); //D2
GPIO_WriteBit(GPIOD,GPIO_Pin_15,(BitAction)((Data&0x02)>>1)); //D1
GPIO_WriteBit(GPIOD,GPIO_Pin_14,(BitAction)((Data&0x01))); //D0
*/
// Delay(25);
Reset_E();
}
void Write_String(uint8_t cmd,uint8_t* p)
{
//uint8_t i=0;
Write_Cmd(cmd);
while(*p!='\\0')
{
Write_Data(*p++);
// Buffer[i++]=Read_Data();
}
}
void LCD1602_Init(void)
{
Write_Cmd(0x38);
Write_Cmd(0x0c);
Write_Cmd(0x06);
Write_Cmd(0x01);
}
//14ns
void Delay(__IO uint32_t t)
{
while(t--);
}
/*
uint8_t Read_Data(void)
{
uint8_t Value;
Busy_Wait();
Reset_WR();
Reset_E();
Delay(5);
Set_E();
Value=GPIO_ReadOutputDataBit(GPIOE,GPIO_Pin_10)||GPIO_ReadOutputDataBit(GPIOE,GPIO_Pin_9)||GPIO_ReadOutputDataBit(GPIOE,GPIO_Pin_8)||GPIO_ReadOutputDataBit(GPIOE,GPIO_Pin_7)||
GPIO_ReadOutputDataBit(GPIOD,GPIO_Pin_1)||GPIO_ReadOutputDataBit(GPIOD,GPIO_Pin_0)||GPIO_ReadOutputDataBit(GPIOD,GPIO_Pin_15)||GPIO_ReadOutputDataBit(GPIOD,GPIO_Pin_14);
Delay(25);
Reset_E();
return Value;
}
*/
因篇幅问题不能全部显示,请点此查看更多更全内容