/***************************************************************
功能:实现ADC0采样芯片温度,通过LCD显示,并通过串口1(J13)发送到PC机,请使用串口调试工具调试!
注:试验时把ADC0的工作基准VREF0(J7_5和J7_6或J7_2和J7_6)和LCD电源跳线(J18_1和J18_2)联接好!
作者:ZDP
时间:2005-11-30
版本:V1.0
用外部基准:
J7
NC 1 2 内部VREF
外部VREF 3 4 内部DAC工作基准输入
外部VREF 5---6 内部ADC0工作基准输入
外部VREF 7 8 内部ADC1工作基准输入
或用内部基准:
J7
NC 1 2- 内部VREF
外部VREF 3 4 | 内部DAC工作基准输入
外部VREF 5 6- 内部ADC0工作基准输入
外部VREF 7 8 内部ADC1工作基准输入
***************************************************************/
#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 UART1_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数组的指针
unsigned char lcd_data_count; //要显示的数据个数
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
UART1_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,侧等待ADC0的第一个数据
while (1) {
EA = 0; // 关中断
temperature = result;
EA = 1; //开中断
//转换为实际温度数据
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);//把温度数据通过串口发送至PC机
//在LCD上显示温度
//LCD显示数据处理
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; //准备送数据
lcdpoint=&NCDdata; //取地址
for(lcd_data_count=6;lcd_data_count>0;lcd_data_count--)
{
data1=*lcdpoint; //读出数据
P3 = data1; //写数据到端口
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配置
//-----------------------------------------------------------------------------
// 配置系统时钟使用外部晶振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 = 0x07; // Enable UART0
// XBR1 = 0x00;
XBR2 = 0x44; // Enable crossbar and weak pull-ups
P0MDOUT |= 0x01; // enable TX0 as a push-pull output
P1MDOUT |= 0x01;
P2MDOUT = 0xe0; // P2口设为推挽方式
P3MDOUT = 0xff; // P2口设为推挽方式
}
//-----------------------------------------------------------------------------
// UART0配置
//-----------------------------------------------------------------------------
// Configure the UART0 using Timer1, for <baudrate> and 8-N-1.
void UART1_Init (void)
{
SCON1 = 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 |= 0x10; // SMOD01 = 1
SCON1 |= 0x2; // Indicate TX0 ready
}
//-----------------------------------------------------------------------------
// ADC0配置,T3定时启动ADC
//-----------------------------------------------------------------------------
void ADC0_Init (void)
{
ADC0CN = 0x05; // ADC0 T3定时采样,左对齐
REF0CN = 0x07; // 启用内部基准源
AMX0SL = AMX0SL_AIN; // 选择采样输入源
ADC0CF = (SYSCLK/2500000) << 3; // ADC conversion clock = 2.5MHz
ADC0CF |= 0x01; // PGA gain = 2
EIE2 |= 0x02; // 启用 ADC 中断
}
//-----------------------------------------------------------------------------
// Timer3配置,T3定时启动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 中断标志位
accumulator += ADC0; // 累加ADC采样数据
int_dec--; // 指针减1
if (int_dec == 0) { // 累加完了吗?
int_dec = INT_DEC; // 指针复位
result = accumulator >> 8;
accumulator = 0L; // 累加和变量清0
}
}
//LCD初始�