#include "stdio.h" #include <stdint.h> #include <stdbool.h> #include "hw_memmap.h" #include "hw_types.h" #include "hw_gpio.h" #include "debug.h" #include "fpu.h" #include "gpio.h" #include "pin_map.h" #include "rom.h" #include "sysctl.h" #include "uart.h" #include "uartstdio.h" #ifdef DEBUG void __error__(char *pcFilename, uint32_t ui32Line) { } #endif /* ********************************************************************************************************* * 函 数 名: PrintfLogo * 功能说明: 打印例程名称和例程发布日期, 接上串口线后,打开PC机的串口终端软件可以观察结果 * 形 参: 无 * 返 回 值: 无 ********************************************************************************************************* */ void PrintfLogo(void) { printf("*************************************************************\n\r"); printf("* 例程名称 : %s\r\n", "串口0发送与RGB灯驱动"); /* 打印例程名称 */ printf("* 例程版本 : %s\r\n", "V1.0"); /* 打印例程版本 */ printf("* 发布日期 : %s\r\n", "20200310"); /* 打印例程日期 */ printf("* 标准库版本 :TM4C123GH6PM\r\n"); printf("* \r\n"); /* 打印一行空格 */ printf("* QQ : 3138372165 \r\n"); printf("* Email : [email protected] \r\n"); printf("* Copyright www.nameless.tech 无名创新\r\n"); printf("*************************************************************\n\r"); } void ConfigureUART(void) { ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);// Enable the GPIO Peripheral used by the UART. ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);// Enable UART0 ROM_GPIOPinConfigure(GPIO_PA0_U0RX);// Configure GPIO Pins for UART mode. ROM_GPIOPinConfigure(GPIO_PA1_U0TX); ROM_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1); // Use the internal 16MHz oscillator as the UART clock source. // Initialize the UART for console I/O. UARTStdioConfig(0, 115200, 16000000);UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC); } int fputc(int ch, FILE *f){UARTCharPut(UART0_BASE,ch); return (ch);}//重新映射printf函数 int fgetc(FILE *f) {int ch=UARTCharGet(UART0_BASE); return (ch);} int main(void) { ROM_F
时间: 2025-04-21 15:39:05 浏览: 27
### 配置CC2538温度传感器并实现UART通信
为了配置CC2538温度传感器并与UART模块协同工作,以下是完整的代码示例。此代码不仅初始化了UART接口用于调试打印,还实现了获取温度的功能。
```c
#include "stdio.h"
#include <stdint.h>
#include <stdbool.h>
#include "hw_memmap.h"
#include "hw_types.h"
#include "hw_gpio.h"
#include "debug.h"
#include "fpu.h"
#include "gpio.h"
#include "pin_map.h"
#include "rom.h"
#include "sysctl.h"
#include "uart.h"
#include "uartstdio.h"
// 温度传感器头文件定义
#ifndef CC2538_TEMP_SENSOR_H_
#define CC2538_TEMP_SENSOR_H_
#define CONST 0.58134 //(VREF / 2047) = (1190 / 2047), VREF from Datasheet[^1]
#define OFFSET_DATASHEET_25C 827 // 1422*CONST, from Datasheet
#define TEMP_COEFF (CONST * 4.2) // From Datasheet
#define OFFSET_0C (OFFSET_DATASHEET_25C - (25 * TEMP_COEFF))
uint16_t Get_CC2538_Temp(void);
#endif
#ifdef DEBUG
void __error__(char *pcFilename, uint32_t ui32Line)
{
}
#endif
/*
*********************************************************************************************************
* 函 数 名: PrintfLogo
* 功能说明: 打印例程名称和例程发布日期, 接上串口线后,打开PC机的串口终端软件可以观察结果
* 形 参: 无
* 返 回 值: 无
*********************************************************************************************************
*/
void PrintfLogo(void)
{
printf("*************************************************************\n\r");
printf("* 例程名称 : %s\r\n", "串口0发送与RGB灯驱动");
printf("* 例程版本 : %s\r\n", "V1.0");
printf("* 发布日期 : %s\r\n", "20200310");
printf("* 标准库版本 :TM4C123GH6PM\r\n");
printf("* \r\n");
printf("* QQ : 3138372165 \r\n");
printf("* Email : [email protected] \r\n");
printf("* Copyright www.nameless.tech 无名创新\r\n");
printf("*************************************************************\n\r");
}
void ConfigureUART(void)
{
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); // Enable the GPIO Peripheral used by the UART.
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0); // Enable UART0
ROM_GPIOPinConfigure(GPIO_PA0_U0RX); // Configure GPIO Pins for UART mode.
ROM_GPIOPinConfigure(GPIO_PA1_U0TX);
ROM_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
UARTStdioConfig(0, 115200, 16000000);
UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);
}
int fputc(int ch, FILE *f){
UARTCharPut(UART0_BASE,ch);
return (ch);
}
int fgetc(FILE *f) {
int ch=UARTCharGet(UART0_BASE);
return (ch);
}
/// 获取温度读数的具体实现
uint16_t Get_CC2538_Temp(void) {
uint16_t adc_value;
adc_value = ADC_getValue(); // 假设有一个ADC读取函数
float temperature_celsius = ((float)(adc_value - OFFSET_0C)) / TEMP_COEFF; // 计算实际温度
return (uint16_t)temperature_celsius;
}
int main(void)
{
ROM_FPUEnable();
ROM_FPULazyStackingEnable();
PrintfLogo();
ConfigureUART();
while(true) {
uint16_t temp = Get_CC2538_Temp();
char buffer[50];
sprintf(buffer, "Current Temperature is: %d C\n\r", temp);
UARTSend((unsigned char*)buffer, strlen(buffer));
delay_ms(1000); // 使用提供的延迟函数来间隔输出温度值
}
}
```
在此代码片段中,`Get_CC2538_Temp()` 函数利用预处理器宏计算环境温度,并将其转换为摄氏度单位返回给调用者。此外,在 `main()` 中循环调用了该方法并将结果通过UART端口每秒一次地发送出去。
阅读全文
相关推荐












