STM32G030 如何让NTC值打印在LCM屏上, 代码怎么写?
时间: 2024-02-12 11:08:00 浏览: 105
要让NTC值打印在LCM屏上,你需要先读取NTC值并将其转换为温度值,然后将温度值转换为字符串并通过串口输出或者显示在LCM屏幕上。
以下是一个简单的代码示例,假设你的NTC连接在ADC1通道5上,LCM屏幕连接在SPI1上:
```c
#include "stm32g0xx.h"
#include "stdlib.h"
#include "stdio.h"
#define SPI1_CS_Pin GPIO_PIN_4
#define SPI1_CS_GPIO_Port GPIOA
#define ADC_CHANNEL_NTC ADC_CHANNEL_5
void LCM_SPI_Send(uint8_t data);
void LCM_Write_Command(uint8_t cmd);
void LCM_Write_Data(uint8_t data);
void LCM_Init(void);
void LCM_Clear(void);
void LCM_Set_Cursor(uint8_t col, uint8_t row);
void LCM_Print(char* str);
void ADC_Init(void);
uint16_t ADC_Read_Channel(uint32_t channel);
int main(void)
{
char temp_str[10];
float temp_value;
RCC->APBENR2 |= RCC_APBENR2_SYSCFGEN; // enable system configuration controller clock
SYSCFG->CFGR1 &= ~SYSCFG_CFGR1_BOOSTEN; // disable I/O analog switch voltage booster
RCC->IOPENR |= RCC_IOPENR_GPIOAEN; // enable GPIOA clock
GPIOA->MODER &= ~(GPIO_MODER_MODE4_Msk); // set PA4 as output
GPIOA->MODER |= (GPIO_MODER_MODE4_0); // set PA4 as output
LCM_Init();
ADC_Init();
while(1)
{
uint16_t adc_value = ADC_Read_Channel(ADC_CHANNEL_NTC);
temp_value = (float)adc_value / 4096.0f * 3.3f; // calculate voltage
temp_value = (temp_value - 0.76f) / 0.0025f; // calculate temperature
sprintf(temp_str, "%.1f C", temp_value); // convert temperature to string with one decimal place
LCM_Clear();
LCM_Set_Cursor(0, 0);
LCM_Print(temp_str);
}
}
void LCM_SPI_Send(uint8_t data)
{
while(!(SPI1->SR & SPI_SR_TXE_Msk)); // wait until transmit buffer is empty
SPI1->DR = data; // send data
while(!(SPI1->SR & SPI_SR_RXNE_Msk)); // wait until receive buffer is not empty
SPI1->DR; // read received data and clear RXNE flag
}
void LCM_Write_Command(uint8_t cmd)
{
GPIOA->BSRR |= SPI1_CS_Pin << 16; // assert CS
LCM_SPI_Send(0xF8); // send command prefix
LCM_SPI_Send(cmd); // send command
GPIOA->BSRR |= SPI1_CS_Pin; // deassert CS
}
void LCM_Write_Data(uint8_t data)
{
GPIOA->BSRR |= SPI1_CS_Pin << 16; // assert CS
LCM_SPI_Send(0xFA); // send data prefix
LCM_SPI_Send(data); // send data
GPIOA->BSRR |= SPI1_CS_Pin; // deassert CS
}
void LCM_Init(void)
{
RCC->APBENR2 |= RCC_APBENR2_SPI1EN; // enable SPI1 clock
RCC->IOPENR |= RCC_IOPENR_GPIOAEN; // enable GPIOA clock
GPIOA->MODER &= ~(GPIO_MODER_MODE5_Msk | GPIO_MODER_MODE6_Msk | GPIO_MODER_MODE7_Msk);
GPIOA->MODER |= (GPIO_MODER_MODE5_1 | GPIO_MODER_MODE6_1 | GPIO_MODER_MODE7_1); // set PA5/PA6/PA7 to alternate function mode
GPIOA->AFR[0] |= (0x5 << GPIO_AFRL_AFSEL5_Pos) | (0x5 << GPIO_AFRL_AFSEL6_Pos) | (0x5 << GPIO_AFRL_AFSEL7_Pos); // set alternate function to SPI1
GPIOA->MODER &= ~(GPIO_MODER_MODE4_Msk);
GPIOA->MODER |= (GPIO_MODER_MODE4_0); // set PA4 to output mode
GPIOA->BSRR |= SPI1_CS_Pin; // deassert CS
SPI1->CR1 |= SPI_CR1_MSTR; // set SPI1 as master
SPI1->CR1 |= SPI_CR1_SSM; // enable software slave management
SPI1->CR1 |= SPI_CR1_SSI; // set internal slave select
SPI1->CR1 |= SPI_CR1_SPE; // enable SPI1
LCM_Write_Command(0x30); // wake up
LCM_Write_Command(0x0C); // turn off cursor
LCM_Write_Command(0x01); // clear screen
LCM_Write_Command(0x06); // set entry mode
}
void LCM_Clear(void)
{
LCM_Write_Command(0x01);
}
void LCM_Set_Cursor(uint8_t col, uint8_t row)
{
uint8_t address = col + row * 0x40;
LCM_Write_Command(0x80 | address);
}
void LCM_Print(char* str)
{
while(*str)
{
LCM_Write_Data(*str++);
}
}
void ADC_Init(void)
{
RCC->APBENR2 |= RCC_APBENR2_ADCEN; // enable ADC clock
ADC1->CFGR1 |= ADC_CFGR1_RES_1; // set 10-bit resolution
ADC1->SMPR |= ADC_SMPR_SMP_1 | ADC_SMPR_SMP_0; // set sampling time to 13.5 cycles
ADC1->CR |= ADC_CR_ADEN; // enable ADC
while(!(ADC1->ISR & ADC_ISR_ADRDY_Msk)); // wait for ADC to be ready
ADC1->CR |= ADC_CR_ADSTART; // start ADC conversion
}
uint16_t ADC_Read_Channel(uint32_t channel)
{
ADC1->CHSELR = channel; // select channel
while(!(ADC1->ISR & ADC_ISR_EOC_Msk)); // wait for conversion to complete
return ADC1->DR; // read conversion result
}
```
以上代码中,我们使用了SPI1控制LCM屏幕,使用ADC1读取NTC电阻的电压并计算温度值,使用sprintf函数将温度值转换为字符串,最后显示在LCM屏幕上。你需要根据实际的硬件连接和需求进行修改。
阅读全文