This code is tested on Pic 16f876
This code appears to be for a simple counting mechanism, where a value is incremented each time a certain input condition is met (RB7 is high). The count value is displayed on a seven-segment display, and it resets after reaching 10.
Code For counter
--------------------------------------------------------------
#include <pic.h> // Include the header file for PIC microcontrollers
#define _XTAL_FREQ 4000000 // Define the crystal frequency to be 4 MHz
unsigned char count; // Declare an 8-bit variable to store the count value
unsigned char PORTB_value[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}; // Define an array to hold values for displaying digits 0-9 on a 7-segment display
void main()
{
TRISA=0; // Configure all pins of PORTA as output
ADCON1=0x07; // Configure PORTA pins as digital I/O
TRISB=0x80; // Configure RB7 pin as input and others as output
PORTB=0; // Initialize PORTB to 0
PORTA=0; // Initialize PORTA to 0
count=0; // Initialize count variable to 0
while(1) // Infinite loop
{
if(RB7==1) // Check if RB7 (input pin) is high
{
__delay_ms(150); // Introduce a delay of 150 milliseconds
count++; // Increment count variable
if(count==10) // If count reaches 10
{
count=0; // Reset count to 0
}
RA1=1; // Turn on digit1 (assuming it controls a multi-digit display)
PORTB=PORTB_value[count]; // Display the value corresponding to count on PORTB using the PORTB_value array
while(RB7==1) // Wait until RB7 becomes low
{
// This loop ensures that the external event that triggered the count increment is over before proceeding
}
__delay_ms(150); // Introduce a delay of 150 milliseconds
}
}
}
--------------------------------------------------------------------
#define _XTAL_FREQ 4000000
This line defines the crystal frequency of the microcontroller, which is 4 MHz in this case.
unsigned char count; //8-bit variable
unsigned char PORTB_value[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
count: An 8-bit unsigned variable used for counting.
PORTB_value: An array of 10 elements, each representing a value to be displayed on a 7-segment display. These values are in hexadecimal notation.
void main()
{
// Configuration settings
TRISA=0; // PORTA output
ADCON1=0x07; // PORTA pin assigned as digital I/O
TRISB=0x80; // RB7 input
PORTB=0;
PORTA=0;
count=0;
// Infinite loop
while(1){
// Check if RB7 is high
if(RB7==1){
__delay_ms(150); // Delay
// Increment count
count++;
if(count==10){
count=0; // Reset count if it reaches 10
}
RA1=1; // Turn on digit1
// Display the value on PORTB corresponding to the count
PORTB=PORTB_value[count];
// Wait until RB7 becomes low
while(RB7==1){}
__delay_ms(150); // Delay
}
}
}
Configuration Settings:
TRISA=0: Configures all pins of PORTA as output.
ADCON1=0x07: Configures PORTA pins as digital I/O.
TRISB=0x80: Configures RB7 pin as input and all other pins as output.
PORTB=0 and PORTA=0: Initialize PORTB and PORTA to 0.
count=0: Initialize count variable to 0.
Infinite Loop:
*The program continuously executes the code inside the infinite loop.
*It checks if RB7 (input pin) is high.
*If RB7 is high, it increments the count variable.
*If count reaches 10, it resets count to 0.
*It then turns on digit1 (assuming it controls a multi-digit display), and displays the value corresponding to count on PORTB using the PORTB_value array.
*It waits until RB7 becomes low (assuming RB7 is some sort of input switch).
After that, it introduces a delay of 150 milliseconds before restarting the loop.
Comments
Post a Comment