CH32V003 通过定时器采集NST1001温度数据
本帖最后由 Cool_Breeze 于 2024-5-28 15:25 编辑/********************************** (C) COPYRIGHT *******************************
* File Name : main.c
* Author : WCH
* Version : V1.0.0
* Date : 2023/12/25
* Description : Main program body.
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
/*
*@Note
*Multiprocessor communication mode routine:
*Master:USART1_Tx(PD5)\USART1_Rx(PD6).
*This routine demonstrates that USART1 receives the data sent by CH341 and inverts
*it and sends it (baud rate 115200).
*
*Hardware connection:PD5 -- Rx
* PD6 -- Tx
*
*/
#include "debug.h"
#include "ch32v00x_conf.h"
void USART1_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void EXTI7_0_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void TIM2_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
/* Global define */
/* Global Variable */
/* NST1001 */
volatile int32_t Nst1001_count = 0;
char Nst1001_Transfor_time = 0;
FlagStatus Nst1001_Status = RESET;
int32_t Nst1001_Temp_count = 0;
/* USART */
static uint8_t USART_RxText;
const static char USART_RxText_Len = 20;
static char USART_RxText_index = 0;
FlagStatus USART_RxText_OK = RESET;
/*********************************************************************
* @fn USARTx_CFG
*
* @brief Initializes the USART2 & USART3 peripheral.
*
* @Returnnone
*/
void USARTx_CFG(void)
{
GPIO_InitTypeDefGPIO_InitStructure = {0};
USART_InitTypeDef USART_InitStructure = {0};
NVIC_InitTypeDef NVIC_InitStruct = {0};
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_USART1, ENABLE);
/* USART1 TX-->D.5 RX-->D.6 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOD, &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_Tx | USART_Mode_Rx;
USART_Init(USART1, &USART_InitStructure);
NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
USART_Cmd(USART1, ENABLE);
}
/*****************
* @fn NST1001_Init
*
* @brief 初始化gpiod pin0
*/
void NST1001_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure = {0};
EXTI_InitTypeDef EXTI_InitStructure = {0};
NVIC_InitTypeDef NVIC_InitStructure = {0};
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_PA1_2, DISABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource1);
EXTI_InitStructure.EXTI_Line = EXTI_Line1;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
EXTI_ClearITPendingBit(EXTI_Line1);
NVIC_InitStructure.NVIC_IRQChannel = EXTI7_0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/**
* 放大3位小数,转整形
*/
int32_t NST1001_Temp(int32_t nst1001_count)
{
float t = nst1001_count*0.0625-50.0625;
if (t < 30)
{
t = t + (t-30)*0.005;
}
if (t > 100)
{
t = t + (100 - t) * 0.012;
}
return t * 1000;
}
/**
*
*
*/
void Key_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure = {0};
EXTI_InitTypeDef EXTI_InitStructure = {0};
NVIC_InitTypeDef NVIC_InitStructure = {0};
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOD, GPIO_PinSource3);
EXTI_InitStructure.EXTI_Line = EXTI_Line3;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
EXTI_ClearITPendingBit(EXTI_Line3);
NVIC_InitStructure.NVIC_IRQChannel = EXTI7_0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/***
*
*
*/
void RGB_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure = {0};
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_ResetBits(GPIOC, GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3);
}
/***
*
*
*
*/
void TIME2_Init_1ms(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_InternalClockConfig(TIM2);
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Prescaler = 4799;
TIM_TimeBaseInitStructure.TIM_Period = 9;
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0x00;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
NVIC_InitTypeDef NVIC_InitStruct;
NVIC_InitStruct.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
TIM_Cmd(TIM2, ENABLE);
}
/*********************************************************************
* @fn main
*
* @brief Main program.
*
* @returnnone
*/
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
SystemCoreClockUpdate();
Delay_Init();
#if (SDI_PRINT == SDI_PR_OPEN)
SDI_Printf_Enable();
#else
USART_Printf_Init(115200);
#endif
USARTx_CFG();
printf("SystemClk:%d\r\n",SystemCoreClock);
printf( "ChipID:%08x\r\n", DBGMCU_GetCHIPID());
NST1001_Init();
Key_Init();
RGB_Init();
TIME2_Init_1ms();
while(1)
{
Delay_Ms(100);
printf("%d,%d\r\n", Nst1001_Temp_count,NST1001_Temp(Nst1001_Temp_count));
if (USART_RxText_OK)
{
printf("%s\r\n", USART_RxText);
for (char i=0; i<USART_RxText_Len; ++i)
{
USART_RxText = 0;
}
USART_RxText_OK = RESET;
}
}
}
/**
*
*/
void USART1_IRQHandler(void)
{
if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
{
USART_RxText = USART_ReceiveData(USART1);
if ((USART_RxText) == '\n')
{
USART_RxText_OK = SET;
USART_RxText_index = 0;
}
else
{
USART_RxText_index++;
}
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
}
}
/***
*
* 中断
*/
void EXTI7_0_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line1) == SET)
{
Nst1001_count++;
Nst1001_Status = SET;
EXTI_ClearITPendingBit(EXTI_Line1);
}
if (EXTI_GetITStatus(EXTI_Line3) == SET)
{
Nst1001_count=0;
EXTI_ClearITPendingBit(EXTI_Line3);
}
}
/**
*
*
*
*/
void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)
{
if (Nst1001_Status == RESET)
{
Nst1001_Transfor_time++;
// NST1001最小转换时间为16ms,在转换之前重置脉冲计数
if (Nst1001_Transfor_time > 10)
{
if (Nst1001_count > 0)
{
Nst1001_Temp_count = Nst1001_count;
}
Nst1001_count=0;
Nst1001_Transfor_time = 0;
}
}
else
{
Nst1001_Transfor_time = 0;
Nst1001_Status = RESET;
}
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
}
}
串口数据
温度被放大了1000倍从float转为了int类型。
收←◆1283,30125
收←◆1288,30437
收←◆1291,30625
收←◆1292,30687
收←◆1296,30937
收←◆1297,31000
收←◆1299,31125
收←◆1301,31250
收←◆1303,31375
收←◆1305,31500
收←◆1306,31562
加个UI好看多了,NST1001升温很快,降温还行!
NST1001数据手册说是最大单针周期是70ms,那么1秒中可以更新14次温度数据。使用定时器不会阻塞IO。 顶你,单片机高手 原来用在单片机上的啊 可以多来点?
页:
[1]