STM32的HAL库的 I2C和UART使用函数,几个好用的

HAL_StatusTypeDef I2C_Write(uint8_t* pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr,uint16_t NumByteToWrite)
{
    uint8_t data[10];
    data[0] = RegisterAddr;
    data[1] = pBuffer[0];
    HAL_StatusTypeDef result = HAL_I2C_Master_Transmit(&hi2c1, (uint16_t)DeviceAddr, data, 2, 1000);//1000ms 1s 100K字节s
    return result;
}

void I2C_Read(uint8_t* pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr, uint16_t NumByteToRead){
  uint8_t Register[3]={RegisterAddr};
  HAL_I2C_Master_Transmit(&hi2c1, (uint16_t)DeviceAddr, Register, 1, 1000);
  HAL_I2C_Master_Receive(&hi2c1, (uint16_t)DeviceAddr, pBuffer, NumByteToRead, 1000);
}
HAL_UART_Transmit(&huart1, testChar, 15, 1000);
// UART1发送函数
void UART1_Send_Char(unsigned char data)
{
    while((USART1->SR & USART_SR_TXE) == 0);  // wait send cache empty
    USART1->DR = data;
}

void UART1_Send_String(unsigned char* buff)
{
    //HAL_UART_Transmit(&huart1, buff, 11, 1000);
    while(*buff != 0)                      // the end of string
    {
        UART1_Send_Char(*buff);
        buff++;
    }
}

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值