📄 ad0_ad3.c
字号:
OSCICN = 0x88; // select external oscillator as SYSCLK
// source and enable missing clock
// detector
}
//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This function configures the crossbar and GPIO ports.
//
// P0.0 digital push-pull UART TX
// P0.1 digital open-drain UART RX
// P1.6 digital push-pull LED
// AIN0.1 analog Analog input (no configuration necessary)
//-----------------------------------------------------------------------------
void PORT_Init (void)
{
XBR0 = 0x04; // Route UART0 to crossbar
XBR2 |= 0x40; // Enable crossbar, weak pull-ups
P0MDOUT |= 0x01; // enable TX0 as a push-pull output
P1MDOUT |= 0x40; // enable LED as push-pull output
P0MDOUT |= 0x01; // Set TX1 pin to push-pull
P1MDOUT |= 0x40; // Set P1.6(LED) to push-pull
}
//-----------------------------------------------------------------------------
// UART0_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// Configure the UART1 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_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// Configure ADC0 to use Timer3 overflows as conversion source, to
// generate an interrupt on conversion complete, and to use right-justified
// output mode. Enables ADC end of conversion interrupt. Leaves ADC disabled.
//
//-----------------------------------------------------------------------------
void ADC0_Init (void)
{
ADC0CN = 0x04; // ADC0 disabled; normal tracking
// mode; ADC0 conversions are initiated
// on overflow of Timer3; ADC0 data is
// right-justified
REF0CN = 0x07; // Enable temp sensor, on-chip VREF,
// and VREF output buffer
AMX0CF = 0x00; // AIN inputs are single-ended (default)
AMX0SL = 0x00; // Select AIN0.0 pin as ADC mux input
// ISR will change this to step through
// inputs
ADC0CF = (SYSCLK/SAR_CLK) << 3; // ADC conversion clock = 2.5MHz
ADC0CF |= 0x00; // PGA gain = 1 (default)
EIE2 |= 0x02; // enable ADC interrupts
}
//-----------------------------------------------------------------------------
// TIMER3_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters :
// 1) none
//
// Configure Timer3 to auto-reload at interval specified by <counts> (no
// interrupt generated) using SYSCLK as its time base.
//
//-----------------------------------------------------------------------------
void TIMER3_Init (void)
{
TMR3CN = 0x02; // Stop Timer3; Clear TF3; set sysclk
// as timebase
RCAP3 = 65535 -(SYSCLK / 50000); // Init reload values for 20uS
TMR3 = RCAP3; // Set to reload immediately
EIE2 |= 0x01; // Disable Timer3 interrupts
TMR3CN |= 0x04; // start Timer3
}
//-----------------------------------------------------------------------------
// Interrupt Service Routines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// ADC0_ISR
//-----------------------------------------------------------------------------
//
// This ISR is called when the ADC0 completes a conversion. Each value is
// added to a running total <accumulator>, and the local decimation counter
// <int_dec> decremented. When <int_dec> reaches zero, we post the decimated
// result in the global variable <Result[]>.
//
// The analog input is sampled, held, and converted on a Timer2 overflow. To
// maximize input settling time, the analog mux is also advanced to the next
// input on the Timer2 overflow. Two different indices are held globally:
// amux_convert: index of the analog input undergoing conversion
// amux_input: index of the analog input selected in the analog
// multiplexer
//
//
//-----------------------------------------------------------------------------
void ADC0_ISR (void) interrupt 15
{
static unsigned int_dec=INT_DEC; // Integrate/decimate counter
// we post a new result when
// int_dec = 0
static long accumulator[ANALOG_INPUTS] ={0L};
// Here's where we integrate the
// ADC samples from input AIN0.0
unsigned char i;
AD0INT = 0; //clear ADC conversion complete overflow
accumulator[amux_convert] += ADC0; // Read ADC value and add to running
// total
if(amux_convert == (ANALOG_INPUTS-1))// reset input index if the last input
//was just read
{
int_dec--; // Update decimation counter
// when last of the analog inputs
// sampled
}
if (int_dec == 0) // If zero, then post result
{
int_dec = INT_DEC; // Reset counter
for(i=0; i<ANALOG_INPUTS; i++)
{
Result[i] = accumulator[i] >> 8; //Copy decimated values into Result
accumulator[i] = 0L; // Reset accumulators
}
}
amux_convert = amux_input; // now that conversion results are
// stored, advance index to the analog
// input currently selected on the mux
LED = 1;
}
//-----------------------------------------------------------------------------
// TIMER4_ISR
//-----------------------------------------------------------------------------
//
// The timer4 overflow triggers the ADC0 conversion on the analog MUX input
// previously selected. It is permissable to change the analog MUX
// input once conversion has started, as the ADC has an internal sample
// and hold.
//
// This ISR routine will then select the next analog MUX input so as to
// maximize the settling time.
//
//-----------------------------------------------------------------------------
void TIMER3_ISR(void) interrupt 14
{
TMR3CN &= ~0x80; // acknowledge interrupt
amux_input ++; // step to the next analog mux input
if(amux_input == ANALOG_INPUTS) // reset input index if the last input
{ // was just read
amux_input=0; // reset input index back to AIN0.0
}
AMX0SL = amux_input; // select the next input on the analog
// multiplexer
LED = 0;
}
//-----------------------------------------------------------------------------
// Support Subroutines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Wait_MS
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters:
// 1) unsigned int ms - number of milliseconds of delay
// range is full range of integer: 0 to 65335
//
// This routine inserts a delay of <ms> milliseconds.
//
//-----------------------------------------------------------------------------
void Wait_MS(unsigned int ms)
{
CKCON &= ~0x20; // use SYSCLK/12 as timebase
RCAP2 = -(SYSCLK/1000/12); // Timer 2 overflows at 1 kHz
TMR2 = RCAP2;
ET2 = 0; // Disable Timer 2 interrupts
TR2 = 1; // Start Timer 2
while(ms)
{
TF2 = 0; // Clear flag to initialize
while(!TF2); // Wait until timer overflows
ms--; // Decrement ms
}
TR2 = 0; // Stop Timer 2
}
//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -