STM8 timer1触发ADC1示例
进来整个STM8的东东,用到了timer1周期性触发ADC1:void adc_timer_init(void)
{
clk_timer1_ctrl(CLK_ENABLE);
TIM1_DeInit();
adc_timer_set_prescaler();
adc_timer_set_frequency();
adc_timer_set_trigger_adc();
}
result_t adc_init(void)
{
adc_timer_init();
clk_adc_ctrl(CLK_ENABLE);
ADC1_DeInit();
adc_config_clock();
adc_set_schmitt_tigger_diable();
adc_config_data_align_right();
adc_external_tigger_enable();
adc_config_single_mode();
adc_power_on();
return RESULT_SUCCES;
}
result_t adc_start(void)
{
uint8_t i;
uint8_t int_status = interrupt_disable();
adc_disable();
for(i = 0;i<(ADC_CHANEL_COUNT * ADC_SAMPLE_COUNTER_FOR_AVERAGE);i++)
{
((uint16_t *) adc_convert_result) =0xFFFF;
}
adc_is_scan_mode = true;
adc_is_first_convert = true;
adc_sample_counter = 0;
adc_scan_mode_enable();
adc_config_channel(ADC_CHANNAL_SCAN);
adc_enable();
interrupt_restore_mask(int_status);
return RESULT_SUCCES;
}
#ifdef IRQ_HANDLER_22
INTERRUPT_HANDLER(ADC1_IRQHandler, 22)
{
}
#endif 头文件定义:
#include "stm8s.h"
#define ADC_SYSTEM_FREQUENCY (16000000)
#define ADC_SAMPLE_FREQUENCY (1000)
#define ADC_SAMPLE_COUNTER_FOR_AVERAGE(10)
#define ADC_CHANEL_COUNT (5)
#define ADC_CLOCK (ADC1_PRESSEL_FCPU_D8)
#define ADC_TIMER_PRESCALER (16-1)
#define ADC_TIMER_COUNTER (ADC_SYSTEM_FREQUENCY/((ADC_TIMER_PRESCALER+1)*ADC_SAMPLE_FREQUENCY))
#define adc_timer_set_prescaler() {\
TIM1->PSCRH = (uint8_t)(ADC_TIMER_PRESCALER>>8);\
TIM1->PSCRL = (uint8_t)(ADC_TIMER_PRESCALER);\
}
#define adc_timer_set_frequency() {\
TIM1->ARRH = (uint8_t)(ADC_TIMER_COUNTER>>8);\
TIM1->ARRL = (uint8_t)(ADC_TIMER_COUNTER);\
}
#define adc_timer_set_trigger_adc() {TIM1->CR2 |=(uint8_t)TIM1_TRGOSOURCE_UPDATE;}
#define adc_timer_enable() {TIM1->CR1 |= (uint8_t)TIM1_CR1_CEN;}
#define adc_timer_disable() {TIM1->CR1 &= (uint8_t)(~TIM1_CR1_CEN);}
#define ADC_TRIGGER_SOURCE ADC1_EXTTRIG_TIM
#define ADC_CHANNELS_LOWER (\
(1<<ADC1_SCHMITTTRIG_CHANNEL0)|\
(1<<ADC1_SCHMITTTRIG_CHANNEL1)|\
(1<<ADC1_SCHMITTTRIG_CHANNEL2)|\
(1<<ADC1_SCHMITTTRIG_CHANNEL3)\
)
#define ADC_CHANNELS_HIGHER (\
(1<<ADC1_SCHMITTTRIG_CHANNEL12)\
)
#define adc_external_tigger_enable() {ADC1->CR2 |= (uint8_t)(ADC1_CR2_EXTTRIG);}
#define adc_external_tigger_disable() {ADC1->CR2 &= (uint8_t)(~ADC1_CR2_EXTTRIG);}
#define adc_set_schmitt_tigger_diable() {\
ADC1->TDRH = (uint8_t)ADC_CHANNELS_HIGHER;\
ADC1->TDRL = (uint8_t)ADC_CHANNELS_LOWER;\
}
#define adc_config_clock() {ADC1->CR1 |= (uint8_t)(ADC_CLOCK);}
#define adc_scan_mode_enable() {ADC1->CR2 |= (uint8_t)(ADC1_CR2_SCAN);}
#define adc_scan_mode_disable() {ADC1->CR2 &= (uint8_t)(~ADC1_CR2_SCAN);}
#define adc_config_single_mode() {ADC1->CR1 &= (uint8_t)(~ADC1_CR1_CONT);}
#define adc_config_continuous_mode() {ADC1->CR1 |= (uint8_t)(ADC1_CR1_CONT);}
#define adc_config_data_align_right() {ADC1->CR2 |= (uint8_t)(ADC1_CR2_ALIGN);}
#define adc_interrupt_enable() {ADC1->CSR |= (uint8_t)(ADC1_IT_EOCIE);}
#define adc_interrupt_disable() {ADC1->CSR &= (uint8_t)(~ADC1_IT_EOCIE);}
#define adc_clear_interrupt_flag() {ADC1->CSR &= (uint8_t)(~ADC1_IT_EOC);}
#define adc_power_on() {ADC1->CR1 |= (uint8_t)ADC1_CR1_ADON;}
#define adc_power_off() {ADC1->CR1 &= (uint8_t)(~ADC1_CR1_ADON);}
#define adc_enable() {\
adc_clear_interrupt_flag();\
adc_interrupt_enable();\
ADC1->CR1 |= (uint8_t)ADC1_CR1_ADON;\
adc_timer_enable();\
}
#define adc_disable() {\
adc_timer_disable();\
adc_interrupt_disable();\
adc_clear_interrupt_flag();\
}
#define ADC_CHANNAL_SCAN ADC1_CHANNEL_3
#define ADC_CHANNAL_POINT ADC1_CHANNEL_12
#define adc_config_channel(x) { ADC1->CSR =((ADC1->CSR & (~ADC1_CSR_CH))|x);}
#define ADC_CH_EMPER_CURRENT 0
#define ADC_CH_CHARGE_CURRENT 1
#define ADC_CH_EMPER_VOLTAG 2
#define ADC_CH_CHARGE_VOLTAG 3
#define ADC_CH_BUTTON 4
页:
[1]