Professional Documents
Culture Documents
Microcontroller Applications: Input / Output Ports
Microcontroller Applications: Input / Output Ports
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
5 6
1
17/02/2019
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
squareWave()
set pin
wait
Delay clear pin
wait
9 10 End squareWave()
Square
START Wave
Delay
End
11 12
2
17/02/2019
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
15 16
17 18
3
17/02/2019
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
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
4
17/02/2019
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
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.
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
31 32
35