九齐单片机定时器

/* =========================================================================
 *  Project:       Timer Interrupt & WDT Interrupt
 *  File:          main.c
 *  Description:   Set Timer0、Timer1、WDT Interrupt
 *                 1. FINST = 4MHz/4T(I_HRC) 
 *                 2. Timer0 overflow interrupt flag bit will be set every 2048us then toggle PB0 output state  (2048us = 1us*256*8)
 *                 3. Timer1 underflow interrupt flag bit will be set every 1024us then toggle PB1 output state  (1024us = 1us*256*4)
 *                 4. WDT timeout interrupt flag bit will be set every 3.5ms then toggle PB2 output state
 *                    4.1  Set project options : WDT Time Base = 3.5ms
 * 
 *  Author:        JasonLee
 *  Version:       V1.0
 *  Date:          2017/11/08
 =========================================================================*/
#include <ny8.h>
#include "ny8_constant.h"
#define UPDATE_REG(x)    __asm__("MOVR _" #x ",F")

//! interrupt service routine
void isr(void) __interrupt(0)
{
    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;    // Enable Timer0、Timer1、WDT overflow interrupt

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值