gcht101hello/code20240411.html 20240411
Sorce Code:P6MX887timer0
hello

    // P6MX887timer0 20240410
    // description: An LED is blinking  using timer0
    // board: breadboard PIC16F887 5V crystal 16MHz
    // MPU; PIC16F887 Flash:8KW, SRAM:358B, EEPROM:256B 
    // DEVICE: TEL USB-TTL
    // TIMER:3, CCP:1 USART:1,MSSP:1,10bitADC:14
    // DEVICE: crystal 16MHz,LED, USB-TTL
    // language: C XC8 v2.46
    // IDE: MPLAB X v6.20, XC8 v2.46:PIC16F1xxx_DFP 1.6.158
    // Debug Tool; PICkit3,   LVP = OFF
    // PINOUT: PIC16F887 DIP40pin
    //RST    1 RE3/MCLR   RB7 40 ICSPDAT 
    //       2 RA0        RB6 39 ICSPCLK
    //       3 RA1        RB5 38
    //       4 RA2        RB4 37
    //       5 RA3        RB3 36 
    //       6 RA4        RB2 35
    //       7 RA5        RB1 34
    //       8 RE0        RB0 33 LED
    //       9 RE1        VDD 32 5V 
    //       10 RE2       VSS 31 GND
    //5V     11 VDD       RD7 30
    //GND    12 VSS       RD6 29
    //C Xtal 13 RA7       RD5 28
    //C Xtal 14 RA6       RD4 27
    //       15 RC0       RC7 26
    //       16 RC1       RC6 25
    //       17 RC2       RC5 24
    //       18 RC3       RC4 23
    //       19 RD0       RD3 22
    //       20 RD1       RD2 21
    //----------------------------------------
    
    #pragma config FOSC = HS, PWRTE = OFF,PWRTE = OFF, MCLRE = ON   // XtalOSC 16MHz
    #pragma config WDTE = OFF        // Watchdog Timer Enable bit (WDT enabled)
    #pragma config CP = OFF,  CPD = OFF, BOREN = OFF    // 
    #pragma config IESO = OFF ,FCMEN = OFF , LVP = OFF   // 
    #pragma config BOR4V = BOR40V, WRT = OFF 
    //------------------------------------
    #include 
    #define _XTAL_FREQ 16000000
    #define LEDB0 RB0
    struct Codeinfo{
        char codeIed[8];
        char function[8];
        char mpu[8];
        char version;
        long date;
    };
    struct Led{
        char count;
    };
    struct Codeinfo ci={"P6MX887","timer0","P6F887",0,20240410};
    struct Led le={0};
    //--------------------------------------
    void ledBlink(void);
    void gpioBegin(void);
    void timer0Begin(void);
    //---------------------------------------
    int main(void){  
      gpioBegin();  
      timer0Begin();
      PEIE=1;  // enable interrupt
      GIE=1;   // enable interrupt
      RB0=1;   // LED ON
    while(1){
        if(le.count>50){   // check count 
            le.count=0;
            LEDB0=!LEDB0;      // toggle LED
        }
    }//end main
    }//end main 
    //--------------------------------------------
    void __interrupt() T0isr(void){
        if(T0IF){
             T0IF=0;
             le.count++;
        }
    }
    //-------------------------------------
    void gpioBegin(void){
      ANSEL=0;             // all digital
      ANSELH=0;            // all digital
      TRISB = 0b000000000; //RB as Output PIN
    }
    
    void timer0Begin(void){
        OPTION_REG = 0xD7; //timer0, internal clock(FOSC/4), prescaler 1:256   
        TMR0 = 0;          //
        T0IE=1;            // enable interrupt
        T0IF=0;            // flag clear
    }
    
    
    /*
    data:  35/368bytes 10%
    program: 151/8192bytes 2%
    */
    /*
     OPTION_REG: OPTION REGISTER
    RBPU INTEDG T0CS T0SE PSA PS2 PS1 PS0
    bit 7 RBPU: PORTB Pull-up Enable bit
    1 = PORTB pull-ups are disabled
    0 = PORTB pull-ups are enabled by individual PORT latch values
    bit 6 INTEDG: Interrupt Edge Select bit
    1 = Interrupt on rising edge of INT pin
    0 = Interrupt on falling edge of INT pin
    bit 5 T0CS: Timer0 Clock Source Select bit
    1 = Transition on T0CKI pin
    0 = Internal instruction cycle clock (FOSC/4)
    bit 4 T0SE: Timer0 Source Edge Select bit
    1 = Increment on high-to-low transition on T0CKI pin
    0 = Increment on low-to-high transition on T0CKI pin
    bit 3 PSA: Prescaler Assignment bit
    1 = Prescaler is assigned to the WDT
    0 = Prescaler is assigned to the Timer0 module
    bit 2-0 PS<2:0>: Prescaler Rate Select bits
    000 1:2
    001 1:4
    010 1:8
    011 1:16
    100 1:32
    101 1:64
    110 1:128
    111 1:256
    */