Časovací hodiny - úprava programu pro pípání na konci

Nazdar.
Bol bi tu niekto ochotný upraviť tento program? Jedna sa o časovač len potrebujem aby to po skončení pípalo.
Môže sa použit niektorí z portov PC1 až PC7. Inak program funguje.

S písaním programu nemam skúsenosti tak prosím vás. Dakujem

[code]#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

#include “lcd.h”

//Connection of Load
#define LOAD_DDR DDRC
#define LOAD_PORT PORTC
#define LOAD_POS PC0
#define LOAD_PO1 PC1

//Global variable for the clock system
volatile unsigned int clock_millisecond=0;

volatile char clock_second=0;
volatile char clock_minute=0;
volatile char clock_hour=0;

void tim0_start(void);

void Wait(uint8_t n)
{

uint8_t i,temp;
temp=n*28;

for(i=0;i<temp;i++)
_delay_loop_2(0);
}

void LoadOn()
{
LOAD_PORT|=(1<<LOAD_POS);
}

void LoadOff()
{
LOAD_PORT&=(~(1<<LOAD_POS));
}

void LoadOn1()
{
LOAD_PORT|=(1<<PC1);
}

void LoadOff1()
{
LOAD_PORT&=(~(1<<PC1));
}

main()

{

while(1)
{
LOAD_DDR|=(1<<LOAD_POS);
LOAD_DDR|=(1<<LOAD_PO1);
DDRC |= (1<<PC3);

  LoadOff();
  

  //Enable Pullups on Keypad

  PORTB|=((1<<PB5)|(1<<PB4)|(1<<PB0));

  int8_t hr,min,sec; //Target Time
  hr=min=sec=0;

  //Initialize the LCD Subsystem
  InitLCD(0);
  //Clear the display

  LCDClear();

  //Set up the timer1 as described in the
  //tutorial
  TCCR1B=(1<<WGM12)|(1<<CS11)|(1<<CS10);
  OCR1A=250;

  //Enable the Output Compare A interrupt

  TIMSK|=(1<<OCIE1A);

  //Enable interrupts globally
  sei();

  LCDClear();
  LCDWriteString("    Welcome     ");
  LCDWriteStringXY(0,1,"   Relay Timer  ");

  Wait(1);

  LCDClear();
  LCDWriteString("Set Time - 00:00");
  LCDWriteStringXY(0,1," Start     ^");

  uint8_t selection=1;
  uint8_t old_pinb=PINB;

  while(1)
  {
     while((PINB & 0b00110001) == (old_pinb & 0b00110001));

     //Input received


     if(!(PINB & (1<<PINB5)) && (old_pinb & (1<<PB5)))
     {
        //Selection key Pressed

        selection++;
        if(selection==3)
           selection =0;
     }

     if(!(PINB & (1<<PINB4)) && (old_pinb & (1<<PB4)))
     {
        //Up Key Pressed

        if(selection == 1)
        {
           //Min is selected so increment it

           min++;

           if(min == 60)
             min =0;

        }

        if(selection == 2)
        {
           //Sec is selected so increment it

           sec++;

           if(sec == 60)
              sec =0;

        }

        if(selection == 0)
        {
           //Start Selected
           break;
        }


     }

     if(!(PINB & (1<<PINB0)) && (old_pinb & (1<<PB0)))
     {
     //Down Key Pressed

        if(selection == 1)
        {

           //Min is selected so decrement it

           min--;

           if(min == -1)
              min =59;
        }

        if(selection == 2)
        {
           //Sec is selected so decrement it

           sec--;

           if(sec == -1)
              sec =59;

        }

        if(selection == 0)
        {
           //Start Selected
           break;
        }

     }


     old_pinb=PINB;



     //Update Display

     LCDClear();
     LCDWriteString("Set Time - 00:00");
     LCDWriteStringXY(0,1," Start    ");

     //min
     LCDWriteIntXY(11,0,min,2);

     //sec

     LCDWriteIntXY(14,0,sec,2);

     if(selection == 0)
        LCDWriteStringXY(0,1,">");

     if(selection == 1)
        LCDWriteStringXY(11,1,"^");

     if(selection == 2)
        LCDWriteStringXY(14,1,"^");

     _delay_loop_2(0);
     _delay_loop_2(0);
     _delay_loop_2(0);
     _delay_loop_2(0);

     _delay_loop_2(0);
     _delay_loop_2(0);
     _delay_loop_2(0);
     _delay_loop_2(0);
  }

  //Start the Load

  LoadOn();
  LoadOff1();
  

  //Now start the timer
  clock_hour = hr;
  clock_minute = min;
  clock_second = sec;

  LCDClear();
  LCDWriteString("  Power Off In ");

  while(1)
  {
     LCDWriteIntXY(7,1,clock_minute,2);
     LCDWriteString(":");
     LCDWriteIntXY(10,1,clock_second,2);

     if((clock_hour == 0) && (clock_minute == 0) && (clock_second == 0))
     {
        //Time Out

        LoadOff();
		LoadOn1();


        LCDClear();
        LCDWriteString("Load Turned Off");
		Wait(2);
		LoadOff1();


        while(1)
        {
           LCDWriteStringXY(0,1,"*Press Any Key*");

           Wait(1);

           LCDWriteStringXY(0,1,"                ");

           Wait(1);

           if((~PINB) & 0b00110001)


			  
			  break;

     



        }

        break;



     }

     _delay_loop_2(0);
     _delay_loop_2(0);
     _delay_loop_2(0);
     _delay_loop_2(0);
  }

//Continue again

}

}

//The output compate interrupt handler
//We set up the timer in such a way that
//this ISR is called exactly at 1ms interval
ISR(TIMER1_COMPA_vect)
{
clock_millisecond++;
if(clock_millisecond==1000)
{
clock_second–;
clock_millisecond=0;
if(clock_second==-1)
{
clock_minute–;
clock_second=59;

     if(clock_minute==-1)
     {
        clock_hour--;
        clock_minute=59;
     }
  }

}
}

[/code]

:arrow_right: administrator: přejmenováno z "Uprava programu."

Možná nějak takhle?

[code]// definice:
#define SPK_DDR DDRC
#define SPK_PORT PORTC
#define SPK_PIN PC5

// v Main:
int16_t i;
SPK_DDR |= (1<<SPK_PIN);

// cekaci smycka:
while(1)
{
LCDWriteStringXY(0,1,“Press Any Key”);

           for (i = 2000; i > 0; i--)
           {
               SPK_PORT ^= (1<<SPK_PIN);
               _delay_us(250);
           }

           LCDWriteStringXY(0,1,"                "); 

           _delay_ms(500); 

           if((~PINB) & 0b00110001) break; 
        } 

[/code]

Vďaka idem skúsiť. :slight_smile:

Otázka: Prečo táto funkcia tak nehorázne zvečuje kód? Je to Pípanie. Poprípade viete poradiť niečo iné na pípanie?

void pip(int8_t cas) { int16_t i; for (i = 1000; i > 0; i--) { PODSV_PORT ^= (1<<SPK_PIN); _delay_us(cas); } }

Device: atmega8 Program: 7510 bytes (91.7% Full) (.text + .data + .bootloader) Data: 413 bytes (40.3% Full) (.data + .bss + .noinit) Build succeeded with 0 Warnings...

A bez tej funkcie:

Program: 3588 bytes (43.8% Full) (.text + .data + .bootloader) Data: 149 bytes (14.6% Full) (.data + .bss + .noinit) Build succeeded with 0 Warnings...

Problem je ve volani funkce delay. Pokud tam das cislo na pevno _delay_ms(1000) vezme to mnohem mene pameti nez _delay_ms(cas). Nejvetsi usetreni kodu prinese pouziti casovace, ktery si budes obsluhovat sam.