请问STC8A单片机多通道使用怎么切换达使用,我只会该单通道使用的程序,两个通道同时使用不会更改 请问怎么改
单片机源程序如下:
- #include
- #include // _nop_()function
- #include "STC8.h"
- #include
- #include "DELAY.h"
- #define VCC 2.5
- #define Tstop2 1400 //刹车温度2
- #define Tstop1 1400 //刹车温度1
- #define T 1400 //目标温度
- #define T_poor 20 //目标温度差
- #define Tmax 1500 //最大温度
- #define TAlert 1540 //警戒温度
- #define uint unsigned int
- #define uchar unsigned char
- #define U32 unsigned long
- #define U16 unsigned int
- #define FOSC 24000000UL
- #define BRT (65536 - FOSC / 115200 / 4)
- #define ADC_CHANNEL0 0x00 //转换通道P1.0
- #define ADC_CHANNEL1 0x01 //转换通道P1.1
- #define ADC_CHANNEL2 0x02 //转换通道P1.2
- #define ADC_CHANNEL3 0x03 //转换通道P1.3
- #define ADC_CHANNEL4 0x04 //转换通道P1.4
- #define ADC_CHANNEL5 0x05 //转换通道P1.5
- #define ADC_CHANNEL6 0x06 //转换通道P1.6
- #define ADC_CHANNEL7 0x07 //转换通道P1.7
- #define ADC_CHANNEL8 0x08 //转换通道P0.0
- #define ADC_CHANNEL9 0x09 //转换通道P0.1
- #define ADC_CHANNEL10 0x0a //转换通道P0.2
- #define ADC_CHANNEL11 0x0b //转换通道P0.3
- #define ADC_CHANNEL12 0x0c //转换通道P0.4
- #define ADC_CHANNEL13 0x0d //转换通道P0.5
- #define ADC_CHANNEL14 0x0e //转换通道P0.6
- sbit TEMP1 = P0^2; //控制1
- sbit TEMP2 = P4^3; //控制2
- sbit TEMP3 = P0^3; //控制3
- float code RTD_TAB_PT100[] =
- {
- 88.04,87.64,87.24,86.84,86.44,86.04,85.63,85.23,84.83,84.43, //-30
- 92.04,91.64,91.24,90.84,90.44,90.04,89.64,89.24,88.84,88.44, //-20
- 98.03,95.63,95.23,94.83,94.43,94.03,93.63,93.24,92.84,92.44, //-10
- 100.00,99.60,99.21,98.81,98.41,98.01,97.62,97.22,96.82,96.42, //-0
- 100.00,100.40,100.79,101.19,101.59,101.98,102.38,102.78,103.17,103.57, //0
- 103.96,104.36,104.75,105.15,105.54,105.94,106.33,106.73,107.12,107.52, //10
- 107.91,108.31,108.70,109.10,109.49,109.88,110.28,110.67,111.07,111.46, //20
- 111.85,112.25,112.64,113.03,113.43,113.82,114.21,114.60,115.00,115.39, //30
- 115.78,116.17,116.57,116.96,117.35,117.74,118.13,118.52,118.91,119.31, //40
- 119.70,120.09,120.48,120.87,121.26,121.65,122.04,122.43,122.82,123.21, //50
- 123.60,123.99,124.38,124.77,125.16,125.55,125.94,126.33,126.72,127.10, //60
- };
- U32 AD_value;
- ////PT100温度采集
- //unsigned char xdata ADC1;//外部温度1
- //unsigned char xdata ADC2;//外部温度2
- //unsigned char xdata ADC3;//油温度
- /**********************初始化串口******************************/
- void UartInit(void) //9600bps@24.000MHz
- {
- SCON = 0x50; //8位数据,可变波特率
- AUXR |= 0x40; //定时器1时钟为Fosc,即1T
- AUXR &= 0xFE; //串口1选择定时器1为波特率发生器
- TMOD &= 0x0F; //设定定时器1为16位自动重装方式
- TL1 = 0x8F; //设定定时初值
- TH1 = 0xFD; //设定定时初值
- ET1 = 0; //禁止定时器1中断
- TR1 = 1; //启动定时器1
- TI=1;
- }
- //-----------------------------------------
- //ADC初始化程序
- //
- //-----------------------------------------
- void ADC_INI()
- {
- P1M0 = 0xff; //设置P1为ADC口
- P1M1 = 0x00;
- // ADC_CONTR |= 0x03; //切换AD通道,03为 P1.3
- ADCCFG = 0x0f; //设置ADC时钟为系统时钟/2/16/16
- ADC_CONTR = 0x80; //使能ADC模块
- EADC = 1; //使能ADC中断
- ADC_CONTR |= 0x40; //启动AD转换
- }
- //-----------------------------------------
- //AD中断服务程序
- //
- //-----------------------------------------
- void ADC_Isr() interrupt 5 using 1
- {
- static U32 accumulator = 0;//AD采集数值累加
- static U16 measurements = 20;//数据值平均次数
- U32 result = 0; //平均值结果
- EA = 0;//20190121
- ADC_CONTR|= 0x03;
- ADC_CONTR &= ~0x20; //清中断标志
- ADCCFG = 0x20; //设置结果右对齐
- ACC = ADC_RES; //A[3:0]存储ADC的12位结果的高4位,A[7:4]为0
- B = ADC_RESL; //B存储ADC的12位结果的低8位
- accumulator += ACC * 256 + B; // Add most recent sample
- measurements--;
- if (measurements == 0)
- {
- measurements = 20;//平均值滤波======================
- result = accumulator / measurements;
- accumulator = 0; //=================================
- AD_value = result ;
- }
- EA = 1;
- ADC_CONTR |= 0x40; //继续AD转换
- }
- /*******************************主函数**********************************/
- void main (void)
- {
- float Vin3,T0,AD3;//,//Vin2;
- // uint AD3=0;
- // uint T0=0;
- uint AD2= 100;
-
- //使用内部高精度晶振 ----------------------------------------------------
- P_SW2 = P_SW2 | 0x80; //访问扩展SFR时,要将P_SW2的高位置 1, EAXFR = 1
- CKSEL = 0x00;//使用内部高精度晶振
- CLKDIV = 0x00;//时钟不分频 默认是4分频即:CLKDIV = 0x04;
- while (1) //等待晶振稳定*************
- {
- if ((IRC24MCR & 0x01) == 1)
- {
- IRC24MCR = IRC24MCR |0x80;
- break;
- }
- } //******************************
- P_SW2 = P_SW2 & 0x7F;
- //-------------------------------------------------------------------------
-
- UartInit();
- ADC_INI();
- TEMP1= 0;
- TEMP2= 0;
- TEMP3= 0; //控制端置0
-
- EA = 1;
-
- while(1)
- {
- AD3 = AD_value;
- Vin3 = AD3; //输出通道3的电压值
- T0 = Vin3;
-
-
-
-
- }
- }
复制代码 全部资料51hei下载地址:
TempCtrl.zip
(51.99 KB, 下载次数: 17)
|