-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
56 lines (45 loc) · 1016 Bytes
/
main.cpp
File metadata and controls
56 lines (45 loc) · 1016 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* PhaseAngleControl.cpp
*
* Created: 18-09-2019 18:13:39
* Author : Rushad
*/
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
int ADC_read(unsigned char chnl)
{
chnl= chnl & 0b00000111;
ADMUX = 0x40;
ADCSRA|=(1<<ADSC);
while(!(ADCSRA & (1<<ADIF)));
ADCSRA|=(1<<ADIF);
return (ADC);
}
unsigned int map(int input, float input_start, float input_end, float output_start, float output_end)
{
double slope = 1.0 * (output_end - output_start) / (input_end - input_start);
unsigned int output = output_start + slope * (input - input_start);
return output;
}
int main(void)
{
DDRB = 0b00000001;
ADMUX=(1<<REFS0);
ADCSRA=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
EICRA |= (1 << ISC00);
EIMSK |= (1 << INT0);
sei();
while (1)
{
}
}
ISR (INT0_vect)
{
int pot = ADC_read(0);
int on_time = map(pot, 0, 1023, 0, 1000);
PORTB |= (1 << PB0);
_delay_ms(on_time);
PORTB &= ~(1 << PB0);
_delay_ms(100);
}