Programming: views : 369

Seven segment display code using timer0 (Copy and paste) for AVR and detail explain using timer subroutine

This is very accurate and very easy code for coding any seven segment display. This is tested code amd you can just "copy and paste" it into your code for seven segment display. The best thing in this code is that it has buffer to store integer and you only need to update this buffer from anywhere in your code. I have also posted a seven segment code some year ago. it was also a great code however, this is upgrade version of this code. i have written this code in ATMEL STDIO 7 and it is in embedded C language.
Seven segment display code using timer0 (Copy and paste) for AVR and detail explain using timer subroutine
Seven segment code using timer subroutine


Copy and Paste tested code for seven segment using timer0 subroutine

/*
 * code_pcbGEN01.c
 *
 * Created: 15/02/2020 1:08:59 AM
 *  Author: Mahesh Nigam
 */ 


#define F_CPU 12000000UL //Setting F_CPU to the frequency of the microcontroller
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdio.h>
#include <avr/wdt.h>
#include <stdlib.h>
#include <string.h>
#include "drivers/16x2lcd/hd44780.h"

//FUNCTION DECLARATIONS
void button_tone_delay();
unsigned char READ_EEPROM(unsigned int uiAddress);


//dedicated variables
char str[20];

//START-project specific variables
char seven_segment_code[11]={0b11100111,0b00100001,0b11001011,0b01101011,0b00101101,0b01101110,0b11101110,0b00100011,0b11101111,0b01101111,0b00010000};
unsigned int buffer_num=0;
unsigned char digit_num1=0;
unsigned char digit_num2=0;
unsigned char digit_num3=0;
unsigned int life_value=0;
char start_test=0;
#define segment_port PORTD //declare this port as output anywhere
//END-project specific variables

//timer variables
unsigned int s=0;
unsigned int s_last=0;
unsigned int s_last_display=0;
unsigned char s_increment_status=0;
unsigned int ms1=0;
unsigned int common_ms1_timer=0;

char sms_sent=0;


//temp_variable
char temp_str[20]="000";
unsigned char char_temp=0;
unsigned int int_temp=0;

//eeprom address assignment
//#define xxxxxxx 0x4001

 


//output defines start
#define output_port2 PORTB
#define output_ddr2 DDRB
#define sel_digit1 0
#define sel_digit2 1
#define sel_digit3 2
#define buzzer 3

//output defines start
#define output_port4 PORTD
#define output_ddr4 DDRD

void init_outputs()
{
    output_ddr2 |= ((1<<sel_digit1) | (1<<sel_digit2) | (1<<sel_digit3) | (1<<buzzer));
    output_port2 &= ~((1<<sel_digit1) | (1<<sel_digit2) | (1<<sel_digit3) | (1<<buzzer));    //initial value as '0's
        
    output_ddr4 = 0xff;    //all as output
    output_port4 = 0x0;    //initial value as '0's
}
//output defines end

 


//START- system time generator
void init_timer0(){                    //initialize timer0
    TIMSK |= (1<<TOIE0);            //ENABLE TIMER0
    TCCR0 = (0x03);                    //PRESCALER=1:64
    TCNT0 = 70;                        // 1ms timer value
    sei();                            // Interrupt Enabled GLOBALLY
}

ISR(TIMER0_OVF_vect) {
    unsigned char sreg = SREG;
    cli();                // Disable Global interrupt
    TCNT0 = 70;         // 1ms timer value
    
    //main system time controlling
    ms1++;
    common_ms1_timer++;
    
    //START-updating seven segment
    if((buffer_num>999) || (buffer_num<0)){buffer_num=0;}
    if(ms1%3==0){
        output_port2 &=~((1<<sel_digit1) | (1<<sel_digit2) | (1<<sel_digit3));
        output_port2 |= ((1<<sel_digit1));
        digit_num1=(buffer_num%100)%10;
        segment_port = ~seven_segment_code[digit_num1];
    }
    
    if(ms1%3==1){
        output_port2 &=~((1<<sel_digit1) | (1<<sel_digit2) | (1<<sel_digit3));
        output_port2 |= ((1<<sel_digit2));
        digit_num2=(buffer_num%100)/10;
        segment_port = ~seven_segment_code[digit_num2];
    }

    if(ms1%3==2){
        output_port2 &=~((1<<sel_digit1) | (1<<sel_digit2) | (1<<sel_digit3));
        output_port2 |= ((1<<sel_digit3));
        digit_num3=(buffer_num/100);
        segment_port = ~seven_segment_code[digit_num3];
    }
    //END-updating seven segment
    
    
    
    if(ms1>1000){ms1=0;
        if(s_increment_status==0){
            s_increment_status=1;
            s++;
            //timeout

        }
    }
    else{s_increment_status=0;}

    SREG = sreg;
}
//END- system time generator


int main(void){
//init_sensor();
init_outputs();
init_timer0();
buffer_num=0;
while(1){

buffer_num++;

_delay_ms(1000);


}
}
 

NOTE: If your have any doubt in coding or not able to understant how to use it in your code, feel free to ask me in below comment section.



Seven Segment Code table with connecting pins

Seven Segment Code table with connecting pins

PIN D7 D6 D5 D4 D3 D2 D1 D0
SEGMENT ALPHABET e d c dp g f a b
0 1 1 1 1 0 1 1 1
1 0 0 1 0 0 0 0 1
2 1 1 0 0 1 0 1 1
3 0 1 1 0 1 0 1 1
4 0 0 1 0 1 1 0 1
5 0 1 1 0 1 1 1 0
6 1 1 1 0 1 1 1 0
7 0 0 1 0 0 0 1 1
8 1 1 1 0 1 1 1 1
9 0 1 1 0 1 1 1 1
dot point DP 1 1 1 0 1 1 1 1

 

This table can be used for both, common cathode (CC) as well as common anode (CA) seven segment display.

NOTE: If your have any doubt in coding or not able to understant how to use it in your code, feel free to ask me in below comment section.

Seven Segment Code table with connecting pins
Sven segment display part


Share this page with your friends
share via Whatsapp

Posted By :
Mahesh Nigam
(Scientist)
2020-02-15 16:49
See Author's other Published Topics

Airtel cheating ther customers by incomplete description of recharge pack of Big companies like Airtel, also cheating their customers by providing incomplete or confusing description of their recharge plans
Big companies like Airtel, also cheating their customers by providing incomplete or confusing description of their recharge plans
साहस और धैर्य का प्रतिक कारगिल विजय दिवस  of साहस और धैर्य का प्रतिक कारगिल विजय दिवस
साहस और धैर्य का प्रतिक कारगिल विजय दिवस
Vaisakhi  of Vaisakhi
Vaisakhi
3 मार्च - विश्व वन्यजीव दिवस of 3 मार्च - विश्व वन्यजीव दिवस
3 मार्च - विश्व वन्यजीव दिवस
1 मार्च- विश्व प्रशंसा दिवस of 1 मार्च- विश्व प्रशंसा दिवस
1 मार्च- विश्व प्रशंसा दिवस
1 मार्च- विश्व नागरिक सुरक्षा दिवस of 1 मार्च- विश्व नागरिक सुरक्षा दिवस
1 मार्च- विश्व नागरिक सुरक्षा दिवस
1 मार्च - शून्य भेदभाव दिवस of 1 march शून्य भेदभाव दिवस
1 march शून्य भेदभाव दिवस
करती रही of पूरा नही बस थोड़ा सा ही समझा होता ....
पूरा नही बस थोड़ा सा ही समझा होता ....
वैलेंटाइन of वैलेंटाइन डे मनाने के पीछे एक रोचक तथ्य व 7 से 14 तक वैलेंटाइन सेलिब्रेट करने की सूची
वैलेंटाइन डे मनाने के पीछे एक रोचक तथ्य व 7 से 14 तक वैलेंटाइन सेलिब्रेट करने की सूची
National Test Agency of UGC NET December 2023 results and download its subject wise cut-off
UGC NET December 2023 results and download its subject wise cut-off
परीक्षा पे चर्चा  of परीक्षा पे चर्चा युवाओं के लिए तनाव मुक्त माहौल बनाने के लिए प्रधान मंत्री नरेंद्र मोदी के नेतृत्व में बड़े आंदोलन - 'एग्जाम वॉरियर्स' का हिस्सा है।
परीक्षा पे चर्चा युवाओं के लिए तनाव मुक्त माहौल बनाने के लिए प्रधान मंत्री नरेंद्र मोदी के नेतृत्व में बड़े आंदोलन - 'एग्जाम वॉरियर्स' का हिस्सा है।

Peoples

Comments...

shivam tayal: what is program language?

Reply 03:41 2021-02-12

Mahesh Nigam(Scientist): Above program was written in C language

Reply 16:31 2021-02-12

Write Your Comment

Related to Programming

Latest topics




More Categories
latest mobile phones ireps.gov.in irctc.co.in Working Principle Viral Discussion True Love quotes True Love Today is Celebrated For Tips for better life Tips and Hacks Technology News Teachers Day Quotes Study Materials Self Motivating Quotes Science Sad Lines (Sayeri) Romantic shayari quotes Robotics Reviews Results Questions_and_Answers Programming Problem Resolved PHP Codes PCB Design Online Works New Year Quotes Shayari Status My World My Responsibilities Motivational lines Mobile Issues Microcontrollers Lucknow Local News Latest Technology Iphone Homeopathy Historical place in India Heart Touching Love Quotes Health Good Night Quotes Good Morning Quotes GeM.gov.in GST portal Funny Lines (Sayeri) Friendship Quotes Food Recipes Exam Time Table Engineering Project Ideas Electronics Electronic and electrical project making ideas Electricals Educations Desh Bhakti Shayeri Criminal Love Cricket News Corona Updates Christmas Quotes Shayari Status CBSE Updates/Circulars Breaking News Breaking Heart Shayeri Break Up Lines (Sayeri) BirthDay Wishes Best kitchen Tips Best Valentine quotes Shayeri Beauty Hacks Basic Knowledges Attitude quotes shayari Attitude quotes Attitude quotes Assembly Election 2022
Go to top

Important Links

  • Contact Us
  • About Us
  • All copyright © 2020 are reserved by Groomix - designed by Groomix

    MORE

    Programming >
    latest mobile phones >
    ireps.gov.in >
    irctc.co.in >
    Working Principle >
    Viral Discussion >
    True Love quotes >
    True Love >
    Today is Celebrated For >
    Tips for better life >
    Tips and Hacks >
    Technology News >
    Teachers Day Quotes >
    Study Materials >
    Self Motivating Quotes >
    Science >
    Sad Lines (Sayeri) >
    Romantic shayari quotes >
    Robotics >
    Reviews >
    Results >
    Questions_and_Answers >
    Problem Resolved >
    PHP Codes >
    PCB Design >
    Online Works >
    New Year Quotes Shayari Status >
    My World My Responsibilities >
    Motivational lines >
    Mobile Issues >
    Microcontrollers >
    Lucknow Local News >
    Latest Technology >
    Iphone >
    Homeopathy >
    Historical place in India >
    Heart Touching Love Quotes >
    Health >
    Good Night Quotes >
    Good Morning Quotes >
    GeM.gov.in >
    GST portal >
    Funny Lines (Sayeri) >
    Friendship Quotes >
    Food Recipes >
    Exam Time Table >
    Engineering Project Ideas >
    Electronics >
    Electronic and electrical project making ideas >
    Electricals >
    Educations >
    Desh Bhakti Shayeri >
    Criminal Love >
    Cricket News >
    Corona Updates >
    Christmas Quotes Shayari Status >
    CBSE Updates/Circulars >
    Breaking News >
    Breaking Heart Shayeri >
    Break Up Lines (Sayeri) >
    BirthDay Wishes >
    Best kitchen Tips >
    Best Valentine quotes Shayeri >
    Beauty Hacks >
    Basic Knowledges >
    Attitude quotes shayari >
    Attitude quotes >
    Attitude quotes >
    Assembly Election 2022 >

    Peoples

    Latest Comments