?? adc0_temp.c
字號(hào):
/***************************************************************
功能:實(shí)現(xiàn)ADC0采樣芯片溫度,通過LCD顯示,并通過串口0(J13)發(fā)送到PC機(jī)
注:試驗(yàn)時(shí)把ADC0的工作基準(zhǔn)VREF0(J7_5和J7_6或J7_2和J7_6)和LCD電源跳線(J18_1和J18_2)聯(lián)接好!
作者:ZDP
時(shí)間:2005-11-30
版本:V1.0
用外部基準(zhǔn):
J7
NC 1 2 內(nèi)部VREF
外部VREF 3 4 內(nèi)部DAC工作基準(zhǔn)輸入
外部VREF 5---6 內(nèi)部ADC0工作基準(zhǔn)輸入
外部VREF 7 8 內(nèi)部ADC1工作基準(zhǔn)輸入
或用內(nèi)部基準(zhǔn):
J7
NC 1 2- 內(nèi)部VREF
外部VREF 3 4 | 內(nèi)部DAC工作基準(zhǔn)輸入
外部VREF 5 6- 內(nèi)部ADC0工作基準(zhǔn)輸入
外部VREF 7 8 內(nèi)部ADC1工作基準(zhǔn)輸入
***************************************************************/
#include <c8051f020.h> // SFR declarations
#include <stdio.h>
#include <INTRINS.H>
//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F02x
//-----------------------------------------------------------------------------
sfr16 DP = 0x82; // data pointer
sfr16 TMR3RL = 0x92; // Timer3 reload value
sfr16 TMR3 = 0x94; // Timer3 counter
sfr16 ADC0 = 0xbe; // ADC0 data
sfr16 ADC0GT = 0xc4; // ADC0 greater than window
sfr16 ADC0LT = 0xc6; // ADC0 less than window
sfr16 RCAP2 = 0xca; // Timer2 capture/reload
sfr16 T2 = 0xcc; // Timer2
sfr16 RCAP4 = 0xe4; // Timer4 capture/reload
sfr16 T4 = 0xf4; // Timer4
sfr16 DAC0 = 0xd2; // DAC0 data
sfr16 DAC1 = 0xd5; // DAC1 data
#define BAUDRATE 115200 // Baud rate of UART in bps
#define SYSCLK 22118400 // SYSCLK frequency in Hz
#define SAMPLE_RATE 50000 // Sample frequency in Hz
#define INT_DEC 256 // integrate and decimate ratio
#define AMX0SL_AIN 8 // 0=AIN0....7=AIN7,8=TEMP
void SYSCLK_Init (void);
void PORT_Init (void);
void UART0_Init (void);
void ADC0_Init (void);
void Timer3_Init (int counts);
void ADC0_ISR (void);
void LCD_Init(void);
unsigned char xdata NCDdata[6]={0x30,0x30,0x30,0x2e,0x30,0x30};
long result;
unsigned char *lcdpoint; //指向 lcddata數(shù)組的指針
unsigned char lcd_data_count; //要顯示的數(shù)據(jù)個(gè)數(shù)
void main (void) {
long temperature,x;
int temp_int, temp_frac;
char data1;
WDTCN = 0xde; // disable watchdog timer
WDTCN = 0xad;
SYSCLK_Init (); // initialize oscillator
PORT_Init (); // initialize crossbar and GPIO
UART0_Init (); // initialize UART0
Timer3_Init (SYSCLK/SAMPLE_RATE); // initialize Timer3 to overflow at
// sample rate
ADC0_Init (); // init ADC
AD0EN = 1; // enable ADC
EA = 1;
while(result==0); //等于0,側(cè)等待
while (1) {
EA = 0; // 關(guān)中斷
temperature = result;
EA = 1; //開中斷
//轉(zhuǎn)換為實(shí)際溫度數(shù)據(jù)
temperature = temperature - 42380;
temperature = (temperature * 100L) / 156;
temp_int = temperature / 100;
temp_frac = temperature - (temp_int * 100);
printf ("Temperature is %+02d.%02d\n", temp_int, temp_frac);//把溫度數(shù)據(jù)通過串口發(fā)送至PC機(jī)
//在LCD上顯示溫度
//LCD顯示數(shù)據(jù)處理
NCDdata[0]=temp_int/100+0x30;NCDdata[1]=(temp_int%100)/10+0x30;NCDdata[2]=(temp_int%100)%10+0x30;
NCDdata[4]=temp_frac/10+0x30;NCDdata[5]=temp_frac%10+0x30;
LCD_Init(); //LCD初始化
P2 = 0xA0; //準(zhǔn)備送數(shù)據(jù)
lcdpoint=&NCDdata; //取地址
for(lcd_data_count=6;lcd_data_count>0;lcd_data_count--)
{
data1=*lcdpoint; //讀出數(shù)據(jù)
P3 = data1; //寫數(shù)據(jù)到端口
P2 = 0X20;
P2 = 0XA0; //控制LCD
lcdpoint++;
for(x=0;x<0x5000;x++);
}
for(data1=0;data1<0x6;data1++)
{
for(x=0;x<0xffff;x++)
{_nop_();}
}
}
}
//-----------------------------------------------------------------------------
// SYSCLK配置
//-----------------------------------------------------------------------------
// 配置系統(tǒng)時(shí)鐘使用外部晶振22.1184MHz
void SYSCLK_Init (void)
{
int i; // delay counter
OSCXCN = 0x67; // start external oscillator with
// 22.1184MHz crystal
for (i=0; i < 256; i++) ; // XTLVLD blanking interval (>1ms)
while (!(OSCXCN & 0x80)) ; // Wait for crystal osc. to settle
OSCICN = 0x88; // select external oscillator as SYSCLK
// source and enable missing clock
// detector
}
//-----------------------------------------------------------------------------
// PORT配置
//-----------------------------------------------------------------------------
void PORT_Init (void)
{
XBR0 = 0x04; // Enable UART0
XBR1 = 0x00;
XBR2 = 0x40; // Enable crossbar and weak pull-ups
P0MDOUT |= 0x01; // enable TX0 as a push-pull output
P2MDOUT = 0xe0; // P2口設(shè)為推挽方式
P3MDOUT = 0xff; // P2口設(shè)為推挽方式
}
//-----------------------------------------------------------------------------
// UART0配置
//-----------------------------------------------------------------------------
// Configure the UART0 using Timer1, for <baudrate> and 8-N-1.
void UART0_Init (void)
{
SCON0 = 0x50; // SCON0: mode 1, 8-bit UART, enable RX
TMOD = 0x20; // TMOD: timer 1, mode 2, 8-bit reload
TH1 = -(SYSCLK/BAUDRATE/16); // set Timer1 reload value for baudrate
TR1 = 1; // start Timer1
CKCON |= 0x10; // Timer1 uses SYSCLK as time base
PCON |= 0x80; // SMOD00 = 1
TI0 = 1; // Indicate TX0 ready
}
//-----------------------------------------------------------------------------
// ADC0配置,T3定時(shí)啟動(dòng)ADC
//-----------------------------------------------------------------------------
void ADC0_Init (void)
{
ADC0CN = 0x05; // ADC0 T3定時(shí)采樣,左對(duì)齊
REF0CN = 0x07; // 啟用內(nèi)部基準(zhǔn)源
AMX0SL = AMX0SL_AIN; // 選擇采樣輸入源
ADC0CF = (SYSCLK/2500000) << 3; // ADC conversion clock = 2.5MHz
ADC0CF |= 0x01; // PGA gain = 2
EIE2 |= 0x02; // 啟用 ADC 中斷
}
//-----------------------------------------------------------------------------
// Timer3配置,T3定時(shí)啟動(dòng)ADC
//-----------------------------------------------------------------------------
void Timer3_Init (int counts)
{
TMR3CN = 0x02;
TMR3RL = -counts;
TMR3 = 0xffff;
EIE2 &= ~0x01;
TMR3CN |= 0x04;
}
//-----------------------------------------------------------------------------
// ADC0采樣中斷
//-----------------------------------------------------------------------------
void ADC0_ISR (void) interrupt 15
{
static unsigned int_dec=INT_DEC;
static long accumulator=0L;
AD0INT = 0; // 清 ADC 中斷標(biāo)志位
accumulator += ADC0; // 累加ADC采樣數(shù)據(jù)
int_dec--; // 指針減1
if (int_dec == 0) { // 累加完了嗎?
int_dec = INT_DEC; // 指針復(fù)位
result = accumulator >> 8;
accumulator = 0L; // 累加和變量清0
}
}
//LCD初始化
void LCD_Init(void)
{unsigned long x;
P2 = 0X80;
for(x=0;x<1000;x++);
//P7 = 0x30; /*一行顯示*/
P3 = 0x38; /*兩行顯示*/
P2 = 0X00;//0x08;
P2 = 0X80;//0x09;
for(x=0;x<1000;x++);
P3 = 0x0e;
P2 = 0x00;
P2 = 0x80;
for(x=0;x<1000;x++);
P3= 0x06;
P2 = 0x00;
P2 = 0x80;
for(x=0;x<5000;x++);
P3 = 0x01;
P2 = 0x00;
P2 = 0x80;
for(x=0;x<5000;x++);
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -