/** * 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 }
Standard input is empty
/**
* 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
}