#include "NTCtp.h"
#include "stdio.h"
#include "timer.h"
#include "usart.h"
uint32_t Resultvolt; //NTC电压
uint32_t Resistance; //NTC电阻
uint32_t electricity; //电流
int Temp; //int有符号,unsigned int是无符号,有符号无符号整型就是能不能存放负数。
//B值:3950 常温阻值为10k时的对照表 单位:Ω
const int NTCTable[][2] = {
{-40, 340928}, {-39, 318877}, {-38, 298397}, {-37, 279368}, {-36, 261676}, {-35, 245221}, {-34, 229907}, {-33, 215648}, {-32, 202366}, {-31, 189987},
{-30, 178445}, {-29, 167678}, {-28, 157629}, {-27, 148246}, {-26, 139480}, {-25, 131288}, {-24, 123629}, {-23, 116464}, {-22, 109760}, {-21, 103482},
{-20, 97603}, {-19, 92094}, {-18, 86930}, {-17, 82087}, {-16, 77544}, {-15, 73279}, {-14, 69275}, {-13, 65514}, {-12, 61980}, {-11, 58658},
{-10, 55534}, {-9, 52595}, {-8, 49829}, {-7, 47225}, {-6, 44772}, {-5, 42462}, {-4, 40284}, {-3, 38230}, {-2, 36294}, {-1, 34466},
{ 0, 32742}, { 1, 31113}, { 2, 29575}, { 3, 28122}, { 4, 26749}, { 5, 25451}, { 6, 24223}, { 7, 23061}, { 8, 21962}, { 9, 20921},
{ 10, 19936}, {11, 19002}, {12, 18118}, {13, 17280}, {14, 16485}, {15, 15731}, {16, 15016}, {17, 14337}, {18, 13693}, {19, 13081},
{ 20, 12500}, {21, 11948}, {22, 11423}, {23, 10925}, {24, 10451}, {25, 10000}, {26, 9570}, {27, 9162}, {28, 8773}, {29, 8403},
{ 30, 8051}, {31, 7715}, {32, 7395}, {33, 7090}, {34, 6799}, {35, 6522}, {36, 6257}, {37, 6005}, {38, 5764}, {39, 5534},
{ 40, 5315}, {41, 5105}, {42, 4905}, {43, 4713}, {44, 4530}, {45, 4355}, {46, 4188}, {47, 4028}, {48, 3875}, {49, 3729},
{ 50, 3589}, {51, 3455}, {52, 3326}, {53, 3203}, {54, 3086}, {55, 2973}, {56, 2865}, {57, 2761}, {58, 2662}, {59, 2566},
{ 60, 2475}, {61, 2387}, {62, 2303}, {63, 2223}, {64, 2145}, {65, 2071}, {66, 1999}, {67, 1931}, {68, 1865}, {69, 1801},
{ 70, 1741}, {71, 1682}, {72, 1626}, {73, 1572}, {74, 1520}, {75, 1470}, {76, 1422}, {77, 1375}, {78, 1331}, {79, 1288},
{ 80, 1247}, {81, 1207}, {82, 1169}, {83, 1132}, {84, 1096}, {85, 1062}, {86, 1029}, {87, 997}, {88, 966}, {89, 937},
{ 90, 908}, {91, 881}, {92, 854}, {93, 828}, {94, 804}, {95, 780}, {96, 757}, {97, 735}, {98, 713}, {99, 692},
{100, 672}, {101, 653}, {102, 634}, {103, 616}, {104, 599}, {105, 582}
};
void NTCtp_config(void)
{
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
SysTick_Config_Init();
NTCtp_gpio_config();
NTCtp_usart_config();
NTCtp_nvic_config();
usart_release_gpio_int();
adc_config();
}
void NTCtp_gpio_config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; //采集电压
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_1); //TX
GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_1); //RX
}
void NTCtp_usart_config(void)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 9600; /*波特率*/
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; /*流控*/
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_InitStructure.USART_WordLength = USART_WordLength_8b; /*数据位,8*/
USART_InitStructure.USART_Parity = USART_Parity_No; /*校验位,n*/
USART_InitStructure.USART_StopBits = USART_StopBits_1; /*停止位,1*/
USART_Init(USART3,&USART_InitStructure);
USART_ClearFlag(USART3,USART_FLAG_TC); //清除发送完成标志位
USART_Cmd(USART3,ENABLE); //使能串口3
USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);
}
void NTCtp_nvic_config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannel = USART3_6_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
int fputc(int ch, FILE *f) //IAR重定向printf函数
{
//while (USART_GetFlagStatus(USART3,USART_FLAG_TC) == RESET )
while (USART_GetFlagStatus(USART3,USART_FLAG_TXE) == RESET )
{
}
USART_SendData(USART3,(uint8_t) ch);
return ch;
}
void adc_config(void)
{
ADC_DeInit(ADC1); //初始化
ADC_InitTypeDef ADC_InitStructure;
ADC_StructInit(&ADC_InitStructure);
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //转换使能
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //右对齐
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; //12位分辨率
ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward; //浏览方向
ADC_Init(ADC1,&ADC_InitStructure);
ADC_ChannelConfig(ADC1, ADC_Channel_3, ADC_SampleTime_239_5Cycles); //PA3 通道3 USART2
ADC_GetCalibrationFactor(ADC1); //校验
ADC_Cmd(ADC1,ENABLE);
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADRDY)); //等待就绪
ADC_StartOfConversion(ADC1); //启动ADC
}
void ADC_Check(void)
{
uint8_t i;
for(i=0; i<2; i++)
{
while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
Resultvolt = (uint32_t) ADC_GetConversionValue(ADC1);
Resultvolt = (Resultvolt*3300)/4096; //电压 mv
electricity = Resultvolt*1000/5100; //电流 uA
Resistance = 3300*1000/electricity - 5100; //电阻 Ω
}
}
void look_up_table(void)
{
uint8_t i;
ADC_Check(); //采集电压
for (i = 0; i < 145; i++)
{
if (NTCTable[i][1] > Resistance)
continue; // continue是结束本次循环。 break是结束循环。
else
{
Temp = NTCTable[i][0]-1;
if (Temp < -40)
Temp = -40;
else if (Temp > 105)
Temp = 105;
break;
}
}
}
void USART3_6_IRQHandler(void)
{
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) //判断读寄存器是否有数据
{
USART_ClearITPendingBit(USART3,USART_IT_RXNE); //清除发送完成标志位
// Resultvolt = USART_ReceiveData(USART3);
}
}
一周热门 更多>