Re: 如何在PIC16實作優先權中斷?

作者 Eigen 於 2017年12月05日 20:19:29
想太多,要什麼優先權? 如果是 i2c master 中斷不用,一直loop 就可

pseudo code 大概就這

i2c_isr
()
{
    
收資料
    
    
if 資料收完
        set data_flag
}



main_LOOP(){

    if(
data_flag=1){
        
data_flag=0
        資料分析
        
        led_status_renew
()
    }

    if
time0_flag(set){    //4ms 中斷一次
        
time0_flag_clear
        ex
:電池正`瓻G、電剩一半 500ms  on off 切換 、 快沒電了 500ms off/ 50ms on
        
        if led_status= 正
`
            
led =on
        
else if led_status = @{
            
led_count--
            
            if(
led_count=0){
                if
led_on{
                    
led_off
                    led_count
= 500/4
                
}
                else {
                    
led_on
                    led_count
= 500/4
                
}
                
            }
        }else if
led_status = 快沒電{
            
led_count--
            
            if(
led_count=0){
                if
led_on{
                    
led_off
                    led_count
= 500/4
                
}
                else {
                    
led_on
                    led_count
= 50/4
                
}
                
            }
        }        
    }

}



如果是 i2c master ,整個都是由你主控,連i2c中斷都不用


main loop
{
    if
timer0_flag set{
        
timer0_flag clr    //4ms觸發一次
        
i2c_count--
        if
i2c_count =0{
            
i2c_count=250    //1sec 處理一次 i2c
            
            
i2c read / write / analyze  generate battery_status
        
}

        if
led_status= `    //4ms 處理一次
            led =on
        else if led_status = 一半
{
            led_count--
            
            if(led_count=0)
{
                if led_on
{
                    led_off
                    led_count= 500/4
                
}
                else
{
                    led_on
                    led_count= 500/4
                
}
                
            
}
        
}else if led_status = 快沒電{
            led_count--
            
            if(led_count=0)
{
                if led_on
{
                    led_off
                    led_count= 500/4
                
}
                else
{
                    led_on
                    led_count= 50/4
                
}
                
            
}
        
}        
        
    
    
}
}

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