?? f06x_adc2_externalinput_mux.c
字號(hào):
P0MDOUT |= 0x01; // Set TX1 pin to push-pull
P1MDOUT |= 0x40; // Set P1.6(LED) to push-pull
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}
//-----------------------------------------------------------------------------
// UART1_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// Configure the UART1 using Timer1, for <baudrate> and 8-N-1.
//
//-----------------------------------------------------------------------------
void UART1_Init (void)
{
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = UART1_PAGE;
SCON1 = 0x10; // SCON1: mode 0, 8-bit UART, enable RX
SFRPAGE = TIMER01_PAGE;
TMOD &= ~0xF0;
TMOD |= 0x20; // TMOD: timer 1, mode 2, 8-bit reload
if (SYSCLK/BAUDRATE/2/256 < 1) {
TH1 = -(SYSCLK/BAUDRATE/2);
CKCON |= 0x10; // T1M = 1; SCA1:0 = xx
} else if (SYSCLK/BAUDRATE/2/256 < 4) {
TH1 = -(SYSCLK/BAUDRATE/2/4);
CKCON &= ~0x13; // Clear all T1 related bits
CKCON |= 0x01; // T1M = 0; SCA1:0 = 01
} else if (SYSCLK/BAUDRATE/2/256 < 12) {
TH1 = -(SYSCLK/BAUDRATE/2/12);
CKCON &= ~0x13; // T1M = 0; SCA1:0 = 00
} else {
TH1 = -(SYSCLK/BAUDRATE/2/48);
CKCON &= ~0x13; // Clear all T1 related bits
CKCON |= 0x02; // T1M = 0; SCA1:0 = 10
}
TR1 = 1; // start Timer1
SFRPAGE = UART1_PAGE;
TI1 = 1; // Indicate TX1 ready
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}
//-----------------------------------------------------------------------------
// ADC2_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// Configure ADC2 to use Timer2 overflows as conversion source, to
// generate an interrupt on conversion complete, and to use left-justified
// output mode. Enables ADC end of conversion interrupt. Leaves ADC disabled.
//
//-----------------------------------------------------------------------------
void ADC2_Init (void)
{
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = ADC2_PAGE;
ADC2CN = 0x0C; // ADC2 disabled; normal tracking
// mode; ADC2 conversions are initiated
// on overflow of Timer2; ADC2 data is
// right-justified, low power tracking
// mode
REF2CN = 0x03; // Enable on-chip VREF and output buffer
AMX2CF = 0x00; // AIN inputs are single-ended (default)
AMX2SL = 0x00; // Select AIN2.0 pin as ADC mux input
// ISR will change this to step through
// inputs
ADC2CF = ((SYSCLK/SAR_CLK)-1) << 2; // ADC conversion clock = 2.5MHz
ADC2CF |= 0x00; // PGA gain = 1 (default)
EIE2 |= 0x10; // enable ADC interrupts
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}
//-----------------------------------------------------------------------------
// TIMER2_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// Configure Timer2 to auto-reload at 20uS rate using SYSCLK as its timebase
//
//-----------------------------------------------------------------------------
void TIMER2_Init (void)
{
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = TMR2_PAGE;
TMR2CN = 0x00; // Stop Timer2; Clear TF2, select
// auto-reload;
TMR2CF = 0x08; // use SYSCLK as timebase, count down
RCAP2 = 65536 -(SYSCLK / 24500); // Init reload values for 20uS
TMR2 = RCAP2; // Set to reload immediately
TR2 = 1; // start Timer2
ET2 = 1; // enable timer 2 interrupt
//IE |= 0x20; // enable timer 2 interrupt
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}
//-----------------------------------------------------------------------------
// Interrupt Service Routines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// ADC2_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 ADC2_ISR (void) interrupt 18
{
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 AIN2.0
unsigned char i;
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = ADC2_PAGE;
AD2INT = 0; //clear ADC conversion complete overflow
accumulator[amux_convert] += ADC2; // 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;
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}
//-----------------------------------------------------------------------------
// TIMER2_ISR
//-----------------------------------------------------------------------------
//
// The timer2 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 TIMER2_ISR(void) interrupt 5
{
SFRPAGE = TMR2_PAGE;
TF2 = 0;
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 AIN2.0
}
SFRPAGE = ADC2_PAGE;
AMX2SL = amux_input; // select the next input on the analog
// multiplexer
LED = 0;
}
//-----------------------------------------------------------------------------
// Support Subroutines
//-----------------------------------------------------------------------------
// wait_ms
//-----------------------------------------------------------------------------
//
// This routine inserts a delay of <ms> milliseconds.
//
void Wait_MS(unsigned int ms)
{
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = TMR3_PAGE;
TMR3CN = 0x00; // Stop Timer3; Clear TF3;
TMR3CF = 0x00; // use SYSCLK/12 as timebase
RCAP3 = -(SYSCLK/1000/12); // Timer 3 overflows at 1 kHz
TMR3 = RCAP3;
TR3 = 1; // Start Timer 3
while(ms)
{
TF3 = 0; // Clear flag to initialize
while(!TF3); // Wait until timer overflows
ms--; // Decrement ms
}
TR3 = 0; // Stop Timer 3
SFRPAGE = SFRPAGE_SAVE; // Restore SFRPAGE
}
//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -