📄 f06x_adc2_externalinput.#2
字号:
// AIN1.1 analog Analog input
//-----------------------------------------------------------------------------
void PORT_Init (void)
{
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = CONFIG_PAGE; // set SFR page
XBR0 = 0x00;
XBR1 = 0x00;
XBR2 = 0x44; // Enable crossbar and weak pull-up
// Enable UART1
P0MDOUT |= 0x01; // Set TX1 pin to push-pull
P1MDOUT |= 0x40; // Set P1.6(LED) to push-pull
P1MDIN = 0xFD; // P1.1 Analog Input, Open Drain
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
}
TL1 = TH1; // Initialize Timer1
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 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 ADC2_Init (void)
{
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = ADC2_PAGE;
ADC2CN = 0x04; // ADC2 disabled; normal tracking
// mode; ADC2 conversions are initiated
// on overflow of Timer3; ADC2 data is
// right-justified
REF2CN = 0x03; // Enable on-chip VREF,
// and VREF output buffer
AMX2CF = 0x00; // AIN inputs are single-ended (default)
AMX2SL = 0x01; // Select AIN2.1 pin as ADC mux input
ADC2CF = (SYSCLK/SAR_CLK) << 3; // ADC conversion clock = 2.5MHz
EIE2 |= 0x10; // enable ADC interrupts
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}
//-----------------------------------------------------------------------------
// TIMER3_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters :
// 1) int counts - calculated Timer overflow rate
// range is postive range of integer: 0 to 32767
//
// Configure Timer3 to auto-reload at interval specified by <counts> (no
// interrupt generated) using SYSCLK as its time base.
//
//-----------------------------------------------------------------------------
void TIMER3_Init (int counts)
{
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = TMR3_PAGE;
TMR3CN = 0x00; // Stop Timer3; Clear TF3;
TMR3CF = 0x08; // use SYSCLK as timebase
RCAP3 = -counts; // Init reload values
TMR3 = RCAP3; // Set to reload immediately
EIE2 &= ~0x01; // Disable Timer3 interrupts
TR3 = 1; // start Timer3
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}
//-----------------------------------------------------------------------------
// Interrupt Service Routines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// ADC2_ISR
//-----------------------------------------------------------------------------
//
// Here we take the ADC2 sample, add it to a running total <accumulator>, and
// decrement our local decimation counter <int_dec>. When <int_dec> reaches
// zero, we post the decimated result in the global variable <Result>.
//
//-----------------------------------------------------------------------------
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=0L; // Here's where we integrate the
// ADC samples
AD2INT = 0; // Clear ADC conversion complete
// indicator
accumulator += ADC2; // Read ADC value and add to running
// total
int_dec--; // Update decimation counter
if (int_dec == 0) // If zero, then post result
{
int_dec = INT_DEC; // Reset counter
Result = accumulator >> 8;
accumulator = 0L; // Reset accumulator
}
}
//-----------------------------------------------------------------------------
// 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)
{
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = TMR2_PAGE;
TMR2CN = 0x00; // Stop Timer3; Clear TF3;
TMR2CF = 0x00; // 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
SFRPAGE = SFRPAGE_SAVE; // Restore SFRPAGE
}
//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -