/***************************************************************************//** * @brief Initial the LCD1602 ******************************************************************************/ void Lcd_Init(void) { char const *p; int i; /* Enable clock for peripheral */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); Delay(1500000); GPIO_WriteBit(GPIOC, GPIO_Pin_8, Bit_SET); /* Set all pins for LCD as outputs */ LCD_ALL_DIR_OUT(); Delay(1500000); GPIO_WriteBit(GPIOA, RS, Bit_RESET); Lcd_Write_4bits(0x3); /* Select 4-bit interface */ Delay(410000); Lcd_Write_4bits(0x3); Delay(10000); Lcd_Write_4bits(0x3); Lcd_Write_4bits(0x2); Lcd_Write_Command(0x28); /* 2 lines, 5x8 character matrix */ Lcd_Write_Command(0x0C); /* Display ctrl:Disp=ON,Curs/Blnk=OFF */ Lcd_Write_Command(0x06); /* Entry mode: Move right, no shift */ /* Load user-specific characters into CGRAM */ Lcd_Write_Command(0x40); /* Set CGRAM address counter to 0 */ p = &UserFont[0][0]; for (i = 0; i < sizeof(UserFont); i++, p++) lcd_print (*p); Lcd_Write_Command(0x80); /* Set DDRAM address counter to 0 */ GPIO_WriteBit(GPIOC, GPIO_Pin_8, Bit_RESET); }
static void Lcd_Cursor(uint8_t column, uint8_t line){ uint8_t position = LCD_2X16_DDRAM_ADDR; if (line > 0) { position |= LCD_2X16_NEXT_LINE; } Lcd_Write_Command(position | column); }
/******************************************************************************* * @brief : Set cursor position on LCD display * @param[in] column : Column position * @param[in] line : Line position *******************************************************************************/ void set_cursor(int column, int line) { unsigned char address; address = (line * 40) + column; address = 0x80 + (address & 0x7F); Lcd_Write_Command(address); /* Set DDRAM address counter to 0 */ }
void Lcd_SetXY(unsigned char x,unsigned char y) //字符初始位置设定,x表示列,y表示行 { unsigned char addr; //D7为位置使能位,D6控制行数 if(y==0) addr=0x80+x; else if(y==1) addr=0xC0+x; Lcd_Write_Command(addr,1) ; }
static void Lcd_Init(void){ lsObjectInit(&myLCD); Lcd_Pin_Dir(); chThdSleepMilliseconds(500); palWritePort(LCD_PORT_CRTL,0x00); palWritePort(LCD_PORT_DATA,0x00); palSetPad(LCD_PORT_DATA,LCD_PIN_D5); palSetPad(LCD_PORT_CRTL,LCD_PIN_EN); chThdSleepMilliseconds(40); palClearPad(LCD_PORT_CRTL,LCD_PIN_EN); chThdSleepMilliseconds(40); Lcd_Write_Command(0x28); chThdSleepMicroseconds(40); Lcd_Write_Command(0x0c); chThdSleepMicroseconds(40); }
void pm25_read() { asm("sim"); //禁止中断 int i = 7; u16 comp; u16 value; u16 pm25_value[6]={0,0,0,0,0,0}; char string_pm25[15]; LED_Y = 0; #if 0 //原始的查找0xff和0xaa函数 u16 temp_value[2]={0,0}; while(1) { temp_value[0] = read_pm25_byte(); temp_value[1] = read_pm25_byte(); if((0xff == temp_value[0])&&(0xaa == temp_value[1])) break; } #endif while(!((read_pm25_byte()==0xff)&&(read_pm25_byte()==0xaa)));//查找0xff与0xaa相连,作为起始; for(i = 0;i < 6;++i) pm25_value[i] = read_pm25_byte(); asm("rim"); //开总中断 comp = pm25_value[0] + pm25_value[1] + pm25_value[2] + pm25_value[3]; if(comp >255) comp -= 256; //处理溢出 if(comp == pm25_value[4]) { value = (u16)((pm25_value[0] << 8 | pm25_value[1])*1.43); //直接转化为ug/m3;3.5mg/m3对应1V /* 标准的比例为3.5mg/m3 ADC转化为电平信号: ADC/1024*5 结合起来,变成 ADC*5/3.5 ug/m3 约为 ADC*1.43 ug/m3; 之后再经过adc_convert函数转化为 千分之一的小数,单位变为mg/m3 */ convert(string_pm25,value); //adc转化为小数 Lcd_Write_Command( 0x01,1); //清屏 Lcd_Puts(0,1,"P25: mg/m3"); Lcd_Puts(4,1,(uchar*)string_pm25); LED_Y = 1; } }
void pm25_read() { asm("sim"); //禁止中断 LED_G = 0; int i = 7; u16 comp; u16 value; u16 pm25_value[6]={0,0,0,0,0,0}; char string_pm25[15]; #if 0 u16 temp_value[2]={0,0}; while(1) { temp_value[0] = read_pm25_byte(); temp_value[1] = read_pm25_byte(); if((0xff == temp_value[0])&&(0xaa == temp_value[1])) break; } #endif while(!((read_pm25_byte()==0xff)&&(read_pm25_byte()==0xaa)));//查找0xff与0xaa相连,作为起始; for(i = 5;i >= 0;--i) { pm25_value[5-i] = read_pm25_byte(); //delay_us(4000); } comp = pm25_value[0] + pm25_value[1] + pm25_value[2] + pm25_value[3]; if(comp >255) //处理数据溢出 comp -= 256; if(comp == pm25_value[4]) { LED_G = 1; } asm("rim"); //开总中断 //value = (pm25_value[0] << 8 | pm25_value[1]) * 5; //转化为mV; 0.2mg/m3对应1V value = (pm25_value[0] << 8 | pm25_value[1]); //直接转化为mg/m3 * 1000;0.2mg/m3对应1V adc_convert(string_pm25,value); Lcd_Write_Command( 0x01,1); //清屏 Lcd_Puts(0,1,"P25: mg/m3"); Lcd_Puts(4,1,(uchar*)string_pm25); }
void Lcd_Init( void ) //初始化,,1602的套路 { Lcd_GPIO_init(); delay_ms(15); //延时15ms Lcd_Write_Command( 0x38,0); // 写指令38H 不检测忙信号 delay_ms(5); //延时5ms Lcd_Write_Command( 0x38,0); // 写指令38H 不检测忙信号 delay_ms(5); //延时5ms Lcd_Write_Command( 0x38,0); // 写指令38H 不检测忙信号 //以后每次写指令、读/写数据操作之前需检测忙信号 Lcd_Write_Command( 0x38,1); //显示模式设置 delay_ms(5); //延时5ms Lcd_Write_Command( 0x08,1); //显示关闭 delay_ms(5); //延时5ms Lcd_Write_Command( 0x01,1); //显示清屏 delay_ms(5); //延时5ms Lcd_Write_Command( 0x06,1); //显示光标移动设置 delay_ms(5); //延时5ms //Lcd_Write_Command( 0x0f,1); Lcd_Write_Command( 0x0C,1); //显示开、光标不显示 //delay_ms(5); //延时5ms }
static void Lcd_Clear (void){ Lcd_Write_Command(0x01); chThdSleepMicroseconds(40); }
/******************************************************************************* * @brief Clear the LCD display * *******************************************************************************/ void lcd_clear (void) { Lcd_Write_Command(0x01); /* Display clear */ set_cursor (0, 0); }