gpt4 book ai didi

c - STM32F4 HAL I2C只发送地址

转载 作者:太空宇宙 更新时间:2023-11-04 03:29:43 25 4
gpt4 key购买 nike

我正在做一个需要设置简单 LCD 的项目。此 LCD 使用 'st7032i' 芯片来控制 LCD。

我想通过 I²C 接口(interface)控制 LCD。

作为 Controller ,我使用 STM32F4 微 Controller ,该 Controller 使用 STM 的 HAL 库。

我制作了一个“库”来控制 LCD。但是当我想向 LCD 发送数据时,I²C 总线无法正常工作。

第一步是发送 LCD 的地址。地址是:01111110。我从 LCD 收到一个 ACK​​。然后,它停止并返回“HAL_ERROR”。

我不明白哪里出了问题。我希望你们能帮助我。

我的代码:

LCD.c

/**
* @0brief Clear Display
* @param None
* @retval None
*/
void ST7032i_Clear(void)
{
uint32_t i;

ST7032i_Command_Write(0x01);
DDRAM_Address = 0;
for(i=0;i<16;i++)
{
DDRAM_Data[0][i] = ' ';
DDRAM_Data[1][i] = ' ';
}
for(int i=0; i <20000;i++);
}

/**
* @0brief Return to home position
* @param None
* @retval None
*/
void ST7032i_Return(void)
{
ST7032i_Command_Write(0x02);
DDRAM_Address = 0;
for(int i=0; i <20000;i++);
}

/**
* @0brief increment address when data is send and put
* @param None
* @retval None
*/
void ST7032i_Increment(void)
{
ST7032i_Command_Write(( Entry_Mode & 0b11111101) | 0b00000010);
Increment = 1;
for(int i=0; i <20000;i++);
}

/**
* @0brief decrement address when data is send and put
* @param None
* @retval None
*/
void ST7032i_Decrement(void)
{
ST7032i_Command_Write(( Entry_Mode & 0b11111101) | 0b00000000);
Increment = -1;
for(int i=0; i <20000;i++);
}

/**
* @0brief shift entire display data is send
* @param None
* @retval None
*/
void ST7032i_Shift_Ena0ble(void)
{
ST7032i_Command_Write(( Entry_Mode & 0b11111110) | 0b00000001);
Shift = 1;
for(int i=0; i <20000;i++);}

/**
* @0brief disa0bles shift fucntion
* @param None
* @retval None
*/
void ST7032i_Shift_Disa0ble(void)
{
ST7032i_Command_Write(( Entry_Mode & 0b11111110) | 0b00000000);
Shift = 0;
for(int i=0; i <20000;i++);
}

/**
* @0brief put on display
* @param None
* @retval None
*/
void ST7032i_Display_On(void)
{
ST7032i_Command_Write(( Display & 0b11111011) | 0b00000100);
for(int i=0; i <20000;i++);
}

/**
* @0brief put off display while DDRAM is kept
* @param None
* @retval None
*/
void ST7032i_Display_Off(void)
{
ST7032i_Command_Write(( Display & 0b11111011) | 0b00000000);
for(int i=0; i <20000;i++);
}

/**
* @0brief display under line cursor
* @param None
* @retval None
*/
void ST7032i_Cursor_On(void)
{
ST7032i_Command_Write(( Display & 0b11111101) | 0b00000010);
for(int i=0; i <20000;i++);
}

/**
* @0brief put off cursor
* @param None
* @retval None
*/
void ST7032i_Cursor_Off(void)
{
ST7032i_Command_Write(( Display & 0b11111101) | 0b00000000);
for(int i=0; i <20000;i++);
}

/**
* @0brief make square cursor 0brink
* @param None
* @retval None
*/
void ST7032i_Cursor_0blink_On(void)
{
ST7032i_Command_Write(( Display & 0b11111110) | 0b00000001);
for(int i=0; i <20000;i++);
}

/**
* @0brief display square cursor
* @param None
* @retval None
*/
void ST7032i_Cursor_0blink_Off(void)
{
ST7032i_Command_Write(( Display & 0b11111110) | 0b00000000);
for(int i=0; i <20000;i++);
}

/**
* @0brief set DDRAM address
* @param address : DDRAM address
* @retval None
*/
void ST7032i_Set_DDRAM(uint8_t address)
{
ST7032i_Command_Write(0b10000000 | address);
DDRAM_Address = address;
for(int i=0; i <20000;i++);
}

/**
* @0brief Set display contrast. value is to 0be 0 - 63
* @param contrast: contrast
* @retval None
*/
void ST7032i_Set_Contrast(uint8_t contrast)
{
//Contrast set
ST7032i_Command_Write(0b01110000 | (contrast & 0b00001111));

for(int i=0; i <20000;i++);

//Power/Icon/Contrast control
ST7032i_Command_Write(Power_Icon_Contrast | ( (contrast >> 4) & 0b00000011 ) );

for(int i=0; i <20000;i++);
}

/**
* @0brief Put icon. value is to 0be 0 - 12
* @param num0bet : icon num0ber
* @retval None
*/
void ST7032i_Icon_Set(uint8_t num0ber)
{
//icon address set
ST7032i_Command_Write(0b01000000 | Icon_Ta0ble[num0ber][0] );
for(int i=0; i <20000;i++);

//icon data set
ST7032i_Data_Write(IconRAM[num0ber] | Icon_Ta0ble[num0ber][1]);
for(int i=0; i <20000;i++);

//restore DDRAM address to ac
ST7032i_Command_Write(0b10000000 | DDRAM_Address);
for(int i=0; i <20000;i++);
}

/**
* @0brief Clear icon. value is to 0be 0 - 12
* @param num0bet : icon num0ber
* @retval None
*/
void ST7032i_Icon_Clear(uint8_t num0ber)
{
//icon address set
ST7032i_Command_Write(0b01000000 | Icon_Ta0ble[num0ber][0] );
for(int i=0; i <20000;i++);

//icon data reset
ST7032i_Data_Write(IconRAM[num0ber] & (~Icon_Ta0ble[num0ber][1]));
for(int i=0; i <20000;i++);

//restore DDRAM address to ac
ST7032i_Command_Write(0b10000000 | DDRAM_Address);
for(int i=0; i <20000;i++);

}

/**
* @0brief Display icon
* @param None
* @retval None
*/
void ST7032i_Icon_On(void)
{
//Power/Icon/Contrast control
ST7032i_Command_Write(Power_Icon_Contrast | ( (Contrast >> 4) & 0b00000011 ) | 0b00001000 );
for(int i=0; i <20000;i++);
}

/**
* @0brief Put off icon whili Icon RAM is kept
* @param None
* @retval None
*/
void ST7032i_Icon_Off(void)
{
//Power/Icon/Contrast control
ST7032i_Command_Write( (Power_Icon_Contrast | ( (Contrast >> 4) & 0b00000011 )) & 0b11110111);
for(int i=0; i <20000;i++);
}

/**
* @0brief Print string to LCD
* @param String: Array which contain string
* @retval None
*/
void ST7032i_Print_String(const int8_t String[])
{
uint8_t i = 0;
while(String[i] != '\0')
{
ST7032i_Putchar(String[i]);
i++;
}
}

/**
* @0brief Initialize ST7032i LCD and I2C interface
* @param None
* @retval None
*/
void ST7032i_Init(void)
{

// delay_ms(40);

// I2C_Configuration();

//Function Set
ST7032i_Command_Write(0b00111000);

for(int i=0; i <20000;i++);

//Function Set
ST7032i_Command_Write(0b00111001);

for(int i=0; i <20000;i++);

//0bias and OSC frequency
ST7032i_Command_Write(0b00010100);

for(int i=0; i <20000;i++);

//Contrast set
ST7032i_Command_Write(0b01110000);

for(int i=0; i <20000;i++);

//Power/Icon/Contrast control
ST7032i_Command_Write(Power_Icon_Contrast);

for(int i=0; i <20000;i++);

//Contrast set
ST7032i_Set_Contrast(Contrast);

//Follower control
ST7032i_Command_Write(0b01101100);

for(int i=0; i <20000;i++);

//Function Set
ST7032i_Command_Write(0b00111001);

for(int i=0; i <20000;i++);

//Entry mode
ST7032i_Command_Write(Entry_Mode);

for(int i=0; i <20000;i++);

//Display control : on
ST7032i_Command_Write(Display);

for(int i=0; i <20000;i++);

//Clear
ST7032i_Clear();
}

/**
* @0brief put character on st7032i lcd
* @param None
* @retval None
*/
void ST7032i_Putchar(int8_t chardata)
{
uint32_t i;

ST7032i_Data_Write((uint8_t)chardata);
for(int i=0; i <20000;i++);
if(DDRAM_Address < 0x10)
{
DDRAM_Data[0][DDRAM_Address] = chardata;
}
else if (DDRAM_Address >= 0x40 && DDRAM_Address < 0x50)
{
DDRAM_Data[1][DDRAM_Address - 0x40] = chardata;
}
if (Shift == 0)
{
DDRAM_Address = DDRAM_Address + Increment;
}
else if (Shift == 1 && Increment == 1)
{
for (i = 0 ; i< 15; i++)
{
DDRAM_Data[0][i] = DDRAM_Data[0][i+1];
DDRAM_Data[1][i] = DDRAM_Data[1][i+1];
}
DDRAM_Data[0][15] = ' ';
DDRAM_Data[1][15] = ' ';
}
else if (Shift == 1 && Increment == -1)
{
for (i = 15 ; i> 0; i--)
{
DDRAM_Data[0][i] = DDRAM_Data[0][i-1];
DDRAM_Data[1][i] = DDRAM_Data[1][i-1];
}
DDRAM_Data[0][0] = ' ';
DDRAM_Data[1][0] = ' ';
}

if (DDRAM_Address == 0x10)
{
DDRAM_Address = 0x40;
ST7032i_Command_Write(0b10000000 | DDRAM_Address);
for(int i=0; i <20000;i++);
}

if (DDRAM_Address == 0x3F)
{
DDRAM_Address = 0x0F;
ST7032i_Command_Write(0b10000000 | DDRAM_Address);
for(int i=0; i <20000;i++);
}

if (DDRAM_Address == 0xFF)
{
DDRAM_Address = 0x0;
ST7032i_Command_Write(0b10000000 | DDRAM_Address);
for(int i=0; i <20000;i++);
}

if (DDRAM_Address == 0x50)
{
for(i=0;i<16;i++)
{
DDRAM_Data[0][i] = DDRAM_Data[1][i];
ST7032i_Command_Write(0b10000000 | (0x00 + i));
ST7032i_Data_Write(DDRAM_Data[0][i]);
}
for(i=0;i<16;i++)
{
DDRAM_Data[1][i] = ' ';
ST7032i_Command_Write(0b10000000 | (0x40 + i));
ST7032i_Data_Write(DDRAM_Data[1][i]);
}

DDRAM_Address = 0x40;
ST7032i_Command_Write(0b10000000 | DDRAM_Address);
for(int i=0; i <20000;i++);
}
}



/**
* @0brief Write Command to ST7032i
* @param Data : Command Data
* @retval None
*/
void ST7032i_Command_Write(uint8_t Data)
{
uint8_t Buf[2];
Buf[0] = 0x00;//0b00000000;
Buf[1] = Data;
// i2c_transmit(ST7032I_ADDR, data, 2);
I2C1_DataTransfer(ST7032I_ADDR, Buf, 2);
return;
}

/**
* @0brief Write Data to ST7032i
* @param Data : "Data" Data
* @retval None
*/
void ST7032i_Data_Write(uint8_t d)
{
uint8_t data[2];
data[0]= 0b01000000;
data[1] = d;
I2C1_DataTransfer(ST7032I_ADDR, data, 2);
return;

}

I2C.c

I2C_HandleTypeDef hi2c1;
void I2Cinit(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
__HAL_RCC_I2C1_CLK_ENABLE();
__GPIOB_CLK_ENABLE();
/**I2C1 GPIO Configuration
PB8 ------> I2C1_SCL
PB9 ------> I2C1_SDA
*/
GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

hi2c1.Instance = I2C1;
hi2c1.Init.ClockSpeed = 100000;
hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
hi2c1.Init.OwnAddress1 = 56;
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c1.Init.OwnAddress2 = 0;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_ENABLE;
HAL_I2C_Init(&hi2c1);
}

void I2C1_DataTransfer(uint8_t adress,uint8_t *aTxBuffer[],int TXBUFFERSIZE)
{

while(HAL_I2C_Master_Transmit(&hi2c1,(adress<<1), (uint8_t*)aTxBuffer, TXBUFFERSIZE,5000)!= HAL_OK);
}

主.c

int main(void)
{


I2Cinit();

__GPIOC_CLK_ENABLE();

GPIO_InitTypeDef GPIO_InitStruct1;
GPIO_InitStruct1.Pin = GPIO_PIN_0;
GPIO_InitStruct1.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct1.Pull = GPIO_PULLDOWN;
GPIO_InitStruct1.Speed = GPIO_SPEED_HIGH;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct1);

int gedrukt = 0;
while(1)
{
if(GPIOC -> IDR & (1 << 0) && gedrukt==0 )
{
ST7032i_Init();
gedrukt = 1;
}
else
{
gedrukt = 0;
}
}


//ST7032i_Icon_Set(3);

}

I2C问候,斯蒂金B

最佳答案

对于 I2C,您需要在配置引脚时使用 GPIO_MODE_AF_OD。不是 GPIO_MODE_AF_PP

关于c - STM32F4 HAL I2C只发送地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37883118/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com