在嵌入式系统中,温度测量是一项常见的任务。本文将介绍如何使用STC单片机实现8路NTC温度测量,并提供相应的源代码。
硬件准备:
- STC单片机开发板
- 8个NTC(负温度系数)温度传感器
- 连接线和电阻
源代码:
#include <reg51.h>
#include <intrins.h>
#define DATA_PORT P2
sbit NTC_CS1 = P1^0;
sbit NTC_CS2 = P1^1;
sbit NTC_CS3 = P1^2;
sbit NTC_CS4 = P1^3;
sbit NTC_CS5 = P1^4;
sbit NTC_CS6 = P1^5;
sbit NTC_CS7 = P1^6;
sbit NTC_CS8 = P1^7;
unsigned int ADC_Value;
void Delay(unsigned int n)
{
unsigned int i, j;
for(i = 0; i < n; i++)
for(j = 0; j < 120; j++);
}
void ADC_Init()
{
ADC_CONTR = 0x80;
Delay(2);
}
unsigned int ADC_Read(unsigned char channel)
{
unsigned int adc_value;
ADC_CONTR = ADC_CONTR & 0xf8;
ADC_CONTR = ADC_CONTR | channel;
ADC_CONTR = ADC_CONTR | 0x08;
_nop_(); //延时
_nop_();
_nop_();
_nop_();
_nop_();