RTES Embedded Systems Week 3 Part2
RTES Embedded Systems Week 3 Part2
Week 3 Part2
sheridancollege.ca
All.
Learning outcomes
Microcontroller System Description (Clock & System Port
configuration & Programming)
Implement a simple PIC24 system that has an in-circuit
programming interface.
Write C code for the PIC24 μC that implements I/O via
pushbutton switches and LEDs using a finite state machine
approach.
Discuss the different features of the PIC24 parallel ports
such as bidirectional capability, weak pull-ups, and open-
drain outputs.
2
All.
while{ } Loops
while(expression)
{
statement1;
statement2;
}
Example:
k = -10;
while(k < 41)
{
Temp = k * 9/5 + 32;
k++;
The while Loop flow chart
printf(“Temp: %f \n”, Temp);
}
4
All.
The answer is the second option, a good practice and a good programmer
always uses functions while writing code in C.
7
All.
What is a function?-contd.
A function is a named block of statements:
The statements have a common purpose, solving a unique problem
The statements may work with global variables declared at the top of the file
The statements inside the function are executed when the function is called or
invoked
The program “remembers” where the call came from and returns to the statement
right after the call when the function completes
8
All.
9
All.
2024-11-26 10
All.
Function Properties
A function has a name, return type, list of parameters, and scope
Name identifies the function (same naming rules as variables)
Return type
The data type of the information returned by the function (e.g. int, double, char,
etc.)
Used when the function calculates or generates a value
If the function does not return a value, its return type is defined as void
Scope is global by default. If you add the static keyword before the return type
then the scope of the function is that .c file (module) only.
11
All.
Function Parameters
They are usually input variables whose values are provided by
the caller
The list of function parameters is enclosed in( parentheses)
A parameter is declared like a local variable
When multiple parameters are needed they are separated
using a comma
It is common for functions to not need any parameters
The function declaration should use (void)
Empty parentheses are needed when calling, e.g. printArea()
12
All.
Function Names
By convention function names start with a lower-case letter with the
following words capitalized (e.g. calculateArea)
Can contain numbers(not at the beginning)
Names are usually made of two words: a verb (predicate) and noun
13
All.
Function parameters
A function parameter is an input variable whose value is provided by the
caller code
Also called arguments
The caller supplies a value
The runtime places the value inside the parameter variable
The function “sees” the value through the parameter variable
The list of function parameters is enclosed in (parenthesis)
A parameter is specified like any other variable <type> <name> but
without a semicolon
When multiple parameters are needed they are separated using a comma
It is possible for functions to NOT need any parameters
Parenthesis are still needed; nothing is placed between parenthesis
14
All.
The sign of a
method
NO semicolon
NO semicolon
15
All.
What is Structure?
Structure is another user defined data type available in C that
allows to combine data items of different kinds.
It is a collection of related variables under one name.
The variables of may be of different data types
Structures are used to represent a record. For example the
student record
Keyword struct introduces the structure definition will be
outside the main program.
Members of the same structure type must have unique
names, but two different structure types may contain
members of the same name without conflict.
16
All.
Structure Examples
Bank account Student information:
information: student id,
account number, last name,
account type first name
account holder gender,
first name
last name
balance
17
All.
Why structs in C?
Assume, you want to store information about a person:
User name, citizenship number, and salary; you can create different
variables name, citNo and salary to store this information.
18
All.
19
All.
20
All.
int main(void)
{
x.re = 1.25; // Initialize real part of x
x.im = 2.50; // Initialize imaginary part of x
y = x; // Set struct y equal to struct x
...
21
All.
22
All.
23
All.
int main() {
// Declaration of a variable using the PORTBBITS structure
PORTBBITS portB;
return 0;
}
24
All.
PIC24 Microcontroller
Basic GPI/O
25
All.
26
All.
27
All.
28
All.
1. Digital Pins: the PIC24 microcontroller has a set of pins that can be
configured as digital inputs or outputs. Each pin can be thought of as a
binary switch that can be in one of two states: high (1) or low (0).
2. Configuration: before using a pin, you need to configure it as either an
input or an output. This is done through configuration registers. For
example, setting a pin as an output might involve configuring the
corresponding TRIS (Tri-State) register.
3. Reading from Digital Input Pins: to read the state of a digital input pin,
you would typically use a function or directly check the state of a specific
bit in a register. For example, reading the state of pin RB0
29
All.
31
All.
33
All.
34
All.
35
All.
36
All.
37
All.
38
All.
2. Pin Multiplexing:
Many PIC24 microcontrollers support multiple functions on a single pin
through pin multiplexing. This allows you to assign different peripheral
functions (e.g., UART, SPI, I2C) to the same physical pin. We configure the
multiplexing by setting bits in the appropriate registers (e.g., RPINR, RPOR).
39
All.
40
All.
41
All.
43
All.
Scope shot of switch bounce; in this case, only bounced once, and settled in about
~500 microseconds. After detecting a switch state change, do not want to sample
again until switch bounce has settled. Our default value of 15 milliseconds is plenty
of time. Do not want to wait too long; a human switch press is always > 50 ms in
duration
44
All.
45
All.
46
All.
47
All.
48
All.
#include “config”
49
All.
50