Skip to content

Commit

Permalink
Add output enable and disable invert
Browse files Browse the repository at this point in the history
Closes #115
  • Loading branch information
MCUdude committed Apr 8, 2021
1 parent 4ed967a commit 3f20e21
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
12 changes: 7 additions & 5 deletions megaavr/libraries/Comparator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,14 @@ Comparator.hysteresis = hyst::large; // Use 50V hysteresis


### output
Variable for setting the comparator output pin (PA7).
Variable for setting the comparator output, internally and/or externally.
Accepted values:
``` c++
out::disable; // No output pin
out::enable; // Enable output pin (PA7)
out::invert; // Invert output pin (PA7)
out::disable; // No output pin, signal not inverted internally
out::disable_invert; // No output pin, signal inverted internally
out::enable; // Enable output pin (PA7), signal not inverted internally
out::invert; // Enable output pin (PA7), signal inverted internally
out::enable_invert; // Identical to out::invert
```

##### Usage
Expand Down Expand Up @@ -148,7 +150,7 @@ Comparator.stop(); // Stop comparator


## read()
Reads the state of the analog comparator output.
Reads the state of the analog comparator output. Works also when the physical output pin is disabled.

##### Usage
```c++
Expand Down
8 changes: 7 additions & 1 deletion megaavr/libraries/Comparator/src/Comparator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void AnalogComparator::init()
AC.CTRLA |= out::enable;
PORTA.DIRSET = PIN7_bm;
}
else if(output == out::invert)
else if(output == out::invert /* || output == out::invert_enable */)
{
AC.MUXCTRLA |= out::invert;
AC.CTRLA |= out::enable;
Expand All @@ -71,6 +71,12 @@ void AnalogComparator::init()
AC.CTRLA &= ~out::enable;
PORTA.DIRCLR = PIN7_bm;
}
else if(output == out::disable_invert)
{
AC.MUXCTRLA |= out::invert;
AC.CTRLA &= ~out::enable;
PORTA.DIRCLR = PIN7_bm;
}
}

void AnalogComparator::start(bool state)
Expand Down
8 changes: 5 additions & 3 deletions megaavr/libraries/Comparator/src/Comparator.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ namespace out
{
enum output_t : uint8_t
{
disable = 0x00,
enable = 0x40,
invert = 0x80,
disable = 0x00,
disable_invert = 0xC0,
enable = 0x40,
invert = 0x80,
enable_invert = 0x80,
};
};

Expand Down

2 comments on commit 3f20e21

@SpenceKonde
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you have disable invert and enable invert backwards.

@MCUdude
Copy link
Owner Author

@MCUdude MCUdude commented on 3f20e21 Apr 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's actually the opposite, I just couldn't figure out a better naming scheme. It's disable_invert means "disable output and invert", while enable_invert means "enable output and invert".

This is what the README now says:

 out::disable;        // No output pin, signal not inverted internally
 out::disable_invert; // No output pin, signal inverted internally
 out::enable;         // Enable output pin (PA7), signal not inverted internally
 out::invert;         // Enable output pin (PA7), signal inverted internally
 out::enable_invert;  // Identical to out::invert

Do you have a better suggestion? I realize that disable_invert and enable_invert might be a bit misleading. At the same time, I didn't want to create a new variable just to set the invert bit.

Please sign in to comment.