📄 adc1.c
字号:
/*****************************************************************************
功能:实现ADC采样芯片外的模拟电压,通过LCD显示,并通过串口发送到PC机
实验时短接以下跳线:J7.1--J7.2;J9.1--J9.2
版本:V1.0
*****************************************************************************/
#include <c8051f320.h> // SFR declarations
#include <stdio.h>
#include <INTRINS.H>
//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F02x
//-----------------------------------------------------------------------------
sfr16 DP = 0x82; // data pointer
sfr16 TMR2RL = 0xca; // Timer2 reload value
sfr16 TMR2 = 0xcc; // Timer2 counter
sfr16 TMR3RL = 0x92; // Timer3 reload value
sfr16 TMR3 = 0x94; // Timer3 counter
sfr16 ADC0 = 0xbd; // 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
#define BAUDRATE 38400 // Baud rate of UART in bps
#define SYSCLK 12000000 // 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 Timer2_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
PORT_Init (); // initialize crossbar and GPIO
SPI0_Init ();
Timer2_Init (SYSCLK/SAMPLE_RATE); // initialize Timer3 to overflow at
// sample rate
UART0_Init (); // initialize UART0
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<400;x++);
for(x=0;x<400;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<7;i++)
{
Write_CHAR(NCDdata[i]);}
}
}
//-----------------------------------------------------------------------------
// PORT配置
//-----------------------------------------------------------------------------
void PORT_Init (void)
{
P0SKIP = 0x4E;
XBR0 = 0x03; // Enable UART on P0.4(TX) and P0.5(RX)
XBR1 = 0x40; // Enable crossbar and weak pull-ups
P2MDIN = 0x7f; // P2.7配置为模拟输入
// P1SKIP = 0x02;
P0MDOUT |= 0xff; // enable TX0 as a push-pull output
P1MDOUT |= 0xff;
P2MDOUT |= 0xF7;
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: 8-bit variable bit rate
// level of STOP bit is ignored
// RX enabled
// clear RI0 and TI0 bits
if (SYSCLK/BAUDRATE/2/256 < 1) {
TH1 = -(SYSCLK/BAUDRATE/2);
CKCON &= ~0x0B; // T1M = 1; SCA1:0 = xx
CKCON |= 0x08;
} else if (SYSCLK/BAUDRATE/2/256 < 4) {
TH1 = -(SYSCLK/BAUDRATE/2/4);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 01
CKCON |= 0x09;
} else if (SYSCLK/BAUDRATE/2/256 < 12) {
TH1 = -(SYSCLK/BAUDRATE/2/12);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 00
} else {
TH1 = -(SYSCLK/BAUDRATE/2/48);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 10
CKCON |= 0x02;
}
TL1 = TH1; // init Timer1
TMOD &= ~0xf0; // TMOD: timer 1 in 8-bit autoreload
TMOD |= 0x20;
TR1 = 1; // START Timer1
TI0 = 1; // Indicate TX0 ready
}
//-----------------------------------------------------------------------------
// ADC0配置,T3定时启动ADC
//-----------------------------------------------------------------------------
void ADC0_Init (void)
{
ADC0CN = 0xc2; // ADC0 T3定时采样
REF0CN = 0x0e; // 启用内部基准源
AMX0P = 0x0F;
AMX0N = 0x1F; //选择采样输入源GND作为负输入ADC工作在单端方式
ADC0CF = 0x38;
EIE1 |= 0x08; // 启用 ADC 中断
}
//-----------------------------------------------------------------------------
// Timer2配置,T2定时启动ADC
//-----------------------------------------------------------------------------
void Timer2_Init (int counts)
{
TMR2CN = 0x00; // STOP Timer2; Clear TF2H and TF2L;
// disable low-byte interrupt; disable
// split mode; select internal timebase
CKCON |= 0x10; // Timer2 uses SYSCLK as its timebase
TMR2RL = -counts; // Init reload values
TMR2 = TMR2RL; // Init Timer2 with reload value
ET2 = 0; // disable Timer2 interrupts
TR2 = 1; // start Timer2
}
//-----------------------------------------------------------------------------
// 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -