大概还有八天国赛,好久没练了,手动配置的代码又有些生疏了,再来回顾回顾
c
void OLED_Write(unsigned char type, unsigned char data){ // 主要是给显示屏写指令
unsigned char WriteData[2];
WriteData[0] = type;
WriteData[1] = data;
HAL_I2C_Master_Transmit(&hi2c3, 0x78, WriteData, 2, 0xff); // 函数是主机发送给显示屏用的i2c3线
}
void Function_OledInit(unsigned char ms){
HAL_GPIO_WritePin(OLED_Power_GPIO_Port, OLED_Power_Pin, GPIO_PIN_RESET);
HAL_Delay(ms); // 这里延时一下是等主机通上电再调用初始化函数写指令
OLED_Init(); // 写指令
}
/*
初始化指令不用返还数据
取数据指令会返还数据
用的spi1线
*/
unsigned char SPI_WriteRead(unsigned char address, unsigned char data){
unsigned char TxData[2], RxData[2];
TxData[0] = address;
TxData[1] = data;
HAL_GPIO_WritePin(SPI1_NSS_GPIO_Port, SPI1_NSS_Pin, GPIO_PIN_RESET); // 片选
HAL_SPI_TransmitReceive(&hspi1, TxData, RxData, sizeof(TxData), 0xff);
HAL_GPIO_WritePin(SPI1_NSS_GPIO_Port, SPI1_NSS_Pin, GPIO_PIN_SET);
return RxData[1];
}
float* Function_AdcGet(unsigned char ms){
uint16_t AdcData[2];
static float AdcValue[3];
for(unsigned char i = 0; i < 2; i ++){
HAL_ADC_Start(&hadc);
HAL_ADC_PollForConversion(&hadc, 0xff); // 等待轮转
AdcData[i] = HAL_ADC_GetValue(&hadc);
AdcValue[i] = AdcData[i] * 3.30f / 4095;
HAL_Delay(ms);
}
HAL_ADC_Stop(&hadc);
AdcValue[2] = AdcValue[0];
AdcValue[0] = AdcValue[1];
AdcValue[1] = AdcValue[2];
return AdcValue;
}
void Function_SendMsg(const char* data, uint16_t len){
HAL_UART_Transmit(&huart2, data, len, 0xff);
}
void Function_ReceiveMsg(char* data, uint16_t len){
HAL_UART_Receive(&huart2, data, len, 0xff);
}
// << 8 等效 * 2 ^ 8
float Function_GetTemp(){
unsigned char Data[2];
Data[0] = 0x24;
Data[1] = 0x0b;
HAL_I2C_Master_Transmit(&hi2c1, 0x94, Data, 2, 0xff); // 向外设发送获取温度指令
HAL_I2C_Master_Receive(&hi2c1, 0x95, Data, 2, 0xff); // 接收获取的温度数据
return (float) (Data[0] << 8 | Data[1]) * 175.0 / 65535 - 45; // 转换返还
}
再次分析一下在于OLED用的是i2c3,温度用的是i2c1,LORA用的是spi1,串口用的uart2,剩下就是一个adc了
配置对应引脚也是相应的配就可以了