1.首先我们要先理解 HAL_I2C_Master_Transmit 和 HAL_I2C_Mem_Write这两者的区别
HAL_I2C_Master_Transmit(hi2c, DevAddress, pData, Size, Timeout);
HAL_I2C_Mem_Write(hi2c, DevAddress, MemAddress, MemAddSize, pData, Size, Timeout)
差的是这两个参数 MemAddress, MemAddSize,即寄存器的地址与位数
对于普通的传感器,我们直接往对应的I2C设备里面去写指令数据即可
而对于当前的屏幕 他有两个寄存器,一个是指令寄存器 一个是数据寄存器,
控制屏幕的指令需要往指令寄存器写入,而屏幕要显示的内容就需要往屏幕的寄存器中写入,这个时候就需要HAL_I2C_Mem_Write 这个函数写入寄存器
* @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
* the configuration information for the specified I2C.
* @param DevAddress Target device address: The device 7 bits address value
* in datasheet must be shifted to the left before calling the interface
* @param pData Pointer to data buffer
* @param Size Amount of data to be sent
* @param Timeout Timeout duration
HAL_I2C_Mem_Write(hi2c, DevAddress, MemAddress, MemAddSize, pData, Size, Timeout)
* @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
* the configuration information for the specified I2C.
* @param DevAddress Target device address: The device 7 bits address value
* in datasheet must be shifted to the left before calling the interface
* @param MemAddress Internal memory address
* @param MemAddSize Size of internal memory address
* @param pData Pointer to data buffer
* @param Size Amount of data to be sent
* @param Timeout Timeout duration
void sendCommand(uint8_t command){
HAL_I2C_Mem_Write(&hi2c1, I2C_ADDRESS, 0x00, 1, &command, 1, 0xff);
}
void sendData(uint8_t* data,size_t size){
HAL_I2C_Mem_Write(&hi2c1, I2C_ADDRESS, 0x40, I2C_MEMADD_SIZE_8BIT, data, size, 0xff);
}
void ssd1306_Fill(SSD1306_COLOR color)
{
// Fill screenbuffer with a constant value (color)
uint32_t i;
for(i = 0; i < sizeof(SSD1306_Buffer); i++)
{
SSD1306_Buffer[i] = (color == Black) ? 0x00 : 0xFF;
}
}
void ssd1306_UpdateScreen()
{
uint8_t i;
for (i = 0; i < 8; i++) {
sendCommand( 0xB0 + i);
sendCommand( 0x00);
sendCommand( 0x10);
HAL_I2C_Mem_Write( &hi2c1,I2C_ADDRESS, 0x40, 1, &SSD1306_Buffer[SSD1306_WIDTH * i], SSD1306_WIDTH, 100);
}
}
void ssd1306_init(){
HAL_Delay(100);
sendCommand(0xAE);
sendCommand(0x20);
sendCommand(0x10);
sendCommand(0xB0);
sendCommand(0xC8);
sendCommand(0x00);
sendCommand(0x10);
sendCommand(0x40);
sendCommand(0x81);
sendCommand(0xFF);
sendCommand(0xA1);
sendCommand(0xA6);
sendCommand(0xA8);
sendCommand(SSD1306_HEIGHT - 1);
sendCommand(0xA4);
sendCommand(0xD3);
sendCommand(0x00);
sendCommand(0xD5);
sendCommand(0xF0);
sendCommand(0xD9);
sendCommand(0x22);
sendCommand(0xDA);
sendCommand((SSD1306_COM_LR_REMAP << 5 | SSD1306_COM_ALTERNATIVE_PIN_CONFIG << 4 | 0x02));
sendCommand(0xDB);
sendCommand(0x20);
sendCommand(0x8D);
sendCommand(0x14);
sendCommand(0xAF);
ssd1306_Fill(White);
ssd1306_UpdateScreen();
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_I2C1_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
ssd1306_init();
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}