1 | /*
|
2 | * ProjektAttiny25.c
|
3 | *
|
4 | * Created: 12.05.2014 15:38:55
|
5 | * Author: Test1
|
6 | */
|
7 |
|
8 |
|
9 | #include <avr/io.h>
|
10 | #include <avr/interrupt.h>
|
11 | //#include <util/delay.h>
|
12 | #define UP 0
|
13 | #define DOWN 1
|
14 | static int maxpwm = 190;
|
15 | static int dutypwm = 80;
|
16 |
|
17 | #ifndef F_CPU
|
18 |
|
19 | # define F_CPU 1000000UL
|
20 | #endif
|
21 |
|
22 | void Timer1(){
|
23 |
|
24 | TCCR1 |=(1<<PWM1A)|(1<<COM1A1)|(1<<CS12);
|
25 | OCR1C = maxpwm;
|
26 | OCR1A = dutypwm;
|
27 |
|
28 | TIMSK |=(1<<OCR1A);
|
29 |
|
30 | }
|
31 |
|
32 |
|
33 |
|
34 | ISR (TIMER1_COMPA_vect)
|
35 | {
|
36 | static uint8_t sweepDirection= UP;
|
37 | static uint16_t pulseCount =0;
|
38 |
|
39 | pulseCount++;
|
40 |
|
41 | if(pulseCount==1000)
|
42 | {
|
43 | pulseCount=0;
|
44 |
|
45 | if(sweepDirection ==UP)
|
46 | {
|
47 | maxpwm+=10;
|
48 | if(maxpwm>190)
|
49 | sweepDirection=DOWN;
|
50 |
|
51 | }
|
52 | else
|
53 | {
|
54 | maxpwm-=10;
|
55 | if(maxpwm<80)
|
56 | sweepDirection =UP;
|
57 | }
|
58 |
|
59 | OCR1C=maxpwm;
|
60 |
|
61 | PORTB^=(1<<PB1); //Output PB1 for ultra sound on /off
|
62 |
|
63 | }
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | };
|
69 |
|
70 | int main(void){
|
71 |
|
72 | DDRB=0x07;
|
73 | PORTB|=(1<<PB3);
|
74 | Timer1(); //Timer1 Setup
|
75 | sei();
|
76 |
|
77 | while(1)
|
78 | {
|
79 |
|
80 | if(PINB &(1<<PB3))
|
81 | TCCR1|=(1<<CS12);
|
82 |
|
83 | else
|
84 | TCCR1 &=~(1<<CS12);
|
85 | }
|
86 | return 0;
|
87 | }
|