PIC16F690 that runs 3 7-segment displays with the help of 74ls47


/*
 * File:   main.c
 * Author: root
 * operate 3 bit 7-segment display
 * Created on April 10, 2025, 7:15 PM
 */

// CONFIG
#pragma config FOSC = INTRCIO   // Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON       // MCLR Pin Function Select bit (MCLR pin function is MCLR)
#pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = OFF      // Brown-out Reset Selection bits (BOR disabled)
#pragma config IESO = OFF       // Internal External Switchover bit (Internal External Switchover mode is disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include 
#define _XTAL_FREQ 4000000
#define dly 3

char run = 1;

void __interrupt() isr(void) {
    // RA2
    if (INTCONbits.INTF == 1) {
        run = 0; 
        INTCONbits.INTF = 0;
    }
}

void main(void) {
    ANSEL = 0x00;
    ANSELH = 0x00;
    TRISA = 0x0C;  // RA0-RA5 address outputs, RA2, RA3 input;   
    TRISB = 0x00;  // RB4-RB7 data outputs
    TRISC = 0xFF;  // RC0-RC7 lines inputs
    PORTA = 0x00;
    PORTB = 0x00;
   
    INTCONbits.GIE = 1;
    INTCONbits.PEIE = 1;
    INTCONbits.INTE = 1;
    OPTION_REGbits.INTEDG = 1;  // rising edge RA2
    
    unsigned char data_in, n100, n10, n1;
    unsigned char once = 1;    

    while (1) {
    // read and convert
       PORTB = 0x00; 
       data_in = PORTC;
       if (once) {
          data_in = 0;
          once = 0;
       }
       n100 = data_in / 100;
       n10 = (data_in / 10) % 10;   
       n1 = data_in % 10;
       run = 1;
       
       while (run) {
       // output to 7-segments, multiplexing RA0 RA1 RA5
         PORTB = 0x00;
         PORTB |= n100 << 4;  // 0,1,2 
         RA0 = 1;
         __delay_ms(dly);
         RA0 = 0;
         PORTB = 0x00;
         PORTB |= n10 << 4; // 0-9       
         RA1 = 1;
         __delay_ms(dly);
         RA1 = 0;
         PORTB = 0x00;
         PORTB |= n1 << 4;
         RA5 = 1;
         __delay_ms(dly);
         RA5 = 0;
       }
    }
    return;
}