BeepnPulse_defines.c


1
// cpu: ATMega8
2
// speed: 2 mhz
3
// rom: 8192 bytes (0x0000 .. 0x1fff)
4
// ram: 1024 bytes (0x0060 .. 0x045f)
5
// eeprom: 512 bytes (0x0000 .. 0x01ff)
6
// generates Pulsating 3,7kHz-Beep with T_ON and T_OFF
7
8
#include <avr/io.h>
9
#include <avr/interrupt.h>
10
#define RAMSTART 0x0060
11
#define RAMSIZE (RAMEND-RAMSTART+1)
12
#define T_ON 2        //Factor 100ms, Beep on
13
#define T_OFF 20      //Factor 100ms, Beep off
14
15
// Function Prototypes
16
void Port_init(void);    //Port Init 
17
void Timer0_init(void);    //Timer0 Init
18
void Timer1_init(void);    //Timer1 Init
19
void Timer0_start(void);  //Start Timer0 triggering Timer1 via 10Hz Interrupt
20
void Timer1_start(void);  //Funct. to start Timer1
21
void Timer1_stop(void);    //Funct. to stop Timer1
22
23
//Global Variable Declaration
24
int Tick10Hz=0;       //Used in ISR TIMER0
25
26
//Interrupt Service Routine f=10Hz
27
ISR(TIMER0_OVF_vect)
28
{
29
  Tick10Hz = 1;
30
  TCNT0 = 61;  
31
}
32
33
int main(void)
34
{
35
int icount=0;
36
Port_init();
37
Timer0_init();
38
Timer1_init();
39
sei();            //Enable Interrupts  
40
41
Timer0_start();
42
  
43
  while (1)
44
  {
45
    if (Tick10Hz == 1)
46
    {
47
      icount++;    //Count ticks
48
      if ((icount > 0) && (icount < T_ON))
49
      Timer1_start();
50
      
51
      else if (icount == T_ON)
52
      Timer1_stop();
53
      
54
      else if (icount == (T_ON + T_OFF))
55
      icount=0;
56
      Tick10Hz = 0;  
57
    }
58
  }
59
}
60
61
62
void Port_init()
63
{
64
  DDRB = 0x03;  // 0=Out 1=Out 2=In  3=In  4=In  5=In  6=In  7=In  
65
  PORTB = 0x00;  // 0=Low 1=Low 2=Low 3=Low 4=Low 5=Low 6=Low 7=Low 
66
}
67
68
void Timer0_init (void)
69
{
70
  TCNT0 = 61;    //Generates 10Hz overflow
71
  TIMSK |= (1<<TOIE0);  
72
}
73
74
void Timer0_start (void)
75
{
76
  TCCR0 = (1<<CS02)|(1<<CS00);          //Start Software-RTC with 10Hz
77
}
78
79
void Timer1_init()
80
{
81
  TCCR1A = (1<<COM1A0);  //No PWM, Toggle OC1(PB1) 
82
  TCCR1B = (1<<WGM12);  //Reset counter @ compare level
83
  OCR1AH = 1;        //Compare Register High
84
  OCR1AL = 14;       //Compare Register Low 
85
  //OCR1AH  OCR1AL  f out
86
  //  0     180  5,5kHz
87
  //  1      14  3,7kHz
88
}
89
90
void Timer1_start()
91
{
92
  TCCR1B |= (1<<CS10);          //Start Timer1 by setting Prescaler @ clk/1
93
}
94
95
void Timer1_stop()
96
{
97
  TCCR1B &= ~((1<<CS10)|(1<<CS11)|(1<<CS12));    //Clear all Prescaler Bits to Stop Timer1
98
}