PIC定时器中断

2019-04-15 14:28发布


----------------------------------------------------------------------------------- #ifndef __PIC18F4580_h
#define __PIC18F4580_h #include
#include
#include
#include
#include #define InterruptEnable  INTCONbits.PEIE = 1,INTCONbits.GIE = 1
#define InterruptDisable INTCONbits.PEIE = 0,INTCONbits.GIE = 0
extern void IntTimer0(void); #endif -------------------------------------------------------------------------------------- #include"PIC18F4580.h"
#pragma interrupt SelectHPrioInt
#pragma code HiPrio_Int = 0x08
void HighPrioInt(void)
{
 _asm
  goto SelectHPrioInt
 _endasm
}
#pragma code void SelectHPrioInt (void)
{
 if(INTCONbits.TMR0IF) 
 {
  IntTimer0(); 
 }
} --------------------------------------------------------------------------------------------- #include"PIC18F4580.h"
#pragma config OSC =IRCIO67, WDTPS = 1024, WDT = ON,PBADEN = OFF,DEBUG = OFF,MCLRE = OFF,LVP = OFF
#define u16 unsigned int
#define u8 unsigned char
#define LED LATCbits.LATC2
void WdtReset(void)
{
 _asm
  CLRWDT
 _endasm
} void delay_n_ms(u16 n)//8MHz时钟下的ms延时
{
 T1CON = 0xB0; //16位定时器,系统8分频
    while(n!=0)
    {
        TMR1L = 0x31;
     TMR1H = 0xF8;
        PIR1bits.TMR1IF = 0;   //标志位清空
     T1CONbits.TMR1ON = 1;  //使能定时器
        n--;
        while(!PIR1bits.TMR1IF){;}
        T1CONbits.TMR1ON =0; //关闭定时器
    }
} void InitTimer0(void)
{
 T0CON = 0x05; //系统64分频
 TMR0L = 0XEF; //1秒中断
 TMR0H = 0x85;
 INTCONbits.TMR0IE = 1;  //打开定时器0中断
 INTCON2bits.TMR0IP = 1; //设置优先级为最高
 T0CONbits.TMR0ON =1;    //打开定时器0
 INTCONbits.TMR0IF = 0;  //标志位清空
 T0CONbits.TMR0ON = 1;
}
void IntTimer0(void)

 TMR0L = 0XEF;
 TMR0H = 0x85;
 T0CONbits.TMR0ON =1;
 LED=~LED;
    WdtReset();
 INTCONbits.TMR0IF = 0;
} void main(void)
{
    OSCCON = 0x72;//OSCCON = 0x72;设置OSC时钟输出=8M
                  //OSCCON = 0x62;设置OSC时钟输出=4M
                  //OSCCON = 0x52;设置OSC时钟输出=2M
                  //OSCCON = 0x42;设置OSC时钟输出=1M
                  //OSCCON = 0x32;设置OSC时钟输出=500KHz
                  //OSCCON = 0x22;设置OSC时钟输出=250KHz
                  //OSCCON = 0x12;设置OSC时钟输出=125KHz
                  //OSCCON = 0x02;设置OSC时钟输出=31KHz(from either INTOSC/256 or INTRC directly)     InterruptDisable;
    InitTimer0();
    InterruptEnable;     TRISC=0X00; //TRISC寄存器被赋值,PORTC每一位都为输出
    LED=0;
    for(;;)
    {
        //LED=0;
        //delay_n_ms(500);
        //LED=1;
        //delay_n_ms(500);
    }
}