📄 adc0mode1.#3
字号:
#include <c8051F020.h>
#include <intrins.h>
typedef unsigned char tByte;
typedef unsigned int tWord;
typedef unsigned long tLong;
sbit CS1=P3^6;
#define SYSCLK 11059200
#define ADCFrequency 10000 //ADC frequency is 10K
// Define Timer 0 reload values for ~1 msec delay
#define PRELOAD01 (65536 - (unsigned int)(SYSCLK / (12 * 1000)))
#define PRELOAD01H (PRELOAD01 / 256)
#define PRELOAD01L (PRELOAD01 % 256)
sfr16 ADC0 = 0xbe; //define 16 bits SFR for ADC0
void SYSCLK_Init (void);
void PORT_Init (void);
void TIMER0_Init(void);
void ADC0_Init(void);
void LCD_Init(void);
void LCD_Update(void);
void Timer0update(void);
void delay(void);
void Delay_n_ms(unsigned int n);
//grobal varibles
tByte first = 0;
tByte second = 0;
tByte third = 0;
tByte forth = 0;
tWord temperature;
tWord result; //grobal variable ,the result of AD convertion
//---------------************************-----------------//
void main(void)
{
tWord temp_int,temp_frac;
tWord temp_differ;
tByte i;
tByte tempint,tempfrac;
tWord temp[10];
WDTCN = 0xde;
WDTCN = 0xad; // disable the Watchdog
SYSCLK_Init (); // initialize oscillator
PORT_Init (); // initialize crossbar and GPIO
LCD_Init(); //initialize the display of LCD
TIMER0_Init(); // initialize TIMER01
ADC0_Init();
EA=0; //disnable all interrupt
AD0INT = 0;
Delay_n_ms(1);
AD0BUSY =1; //sample of the temperature of surrounding
while(AD0INT == 0);
temperature = ADC0&0x0FFF;
for(i=0;i<5;i++) Delay_n_ms(60000); //wait five minutes
AD0INT = 0;
AD0BUSY =1;
while(AD0INT == 0); //sample the temperature of internal
temp_differ = (ADC0&0x0FFF) - temperature; //obtain the differance between the temperature of surrounding and internal
while(1)
{
temperature = 0;
for(i=0;i<10;i++)
{
AD0INT = 0;
AD0BUSY =1;
while(AD0INT == 0); //sample the temperature of internal
temp[i] = ADC0;
Delay_n_ms(1);
temperature += temp[i];
}
temperature = temperature/10;
temperature = (temperature-temp_differ)- 2582;
temperature = (temperature*100)/9;
temp_int = temperature/100;
temp_frac = temperature - temp_int*100;
tempint = (unsigned char)temp_int;
tempfrac = (unsigned char)temp_frac;
forth = tempint/10;
third = tempint - forth*10;
second = tempfrac/10;
first = tempfrac - second*10;
LCD_Update();
Delay_n_ms(50);
}
}
//--------***************************---------------------------//
void SYSCLK_Init(void)
{
tWord n;
OSCXCN = 0x67; // EXTERNAL Oscillator Control Register
//use the Crystal Oscillator, Frequency is 11.0592MHz
for (n = 0; n < 255; n++) ; // wait for osc to start while ( !(OSCXCN & 0x80)); // wait for xtal to stabilize
OSCICN = 0x88; // Internal Oscillator Control Register
//disable the Internal Oscillator, enable the Oscillator Checker
}
void PORT_Init(void)
{
XBR0 = 0x00; // XBAR0: Initial Reset Value
XBR1 = 0x00; // XBAR1: Initial Reset Value
XBR2 = 0x40; // XBAR2: Enable crossbar
//P0~P3 only are GPIO ports
P0MDOUT = 0x00; // Output configuration for P0
P1MDOUT = 0x00; // Output configuration for P1
P2MDOUT = 0x00; // Output configuration for P2
P3MDOUT = 0x00; // Output configuration for P3
P74OUT = 0x00; // Output configuration for P4-7
//every pin of P0~P7 is Open-Drain Output/Input(Digital)
P1MDIN = 0xFF; // Input configuration for P1
}
void ADC0_Init(void)
{
REF0CN = 0x03; //tempture sensor opened, Internal VREF enable
AMX0CF = 0x00; // AMUX Configuration Register
AMX0SL = 0x02; // AMUX Channel Select Register // select temp sensor
ADC0CF = 0x41; // ADC Configuration Register //10 sysclk; PGA is 2
ADC0CN = 0x80; // ADC Control Register //enable ADC0, start source of ADC is AD0BUSY
}
void TIMER0_Init(void)
{
TMOD = 0x01; // Timer Mode Register
TH0=PRELOAD01H;
TL0=PRELOAD01L;
TR0 =0;
}
void LCD_Init(void) //LCD display "0000"
{
P3 = 0x00;
CS1 = 1;
delay();
CS1 = 0;
P3 = 0x10;
CS1 = 1;
delay();
CS1 = 0;
P3 = 0x20;
CS1 = 1;
delay();
CS1 = 0;
P3 = 0x30;
CS1 = 1;
delay();
CS1 = 0;
}
void LCD_Update(void)
{
P3 = (0x00|first);
CS1=1;
delay();
CS1=0;
P3 = (0x10|second);
CS1=1;
delay();
CS1=0;
P3 = (0x20|third);
CS1=1;
delay();
CS1=0;
P3 = (0x30|forth);
CS1=1;
delay();
CS1=0;
}
void Timer0update(void)
{
TF0=0;
TR0=0;
TH0=PRELOAD01H;
TL0=PRELOAD01L;
}
void Delay_n_ms(unsigned int n)
{
while(n--)
{
TR0=1;
while(TF0==0);
Timer0update();
}
}
void delay(void)
{
_nop_();
_nop_();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -