3 1 RL Ecee Pic18f4580
3 1 RL Ecee Pic18f4580
Team Emertxe
Important Terms
Microcontrollers
Important Terms
● Host:
A system which is used to develop the target.
● Target:
A system which is being developed for specific
application.
● Cross Compiler:
An application used to generated code for another
architecture being in another architecture
Workspace Creation
Microcontrollers
Workspace Creation
● For better data organization lets create some project
working directories
● Please follow the below steps
Step 1
user@user:~] cd
Step 2
user@user:~] mkdir -p ECEP/Microcontrollers/ClassWork
Step 3
user@user:~] cd ECEP/Microcontrollers/ClassWork
Lets Start Coding
Microcontrollers
Embedded Programming - Let's Start Coding
● Well, come on lets make our hand a bit dirty in
embedded coding with the following code
Example ●
Nice, but few questions here
#include <stdio.h>
– Why did you write this code?
int main()
{ Hmm, Just to say hello world to
int x = 20;
embedded programming
printf(“%d\n”, x);
– Fine, where are you planning to run
return 0; this code?
}
Of course on a embedded target!
– Does it have a OS already running?
Ooink, Hmm noo, may be …
Microcontrollers
Embedded Programming - Let's Start Coding
● So questions raised in the previous slide has to be answered
before we can start our code.
● The answers to these questions are little tricky and depends
on
– Complexity of the work you do
– The requirement of the project
and many other factors
● Now the scope of this module is to learn low level
microcontroller programming which is non OS (called as
bare metal)
● So let's rewrite the same example as shown in the next slide
Microcontrollers
Embedded Programming - Let's Start Coding
Example ●
The change you observe is void
#include <stdio.h>
main(void)
void main(void)
{
int x = 20;
● Why?
printf(“%d\n”, x); – As mentioned generally the low end
}
embedded system are non OS based
– The code you write would be the first piece of code coming
to existence
● Now, lets not take this too seriously. This could again
depend the development environment
● There could be some startup codes, which would call the
main
Microcontrollers
Embedded Programming - Let's Start Coding
Example ● The next questions is, where are
#include <stdio.h>
trying to print? On Screen?
void main(void)
{
int x = 20;
– Does your target support that?
printf(“%d\n”, x); – Does your development
}
environment support that?
● Now again, all these are depends on your target board
and development environment
● Maaan, So many questions? Well, what should I write
then?
– Well that too depends on your target board!!
Microcontrollers
Embedded Programming - Let's Start Coding
ON Chip
RS232
Bootloader
LEDs
GPIO
Buzzer
CAN
TSOP SSDs
IR Sensor
ADC CLCD
(Single Port)
Microcontrollers
Embedded Programming - Let's Start Coding
●
So from the architecture we come to know that the board
has few LEDs, So why don't we start with it?
● So simple right? Well I hope you
Example know whats going happen with
#include <stdio.h>
this code!!
void main(void)
{
int led;
Any C programmer knows that the
●
/*
* Writing just a random value on the data latch register where
* LEDs are connected
*/
*latb = 0x55;
}
Microcontrollers
Embedded Programming - Let's Start Coding
or
Compile like
user@user:~] /<path>/xc8 -–ROM=0-3000 --chip=18f4580 main.c
●
As a last step we need to dump the main.hex to the target
board.
●
Sometimes we need a separate hardware to do this
(typically depends on the target board design)
●
RL-eCee board comes the microcontroller dumped with a
serial boot loader in it
●
So we need a system with a Serial Port or a USB to serial
adapter from hardware prospective
●
We will be using tiny boot loader software to transfer the
code to the target
●
Refer the next slide for the downloading steps
Microcontrollers
EP - Let's Start Coding – Hex Download
Download like
user@user:~] tinybldlin.py –-port /dev/ttyUSB0 --file main.hex
or
Download like
user@user:~] <path>/tinybldlin.py –-port /dev/<device_file> --file main.hex
void main(void)
{
/* Setting the pin direction as output (0 – output and 1 – Input) */
TRISB = 0x00;
/*
* Writing just a random value on the data latch register where
* LEDs are connected
*/
LATB = 0x55;
}
#include#include
“supporting_file.h”
“supporting_file.h” #include “main.h” #ifndef MAIN_H #ifndef #ifndef
SUPPORTING_FILE_H
SUPPORTING_FILE_H
#define MAIN_H #define #define
SUPPORTING_FILE_H
SUPPORTING_FILE_H
void function_1(void)
void function_1(void) void init_config(void)
{ { { #include <htc.h> #endif #endif
/* Function
/* Function
Code */ Code */ /* Initilization Code */
} } } #endif
void main(void)
{
init_config();
while (1)
{
/* Application Code */
}
}cc
void main(void)
{
init_config();
while (1)
{
/* Application Code */
}
}
Lets Roll Out on Interfaces
Microcontrollers
Lets Role Out on Interfaces
● LEDs
● Digital Keypad
● Interrupts
● Timers
● Clock I/O
● SSDs
● CLCD
● Matrix Keypad
● Analog Inputs
Light Emitting Diodes
Microcontrollers
Interfaces – LEDs - Introduction
S R S R
Which side will
work?
VDD VDD
Microcontrollers
Interfaces – LEDs – Working Principle
S R S R
Oops, wrong
choice. Can you
VDD VDD
explain why?
Microcontrollers
Interfaces – LEDs – Working Principle
LED1
RB0
LED2
RB1
LED3
RB2
LED4
RB3
LED5
RB4
LED6
RB5
LED7
RB6
LED8
RB7
To
Controller
SW1
Microcontrollers
Interfaces – Digital Keypad - Tactile Switches
To
Controller
SW1
Microcontrollers
Interfaces – Digital Keypad - Tactile Switches
To
Controller
SW1
Microcontrollers
Interfaces – Digital Keypad - Tactile Switches
this design?
● Is there any potential SW1
problem?
To
Controller
R1
Microcontrollers
Interfaces – Digital Keypad - Tactile Switches
this design?
● Is there any potential SW1
problem?
R2
To
Controller
R1
Microcontrollers
Interfaces – Digital Keypad - Triggering Methods
Microcontrollers
Interfaces – Digital Keypad - Bouncing Effects
VDD
SW15
RC0
SW16
RC1
SW17
RC2
SW18
RC3
Interrupts
Microcontrollers
Interrupts
● Basic Concepts
● Interrupt Source
● Interrupt Classification
● Interrupt Handling
Microcontrollers
Interrupts - Basic Concepts
● An interrupt is a communication process set up in a
microprocessor or microcontroller in which:
– An internal or external device requests the MPU to stop
the processing
– The MPU acknowledges the request
– Attends to the request
– Goes back to processing where it was interrupted
● Polling
Microcontrollers
Interrupts - Interrupt vs Polling
● Loss of Events
● Response
● Power Management
Microcontrollers
Interrupts - Sources
● External
● Timers
● Peripherals
Microcontrollers
Interrupts - Classification
Interrupts
Hardware Software
External Internal
Microcontrollers
Interrupts - Handling
Interrupt
Occurs Here
i
i+1
Microcontrollers
Interrupts - Service Routine (ISR)
● Similar to a subroutine
● Attends to the request of an interrupting source
– Clears the interrupt flag
– Should save register contents that may be affected by the code
in the ISR
– Must be terminated with the instruction RETFIE
● When an interrupt occurs, the MPU:
– Completes the instruction being executed
– Disables global interrupt enable
– Places the address from the program counter on the stack
● Return from interrupt
Microcontrollers
Interrupts - Service Routine (ISR)
● What / What Not
Microcontrollers
Interrupts - Latency
VDD
SW14
R48
1K
RB0
R24
10K
Microcontrollers
Interrupts - External Interrupt - Example
Example
#include <xc8> bit glow_led;
while (1)
{
while (!glow_led)
{
if (SWITCH == 1)
glow_led = 1;
● PW or PP Measurement etc.,
● Examples
Microcontrollers
Timers - Example
●
Requirement – 5 pulses of 8 µsecs
● Resolution – 8 Bit
●
Quantum – 1 µsecs
● General
Microcontrollers
Timers - Example
Timer Register
Overflows
28μs 20μs 12μs 4μs
Clock I/O
Microcontrollers
Clock I/O - Introduction
TON TOFF
TON TOFF
TON TOFF
Seven Segment Displays
Microcontrollers
Interfaces – SSDs - Introduction
VDD
a a
b b
c c
d d
e e
f f
g g
dp dp
Microcontrollers
Interfaces – SSDs - Example
● Assuming a common
Data Control
anode display, what a b c d e f g dp C1 C2 C3 C4
will the output for 0 0 1 0 0 1 0 1 0 1 0 0
provided table? 0 1 0 0 1 0 0 1 0 0 1 0
Segment Map
Microcontrollers
Interfaces – SSDs - Example
● Assuming a common
Data Control
anode display, what a b c d e f g dp C1 C2 C3 C4
will the output for 0 0 1 0 0 1 0 1 0 1 0 0
provided table?
DISP1 DISP2 DISP3 DISP4
a a
b
c
f b d
e
f
g g
dp
e c C1
C2
d dp C3
C4
Segment Map
Microcontrollers
Interfaces – SSDs - Example
● Assuming a common
Data Control
anode display, what a b c d e f g dp C1 C2 C3 C4
will the output for
provided table? 0 1 0 0 1 0 0 1 0 0 1 0
Segment Map
Microcontrollers
Interfaces – SSDs – Multiplexing
C2
C3
Microcontrollers
Interfaces – SSDs – Multiplexing
C2
Microcontrollers
Interfaces – SSDs – Multiplexing
RA3
RA2
RA1
RA0
Character Liquid Crystal Display
Microcontrollers
Interfaces – CLCD - Introduction
● Most commonly
used display ASCII
characters
● Some
customization in
symbols possible
● Communication
Modes
– 8 Bit Mode
– 4 Bit Mode
Microcontrollers
Interfaces – CLCD - Circuit on Board
D0
RD0 D1
RD1 D2
RD2 D3
RD3 D4
RD4 D5
RD5 D6
RD6 D7
RD7
RW
RC0 RS
RC1 EN
RC2
Matrix Keypad
Microcontrollers
Interfaces – Matrix Keypad - Introduction
COL1 COL2
SW
SW
1
2
SW ROW1
SW
3
4
ROW2
Microcontrollers
Interfaces – Matrix Keypad – Circuit on Board
RB5
SW2 SW5 SW8 SW11
RB6
RB7
Analog Inputs
Microcontrollers
Analog Inputs - Introduction