Programming: heading views : 0

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.



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