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 =30000;
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<<COM1A1)|(1<<COM1A0)|(1<<CS12);
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
  //GTCCR |=(1<<TSM);
43
  
44
  //TCCR0A |=(0<<COM1A0)|(0<<COM1A1);
45
  TCCR0B |=(1<<CS01);
46
  
47
  TCNT0 |=(1<OCR0A);
48
49
  OCR0A = dutypwm;
50
51
  
52
  TIMSK |=(1<< OCIE0A);
53
  TIFR |=(1<<OCF0A);
54
  
55
}
56
57
ISR (TIM1_COMPA_vect){
58
static uint8_t sweepDirection= UP;
59
static uint16_t pulseCount =0;
60
61
pulseCount++;
62
63
  if(pulseCount==1000)
64
  {
65
    pulseCount=0;
66
    
67
    if(sweepDirection ==UP)
68
    {
69
      maxpwm+=10;
70
      if(maxpwm>200)
71
      sweepDirection=DOWN;
72
  
73
    }
74
    else
75
    {
76
    maxpwm-=10;
77
    if(maxpwm<100)
78
    sweepDirection =UP;
79
    }
80
    
81
      OCR1C=maxpwm;
82
    
83
      //PORTB|=(1<<PB1);  //Output PB1 for ultra sound on /off
84
  }
85
};
86
87
ISR(TIM0_COMPA_vect){
88
  uint8_t time= 0;
89
     time++;
90
91
     if( time ==10 )
92
    {
93
94
    time = 0;
95
96
    if (time == 0)
97
98
    {
99
      TCCR1 &= ~(1<<CS12);
100
      _delay_ms(100);
101
   }
102
  }
103
  else
104
  TCCR1 |= (1<<CS12);
105
  
106
 
107
}
108
109
110
int main(void){
111
//uint8_t time= 0;
112
DDRB=0X07;
113
114
PORTB |=(1<<PB3);  //Schalter ein
115
116
//  PORTB  &=~(1<<PB1);//Schalter aus
117
118
Timer0_setup(); //Timer0 Setup
119
120
Timer1_setup(); //Timer1 Setup
121
sei();
122
123
    while(1){
124
 // erst mal brauchen wir eine Variable die bis 600 zählt.
125
 // 600 deshalb, weil wir 30 Sekunden Ruhe haben wollen und 30 Sekunden
126
 // gepiepse. D.h. nach 60 Sekunden (30 + 30), oder eben 600 mal
127
 // 100 Millisekunden wiederholt sich alles wieder.
128
129
// time++;
130
131
// if( time == 60 )
132
 
133
 //time = 0;
134
135
//if (time == 0)
136
137
//{  
138
//  TCCR1 &= ~(1<<CS12);
139
//  _delay_ms(18);
140
//}
141
 // nachdem das jetzt klar ist, ist auch klar, dass der Sender
142
 // eingeschaltet sein soll, wenn
143
 // a) die time Variable kleiner als 300 ist, denn dann befinden
144
 //    wir uns in den ersten 30 Sekunden dieses 60 Sekunden Zykluses
145
 // b) der externe Schalter so steht, dass vom Benutzer aus die
146
 //    Freigabe für das Gequietsche gegeben wurde
147
 //
148
 // wenn keins der beiden der Fall ist, dann soll sich auch nichts tun
149
150
  TCCR0B |=(0<<CS00)|(0<<CS01);
151
  if(PINB & (1<<PB3)){
152
 //if(time < 30)
153
154
  TCCR1 |= (1<<CS12);
155
  _delay_ms(100);
156
  }
157
  
158
 else{
159
   
160
  TCCR0B  &=~(1<<CS00)|(1<<CS01);
161
  TCCR1 &= ~(1<<CS12);
162
  _delay_ms( 100 );
163
 
164
 }
165
 
166
  }
167
  
168
}