S
S
Sashachern32021-12-03 03:46:28
C++ / C#
Sashachern3, 2021-12-03 03:46:28

HOW do I set up a countdown timer in PROTEUSE in C for an ATTINY2331 microcontroller?

And so, hello everyone, I'm new to electronics, and unfortunately, or maybe not, I got a term paper on microprocessors. What did I do and why am I here. The task was this, in C language (used Microchip studio) to make a clock and a countdown timer on an ATTINY2313 microcontroller with 5 buttons. I did everything but the countdown timer. There is an assumption that I'm setting it up wrong, but two days have passed, I feel I've been sitting in an empty space, please respond for help. Help with your professional eyes to see the truth.

#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#include <avr/interrupt.h>
#include "lcd.h"
#include <stdio.h>
#include <string.h>z
#include <stdbool.h>

#define F_CPU 1000000UL
#define enable            2
#define registerselection 0

ISR(TIMER1_COMPA_vect);
ISR(TIMER0_COMPB_vect);

static volatile int SEC =0;
static volatile int MIN =0;
static volatile int HOU =0;
static volatile int TRSEC =0;
static volatile int TRMIN =0;
static volatile int TRHOU =0;

static bool min_plus_presd =0;
static bool hou_plus_presd =0;
static bool min_minus_presd =0;
static bool hou_minus_presd =0;

int main(void)
{
  DDRA = 0xFF;
  DDRD = 0b1100000;
  DDRB = 0b11110111;
  
  TCCR1B |=(1<<CS12)|(1<<CS10)|(1<<WGM12); // setting prescaler and CTC mode деление частоты на 1024 = 977
  TIMSK  |=(1<<OCIE1A);//compare match interrupt enable
  OCR1A = 487;//setting compare value equal to counter clock frequency to get an interrupt every second
  
  TCCR0B |=(1<<CS02)|(1<<CS00)|(1<<WGM02);
  TIMSK  |=(1<<OCF0A);//compare match interrupt enable
  OCR0A = 487;//setting compare value equal to counter clock fr
  //TIFR |=(1<<TOV1); // setting prescaler and CTC mode
  //TIMSK  |=(1<<OCIE1A);
  sei(); // enabling global interrupts
  
  //TIFR   |=(1<<OCF1A);//compare match interrupt enable
  
  lcd_init(LCD_DISP_ON);
  while(1)
  {
    if (!(PIND & (1<<4)))//if the button is not pressed, then we manage the time
    {
      lcd_home();
      char SHOWSEC [2];
      char SHOWMIN [2];
      char SHOWHOU [2];
      //lcd_puts("Time  ");
      SHOWHOU[1] = HOU % 10;
      SHOWHOU[0] = (HOU / 10) % 10;
      SHOWMIN[1] = MIN % 10;
      SHOWMIN[0] = (MIN / 10) % 10;
      SHOWSEC[1] = SEC % 10;
      SHOWSEC[0] = (SEC / 10) % 10;
      lcd_putc(SHOWHOU[0] + '0');
      lcd_putc(SHOWHOU[1] + '0');
      lcd_putc(':');
      lcd_putc(SHOWMIN[0] + '0');
      lcd_putc(SHOWMIN[1] + '0');
      lcd_putc(':');
      lcd_putc(SHOWSEC[0] + '0');
      lcd_putc(SHOWSEC[1] + '0');

      PORTA&=~(1<<PIND5); //buzzer off
      PORTA&=~(1<<PIND6); //LED-GREEN off
      
      MIN_P();
      MIN_M();
      HOU_P();
      HOU_M();
    }
      
    //===============================================================================

    if (PIND & (1<<4))//if the button is pressed, then we manage the timer
    {
      lcd_home();
      char SHOWTRSEC [2];
      char SHOWTRMIN [2];
      char SHOWTRHOU [2];
      //lcd_puts ("Timer ");
      SHOWTRHOU[1] = TRHOU % 10;
      SHOWTRHOU[0] = (TRHOU / 10) % 10;
      SHOWTRMIN[1] = TRMIN % 10;
      SHOWTRMIN[0] = (TRMIN / 10) % 10;
      SHOWTRSEC[1] = TRSEC % 10;
      SHOWTRSEC[0] = (TRSEC / 10) % 10;
      lcd_putc(SHOWTRHOU[0] + '0');
      lcd_putc(SHOWTRHOU[1] + '0');
      lcd_putc(':');
      lcd_putc(SHOWTRMIN[0] + '0');
      lcd_putc(SHOWTRMIN[1] + '0');
      lcd_putc(':');
      lcd_putc(SHOWTRSEC[0] + '0');
      lcd_putc(SHOWTRSEC[1] + '0');

      if (TRHOU==0 || TRMIN==0 || TRSEC==0)
      {
      PORTD|=(1<<5); //buzzer on
      PORTD|=(1<<6); //LED-GREEN on
      }
      TRMIN_P();
      TRMIN_M();
      TRHOU_P();
      TRHOU_M();
    }
  }
}

ISR(TIMER1_COMPA_vect){
  if (++SEC==60)
  {
    SEC=0;
    if (++MIN==60)
    {
      MIN=0;
      if (++HOU==24)
      {
        HOU=0;
      }
    }
  }
}

ISR(TIMER0_COMPA_vect){
  
}

void MIN_P()
{
  if (PIND & (1<<0))
  {
    if (min_plus_presd == 0)
    {
      if (MIN<60)
      {
        MIN++;
      }
      if (MIN==60)
      {
        if (HOU<24)
        {
          HOU++;
        }
        MIN=0;
      }
      min_plus_presd = 1;
    }
  }
  else
  {
    min_plus_presd = 0;
  }
}

void MIN_M(){
  if (PIND & (1<<1))
  {
    if (min_minus_presd == 0)
    {
      if (MIN>0)
      {
        MIN--;
      }
      min_minus_presd = 1;
    }
  }
  else{
    min_minus_presd = 0;
  }
};

void HOU_P(){
  if (PIND & (1<<2))
  {
    if (hou_plus_presd == 0)
    {
      if (HOU<24)
      {
        HOU++;
      }
      if (HOU==24)
      {
        HOU=0;
      }
      hou_plus_presd = 1;
    }
  }
  else
  {
    hou_plus_presd = 0;
  }
};

void HOU_M(){
  if (PIND & (1<<3))
  {
    if (hou_minus_presd == 0)
    {
      if (HOU>0)
      {
        HOU--;
      }
      hou_minus_presd = 1;
    }
  }
  else{
    hou_minus_presd = 0;
  }
  };
  
void TRMIN_P()
{
  if (PIND & (1<<0))
  {
    if (min_plus_presd == 0)
    {
      if (TRMIN<60)
      {
        TRMIN++;
      }
      if (TRMIN==60)
      {
        if (TRHOU<24)
        {
          TRHOU++;
        }
        TRMIN=0;
      }
      min_plus_presd = 1;
    }
  }
  else
  {
    min_plus_presd = 0;
  }
}

void TRMIN_M(){
  if (PIND & (1<<1))
  {
    if (min_minus_presd == 0)
    {
      if (TRMIN>0)
      {
        TRMIN--;
      }
      min_minus_presd = 1;
    }
  }
  else{
    min_minus_presd = 0;
  }
};

void TRHOU_P(){
  if (PIND & (1<<2))
  {
    if (hou_plus_presd == 0)
    {
      if (TRHOU<24)
      {
        TRHOU++;
      }
      if (TRHOU==24)
      {
        TRHOU=0;
      }
      hou_plus_presd = 1;
    }
  }
  else
  {
    hou_plus_presd = 0;
  }
};

void TRHOU_M(){
  if (PIND & (1<<3))
  {
    if (hou_minus_presd == 0)
    {
      if (TRHOU>0)
      {
        TRHOU--;
      }
      hou_minus_presd = 1;
    }
  }
  else{
    hou_minus_presd = 0;
  }
};
/*
if (++SEC==60)
{
  SEC=0;
  if (++MIN==60)
  {
    MIN=0;
    if (++HOU==24)
    {
      HOU=0;
    }
  }
}

*/


61a966f724d91379459886.png

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question