Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

17/02/2019

Input / Output Ports


Microcontroller Applications
 The IO ports of a microcontroller are some of
the most used features. They allow the
embedded system designer to drive switches
Using IO Ports / Code and all kinds of digital displays, generate
Modularisation digital signals, drive keypads, stepper
motors, etc.

Input / Output Ports Using IO to Generate a Signal

 For this lecture, we will go through some  A squarewave waveform can be easily
algorithms for some of these applications. generated in software by using IO Ports.
During your practical sessions you’ll then be  To generate one, you need to:
asked to compete the code. – Set an output Pin to logic ‘1’.
– Wait or delay the microcontroller.
– Then clear that same Pit to ‘0’.
– Wait or delay the microcontroller again.
– Repeat the process.

3 4

Using IO to Generate a Signal Using IO to Generate a Signal

 Clearing and Setting Bits  Clearing and Setting Bits


– To CLEAR a bit, we use the AND operator and a – To SET a bit, we use the OR operator and a mask
mask that has been initialised with ‘1’, except the that has been initialised with ‘0’, except the bit you
bit you want to clear. want to set.

PORTA = PORTA & 0xF7 // In Hex PORTA = PORTA | 0x08 // In Hex


PORTA = PORTA & 0b11110111 // In Binary PORTA = PORTA | 0b00001000 // In Binary
or
PORTA = PORTA & ~0x08 // In Hex
PORTA = PORTA & ~0b00001000 // In Binary

5 6

1
17/02/2019

Using IO to Generate a Signal Using IO to Generate a Signal

 Find a Solution for the design.  Find a Solution for the design.
1. Clarify the Requirements 3. Write an Algorithm (Pseudo Code)
 Create a Squarewave waveform Start
2. Hardware Configurations while(1)
 PORTA = Output set pin
wait
 Select an output Pin = Pin 7
clear pin
 Hardware Connections = None wait
endwhile
End

7 8

START

Loop Using IO to Generate a Signal


Forever

 Find a Solution for the design.


Consider the structure of the code.
Set Pin  Will this need a function?
 If Yes, what is the function name and prototype?
 Eg Function = squareWave()
 Prototype = void squareWave (void)
Delay  Will it change the algorithm?
 Start
 while(1)
 call squareWave()
 endwhile
Clear Pin  End

 squareWave()
 set pin
 wait
Delay  clear pin
 wait
9 10  End squareWave()

Square
START Wave

Using IO to Generate a Signal


Loop Set Pin
Forever
 Find a Solution for the design.
Square
Delay 4. Test Method
Wave
 Think of methods on how this can be tested?
5. Write the Code
Clear Pin
6. Test and Debug

Delay

End
11 12

2
17/02/2019

Serial Communications Serial Communications

 The ATmega2560 consists of 4 serial ports  Configuration of the Serial Port on the micro
which can be used to communicate to in C can difficult.
external peripheral devices.  Hence, we should simply use the Serial
 USART0, USART1, USART2, USART3 library that has already been provided by
 For the ATmega2560 board, PORTE Pins 0 the Arduino IDE.
and 1 are used for RXD0 and TXD0.
 An advantage of using the Serial Port is that
we can debug the microcontroller using a
standard serial terminal on a PC.
13 14

Serial Commands Examples Practical 2

 Serial.begin(9600) Initiate  Using the provided lecture notes to help


 Serial.println() Print to Serial Port
with this practical.
 Serial.read() Read from Serial Port
 Serial.readBytes() Read bytes  Creating a Squarewave
 Serial.readString() Read a string of characters  Using the Serial Port
– Just been through
 An example on how and where to use these will be
demonstrated in your Practical.  Code Modularisation
 Always remember. Use the Serial Comms port as a method to – Using DevC++
help debug your code!!!

15 16

Code Modularisation Code Modularisation

 “Modular Programming is a software design  The benefits of Modular code are:


technique that emphasizes separating the – Less needs to be written.
functionality of a program into independent, – Single procedure can be developed for re-use. No need to
interchangeable modules, such that each contains retype the code again.
everything necessary to execute only the one – Code is stored over multiple files.
aspect of the desired functionality” – Code can be short and easier to read and understand.
– Is useful for debugging faulty code.
 Modular Programming, – The same code can be used for many applications.
Wikipedia:https://en.wikipedia.org/wiki/Modular_programming
– The scoping of variables can be easily controlled.

17 18

3
17/02/2019

Functions Function Prototype

 The first step of modularising code is the  In order to create a function, we must first
creation of functions. create a function prototype.
 A function, (subroutine), is a sequence of  Function Prototypes are essentially written
program instructions that perform a specific in a very similar manner as the function
task. These instructions are then packaged name itself
in a single unit. – Eg: SquareWave
 That unit of code can then be easily called – Prototype: void squareWave (int);
throughout any part of program. – Function: void squareWave (int x);

19 20

/* CODE WITHOUT FUNCTIONS */

const int DELAY_MS = 500;

int main(void)
Function Prototype {
int x;
DDRA = 0xFF; // PORT A an output
 A Function Prototype is required by the compiler in
while(1)
order for it to identify that these functions have
{
been declared. PORTA = 0xAA; // Toggle on RED
 Function Prototypes are always declared at the _delay_ms(DELAY_MS); // Delay the code
beginning of your code, along with any declaration PORTA = 0x55; // Toggle on GREEN
of any global variables or constants. _delay_ms(DELAY_MS); // Delay the code
}
 Think of a Function Prototype is like a variable. }
When you create a new function, you need to
declare it like you do with any other variable in your
code.

21 22

/* CODE WITH FUNCTIONS */ /* CODE WITH FUNCTIONS */

const int DELAY_MS = 500; void delay (int); // FUNCTION PROTOTYPE

void delay (void); // FUNCTION PROTOTYPE int main(void)


{
int main(void) DDRB = 0xFF; // PORT B an output
{
DDRA = 0xFF; // PORT A an output while(1)
{
while(1) PORTB = 0xAA; // Toggle on RED
{ delay(100);
PORTA = 0xAA; // Toggle on RED PORTB = 0x55; // Toggle on GREEN
delay(); delay(50);
PORTA = 0x55; // Toggle on GREEN }
delay(); }
} void delay (int time)
} {
void delay (void) _delay_ms(time); // Delay the code
{ }
_delay_ms(DELAY_MS); // Delay the code
}
23 24

4
17/02/2019

Modularisation – Function File Modularisation – Header File

 Functions play a key role in the  A compiler still needs a method of ‘finding’ what
functions have been declared for the current
modularisation of code. program.
 Created Functions can be separated from a  Previously, the compiler used the ‘Function
‘Main’ file of code. They are to be stored in Prototypes’ which were declared at the beginning
of the code.
a separate ‘Functions File’ or library.
 This cannot be the case when modularising code.
 This file can be imported into any C code Function Prototypes need to be declared in a
you are currently developing, and be called ‘Header’ File.
at any time.  Header Files are affixed with the .h extension.

25 26

Modularisation Arduino File Extension

 Most C Microcontroller applications will use the ‘.c’


extension for files made from C code.
Main File Functions File Header File
 Arduino is different. It can ONLY open and use it’s
ledsFLASH.c ledFunctions.c ledFunctions.h
own file extensions designated as ‘.ino’.

All of your main All of the created All of the Function – C Code – squareWave.c
code and calls to functions reside in Prototypes or
your functions this file. Global Variable – Arduino – squareWave.ino
reside in this file. reside in this file.

 Remember, when using other IDE compliers, you


will most likely use the ‘.c’ extension.
27 28

REVISITING THE PREVIOUS CODE /* MAIN CODE FILE – main.c */


#include <avr/io.h>
void delay (int); // FUNCTION PROTOTYPE #include “util.h”

int main(void) void user_forever_loop()


{ {
DDRA = 0xFF; // PORT A an output DDRA = 0xFF; // PORT A an output

while(1) while(1)
{ {
PORTA = 0xAA; // Toggle on RED PORTA = 0xAA; // Toggle on RED
delay(100); UTIL_DelayMs(100);
PORTA = 0x55; // Toggle on GREEN PORTA = 0x55; // Toggle on GREEN
delay(50); UTIL_DelayMs(50);
} }
} }
void delay (int time)
{
_delay_ms(time); // Delay the code
}

29 30

5
17/02/2019

/* FUNCTIONS FILE – util.c */ /* HEADER FILE – util.h */

#include <util/delay.h> // FUNCTION PROTOTYPE

void UTIL_DelayUs (int time) void UTIL_DelayUs (int);


{ void UTIL_DelayMs (int);
_delay_us(time); // Delay in Microseconds void UTIL_DelaySec (int);
}

void UTIL_DelayMs (int time)


{
_delay_ms(time); // Delay in Milliseconds
}

void UTIL_DelaySec (int time)


{
_delay_ms(time * 1000); // Delay in Seconds
}

31 32

Modularisation – Inputting Files

 The ‘Function Files’ is also required to be added


into your Arduino project.
 These files are to be included into the working
directory of the ‘main’ project file.

 The Header File is explicitly imported into the IDE


by adding #include”function_file.h” line into the
code.
33 34

Next Lecture: IO Ports LCD Interface

 We will look into interfacing the LCD Display


to the Microcontroller.

35

You might also like