[ create a new paste ] login | about

Link: http://codepad.org/JcyFEDoY    [ raw code | fork ]

C, pasted on Aug 4:
uint16_t led_1_counter = 0;                                                     
uint16_t led_2_counter = 0;                                                     
                                                                                
#define led_1_ON_threshold   < LED_1 暗的時間 >                                 
#define led_1_OFF_threshold  < LED_1 亮的時間 > + led_1_ON_threshold            
#define led_2_ON_threshold   < LED_2 暗的時間 >                                 
#define led_2_OFF_threshold  < LED_2 亮的時間 > + led_2_ON_threshold            
                                                                                
#define LED_ON  1                                                               
#define LED_OFF 0                                                               
                                                                                
uint8_t led_1_action = LED_OFF;                                                 
uint8_t led_2_action = LED_OFF;                                                 
                                                                                
void timer_interrupt_handler()                                                  
{                                                                               
    led_1_counter++;                                                            
    led_2_counter++;
    
    if(led_1_counter == led_1_ON_threshold) led_1_action = LED_ON;              
    if(led_1_counter >= led_1_OFF_threshold)                                    
    {                                                                           
        led_1_counter = 0;                                                      
        led_1_action = LED_OFF;                                                 
    }                                                                           
                                                                                
    if(led_2_counter == led_2_ON_threshold) led_2_action = LED_ON;              
    if(led_2_counter >= led_2_OFF_threshold)                                    
    {                                                                           
        led_2_counter = 0;                                                      
        led_2_action = LED_OFF;                                                 
    }                                                                           
}   

void main()                                                                     
{                                                                               
    // 初始化你的 timer,timer中斷時間的長短依據你的閃爍頻率調整                
    initialize_timer_module(&timer_interrupt_handler);                          
                                                                                
    while(1)                                                                    
    {                                                                           
        if(led_1_action == LED_ON) set_LED_ON(LED_1);                           
        else if(led_1_action == LED_OFF) set_LED_OFF(LED_1);                    
                                                                                
        if(led_2_action == LED_ON) set_LED_ON(LED_2);                           
        else if(led_2_action == LED_OFF) set_LED_OFF(LED_2);                    
    }                                                                           
}


Create a new paste based on this one


Comments: