-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomparator85.c
More file actions
75 lines (48 loc) · 1.3 KB
/
comparator85.c
File metadata and controls
75 lines (48 loc) · 1.3 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* comparator85.c
* Created on: 04.06.2018
* Last modified:
* Author: Andrew Jason Bishop
*/
#define F_CPU 16000000UL
#include <avr/io.h>
#define BIT(x) (0x01 << (x))
#define setBit(reg, bit) ( (reg) |= (1 << (bit)) )
#define clrBit(reg, bit) ( (reg) &= ~(1 << (bit)) )
#define flpBit(reg,bit) ( (reg) ^= (1 << (bit)) )
#define getBit(reg,bit) ( (reg) & (1 << (bit)) )
#define setWithMask(reg, mask) ( (reg) |= (mask) )
#define clrWithMask(reg, mask) ( (reg) &= ~(mask) )
void
crlPortRegAndSetAsOutPB4(void) {
PORTB = 0x00;
DDRB |= (1 << PB4); }// PB4 as LED out
void
disable_ADC(void) {
clrBit(ADCSRA, ADEN); }//
void
enable_comparator_multiplexed_input(void) {
setBit(ADCSRB, ACME); }//
void
select_ADC_input_channel(unsigned char _ADCpin) {
clrWithMask( ADMUX, 0x03 ); // clr MUX[1:0]
setWithMask( ADMUX, _ADCpin ); }// set MUX[1:0]
void
setup(void) {
crlPortRegAndSetAsOutPB4();
disable_ADC();
enable_comparator_multiplexed_input();
select_ADC_input_channel( 3 ); }//
unsigned char
isComparatorHigh(void) {
return getBit(ACSR,ACO); }//
int main(void) {
setup();
while(1) {
if ( isComparatorHigh() )
setBit( PORTB, PB4 );
else
clrBit( PORTB, PB4 );
}
}// main
/* END */