简单的C8051单片机AD转换程序?
时间:10-02
整理:3721RD
点击:
程序代码对内部的温度传感器进行采样转换,不知道程序逻辑上哪里出错了?为什么在中断里isnewdata的值不会发生变化?求助!
- //-----------------------------------------------------------------------------
- // Includes
- //-----------------------------------------------------------------------------
- #include <C8051F020.h> // SFR declarations
- #include <stdio.h>
- //-----------------------------------------------------------------------------
- // 16-bit SFR Definitions for 'F02x
- //-----------------------------------------------------------------------------
- sfr16 ADC0 = 0xbe; // ADC0 data
- //-----------------------------------------------------------------------------
- // Global Constants
- //-----------------------------------------------------------------------------
- #define SYSCLK 22118400 // External crystal oscillator frequency
- #define SAR_CLK 2500000 // Desired SAR clock speed
- //-----------------------------------------------------------------------------
- // Global Variables
- //-----------------------------------------------------------------------------
- bit isnewdata;
- xdata float t[20];//存放温度检测数据,为20个数据
- //-----------------------------------------------------------------------------
- // Function Prototypes
- //-----------------------------------------------------------------------------
- void OSCILLATOR_Init (void);
- void PORT_Init (void);
- void ADC0_Init (void);
- void TIMER3_Init (int counts);
- void ADC0_ISR (void);
- void delay1ms (unsigned int time);
- //-----------------------------------------------------------------------------
- // main() Routine
- //-----------------------------------------------------------------------------
- void main (void)
- {
- unsigned int i,kk;
- float temp;
-
- WDTCN = 0xde; // Disable watchdog timer
- WDTCN = 0xad;
- OSCILLATOR_Init (); // Initialize oscillator
- PORT_Init (); // Initialize crossbar and GPIO
- ADC0_Init (); // Init ADC
- AD0EN = 1; // Enable ADC (ADC0CN.7)
- AD0BUSY = 1;
- EA = 1; // Enable global interrupts
- while (1)
- {
- if(isnewdata)
- {
- kk=ADC0;
- temp=(float)kk/4096.0;
- temp*=2.43;
- temp-=0.776;
- temp/=0.00286;//将测量值转化成真实温度值
- isnewdata=0;
- t[i]=temp;//将温度检测值存入长度为20的数组中
- i++;
- if(i>19)
- i=0;//在此设断点,观察程序运行结果
- delay1ms(100);
- AD0BUSY = 1;
- }
- }
- }
- //-----------------------------------------------------------------------------
- // Initialization Subroutines
- //-----------------------------------------------------------------------------
- //-----------------------------------------------------------------------------
- // OSCILLATOR_Init
- //-----------------------------------------------------------------------------
- void OSCILLATOR_Init (void)
- {
- int i;
- OSCXCN = 0x67;
- for (i=0; i < 256; i++) ;
- while (!(OSCXCN & 0x80)) ;
- OSCICN = 0x88;
- }
- //-----------------------------------------------------------------------------
- // PORT_Init
- //-----------------------------------------------------------------------------
- //-----------------------------------------------------------------------------
- void PORT_Init (void)
- {
- XBR2 |= 0x40; // Enable crossbar, weak pull-ups
- }
- void delay1ms(unsigned int time)
- { //延迟1ms
- unsigned int i;
- unsigned int j;
- for (i=0;i<time;i++){
- for(j=0;j<300;j++);
- }
- }
- //-----------------------------------------------------------------------------
- // ADC0_Init
- //-----------------------------------------------------------------------------
- //-----------------------------------------------------------------------------
- void ADC0_Init (void)
- {
- ADC0CN = 0x00;
- AMX0SL = 0x0F;
- ADC0CF = (SYSCLK/SAR_CLK) << 3;
- ADC0CF &= 0xF8;
- EIE2 |= 0x02;
- }
- //-----------------------------------------------------------------------------
- // Interrupt Service Routines
- //-----------------------------------------------------------------------------
- //-----------------------------------------------------------------------------
- // ADC0_ISR
- //-----------------------------------------------------------------------------
- //-----------------------------------------------------------------------------
- void ADC0_ISR (void) interrupt 15
- {
- AD0INT = 0;
- isnewdata = 1;
- }
- //-----------------------------------------------------------------------------
- // End Of File
- //-----------------------------------------------------------------------------