saefra 发表于 2015-4-21 15:39:35

本帖最后由 saefra 于 2015-4-21 20:07 编辑

改完后没什么用,不知道改的对不对

枫天2015 发表于 2015-4-21 16:03:59

额也新手发表一下个人看法。楼主想按键唤醒后启动定时器和ADC采样。中断函数只要清标记就行了,其他置于主函数里面。定时器中断是不能唤醒HALT的哦。ADC只要初始化不管halt还是全速运行都不会影响,除非halt前你关闭了(不关闭也不怎耗电)。附代码,运行ok的:

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);       
}




发表于 2015-4-21 16:23:26

你在while1里面加休眠,不是一直在休眠唤醒,休眠唤醒啊。要做一个判断,需要的时候休眠。唤醒以后就别休眠了。

枫天2015 发表于 2015-4-21 16:46:46

嗯,例程而已。自己根据实际情况调整拜。实际我也是有条件的,只是代码复杂没发上来,呵呵
页: 1 [2]
查看完整版本: stm8s的halt模式问题