Penguino Library - IO
Contents
1 #include "penguino/io.h"
Note: functions in this file are inlined where possible.
Ports and Pins
The Pin Type:
Individual pins are referred to as an enum in the form A0, where A is the port letter and 0 is the pin number on that port
1 typedef enum {
2 A0, etc...
3 } Pin;
The Port Type:
Whole ports are referred to as an enum of the individual port letter in uppercase. e.g. B for port B.
1 typedef enum {
2 A,
3 B, etc...
4 } Port;
Status LED
Function |
Functionality |
void statusLed_init(void) |
Initialises the on-board tri-colour status LED for use. Must be called before using the below functions. |
void statusLed_red(void) |
Turns on the status LED with the colour as red |
void statusLed_green(void) |
Turns on the status LED with the colour as green |
void statusLed_orange(void) |
Turns on the status LED with the colour as yellow/orange (a combination of red and green) |
void statusLed_off(void) |
Turns the status LED off |
Example:
1 #include "penguino/io.h"
2
3 int main( void ) {
4
5 statusLed_init( );
6
7 // turn status LED on as orange
8 statusLed_orange( );
9
10 while( 1 ) {
11 // spin
12 }
13 }
PinValue
The PinValue type is used to describe if a pin is high (driven by Vcc or above the digital threshold voltage) or low (connected to ground or below the digital threshold voltage).
typedef enum { low=0, high=1, Low=0, High=1 } !PinValue;
Digital Inputs
Digital inputs can be configured as either a floating or a pullup input.
When not connected, a floating input has an undefined value (most likely a noisy oscillation between low and high values). Floating inputs must therefore be connected to Vcc or Ground for a high and low value, respectively.
Configuring an input as a pullup input internally connects the input to Vcc via a resistor. This has the effect of having a high value by default (when not connected) and the input must be driven low (connected to ground) in order to register a low reading.
Function |
Functionality |
void floatingInput_init(Pin portPin) |
Initialises a pin as a floating input |
void pullupInput_init(Pin portPin) |
Initialises a pin as a pullup input |
void floatingInput_initPort(Port port) |
Initialises all pins on a port as floating inputs |
void pullupInput_initPort(Port port) |
Initialises all pins on a port as pullup inputs |
PinValue readPin(Pin portPin) |
Returns Low or High (0 or 1) respective to a pin value |
uint8_t readPort(Port port) |
Returns the number represented by the binary pattern of high and low values on each pin of a port |
Example:
1 #include "penguino/io.h"
2
3 int main( void ) {
4
5 statusLed_init( );
6
7 // configure port B pin 0 as a pullup input
8 pullupInput_init( B0 );
9
10
11 while( 1 ) {
12 // loop forever
13
14 if ( readPin( B0 ) ) {
15 // if B0 is active, turn the status led green
16 statusLed_green( );
17 } else {
18 // B0 is not active, turn the status led red
19 statusLed_red( );
20 }
21 }
22 }
Digital Outputs
Function |
Functionality |
void output_init(Pin portPin) |
Initialises a pin as a digital output |
void output_initPort(Port port) |
Initialises all pins on a port as digital outputs |
void drivePin(Pin portPin, PinValue value) |
Sets a pin to be the specified pin value: either High or Low (1 or 0) |
void drivePort(Port port, uint8_t value) |
Sets the pins on a port be High (1) or Low (0) according to the binary representation of the specified value |
Example:
1 #include "penguino/io.h"
2
3 int main( void ) {
4
5 // configure port B pin 0 as an output
6 output_init( B0 );
7
8 // configure port B pin 1 as an output
9 output_init( B1 );
10
11 // drive B0 High
12 drivePin( B0, High );
13
14 // drive B1 Low
15 drivePin( B1, Low );
16
17 while( 1 ) {
18 // spin
19 }
20 }
Analog Inputs
ADC refers to the internal Analog to Digital Converter. The default setup is:
- 10 bit conversion
- ADC runs at 1/128th of the clock speed
- The voltage on the AVCC pin is used as the reference voltage
- AVCC should be connected to VCC (using a capacitor for extra noise reduction)
Note: On the Penguino AVR only Port A has ADC functionality.
Function |
Functionality |
void adc_init( ) |
Initialises the Analog to Digital Converter (with default settings) |
void adcInput_init(Pin portPin) |
Initialises a pin as an analog input |
uint16_t adc_read(Pin portPin) |
Read the ADC value of a pin |
Example:
1 #include "penguino/io.h"
2
3 #define MAXREADING 1023
4 #define ONE_THIRD_OF_MAXREADING (MAXREADING/3)
5 #define TWO_THIRD_OF_MAXREADING (MAXREADING-LOW_READING)
6
7 int main( void ) {
8 // initalise Status LED
9 statusLed_init( );
10
11 // initialise ADC
12 adc_init( );
13
14 // use port A pin 0 as an analog input
15 adcInput_init( A0 );
16
17 while( 1 ) {
18 // read ADC reading on port A, pin 0
19 uint16_t analogReading = adc_read( A0 );
20
21 if ( analogReading < ONE_THIRD_OF_MAXREADING ) {
22 statusLed_green( );
23 } else if ( analogReading > TWO_THIRD_OF_MAXREADING ) {
24 statusLed_red( );
25 } else {
26 // in the middle
27 statusLed_orange( );
28 }
29 }
30 }
The following is a movie of the above program in action. A potentiometer is used as a voltage divider (outer pins connected to Vcc and Gnd, middle pin wired into A0).
![icy [labs]](/moin_static171/common/wikilogo.png)