/*****************************************************************************
功能:实现ADC采样芯片外的模拟电压,通过LCD显示,并通过串口(J6)发送到PC机
注:试验时把LCD电源跳线(J20_1和J20_2)联接好!
版本:V1.0
*****************************************************************************/
#include <c8051f310.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计数器/定时器3重载值
sfr16 TMR3 = 0x94; // Timer3 counter计数器/定时器3
sfr16 ADC0 = 0xbd; // ADC0 data
sfr16 ADC0GT = 0xc4; // ADC0 greater than window ADC0下限(大于)比较字
sfr16 ADC0LT = 0xc6; // ADC0 less than window ADC0上限(小于)比较字
sfr16 RCAP2 = 0xca; // Timer2 capture/reload
sfr16 T2 = 0xcc; // Timer2
#define BAUDRATE 9600 // Baud rate of UART in bps 波特率
#define SYSCLK 24500000 // SYSCLK frequency in Hz 系统时钟频率
#define SAMPLE_RATE 50000 // Sample frequency in Hz 抽样频率
#define INT_DEC 256 // integrate and decimate ratio
void PORT_Init (void);
void UART0_Init (void);
void ADC0_Init (void);
void Timer3_Init (int counts);
void ADC0_ISR (void);
void Write_CHAR(unsigned char yjchar);
void Write_COM(unsigned char yjcom );
void YJ_Init(void);
void SPI0_Init (void);
long result;
unsigned char NCDdata[10]={0x30};
//------------------------------------------------------------------------------
//main
//------------------------------------------------------------------------------
void main (void) {
int temp_int, temp_frac,i; // integer and fractional portions of
unsigned int temperature;
long x;
// Disable Watchdog timer
PCA0MD &= ~0x40; // WDTE = 0 (clear watchdog timer 看门狗定时器被禁止
// enable)
OSCICN |= 0x03; // Set internal oscillator to highest SYSCLK为内部振荡器输出
// setting (24500000)
PORT_Init (); // initialize crossbar and GPIO
UART0_Init (); // initialize UART0
SPI0_Init ();
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,侧等待
while (1) {
EA = 0; // 关中断
temperature = result;
temperature &=0x000fff;
EA = 1; //开中断
temperature = temperature*100L*3/1024;
temp_int = temperature/100;
temp_frac = temperature - (temp_int * 100);
for(x=0;x<4000;x++);
for(x=0;x<4000;x++);
printf ("V_input is %02d.%02dV\n", temp_int, temp_frac);
YJ_Init();
NCDdata[0]=temp_int/100+0x30;NCDdata[1]=(temp_int%100)/10+0x30;NCDdata[2]=(temp_int%100)%10+0x30;NCDdata[3]=0x2e;
NCDdata[4]=temp_frac/10+0x30;NCDdata[5]=temp_frac%10+0x30;NCDdata[6]=0x20;NCDdata[7]=0x20;NCDdata[8]=0x20;
NCDdata[9]=0x20;NCDdata[10]=0x20;NCDdata[11]=0x20;NCDdata[12]=0x20;NCDdata[13]=0x20;NCDdata[14]=0x20;
YJ_Init();
for(i=0;i<6;i++)
{
Write_CHAR(NCDdata[i]);}
}
}
//-----------------------------------------------------------------------------
// PORT配置
//-----------------------------------------------------------------------------
void PORT_Init (void)
{
P0SKIP = 0xCE; //端口0跳过寄存器 p0.0,p0.4,p0.5被交叉开关跳过
P1SKIP = 0x7F; //p1.7被交叉开关跳过
XBR0 = 0x03; // Enable UART on P0.4(TX) and P0.5(RX)
// SPI I/O连到端口引脚;UART TX0,RX0连到端口引脚P0.4和P0.5
XBR1 = 0x40; // Enable crossbar and weak pull-ups
//弱上拉使能;交叉开关使能;T1、T0、ECI、所有的PCA I/O都不连到端口引脚
P2MDIN = 0xef; //P2.4配置为模拟输入
// P1SKIP = 0x02;
P0MDOUT |= 0xff; // enable TX0 as a push-pull output P0.7-0.0输出方式为推挽方式
P1MDOUT |= 0xff;
P2MDOUT |= 0xF7; //P2.3输出方式为漏极开路
P3MDOUT |= 0x04; // P3.3 push-pull output推挽方式
}
//-----------------------------------------------------------------------------
// UART0配置
//-----------------------------------------------------------------------------
// Configure the UART0 using Timer1, for <baudrate> and 8-N-1.
void UART0_Init (void)
{
SCON0 = 0x10; // SCON0: mode 0, 8-bit UART, enable RX UART接收允许
TMOD = 0x20; // TMOD: timer 1, mode 2, 8-bit reload 方式2:自动重装载8位计数器/定时器
TH1 = 0x96; //-(SYSCLK/BAUDRATE/16);
// set Timer1 reload value for baudrate
TR1 = 1; // start Timer1
// Timer1 uses SYSCLK as time base
PCON |= 0x80; // SMOD00 = 1
TI0 = 1; // Indicate TX0 ready
}
//-----------------------------------------------------------------------------
// ADC0配置,T3定时启动ADC
//-----------------------------------------------------------------------------
void ADC0_Init (void)
{
ADC0CN = 0xc5; // ADC0 T3定时采样
REF0CN = 0x0e; // 启用内部基准源
AMX0P = 0X0C; // 选择采样输入源P2.4作为正输入
AMX0N=0X1F; //选择采样输入源GND作为负输入ADC工作在单端方式
ADC0CF = 0x38;
EIE1 |= 0x08; // 启用 ADC 中断
}
//-----------------------------------------------------------------------------
// Timer3配置,T3定时启动ADC
//-----------------------------------------------------------------------------
void Timer3_Init (int counts)
{
CKCON=0X80;
TMR3RL = -counts;
TMR3 = 0xffff;
EIE1 &= ~0x80;//禁止定时器3中断
TMR3CN |= 0x04;//定时器3允许
}
//-----------------------------------------------------------------------------
// ADC0采样中断
//-----------------------------------------------------------------------------
void ADC0_ISR (void) interrupt 10
{
static unsigned int_dec=INT_DEC;
static long accumulator=0L;
ADC0CN &= ~0x20; // 清 ADC 中断标志位
accumulator += ADC0; // 累加ADC采样数据
int_dec--; // 指针减1
if (int_dec == 0)
{ // 累加完了吗?
int_dec = INT_DEC; // 指针复位
result = accumulator>>8;
accumulator = 0L; // 累加和变量清0
}
}
//------------------------------------------------------------------------------
//END OF FILE