#include "main.h"
main()
{
CLK_Init();
GPIO_Init();
ADC_Init();
TIM1_Init();
while(1)
{
while (!(TIM1->SR1&0x01)); // wait until reach 1s
TIM1->SR1 &=~0x01;
ADC1->CR1 |= ADC1_CR1_ADON ; // enable AD converter
}
}
void CLK_Init (void)
{
CLK->ICKR |= CLK_ICKR_HSIEN;
while(!(CLK->ICKR&0x02));
CLK->CKDIVR = CLK_CKDIVR_HSIDIV; //Fcpu=Fmaster=2MHz
}
void GPIO_Init (void) // IO collocation
{
GPIOD->DDR |= 0xff;
GPIOD->CR1 |= 0xff;
GPIOD->CR2 = 0x00;
GPIOB->DDR |= 0xff;
GPIOB->CR1 |= 0xff;
GPIOB->CR2 = 0x00;
}
void ADC_Init (void)
{
u8 i;
ADC1->CSR = 0x23; /* select AIN3 and enable EOC interrupt */
ADC1->CR1 = 0x01; /* single mode and awake the ADC */
ADC1->CR2 = 0x08; /* disable extern interrupt and right data alignment */
ADC1->CR3 = 0x00; /* disable data buffer */
for(i=100;i>0;i--); /* ensure the power of ADC is ready */
}
void TIM1_Init (void)
{
TIM1->PSCRH = 0x27;
TIM1->PSCRL = 0x0f; // Fck_cnt=Fck_psc/10000=200Hz
TIM1->CNTRH = 0x0;
TIM1->CNTRL = 0x0;
TIM1->ARRH = 0x0;
TIM1->ARRL = 0xc8; // ARR=200,timing 1s
TIM1->IER &=~0x01; // disable update interrupt
TIM1->CR1 |= TIM1_CR1_CEN; //enable couter
}
@far @interrupt void ADC1_EOC_AW_IRQHanlder (void)
{
ADC1->CSR &=~0x80;
GPIOD->ODR = ADC1->DRH;
GPIOB->ODR = ADC1->DRL;
return;
} |