diplom/научка/code/pwm_motor_control/Interface/i2c_lcd.c
2025-05-09 21:26:59 +03:00

110 lines
2.6 KiB
C

#include "i2c_lcd.h"
extern unsigned tim1_cnt;
//extern struct var_result VAR_RESULT;
//LCD Delays (Delay1 - )
/**
* @brief Initialization of LCD.
* @note This called from main
*/
void LCD_Init(LCDI2C_HandleTypeDef *hlcd)
{
for (hlcd->Address = 0; hlcd->Address < 128; hlcd->Address++)
{
if(HAL_I2C_IsDeviceReady(hlcd->hi2c, hlcd->Address << 1, 1, HAL_MAX_DELAY)==HAL_OK) break; // scan i2c adresses
}
if (hlcd->Address >= 128)
return;
hlcd->Address = hlcd->Address << 1;
osDelay(500);
LCD_Send_CMD(hlcd, 0x30);
osDelay(5);
LCD_Send_CMD(hlcd, 0x30);
osDelay(1);
LCD_Send_CMD(hlcd, 0x30);
osDelay(10);
LCD_Send_CMD(hlcd, 0x20); // 4bit mode
osDelay(10);
//dislay initialisation
LCD_Send_CMD(hlcd, 0x28); // display off
osDelay(1);
LCD_Send_CMD(hlcd, 0x08); // display off
osDelay(50);
LCD_Send_CMD(hlcd, 0x01); // clear display
osDelay(10);
osDelay(10);
LCD_Send_CMD(hlcd, 0x06); // direction of cursor
osDelay(1);
LCD_Send_CMD(hlcd, 0x0C); // display on / cursor off
}
void LCD_Reinit(LCDI2C_HandleTypeDef *hlcd)
{
osDelay(1000);
LCD_Init(hlcd);
hlcd->LCD_REINIT=0;
}
void LCD_Check(LCDI2C_HandleTypeDef *hlcd)
{
if(hlcd->LCD_REINIT) LCD_Reinit(hlcd);
}
void LCD_Send_CMD (LCDI2C_HandleTypeDef *hlcd, char cmd)
{
char data_up, data_low;
uint8_t data_t[4];
data_up = (cmd&0xf0);
data_low=((cmd<<4)&0xf0);
data_t[0]=data_up|0x0C; //en=1, rs=0
data_t[1]=data_up|0x08; //en=0, rs=0
data_t[2]=data_low|0x0C; //en=1, rs=0
data_t[3]=data_low|0x08; //en=0, rs=0
HAL_I2C_Master_Transmit(hlcd->hi2c, hlcd->Address, (uint8_t *)data_t, 4, HAL_MAX_DELAY);
}
void LCD_Send_DATA (LCDI2C_HandleTypeDef *hlcd, char data)
{
char data_up, data_low;
uint8_t data_t[4];
data_up = (data&0xf0);
data_low=((data<<4)&0xf0);
data_t[0]=data_up|0x0D; //en=1, rs=1
data_t[1]=data_up|0x09; //en=0, rs=1
data_t[2]=data_low|0x0D; //en=1, rs=1
data_t[3]=data_low|0x09; //en=0, rs=1
HAL_I2C_Master_Transmit(hlcd->hi2c, hlcd->Address, (uint8_t *)data_t, 4, HAL_MAX_DELAY);
}
void LCD_Send_STRING(LCDI2C_HandleTypeDef *hlcd, char *str)
{
while (*str)
{
LCD_Send_DATA (hlcd, *str++);
osDelay(1);
}
}
void LCD_Send_INT(LCDI2C_HandleTypeDef *hlcd, int int_to_string, uint8_t size)
{
char string_from_int[10];
if(size)
sprintf(string_from_int, "%0*d", size, int_to_string);
else
sprintf(string_from_int, "%d", int_to_string);
LCD_Send_STRING(hlcd, string_from_int);
}
void LCD_Send_NUMB(LCDI2C_HandleTypeDef *hlcd, float numb_to_string, uint8_t size)
{
char string_from_numb[size];
snprintf(string_from_numb, size+1, "%.2f", numb_to_string);
LCD_Send_STRING(hlcd, string_from_numb);
}