技术热线: 4007-888-234

CCSC timer.c程序使用MCD2demo 用PIC16F877A片内Timer1定时

更新时间: 2019-03-25
阅读量:702


CCSC timer.c程序使用MCD2demo 用PIC16F877A片内Timer1定时
/*=============================================================================
使用PIC16F877A片内Timer1定时,运行程序:
        程序开始    -> LED0亮,
        每0.5秒钟   -> LED7交替亮灭,
        循环10次后  -> Timer1中断关闭,LED6亮,LED7保持当前状态。
=============================================================================*/
///////////////////////////////////////////////////////////////////////////////
// MPLAB IDE V7.11 + CCS C3.18
// Use 16F877A internal timer1 for MCD2-demo
//
// by
LW7807@163.com
// 2005/06/22
///////////////////////////////////////////////////////////////////////////////
#include <16F877A.h>                            // PIC16F877 header file
#use delay(clock=4000000)                       // for 4Mhz crystal
#fuses XT, NOWDT, NOPROTECT, NOLVP              // for debug mode


#define TIMER1_500MS            (0xFFFF - 62500 + 1)
#define DO_THIS_TIMES           10

#define LED7_IO                 PIN_C7
#define LED6_IO                 PIN_C6
#define LED0_IO                 PIN_C0
#define LED_ON                  1
#define LED_OFF                 0


int1    led7_status             = LED_OFF;
int8    cnt                     = 0;

///////////////////////////////////////////////////////////////////////////////
//
#int_TIMER1
void timer1_isr(void)
{
        led7_status = ~led7_status;

        ++ cnt;
        if(cnt >= 2*DO_THIS_TIMES)
                {
                disable_interrupts(INT_TIMER1);
                output_bit(LED6_IO, LED_ON);
                }
}//end timer1_isr()

/*===========================================================================*/
void main(void)
{
        // begin program
        output_bit(LED0_IO, LED_ON);

        // setup timer1 for 0.5s interrupt
        setup_timer_1(T1_INTERNAL | T1_DIV_BY_8);
        set_timer1(TIMER1_500MS);
        enable_interrupts(INT_TIMER1);
        enable_interrupts(GLOBAL);

        // show led7 status
        while(1){
                output_bit(LED7_IO, led7_status);
                }