Menu

SimpleIO Log in to Edit

Simple IO

The simple IO is available for all supported microprocessors (see the [Status]).

Preparation

It is common practice to define the microprocessor in the Makefile

MCU=atmega8

and access the part specific definitions in your Ada code via AVR.MCU. That is

with AVR;                         use AVR;
with AVR.MCU;

All MCU registers can be accessed as arrays of 8 booleans, ie. they have the type AVR.Bits_In_Byte. See the file avr.ads. Single bits can either be True or False. There are renamed constants

True, High, Set

and

False, Low, Clear

that you can use equivalently.

For setting bits in the port data direction registers you can also use

DD_Output

and

DD_Input

Output

Let's say you want to switch a LED that is connected to ground and pin 4 of port D.

Define two boolean variables for easy access:

LED_DD : Boolean renames MCU.DDRD_Bits(4);
LED    : Boolean renames MCU.PortD_Bits(4);

and then set the port direction as output and pull the line high

LED_DD := DD_Output;
LED := High;

Input

Reading an input uses the same technique. Assume a key between pin 1 of port D and ground. If pressed it pulls the input pin to low level. If open the internal pull-up resistor ensures high level. Again we define corresponding boolean variables for access to the mcu's registers

Key_DD      : Boolean renames MCU.DDRD_Bits(1);
Key_Pull_Up : Boolean renames MCU.PortD_Bits(1);
Key         : Boolean renames MCU.PinD_Bits(1);

Now we set the port direction, activate the internal pull-up and then read the status of the key.

Key_DD := DD_Input;
Key_Pull_Up := High;
-- key pressed is indicated by 0V
if Key = Low then
   -- do something if the key was pressed

Related

Wiki: Examples
Wiki: Home
Wiki: Introductory Tutorial
Wiki: Status

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.