/* Sample AIN voltage in ADC single mode */
ADC_CR1 |= 0x01; /* First set ADON to power on the ADC module. */
i = 26; /* Wait >7us to ensure the ADC power on finished.*/
while(i--);
/* Store ADC value to AD_Value */
//AD_Value = ((((unsigned int)ADC_DRH)<<2)+ADC_DRL);
for( i = 0; i < 4; i++ )
{
ADC_CR1 |= 0x01; /* Set ADON again to start AD convert. */
while(!(ADC_CSR & 0x80));/* Waiting for AD convert finished (EOP=1). */
value = ADC_DRH << 2; /*Remove four invalid bits*/
value |= ADC_DRL; /*Combine with four valid bits*/
accValue += value; /*Accumulate sampling value twice*/
}
改完后没什么用,不知道改的对不对
main()
{
WORD j=0;
INT_GLOBAL_DISABLE();/* Disable interrupts */
CLK_Init();
GPIO_Init();
ADC_Init();
AWU_Config(AWU_TIMEBASE_512MS);
INT_GLOBAL_ENABLE();
while(1)
{
CLK->ICKR |= CLK_ICKR_FHWU;
FLASH->CR1 |= FLASH_CR1_AHALT;//active halt mode
//FLASH->CR1 |= FLASH_CR1_HALT;//halt mode
halt();
if(GetAdcValue() < ADC_POWER_THREOLD)
{
ADC_count ++;
if(ADC_count >= ADC_POWER_NUM)
{
ADC_count = 0;
low_power_flag = 1;
}
}
else
{
ADC_count = 0;
low_power_flag = 0;
}
}
}///////
void ADC_Init(void)
{
ADC_CR2 = 0x00;
ADC_CR1 = 0x00;
ADC_CSR = 0x03;
//ADC_TDRL = 0x04;//
ADC_CLK_EN = 1;////CLK ENABLE
}
u16 GetAdcValue(void)//8bits
{
u8 i;
u16 accValue,value; /*Variable of accumulate temperature ADC value*/
accValue = 0;
/* Sample AIN voltage in ADC single mode */
ADC_CR1 |= 0x01; /* First set ADON to power on the ADC module. */
i = 26; /* Wait >7us to ensure the ADC power on finished.*/
while(i--);
/* Store ADC value to AD_Value */
//AD_Value = ((((unsigned int)ADC_DRH)<<2)+ADC_DRL);
for( i = 0; i < 4; i++ )
{
ADC_CR1 |= 0x01; /* Set ADON again to start AD convert. */
while(!(ADC_CSR & 0x80));/* Waiting for AD convert finished (EOP=1). */
value = ADC_DRH << 2; /*Remove four invalid bits*/
value |= ADC_DRL; /*Combine with four valid bits*/
accValue += value; /*Accumulate sampling value twice*/
}
return (accValue>>2);
}