fork download
  1. /**
  2.  * File: main.c
  3.  * Target: PIC18F4520
  4.  * Compiler: MPLAB XC8
  5.  *
  6.  * Description: Displays the first 5 digits of student ID (82258) in 4-bit BCD
  7.  * format on PORTB (RB0-RB3). Uses Timer3 to generate a precise
  8.  * 2-second delay between digit transitions.
  9.  */
  10.  
  11. // PIC18F4520 Configuration Bit Settings
  12. #pragma config OSC = INTIO67 // Internal oscillator, port function on RA6 and RA7
  13. #pragma config WDT = OFF // Watchdog Timer Enable bit (WDT disabled)
  14. #pragma config PWRT = OFF // Power-up Timer Enable bit (PWRT disabled)
  15. #pragma config BOREN = OFF // Brown-out Reset Enable bits (Brown-out Reset disabled)
  16. #pragma config PBADEN = OFF // PORTB A/D Enable bit (PORTB pins configured as digital I/O on Reset)
  17. #pragma config LVP = OFF // Single-Supply ICSP Enable bit (LVP Disabled)
  18.  
  19. #include <xc.h>
  20.  
  21. // Function Declarations
  22. void init_system(void);
  23. void wait_2_seconds(void);
  24.  
  25. void main(void) {
  26. // Array storing the 4-bit BCD codes for the student ID: 8, 2, 2, 5, 8
  27. unsigned char student_id[5] = {0x08, 0x02, 0x02, 0x05, 0x08};
  28. unsigned char i = 0;
  29.  
  30. init_system(); // Initialize I/O pins and peripherals
  31.  
  32. while(1) {
  33. for(i = 0; i < 5; i++) {
  34. PORTB = student_id[i]; // Output current BCD digit to RB0-RB3
  35. wait_2_seconds(); // Block for 2 seconds using Timer3 hardware
  36. }
  37. }
  38. }
  39.  
  40. /**
  41.  * Initializes the I/O configurations and setup requirements for Timer3
  42.  */
  43. void init_system(void) {
  44. ADCON1 = 0x0F; // Disable analog functions, set all pins to digital I/O
  45. TRISB = 0x00; // Configure all PORTB pins as outputs
  46. PORTB = 0x00; // Initialize PORTB to 0x00 (Clear all LEDs)
  47.  
  48. // Timer3 Configuration
  49. // RD16 = 0: Read/Write Timer3 in two 8-bit operations
  50. // T3CCP2:T3CCP1 = 00: Timer3 is not the clock source for CCP modules
  51. // T3CKPS1:T3CKPS0 = 11: Set internal prescaler to 1:8
  52. // T3SYNC = 1: Do not synchronize external clock input (ignored for internal clock)
  53. // TMR3CS = 0: Use internal instruction cycle clock (Fosc / 4)
  54. // TMR3ON = 0: Leave Timer3 turned off initially
  55. T3CON = 0x30;
  56. }
  57.  
  58. /**
  59.  * Uses Timer3 to generate a hardware-accurate 2-second blocking delay
  60.  */
  61. void wait_2_seconds(void) {
  62. // Load calculated pre-scale initial values to hit exactly 2 seconds from overflow
  63. // Target Delay = 2s, Fosc = 1MHz, Fcyc = 250kHz, Prescaler = 1:8 -> 62500 counts
  64. // 65536 - 62500 = 3035 -> 0x0BDB
  65. TMR3H = 0x0B; // Load High Byte first
  66. TMR3L = 0xDB; // Load Low Byte second
  67.  
  68. PIR2bits.TMR3IF = 0; // Clear the Timer3 Overflow Interrupt Flag
  69. T3CONbits.TMR3ON = 1; // Turn on Timer3 to start counting
  70.  
  71. // Poll the overflow flag until Timer3 finishes counting 62,500 increments
  72. while(PIR2bits.TMR3IF == 0);
  73.  
  74. T3CONbits.TMR3ON = 0; // Stop Timer3 until the next function call
  75. }
  76.  
Success #stdin #stdout 0.03s 25316KB
stdin
Standard input is empty
stdout
/**
 * File:   main.c
 * Target: PIC18F4520
 * Compiler: MPLAB XC8
 * 
 * Description: Displays the first 5 digits of student ID (82258) in 4-bit BCD 
 *              format on PORTB (RB0-RB3). Uses Timer3 to generate a precise 
 *              2-second delay between digit transitions.
 */

// PIC18F4520 Configuration Bit Settings
#pragma config OSC = INTIO67    // Internal oscillator, port function on RA6 and RA7
#pragma config WDT = OFF        // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRT = OFF       // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF      // Brown-out Reset Enable bits (Brown-out Reset disabled)
#pragma config PBADEN = OFF     // PORTB A/D Enable bit (PORTB pins configured as digital I/O on Reset)
#pragma config LVP = OFF        // Single-Supply ICSP Enable bit (LVP Disabled)

#include <xc.h>

// Function Declarations
void init_system(void);
void wait_2_seconds(void);

void main(void) {
    // Array storing the 4-bit BCD codes for the student ID: 8, 2, 2, 5, 8
    unsigned char student_id[5] = {0x08, 0x02, 0x02, 0x05, 0x08};
    unsigned char i = 0;

    init_system();              // Initialize I/O pins and peripherals

    while(1) {
        for(i = 0; i < 5; i++) {
            PORTB = student_id[i];  // Output current BCD digit to RB0-RB3
            wait_2_seconds();       // Block for 2 seconds using Timer3 hardware
        }
    }
}

/**
 * Initializes the I/O configurations and setup requirements for Timer3
 */
void init_system(void) {
    ADCON1 = 0x0F;              // Disable analog functions, set all pins to digital I/O
    TRISB = 0x00;               // Configure all PORTB pins as outputs
    PORTB = 0x00;               // Initialize PORTB to 0x00 (Clear all LEDs)

    // Timer3 Configuration
    // RD16 = 0: Read/Write Timer3 in two 8-bit operations
    // T3CCP2:T3CCP1 = 00: Timer3 is not the clock source for CCP modules
    // T3CKPS1:T3CKPS0 = 11: Set internal prescaler to 1:8
    // T3SYNC = 1: Do not synchronize external clock input (ignored for internal clock)
    // TMR3CS = 0: Use internal instruction cycle clock (Fosc / 4)
    // TMR3ON = 0: Leave Timer3 turned off initially
    T3CON = 0x30;               
}

/**
 * Uses Timer3 to generate a hardware-accurate 2-second blocking delay
 */
void wait_2_seconds(void) {
    // Load calculated pre-scale initial values to hit exactly 2 seconds from overflow
    // Target Delay = 2s, Fosc = 1MHz, Fcyc = 250kHz, Prescaler = 1:8 -> 62500 counts
    // 65536 - 62500 = 3035 -> 0x0BDB
    TMR3H = 0x0B;               // Load High Byte first
    TMR3L = 0xDB;               // Load Low Byte second

    PIR2bits.TMR3IF = 0;        // Clear the Timer3 Overflow Interrupt Flag
    T3CONbits.TMR3ON = 1;       // Turn on Timer3 to start counting

    // Poll the overflow flag until Timer3 finishes counting 62,500 increments
    while(PIR2bits.TMR3IF == 0);

    T3CONbits.TMR3ON = 0;       // Stop Timer3 until the next function call
}