📄 ad1.c
字号:
/*------------------------------------------------------------------------------
AD_Interrupt.C: MSC 1210 A/D Conversion for 1 input signals (-2.5V ... +2.5V)
Inputs pairs AIN0-AIN1 read in an interrupt service routine
Copyright 2003 Keil Software, Inc.
------------------------------------------------------------------------------*/
#include <REG1210.H>
#include <stdio.h>
#include "ISD51.H"
#ifndef XTAL // if no XTAL defined use:
#define XTAL 11059200 // XTAL frequency 11.0592 MHz
#endif
sbit RedLed = P2^4;
sbit YellowLed = P2^5;
// defines for UART BAUDRATE
#define BAUDRATE 9600 // 9600bps communication baudrate
#define T2RELOAD (65536-(XTAL/32/BAUDRATE))
// defines for A/D Converter setup
#define A_CLK (((XTAL+500000)/1000000)-1) // about 1MHz Analog Clock
#define ANA_CLK (XTAL/(A_CLK+1)) // precise Analog Clock
#define DECIMATION (ANA_CLK/64/10) // 10 Hz Decimation
#define CONV_FREQ (ANA_CLK/64/DECIMATION) // Conversion Frequency
// defines for conversion to Volts
#define VBIT (2.5 / 0x800000) // 0x800000 represents 2.5 Volts
// Helper structure to read in ADC values
static union {
unsigned char c[4]; // bytes
signed int i[2]; // signed words
signed long l; // signed long
} res;
/*static bit Second_Interrupt;
static void AUXinterrupts (void) interrupt 6 {
unsigned char x;
switch (PAI) {
case 6: // A/D interrupt
res.c[3] = ADRESL;
res.c[2] = ADRESM;
res.i[0] = (char) ADRESH; // make sign extension
break;
case 8: // Second Interrupt
x = SECINT; // Dummy Read to Clear SEC Interrupt
Second_Interrupt = 1;
break;
}
AI = 0; // Clear AI before returning
} */
void delay(void) {
long i;
i = 0x1000;
while(i--);
}
void main(void) {
T2CON = 0x34; // Use Timer 2 as baudrate generator
RCAP2H = 0xFF;
RCAP2H = (T2RELOAD >> 8); // baudrate reload factor
RCAP2L = T2RELOAD;
SCON0 = 0x50; // enable serial uart & receiver
PCON |= 0x80; // double baudrate for UART0
P3DDRL &= 0xF0; // P3DDRL set port pins of UART0 to input/strong drive output
P3DDRL |= 0x07; // P3DDRL set port pins of UART0 to input/strong drive output
USEC = ((XTAL+500000)/1000000)-1; // USEC timer factor
ONEMS = (XTAL/1000)-1; // (MSECH+MSCL) MS Timer counting at 1.0ms
HMSEC = 100-1; // Hundred MS Timer to 100.0ms
SECINT = (10-1) | 0x80; // SECINT= 10 * HMS Timer Rate = 1 sec
// '0x80' will set the MSB for write immediate
MSINT = (10-1) | 0x80; // MSINT = 10ms for Tone period time base
PDCON = 0x1D; // Powerup SysTimer
FTCON = 0xA5; // setup flash programming times
EICON = 0x40; //DIS enable all auxiliary interrupts
EA = 1; //DIS Enable global interrupt flag
ISDwait (); // wait for connection to uVision2 Debugger
PDCON = 0x15; // ON -> ADC-Vref, SPI Systimers. OFF -> PWM, Watchdog
EA = 1; // Enable global interrupt flag
// Setup ADC
ADCON0 = 0x30; // Vref On, Vref Hi, Buff off, BOD off, PGA
ACLK = A_CLK; // set ACLK factor for about 1MHz
ADCON2 = DECIMATION & 0xFF; // LSB of decimation
ADCON3 =(DECIMATION>>8) & 0x07; // MSB of decimation
ADCON1 = 0x01; // bipolar, auto, self calibration (offset, gain)
ADMUX = 0x68; // AINP = AIN0, AINN = AIN1
AIE = 0x00; // Enable the A/D Interrupt
EAI = 0; // Enable Auxiliary interrupts
printf("\nInput -2.5V to +2.5V on AIN1-AIN2 channel pair\n\n");
while (1) {
RedLed = !RedLed; // toggle red LED
delay();
delay();
while((AIE&0x20) != 0x20) ;
res.c[3] = ADRESL;
res.c[2] = ADRESM;
res.i[0] = (char) ADRESH; // make sign extension
YellowLed = !YellowLed;
// Repeat Output Forever
printf ("%+7.5f Volts\r", res.l * VBIT);
}
/* while (1) {
RedLed = !RedLed; // toggle red LED
delay();
delay();
delay();
YellowLed = !YellowLed;
delay();
delay();
// Repeat Output Forever
while (!Second_Interrupt) { // Wait for Second Interrupt
#ifndef ISD51
PCON |= 1; // Put into Idle Mode
#endif
}
Second_Interrupt = 0;
printf ("%+7.5f Volts\r", res.l * VBIT);
} */
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -