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

0
3108

Timer

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>
// CONFIG
#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;
            Count++;
            if(Count == 125)
            {
                Count = 0;
                PORTCbits.RC0 ^= 1;                     //toggle the LED
            }
        }
    }
}

Mô Phỏng

Link download: https://drive.google.com/drive/folders/1pu7wmjVov95bgEkeI3YDmzlRo4fTOz-m?usp=sharing

Tham khảo tại: https://xcvn.blogspot.com/

LEAVE A REPLY

Please enter your comment!
Please enter your name here