BST1: TIMER 0 PIC16F877A với trình biên dịch XC8



Timer được sử dụng cho nhiều chức năng như :
– Định thì để tạo sự kiện
– Đếm sự kiện
-Tạo dạng sóng…

PIC16F877A có 3 Timers:
– Timer 0
– Timer 1
– Timer 2

Bài 8: Timer & Counter - PIONEER CLUB

Code Timer 0 nhấp nháy đèn LED đơn

#include <stdio.h>
#include <stdlib.h>
#define _XTAL_FREQ 4000000
#include <xc.h>
#pragma config FOSC = XT        // Oscillator Selection bits (XT oscillator)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF      // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = OFF        // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)
unsigned int Count = 0;
void main(void) {
    TRISCbits.TRISC0 = 0;                               //RC0 pin as output
    //config timer0
    TMR0 = 55;
    OPTION_REGbits.PSA = 0;
    OPTION_REGbits.PS2 = 0;
    OPTION_REGbits.PS1 = 1;
    OPTION_REGbits.PS0 = 0;
    OPTION_REGbits.T0CS = 0;
    while (1)
        if(INTCONbits.TMR0IF == 1)
            INTCONbits.TMR0IF = 0;
            TMR0 = 55;
            if(Count == 125)
                Count = 0;
                PORTCbits.RC0 ^= 1;                     //toggle the LED

Mô Phỏng

Link download:

Tham khảo tại:


  1. Do you mind if I quote a couple of your posts as long as I provide credit
    and sources back to your weblog? My website is in the exact same niche as yours and my visitors
    would truly benefit from some of the information you present here.
    Please let me know if this alright with you.
    Appreciate it!

  2. I precisely needed to appreciate you once again. I am not sure the things that I might have handled in the absence of the entire pointers provided by you on such a problem. It has been a very frightening difficulty in my circumstances, however , looking at the skilled mode you processed that made me to jump over fulfillment. I am happier for this help and in addition hope that you know what a great job you are always getting into instructing others through the use of your site. I know that you have never encountered all of us. Jenica Theodoric Ambrosane


Please enter your comment!
Please enter your name here