Re: 請問這個要求需要使用到multi-task甚至是RTOS嗎?

作者 Eigen 於 2017年09月11日 15:46:42
眯著眼睛,隨便寫寫,pseudo code 大概就這樣

跟本不用os

就4ms 中斷一次,主程序每4ms 處理一次時間,再用flag 控制,簡簡單單~~


main_loop
()

    if
1sec_set{
        
read_i2c()
        if
low_battery{
            
led_16_set
        
}else if mid_battery{
            
led_32_set
        
}
    }
    
tbm_4ms_on{
        
1sec_cnt--
        if(
1sec_cnt==0){
            
1sec_cnt=250
            1sec_set
=1
        
}
        
        
led_cnt--
        if(
led_cnt==0){
            if
led_16_set{
                if
led_on{
                    
led_off
                    pwm_out
=0
                    led_cnt
=250-40
                    
                
}
                else{
                    
led_on
                    pwm_out
=1
                    led_cnt
=40
                
}
            }
            else if
led_32_set{
                if
led_on{
                    
led_off
                    pwm_out
=0
                    led_cnt
=250-80
                
}
                else{
                    
led_on
                    pwm_out
=1
                    led_cnt
=80
                
}
            
            }
        }
        
        

    }    
}

來自: http://www.microchip.com.tw/newbb/viewtopic.php?forum=1&topic_id=21604&post_id=76704