LCD1602.C文件
- #include "LCD1602.h"
- #include "delay.h"
- #include "sys.h"
- //8位数按位逆序
- u8 BitReversed(u8 x)
- {
- u8 temp[8];
- u8 i = 0;
- u8 j = 7;
- u8 result = 0;
- for(i = 0;i<8;i++)
- {
- temp[i] = (x>>i)&0x01;
- }
- for(i = 0;i<8;i++)
- {
- result = result|(temp[j]<<(i));
- j--;
- }
- return result;
-
- }
- void LCD_Check_Busy(void)
-
- {
- LCD1602_RS0; //RS????
- delay_us(1000);
- LCD1602_RW1; //RW????
- delay_us(1000);
- LCD1602_EN0;//EN????
- delay_us(1000);
- GPIO_Write(GPIOC,0Xff);
- delay_ms(1000);
- LCD1602_EN1; //EN????
- delay_us(1000);
- }
- /*------------------------------------------------
- 写命令
- ------------------------------------------------*/
- void LCD_Write_Com(unsigned char com)
- { //????
- LCD1602_RS0; //RS???????
- delay_us(1000);
- LCD1602_RW0;
- delay_us(1000); //RW????
- LCD1602_EN1; //EN????
- delay_us(1000);
- com = BitReversed(com);
- GPIO_Write(GPIOA,com&0x00ff);//|(GPIO_ReadOutputData(GPIOA)&0XFF00)); //????????
- delay_us(1000);
- LCD1602_EN0;
- }
- /*------------------------------------------------
- 写数据
- ------------------------------------------------*/
- void LCD_Write_Data(unsigned char Data)
- { //????
- LCD1602_RS1; //RS?????????
- delay_us(1000);
- LCD1602_RW0;
- delay_us(1000);
- LCD1602_EN1; //EN????
- delay_us(1000);
- Data = BitReversed(Data);
- GPIO_Write(GPIOA,Data&0x00ff);//|(GPIO_ReadOutputData(GPIOA)&0XFF00));
- delay_us(1000);
- LCD1602_EN0;
- }
- /*------------------------------------------------
- 写字符串
- ------------------------------------------------*/
- void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s)
- {
- if (y == 0)
- {
- LCD_Write_Com(0x80 + x); delay_ms(5);
- }
- else
- {
- LCD_Write_Com(0xC0 + x); delay_ms(5);
- }
- while (*s)
- {
- LCD_Write_Data( *s); delay_ms(5);
- s ++;
- }
- }
- /*------------------------------------------------
- 写字符
- ------------------------------------------------*/
- void LCD_Write_Char(unsigned char x,unsigned char y,unsigned char Data)
- {
- if (y == 0)
- {
- LCD_Write_Com(0x80 + x); delay_ms(5);
- }
- else
- {
- LCD_Write_Com(0xC0 + x); delay_ms(5);
- }
- LCD_Write_Data( Data);
- delay_ms(5);
- }
-
-
- void LCD1602_write_long(unsigned char x,unsigned char y,u32 data,unsigned char num)
- {
- unsigned char temp[12],i = 12;
- while(i--)
- {
- temp[i] = ' ';
- }
- temp[num] = '\0';
- while(num--)
- {
- if(data || data%10)
- temp[num] = data % 10 + 0x30;
- data = data/10;
- }
- LCD_Write_String(x,y,temp); delay_ms(5);
- }
- /*------------------------------------------------
- 清屏
- ------------------------------------------------*/
- void LCD_Clear(void)
- {
- LCD_Write_Com(0x01);
- delay_ms(5);
- }
- /*------------------------------------------------*/
- void LCD_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC, ENABLE);
- 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;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 |GPIO_Pin_14|GPIO_Pin_15;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOC, &GPIO_InitStructure); //GPIOC
-
- delay_ms(15);
- LCD_Write_Com(0x38);
- delay_ms(5);
- LCD_Write_Com(0x38);
- delay_ms(5);
- LCD_Write_Com(0x38);
- //delay_ms(5);
-
- LCD_Write_Com(0x38);
-
- LCD_Write_Com(0x0C); /*ÏÔ꾿a¼°1a±êéèÖÃ*/
- delay_ms(5);
- LCD_Write_Com(0x06); /*ÏÔê¾1a±êòƶˉéèÖÃ*/
- delay_ms(5);
- //LCD_Write_Com(0x08); /*ÏÔê¾1رÕ*/
- //delay_ms(5);
- LCD_Write_Com(0x01); /*ÏÔê¾ÇåÆá*/
- delay_ms(5);
-
- //LCD_Write_Com(0x80);
- //delay_ms(5);
-
- }
- 主函数
- int main(void)
- {
- u16 test=0;
- u8 temperature=0;
- u8 humidity=0;
-
- delay_init();
- NVIC_Configuration();
- delay_ms(400);
- LCD_Init();
- USART3_Init(2400);
- DHT11_Init();
- delay_ms(100);
- LCD_Write_String(0,0,"Pm2.5: 000ug/m3");delay_ms(5);
- LCD_Write_String(0,1,"Tem:00C Hum:00%");delay_ms(5);
- while(1)
- {
- if(test++ > 200)
- {
- //LCD_Write_Com(0x80);
- test = 0;
- Get_PM();
- if(PM25_Value > 999)PM25_Value=999;
-
- LCD_Write_Char(7,0,PM25_Value/100+0x30);
- LCD_Write_Char(8,0,PM25_Value%100/10+0x30);
- LCD_Write_Char(9,0,PM25_Value%10+0x30);
-
- //LCD_Write_Com(0xC0);
- DHT11_Read_Data(&temperature,&humidity);
- LCD_Write_Char(4,1,temperature%100/10+0x30);
- LCD_Write_Char(5,1,temperature%10+0x30);
- LCD_Write_Char(13,1,humidity%100/10+0x30);
- LCD_Write_Char(14,1,humidity%10+0x30);
- }
- delay_ms(5);
- }
- }
复制代码
|