Skip to main content

Counter

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

Popular posts from this blog

ANALOGUE to DIGITAL (A/D) Converter in PIC16F87X Series

ANALOGUE to DIGITAL (A/D) Converter What is a Sample & Sample rate? A "sample" is a single measurement of amplitude. Sample rate is simply the number of samples (or measurements of amplitude) taken per second. Sampling Intervals Quantization The samples are assigned a binary number approximating their sampled value. Quantizing divides up the sampled voltage range into 2n-1 quantizing intervals, where “n” is the number of bits per sample (the sampling resolution). For example, an 8-bit system can identify 28 (256) discrete sampled signal values (255 quantizing intervals). The amplitude of such a signal can occupy the entire quantizing range. A/D Reference Voltages Successive Approximation A/D Example 1 Example 2  Example 3 A/D Converter Modules in PIC16F87X series 28-pin devices has 5 modules. (AN0-AN4)         PIC16F873        PIC16F876 4...

Other PicBasic commands 01

W e shall n o w brief l y look at the remaining PicBasic commands in alphabetical order w hich are useful during the pr o g ram d e v elopment. More details about these commands can be obtained from the PicBasic manual. EEP R OM EEP R OM Location, (constant, constant,….., constant) Thi s comman d store s constant s i n consecut i v e b yte s i n on-chi p EEP R O M memo r y . Th e command on l y w ork s wit h th e PI C microcontroller s tha t h a v e EEP R OM , suc h a s th e PIC16F84 , PIC16F877, etc . Locatio n i s optional , an d i f omitte d th e f irs t EEP R O M locatio n i s assumed . Constant s ca n be numeri c constant s o r strin g constants . String s ar e store d a s consecut i v e b yte s o f ASCI I v alues . An e xampl e i s g i v e n bel o w . EEP R OM 3, (5, 2, 8)        ‘ Store 5 in location 3, ‘ 2 in location 4, and 8 in ‘ location 5 END END St...

Voltmeter

This code to read an analog temperature sensor, convert the reading to a digital value using the ADC, and display the temperature on a 4-digit 7-segment display. The display is updated periodically based on a timer interrupt. 1.Initialization: Setting up ports, configuring ADC, and Timer0. 2.Infinite Loop: Continuously reads analog input and updates the display. 3.ADC Conversion: Reads analog voltage from a sensor. 4.Voltage Calculation: Converts ADC value to voltage. 5.Digit Extraction: Extracts individual digits from the temperature/voltage value. 6.Display Function: Displays the digits on a 4-digit 7-segment display with slight delays. --------------------------------------------- #include <pic.h> // Include the header file for PIC microcontroller family. unsigned char PORTB_value[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}; // Array containing values for 7-segment display. unsigned int a2d_value,temp; // Variables for analog to digital conversion and temporary sto...