当单片机接收到PC机发送的指定指令,通过串口,讲采集的温度发送到PC机,通过串口助手显示出来代码
时间: 2024-01-01 12:06:55 浏览: 76
好的,以下是一个示例代码,可以让你了解如何在单片机中实现这个功能。这个代码是基于STM32F103C8T6开发板,使用Keil MDK编写的。
```
#include "stm32f10x.h"
#include <stdio.h>
#define USART_RX_BUF_SIZE 64
#define USART_TX_BUF_SIZE 64
uint8_t usart_rx_buf[USART_RX_BUF_SIZE];
uint8_t usart_tx_buf[USART_TX_BUF_SIZE];
uint8_t usart_rx_len = 0;
void USART1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_Cmd(USART1, ENABLE);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
}
void USART1_IRQHandler(void)
{
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
uint8_t ch = USART_ReceiveData(USART1);
if (usart_rx_len < USART_RX_BUF_SIZE)
{
usart_rx_buf[usart_rx_len++] = ch;
}
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
}
}
void USART1_SendString(char *str)
{
while (*str)
{
USART_SendData(USART1, (uint8_t)*str++);
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
}
void delay_ms(uint32_t ms)
{
uint32_t i, j;
for (i = 0; i < ms; i++)
{
for (j = 0; j < 8000; j++);
}
}
void ADC1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_ADC1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
ADC_DeInit(ADC1);
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_Cmd(ADC1, ENABLE);
}
uint16_t ADC1_Read(uint8_t ch)
{
ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_55Cycles5);
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
return ADC_GetConversionValue(ADC1);
}
float Get_Temperature(void)
{
uint16_t adc_val = ADC1_Read(0);
float temp = (float)adc_val * 3.3 / 4096.0;
temp = (temp - 0.5) * 100.0;
return temp;
}
int main(void)
{
USART1_Init();
ADC1_Init();
while (1)
{
if (usart_rx_len > 0)
{
usart_rx_buf[usart_rx_len] = '\0';
if (strcmp((char *)usart_rx_buf, "GET_TEMP") == 0)
{
float temp = Get_Temperature();
sprintf((char *)usart_tx_buf, "%.2f\r\n", temp);
USART1_SendString((char *)usart_tx_buf);
}
usart_rx_len = 0;
}
delay_ms(100);
}
}
```
这段代码的作用是:初始化串口和ADC模块,然后进入一个死循环,不断检测串口是否接收到了指定指令。如果接收到了指定指令,就采集温度值并将其发送给PC机。串口接收到的指令是以字符串的形式存储在usart_rx_buf数组中的,可以使用strcmp函数进行比较。发送温度值时,使用sprintf函数将浮点数转换成字符串,然后用USART1_SendString函数发送出去。
在PC机端,你需要使用串口助手等工具来接收并显示温度值。串口助手一般可以设置波特率、数据位、校验位等参数,需要和单片机端设置的一致。
阅读全文