OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 3879|回复: 6

给正在学习NTC的人参考(int Temp; //int有符号,unsigned int是无符号) 一定要注意Temp类型,不然串口打印的温度是错的!!!

[复制链接]

22

主题

271

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
405
金钱
405
注册时间
2019-3-21
在线时间
107 小时
发表于 2019-3-26 16:50:07 | 显示全部楼层 |阅读模式
#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);
  }
}

正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

22

主题

271

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
405
金钱
405
注册时间
2019-3-21
在线时间
107 小时
 楼主| 发表于 2019-3-27 08:36:37 | 显示全部楼层
串口接收到的数据

串口接收数据

串口接收数据
回复 支持 反对

使用道具 举报

109

主题

5564

帖子

0

精华

资深版主

Rank: 8Rank: 8

积分
10568
金钱
10568
注册时间
2017-2-18
在线时间
1913 小时
发表于 2019-3-27 09:36:05 | 显示全部楼层
多谢分享
回复 支持 反对

使用道具 举报

5

主题

47

帖子

0

精华

初级会员

Rank: 2

积分
118
金钱
118
注册时间
2011-2-1
在线时间
8 小时
发表于 2019-8-30 15:43:47 | 显示全部楼层
"NTCtp.h" 正在学习头文件怎么写,谢谢
回复 支持 反对

使用道具 举报

2

主题

474

帖子

0

精华

论坛元老

Rank: 8Rank: 8

积分
6456
金钱
6456
注册时间
2018-6-27
在线时间
546 小时
发表于 2019-12-25 15:02:41 | 显示全部楼层
学习学习,学习学习。
回复 支持 反对

使用道具 举报

6

主题

28

帖子

0

精华

初级会员

Rank: 2

积分
70
金钱
70
注册时间
2019-9-27
在线时间
19 小时
发表于 2019-12-28 17:53:38 | 显示全部楼层
楼主有全的工程包么,能不能发给我看看,谢谢,948977172@qq.com
回复 支持 反对

使用道具 举报

0

主题

30

帖子

0

精华

初级会员

Rank: 2

积分
73
金钱
73
注册时间
2019-9-5
在线时间
28 小时
发表于 2019-12-30 21:07:49 | 显示全部楼层

"NTCtp.h" ,"NTCtp.C"正在学习头文件怎么写,谢谢
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-5-20 21:20

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表