仿真51单片机定时器延迟一秒

就是插入一个循环没有任何反应,请问这是为什么,该怎么改

#include <ny8.h>
#include "ny8_constant.h"
#define UPDATE_REG(x)    __asm__("MOVR _" #x ",F")
void delay(void)
{
    unsigned char i,j;
    for(i=5;i>0;i--)
    for(j=100;j>0;j--);
}
//! interrupt service routine
void isr(void) __interrupt(1)
{
    if(INTFbits.T1IF)
    { 
        PORTB ^= (1<<1);                    // PB1 Toggle
        INTF= (unsigned char)~(C_INT_TMR1);    // Clear T1IF flag bit    
    }
    
    if(INTFbits.T0IF)
    { 
        PORTB ^= 1;                            // PB0 Toggle
        INTF= (unsigned char)~(C_INT_TMR0);    // Clear T0IF flag bit    
    }
    
    if(INTFbits.WDTIF)
    { 
        PORTB ^= (1<<2);                    // PB2 Toggle
        INTF= (unsigned char)~(C_INT_WDT);    // Clear WDTIF flag bit        
    }    

}

void main(void)
{
unsigned char R_shift_regl = 0xFF;
//;Initial GPIO     
    IOSTB =  C_PB5_Input | C_PB4_Input | C_PB3_Input;     // Set PB0 & PB1 to input mode,others set to output mode
    PORTB = 0x07;                           // PB0、PB1 & PB2 are output High
    DISI();
//;Initial Timer0
    PCON1 = C_TMR0_Dis;                        // Disable Timer0
    TMR0 = 0;                                // Load 0x00 to TMR0 (Initial Timer0 register)
    
    T0MD = C_PS0_TMR0 | C_PS0_Div8 ;        // Prescaler0 is assigned to Timer0, Prescaler0 dividing rate = 1:8,clock source is instruction clock
    
//;--Initial WDT (if WDT needs prescaler0 dividing rate )--------------------------------------------------                                       
//    T0MD = C_PS0_WDT                        // Prescaler0 is assigned to WDT, Prescaler0 dividing rate = 1:2 (WDT select interrupt)
//;--------------------------------------------------------------------------------------------------------        

//;Initial Timer1    
    TMR1 = 0xFF;                            // Load 0xFF to TMR1 (Initial Timer1 register)
    T1CR1 = C_TMR1_Reload | C_TMR1_En;      // Enable Timer1, Initial value reloaded from TMR1, Non-stop mode 
    T1CR2 = C_TMR1_ClkSrc_Inst | C_PS1_Div4;    // Enable Prescaler1, Prescaler1 dividing rate = 1:4, Timer1 clock source is instruction clock
    
//;Setting Interrupt Enable Register    
    INTE = C_INT_WDT | C_INT_TMR1 | C_INT_TMR0;    //        

//;Initial Power control register
    PCON = C_WDT_En | C_LVR_En;                // Enable WDT ,  Enable LVR
    
//;Enable Timer0 & Global interrupt bit 
    PCON1 = C_TMR0_En;                        // Enable Timer0
    ENI();                                    // Enable all unmasked interrupts            
    
    while(1);
}


你没调用延时啊
延时函数
你在需要延时的地方调用

【以下回答由 GPT 生成】

问题分析: 根据给出的代码,可以看出使用了Timer1来实现定时器功能,并在其中插入了中断函数isr()来处理定时器中断。但是循环延时的函数delay()并没有在代码中使用,并且没有调用delay()函数来实现延时一秒的功能。因此,可以在main()函数中添加对delay()函数的调用来实现定时器延迟一秒的功能。

解决方案: 在main()函数中添加对delay()函数的调用来实现定时器延迟一秒的功能。

修改后的代码如下:

void main(void)
{
    unsigned char R_shift_regl = 0xFF;

    //Initial GPIO     
    IOSTB =  C_PB5_Input | C_PB4_Input | C_PB3_Input;     // Set PB0 & PB1 to input mode,others set to output mode
    PORTB = 0x07;                           // PB0、PB1 & PB2 are output High
    DISI();

    //Initial Timer0
    PCON1 = C_TMR0_Dis;                        // Disable Timer0
    TMR0 = 0;                                // Load 0x00 to TMR0 (Initial Timer0 register)

    T0MD = C_PS0_TMR0 | C_PS0_Div8 ;        // Prescaler0 is assigned to Timer0, Prescaler0 dividing rate = 1:8, clock source is instruction clock

    //Initial WDT (if WDT needs prescaler0 dividing rate )
    // T0MD = C_PS0_WDT                        // Prescaler0 is assigned to WDT, Prescaler0 dividing rate = 1:2 (WDT select interrupt)    
//--------------------------------------------------------------------------------------------------------        


    //Initial Timer1    
    TMR1 = 0xFF;                            // Load 0xFF to TMR1 (Initial Timer1 register)
    T1CR1 = C_TMR1_Reload | C_TMR1_En;      // Enable Timer1, Initial value reloaded from TMR1, Non-stop mode 
    T1CR2 = C_TMR1_ClkSrc_Inst | C_PS1_Div4;    // Enable Prescaler1, Prescaler1 dividing rate = 1:4, Timer1 clock source is instruction clock

    //Setting Interrupt Enable Register    
    INTE = C_INT_WDT | C_INT_TMR1 | C_INT_TMR0;    //        

    //Initial Power control register
    PCON = C_WDT_En | C_LVR_En;                // Enable WDT ,  Enable LVR

    //Enable Timer0 & Global interrupt bit 
    PCON1 = C_TMR0_En;                        // Enable Timer0
    ENI();                                    // Enable all unmasked interrupts  

    delay(); // 添加延时函数的调用,延时一秒

    while(1);
}

这样修改后的代码就可以实现使用定时器延迟一秒的功能了。



【相关推荐】



如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^