Zigbee cc2530 AD conversion temperature learning summary

/*
This program explains AD control (on-chip thermometer)
Lecturer Wang Jiahui
*/
#include<iocc2530.h>
#include"stdio.h"
#define uchar  unsigned char
#define uint   unsigned int
void init_UART0(void)/*Initialize serial port*/
{
   P0SEL|=0X3C;/*Here we choose P0_2, P0_3, P0_4, P0_5 as the most I/O ports for serial communication*/
   P2DIR&=~0XC0;/*Here we set the priority of serial port 0 to be higher than that of serial port 1, that is, set the use of serial port 0*/
   PERCFG=0X00;/*PERCFG is the interface control register, here we select the serial port 0 position to 1, that is to select P0-2 and P0-3 as the serial port communication interface*/
   
   U0CSR|=0X80;/*Select the working mode of the serial port, we choose asynchronous communication*/
   U0GCR|=11;/*Set the baud rate to 115200 in combination with U0BAUD, please find the manual*/
   U0BAUD|=216;
   UTX0IF=0;/*UTX0IF is the serial port 0 sending interrupt flag, we will set it to zero*/
}

float recieve_AD(void)/*AD conversion, read the real-time voltage of the on-chip thermometer*/
{
   uint value=0;/*Unsigned integer variable used to store AD conversion value*/
   ADCCON1|=0X30;/*Select the AD conversion startup mode as manual startup*/
   ADCCON3=0X3E;/*Set the information source of AD conversion to the on-chip temperature sensor, select the internal reference voltage to 1.25V, and set the resolution of single-channel AD conversion to 512dec, 12 bits. Check it against the AD register manual here*/
   ADCCON1|=0X40;/*Start AD conversion manually*/
   while(!(ADCCON1&0X80));/*Wait for AD conversion to complete*/
   value=ADCL>>4;/*The AD conversion data of CC2530 is stored from high to low, and the low 4 bits of ADCL are invalid, so we have to move to the right to remove the invalid four bits,
                        I also saw this on the CC2430 school video. I can't give an exact explanation for the time being. If you know anything, please add it, thank you*/
   value|=(uint)ADCH<<4;/*It is explained in the video that the high-order data is shifted to the left by 4 bits to correspond to the low-order data, and then added. I can't give an accurate explanation. I will find the relevant information and make a supplementary explanation*/
   return value*0.06229-303.3-4;/*The calculation formula of temperature is: temperature=((measured voltage-a certain voltage)/temperature coefficient)-temperature error value*/
}

void init_tempurature()/*Initialize temperature sensor*/
{
    IEN0=IEN1=IEN2=0;/*Because AD conversion will generate interrupts, so here we turn off the interrupts used first. For the use of AD interrupts, you can refer to the manual to check the interrupt source*/
    CLKCONCMD&=~0X40;/*Set the clock frequency, we also talked about it in the previous serial communication*/
    while(CLKCONSTA&0X40);
    CLKCONCMD&=~0X47;
    TR0=0X01;/*Here I connect AD to the temperature sensor*/
    ATEST=0X01;/*Start temperature sensor*/
}

void data_UART0(char *character,int length)/*serial port data output function*/
{
    int i;
    for(i=0;i<length;i++)
    {
       U0DBUF=*character++;
       while(UTX0IF==0);/*每次发送数据完成后串口发送中断位硬件自动置一*/
       UTX0IF=0;/*这里我们要软件将其置一*/
    }
    U0DBUF=0X0A;/*这句是在串口中输出一个换行,大家可以试试*/
    while(UTX0IF==0);
    UTX0IF=0;
}
/*告诉大家一个在串口通信中将数字转换成字符输出的简单方法,就是在数字后面加0x30,大家可以尝试*/
void delay_ms(int ti)/*延迟函数*/
{
   int x,y;
   for(x=0;x<ti;x++)
     for(y=0;y<526;y++);
}
void main()
{
   int turn=0;
   float ADTEMP=0;
   char temp[6]={0};
   init_tempurature();//初始化温度传感器
   init_UART0();//初始化串口通信
   while(1)
   {
       for(turn=0;turn<60;turn++)/*累计60次的温度*/
          ADTEMP+=recieve_AD(); 
       ADTEMP=ADTEMP/60;/*求60次温度的平均值*/
       temp[0]=(uchar)(ADTEMP)/10+48;/*将温度的数值存入数组temp中*/
       temp[1]=(uchar)(ADTEMP)%10+48;
       temp[2]='.';
       temp[3]=(uchar)(ADTEMP*10)%10+48;
       temp[4]=(uchar)(ADTEMP*100)%10+48;
       data_UART0(temp,5);//串口输出温度值
       delay_ms(2000);//控制每次输出的时间间隔
   }
}
/*zigbee cc2530的AD控制(片内温度计)就讲到这里,祝大家学的愉快*/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325877698&siteId=291194637