ProjektAttiny25.c


1
/*
2
 * ProjektAttiny25.c
3
 *
4
 * Created: 12.05.2014 15:38:55
5
 *  Author: Adrian Höppener
6
 */ 
7
8
9
#include <avr/io.h>
10
#include <avr/interrupt.h>
11
#include <util/delay.h>
12
#include <avr/sleep.h>
13
#include "util/delay.h"
14
15
#define  UP    0
16
#define  DOWN  1
17
 static int maxpwm = 190;
18
 static int dutypwm =80;
19
 
20
21
#ifndef F_CPU
22
//prevent compiler error by supplying a default
23
# warning "F_CPU not defined for <util/delay.h>"
24
# define F_CPU 1000000UL
25
#endif
26
27
void  Timer1_setup(){
28
  
29
  TCCR1 |=(1<<PWM1A)|(1<<CS12)|(0<<COM1A1)|(1<<COM1A0);
30
31
  OCR1C = maxpwm;
32
  OCR1A = dutypwm;
33
  TCNT1 |=OCR1A;
34
  //OCR1A=80;  //Impulsbreite ändern
35
36
  TIFR |=(1<<TOV1);
37
  TIMSK |=(1<<OCIE1A);
38
  
39
  }
40
41
void Timer0_setup (){
42
  
43
  TCCR0B |=(1<<CS01);
44
  TCNT0 |=(1<<OCR0A);
45
  
46
  TIMSK |=(1<<TOIE0);
47
  
48
  
49
}
50
51
ISR (TIM1_COMPA_vect){
52
static uint8_t sweepDirection= UP;
53
static uint16_t pulseCount =0;
54
55
pulseCount++;
56
57
  if(pulseCount==1000)
58
  {
59
    pulseCount=0;
60
    
61
    if(sweepDirection ==UP)
62
    {
63
      maxpwm+=10;
64
      if(maxpwm>200)
65
      sweepDirection=DOWN;
66
  
67
    }
68
    else
69
    {
70
    maxpwm-=10;
71
    if(maxpwm<100)
72
    sweepDirection =UP;
73
    }
74
    
75
      OCR1C=maxpwm;
76
    
77
      //PORTB|=(1<<PB1);  //Output PB1 for ultra sound on /off
78
  }
79
80
};
81
82
ISR(TIMER0_OVF_vect){
83
  
84
 uint8_t time= 0;
85
  time++;
86
87
  if( time == 600 )   // oder was auch immer den 60 Sekunden entspricht
88
  time = 0;
89
90
91
}
92
93
94
int main(void){
95
 uint8_t time= 0;
96
DDRB=0X07;
97
98
PORTB |=(1<<PB3);  //Schalter ein
99
100
//PORTB  &=~(1<<PB3);//Schalter aus
101
102
103
104
//Timer0_setup(); //Timer0 Setup
105
Timer1_setup(); //Timer1 Setup
106
107
108
sei();
109
110
while(1)
111
112
{
113
  TCCR0B |=(1<<CS01);
114
      // erst mal brauchen wir eine Variable die bis 600 zählt.
115
      // 600 deshalb, weil wir 30 Sekunden Ruhe haben wollen und 30 Sekunden
116
      // gepiepse. D.h. nach 60 Sekunden (30 + 30), oder eben 600 mal
117
      // 100 Millisekunden wiederholt sich alles wieder.
118
      // if( time == 60 )
119
 
120
      //time = 0;
121
122
      //if (time == 0)
123
124
      //{  
125
      //  TCCR1 &= ~(1<<CS12);
126
      //  _delay_ms(18);
127
      //}
128
      // nachdem das jetzt klar ist, ist auch klar, dass der Sender
129
      // eingeschaltet sein soll, wenn
130
      // a) die time Variable kleiner als 300 ist, denn dann befinden
131
      //    wir uns in den ersten 30 Sekunden dieses 60 Sekunden Zykluses
132
      // b) der externe Schalter so steht, dass vom Benutzer aus die
133
      //    Freigabe für das Gequietsche gegeben wurde
134
      //
135
       // wenn keins der beiden der Fall ist, dann soll sich auch nichts tun
136
137
  
138
  while( PINB & (1<<PB3) )    // grundsätzlich ist eingeschaltet
139
  {
140
    if( time < 300 )       // ob tatsächlich gedudelt wird, entscheidet die Zeit
141
    TCCR1 |= (1<<CS12);  // erste 30 Sekunden: ein
142
    if(time == 0)
143
    {
144
    TCCR1 &= ~(1<<CS12); // zweite 30 Sekunden (eigentlich: die restliche Zeit): aus
145
    _delay_ms(1000);}
146
    else
147
    TCCR1 &= ~(1<<CS12); // zweite 30 Sekunden (eigentlich: die restliche Zeit): aus
148
    _delay_ms(1000);
149
  }
150
  if(PINB &~(1<<PB3))
151
  {                        // Nope. Da ist nichts eingeschaltet
152
    TCCR0B &=~(1<<CS01);
153
    TCCR1 &= ~(1<<CS12);
154
    _delay_ms(1000);
155
  }
156
  
157
158
}
159
}