Code Breakdown:
1. Preprocessor Directives:
#include <pic.h>: Includes the header filepic.h, which contains definitions and declarations specific to PIC microcontrollers.#define _XTAL_FREQ 4000000: Defines a preprocessor constant_XTAL_FREQwith the value 4000000, representing the crystal frequency in Hertz. This value is used by the compiler to calculate delay times and other timing-related calculations.
2. Variable Declarations:
unsigned char count = 0;: Declares an unsigned character variablecountand initializes it to 0. This variable is used to keep track of the number of 50ms intervals that have elapsed since the last second change.unsigned char sec = 0;: Declares an unsigned character variablesecand initializes it to 0. This variable represents the current seconds value displayed on the seven-segment display.
3. main() Function:
-
Initialization:
TRISA = 0;: Configures PORTA as output pins.TRISB = 0;: Configures PORTB as output pins.TRISC = 0;: Configures PORTC as output pins.ADCON1 = 0x07;: Configures the analog-to-digital converter (ADC) module.RA1 = 1; RA2 = 1; RA3 = 1; RA5 = 1;: Activates the common anode segments of the seven-segment display.T0CS = 0;: Selects the internal clock source (Fosc/4) for TMR0.PSA = 0;: Assigns the prescaler to TMR0.PS2 = 1; PS1 = 1; PS0 = 1;: Sets the prescaler to 256, providing a timer tick every 50 milliseconds.TMR0 = 61;: Initializes TMR0 with the value 61 to generate a 50ms delay.RC0 = 1;: Sets the output on RC0.
-
Main Loop:
while (1) { ... }: The main loop continues indefinitely.- Interrupt Handling:
if (T0IF) { ... }: Checks if the TMR0 interrupt flag is set.TMR0 = 61;: Reloads TMR0 with the value 61 to start another 50ms delay.count++;: Increments thecountvariable.if (count == 20) { ... }: Checks if 20 50ms intervals have elapsed (1 second).RC0 = !RC0; RC5 = !RC5;: Toggles the RC0 and RC5 outputs.seconds++;: Increments thesecondsvariable.if (seconds == 10) { seconds = 0; }: Resets thesecondsvariable to 0 if it reaches 10.T0IF = 0;: Clears the TMR0 interrupt flag.
- Display Update:
switch (seconds) { ... }: Uses a switch statement to select the appropriate seven-segment display pattern based on the currentsecondsvalue.- The case statements within the switch block assign the corresponding pattern to PORTB to display the seconds on the seven-segment display.
Explanation:
The code implements a digital clock that displays the seconds on a seven-segment display. The main components of the code are:
- Timer Initialization: TMR0 is configured as an internal timer with a prescaler of 256, providing a timer tick every 50 milliseconds.
- Interrupt Handling: The TMR0 interrupt is used to increment a counter and update the seconds display.
- Display Logic: The
switch(sec)statement controls the seven-segment display outputs based on the current seconds value.
#include <pic.h>
#define _XTAL_FREQ 4000000
unsigned char count=0;
unsigned char sec=0;
void main()
{
TRISA=0;
PORTA=0;
PORTB=0;
TRISB=0;
TRISC=0; // PORTC output
PORTC=0;
ADCON1=0x07;
RA1=1; // digit 1 on
RA2=1;
RA3=1;
RA5=1;
T0CS=0; // Internal Mode (Fosc/4)
PSA=0; // Prescaler assigned to TMR0
PS2=1;
PS1=1;
PS0=1;
TMR0=61; // (256-195=61) for 50mS time
RC0=1; // output on
while(1)
{
if(T0IF==1){
TMR0=61;
count++;
if (count==20){
RC0=!RC0; //RC0 toggle
RC5=!RC5;
sec++;
if (sec==10)
{
sec=0;
}
T0IF=0;
}
}
switch(sec)
{
case 0:
PORTB=0x3F;
break;
case 1:
PORTB=0x06;
break;
case 2:
PORTB=0x5B;
break;
case 3:
PORTB=0x4F;
break;
case 4:
PORTB=0x66;
break;
case 5:
PORTB=0x6D;
break;
case 6:
PORTB=0x7D;
break;
case 7:
PORTB=0x07;
break;
case 8:
PORTB=0x7F;
break;
case 9:
PORTB=0x6F;
break;
}
//T0IF=0;
}
}
#define _XTAL_FREQ 4000000
unsigned char count=0;
unsigned char sec=0;
void main()
{
TRISA=0;
PORTA=0;
PORTB=0;
TRISB=0;
TRISC=0; // PORTC output
PORTC=0;
ADCON1=0x07;
RA1=1; // digit 1 on
RA2=1;
RA3=1;
RA5=1;
T0CS=0; // Internal Mode (Fosc/4)
PSA=0; // Prescaler assigned to TMR0
PS2=1;
PS1=1;
PS0=1;
TMR0=61; // (256-195=61) for 50mS time
RC0=1; // output on
while(1)
{
if(T0IF==1){
TMR0=61;
count++;
if (count==20){
RC0=!RC0; //RC0 toggle
RC5=!RC5;
sec++;
if (sec==10)
{
sec=0;
}
T0IF=0;
}
}
switch(sec)
{
case 0:
PORTB=0x3F;
break;
case 1:
PORTB=0x06;
break;
case 2:
PORTB=0x5B;
break;
case 3:
PORTB=0x4F;
break;
case 4:
PORTB=0x66;
break;
case 5:
PORTB=0x6D;
break;
case 6:
PORTB=0x7D;
break;
case 7:
PORTB=0x07;
break;
case 8:
PORTB=0x7F;
break;
case 9:
PORTB=0x6F;
break;
}
//T0IF=0;
}
}

Comments
Post a Comment