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

Happy Teacher’s Day of Dear Madam, Thank You For continually Inspiring me to do my best..
Dear Madam, Thank You For continually Inspiring me to do my best..
Happy teachers day of Thank you for teaching me how to read and write, for guiding me to distinguish between what is wrong and what is right. For allowing me to dream and soar as a kite, thank you for being my...
Thank you for teaching me how to read and write, for guiding me to distinguish between what is wrong and what is right. For allowing me to dream and soar as a kite, thank you for being my...
A good teacher is like a candle of A good teacher is like a candle – it consumes itself to light the way for others..
A good teacher is like a candle – it consumes itself to light the way for others..
The teacher who is indeed wise of The teacher who is indeed wise does not bid you to enter the house of his wisdom but rather leads you..
The teacher who is indeed wise does not bid you to enter the house of his wisdom but rather leads you..
Happy World Teachers' Day! of I am proud to let you know that you have been the great teacher in my life! Happy World Teachers' Day!...
I am proud to let you know that you have been the great teacher in my life! Happy World Teachers' Day!...
Happy teachers day of Without you, we would have been lost. Thank you teacher for guiding us, inspiring us And making us..
Without you, we would have been lost. Thank you teacher for guiding us, inspiring us And making us..
Wishing you a Teacher's Day of I was lucky to have a teacher as wonderful as you are. Wishing you a Teacher's Day that’s full of joyous...
I was lucky to have a teacher as wonderful as you are. Wishing you a Teacher's Day that’s full of joyous...
 Happy Teacher's Day! of Teacher, you have always challenged me to work hard and get good grades. I will always remember you. Happy Teacher's Day!...
Teacher, you have always challenged me to work hard and get good grades. I will always remember you. Happy Teacher's Day!...
Happy Teacher's Day! of The best teachers teach from the heart, not from the book. Thank you for being a wonderful teacher. Happy Teacher's Day!..
The best teachers teach from the heart, not from the book. Thank you for being a wonderful teacher. Happy Teacher's Day!..
Happy Teacher’s Day of Happy Teacher’s Day! It has been an honor to get to learn so many...
Happy Teacher’s Day! It has been an honor to get to learn so many...
Wishing you joy and happiness of Wishing you joy and happiness, you are an amazing teacher, and you only..
Wishing you joy and happiness, you are an amazing teacher, and you only..

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