CC2530 ADC Experiment 1 (read the on-chip temperature and display via the serial port)

Source: Internet
Author: User
Tags function definition
 

/*****************************************//by Unreal Code//name: Read the on-chip temperature and through the serial port Display//Description: Take on-chip temperature sensor for the ad source, through the serial port on the PC display. Experiment using UART0, baud rate is 19200 *****************************************//* Reference ******************************************* */#include <iocc2530.h> #include <stdio.h>/* macro definition *****************************************/#define Uchar unsigned char #define UINT unsigned int #define INT16 short #define UINT16 unsigned      / * Function definition *******************************///Initialize clock void Initclock (void) {  Clkconcmd &=0xbf;//system clock Source select 32MHZ Xosc & nbsp Clkconcmd &=0xf8;//system clock frequency is 32MHZ     Clkconcmd |=0x28;   Clkconcmd &=0xef;//Timing clock frequency 1MHZ   while (Clkconsta & 0X40)//Wait for clock stabilization}//Initialize serial port 0 function void inituart0 (void) {   PERCFG &=0xfe;//USART0 alt 1   P0sel |=0x3c;//p0 ports 2, 3, 4, 5 do peripherals   P2DIR &=0x3f;//p0 Peripheral priority USART0 Max & nbsp U0CSR |=0x80;//USART0 working mode for UART   U0GCR |=9;   U0baud |=59;//set baud rate, theoretically should be 19200, debug serial port is set to 9600, oddBlame.   UTX0IF=0;//UART0 TX Interrupt Flag bit clear 0   U0CSR |=0x40;//usart0 receive enable   IEN0 |=0x04;//usart0 receive interrupt enable   IEN0 |=0x80; Total interrupt Enable}//serial port to send string function void uarttx_send_string (char *data,int len) {  int J;   for (j=0;j<len;j++)   {&N bsp;   u0dbuf=*data++;     while (utx0if==0);     utx0if=0;  }   u0dbuf=0x0a;//line break   while (utx0if==0);   utx0if=0; }//delay function void delay (uint N) {  UINT I;   for (i=0;i<n;i++),   for (i=0;i<n;i++) ;   for (i=0;i<n;i++);   for (i=0;i<n;i++);   for (i=0;i<n;i++); }//Read temperature sensor ad value function uint Gettemperature (void) {  UINT I;   uint16 adcvalue;   uint16 value;     ADC value=0;   for (i=0;i<4;i++)   {    ADCCON3 |=0x0e;//single-channel ad conversion source for temperature sensor-see manual     ADCCON3 &=0x3f;//single-channel AD conversion reference voltage 1.25 internal voltage     ADCCON3 |=0x30;//Single-channel ad conversion resolution is 512dec,12 effective     ADCCON1 |=0X30;//ADC Start Mode selectionStart conversion            for adccon1.st=1 events     ADCCON1 |=0X40;//ADC   while (! ADCCON1&AMP;0X80);//wait for ad conversion to complete     value =  ADCL >> 2; ADCL Register low 2-bit invalid     value |= (((uint16) adch) <<6);//Connect ADCH and ADCL and assign values to value     Adcvalue +=value;//adcvalue is assigned the sum of 4 ad values  }   value=adcvalue>>2;//summation divided by 4 to get the mean   return ((value) >> 4

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.