tmr0.c

来自「这是MICROCHIP 的PIC 系列单片机常用 的C语言编译器。特别对PIC1」· C语言 代码 · 共 93 行

C
93
字号
#pragma option v;
/*
   These routines may be adapted for any purpose when
   used with the MPC Code Development system.  No
   warranty is implied or given as to their usability
   for any purpose.

        (c) Copyright 1995,1996

        Byte Craft Limited
        Waterloo, Ontario
        Canada N2J 4E4
        (519) 888-6911

        Sherif Abdel-Kader

 This example demonstrates using the TMR0 interrupt
 on the 16C74. The technique is the same for all PIC16Cxx devices

 With a 4MHz crystal and a 1:256 prescale value,
 a TMR 0 interrupt should occur approximately every
 65ms. Each time an interrupt occurs, 'i' is incremented
 and sent to PORT B. By connecting PORT B to 8-LEDs,
 one can verify that the value on PORTB is incrementing
 at uniform intervals.
*/

#define NOLONG     // Comment this out if using 'long' variables
#include <16C621.h>

void handle_TMR0();

unsigned int i=0;   // counter


///////////////////////////////////////////////////////////

void __INT(void)
{

 SaveContext();

  if (INTCON.T0IF)   // TMR0 overflowed
   { 
    INTCON.T0IF = 0; // Clear T0IF
    handle_TMR0();   // Call handler
   } // if

 RestoreContext();
}

///////////////////////////////////////////////////////////

void main()
{


  // Mask and clear all interrupts
     INTCON = 0;
     PIR1 = 0;
     PIE1 = 0;

     TRISB =0;       // PORT B in output mode

     OPTION = 0xD7;  // Internal clock for TMR0
                     // Prescaler = 1:256

  // Enable desired interrupts
     INTCON.T0IE = 1;      // Timer 0 overflow interrupt

  // Enable unmasked interrupts
     INTCON.GIE = 1;       // Global interrupt enable

  // Loop until an interrupt occurs
     while(1)
       {
       }
} // main


///////////////////////////////////////////////////////////

void handle_TMR0()
{
  // Code for handling TMR0 overflow interrupt
  //
  PORTB=i;
  i++;
}
//////////////////////////////////////////////////////////


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?