Sensors and Embedded Systems Course Overview
Sensors and Embedded Systems Course Overview
Embedded Systems - 1
Module Introduction
A microcontroller =
microprocessor core +
memory + peripherals
+ others
Control SFR(s)
Microcontroller Peripheral "Outside
Core World"
Data Transfer SFR(s)
Interrupts
Interrupt(s)
Main
Loop Main application code goes here
Comments
Space
Declaration
Statements
Only really four basic types: char, int, float and double
Type Storage size Value range (decimal)
unsigned char 1 byte 0 to 255
signed char 1 byte -128 to 127
int 2 bytes -32,768 to 32,767
unsigned int 2 bytes 0 to 65,535
float 4 bytes 1.2E-38 to 3.4E+38
double 8 bytes 2.3E-308 to 1.7E+308
Decimal 0 63 83
Binary 0b00000000 0b00111111 0b01010011
Hexadecimal 0x00 0x3F 0x53
Comments start with /*, and end with */. In this form they can
run over more than one line.
/******************************************
Example 1. Introductory Example of C Programming
******************************************/
Or for a single line use a double slash, //. This needs no
terminating symbol.
counter = counter + 1; //Increment counter
It’s good practice to use plenty of comments.
They do not impact on code size!
Sensors and Embedded Systems – David Wilson 29
Keywords.
C has 32 reserved "keywords". These are used for program flow control,
and declaration of data types. They cannot be used by the programmer
as names.
Example C Keywords
break double goto return switch
char else if short unsigned
const float int signed void
do for long static while
Sensors and Embedded Systems – David Wilson 30
Functions.
call Function
return
if while
if...else No
TA >>TD?
do...while Yes
switch break
Sensors and Embedded Systems – David Wilson 35
While
while (1)
{
PORTB = counter; //Move value to Port B
counter = counter + 1;
}
The two statements which fall within the while braces thus repeat
indefinitely.
Sensors and Embedded Systems – David Wilson 37
If Statement.
[Link]
if while
if...else No
TA >>TD?
do...while Yes
switch break
Sensors and Embedded Systems – David Wilson 47
Example 1.
• Start
• Input a set of 4 marks
• Calculate their average by summing and dividing by 4
• if average is below 50
• Print “FAIL”
• else
• Print “PASS”
• End
• Easier to modify.
Sensors and Embedded Systems – David Wilson 52
Disadvantages of Pseudocode
• A graphical representation.
Denotes an input
START
Input
Step 1: Input M1,M2,M3,M4
M1,M2,M3,M4 Step 2: GRADE =(M1+M2+M3+M4)/4
GRADE=(M1+M2+M3+M4)/4 Step 3: if (GRADE <50) then
Student Fails
N GRADE Y else
<50
Student Passes
Student Passes Student Fails endif
STOP
• Write an algorithm and draw a flowchart that will calculate the roots
of a quadratic equation 𝑎 𝑥 2 +𝑏𝑥 +𝑐=0
−𝑏 ± √ 𝑏 − 4 𝑎𝑐 2
𝑥=
2𝑎
• Start
• Input the coefficients (a, b, c) of the quadratic equation
• Calculate d
• Calculate x1
• Calculate x2
• End
START
Start
Input a, b, c Input the coefficients (a, b, c)
Calculate d
d = sqrt(b x b – 4 x a x c)
Calculate x1
x1 =(–b + d) / (2 x a) Calculate x2
X2 = (–b – d) / (2 x a)
End
STOP
Main
Loop Main application code goes here
External crystal
WDT
ADC
I/O
LCD
Setup
ADC, SPI, CCP,LCD
Peripherals
Main
Loop Read sensors, update displays
What is the highest number (in Decimal and Hexadecimal) that the following
counters can count up to?
Remember 2n-1 is the counter range
• Further features can easily be designed into the counter, e.g. outputs can
be cleared to zero or be pre-loaded with a certain number.
• If the incoming clock pulses are regular in frequency, the counter can also
measure time.
• If TC is clock period, and n cycles are counted, then the period during
which counting has taken place is nTC.
Once it reaches the Max value, it will roll back to zero setting up an
OverFlow flag and generates the interrupt if enabled.
Sensors and Embedded Systems – David Wilson 71
The Counter as a Timer - Example
• Complete the table below for different clock frequencies and counter sizes.
Clock Period Number of Counter
Frequency Time to fill counter
(Tc) Bits n (2n) range (2n -1)
1MHz 1ms 8 255 255ms
4MHz 250ns 6 63 15.75ms
1MHz 1ms 11 2,047 2047ms
500kHz 2ms 16 65,535 131.07ms
1MHz 1ms 32 4,294,967,295 4294.967295s
100kHz 10ms 24 16,777,215 167.77215s
Actual Timer
• Timer0 is
controlled (SFR)
T0CON.
• The overall
operating mode is
selected by bit 6
(T08BIT), which
determines either
8 or 16 bit mode.
• (page 154 of
datasheet)
Sensors and Embedded Systems – David Wilson 76
Timer0 in 16-bit mode (1)
• Timer 0 in 16-bit mode just extends the concepts of the 8-bit version..
• (Page 156 PIC18F25K22 datasheet)
Solution.
When low byte is read the high byte is also
transferred to a buffer which can then be
read later. This ensures that an accurate
snapshot of all 16bits has been captured.
The same happens during a read.
• 0b110x0000
x 2 = 2s
28-1 = 255
2 x 255 = 510
=1.96kHz
Actual Timer
Actual Timer
Timer 0
• 8 or 16 bit timer/counter
• Dedicated 8-bit software programmable pre-scaler
• Internal or external clock select with edge select for external
clock
• Interrupt on overflow from FFh to 00h
• 16-bit operation.
• Prescaler 1:4
• Secondary Oscillator
T1CON = 0b10101111
• This would give a period of 8 seconds so need to use the preload feature
discussed in slide 4.
F=8192Hz
\ 8192 counts per second
= 8192 x 3 = 24576
In Hex = 6000
• The T2CON register has been set to 10111101, and the PR2 loaded with
1111 1010.
• With what frequency (or period) is the TMR2IF interrupt flag set?
TCON2=0b10111101
Prescaler 1:4
Timer 2 ON
Post scaler 1:8
Fosc/4 = 2MHz
Prescaler (1:4)
Period = 500ns 500ns x 4 = 2s
• Counter mode is selected by setting the clock source bit in the timer control
register.
• The counter will increment either on rising or falling edge of the clock pulses,
which is software selectable by the T0SE (Timer0 Source Edge) bit of the timer
control register.
• The range of the counter can be extended by the use of the prescaler.
A larger number of steps applied, which lengthens the pulse width, also supplies
more power to the load. Lowering the number of steps applied, which shortens the
pulse width, supplies less power
Sensors and Embedded Systems – David Wilson 102
A Practical Implementation of PWM
V
"freewheeling" diode S
L (with resistance R)
d ecay in g cu rren t flo w s
in th is d irectio n , w h en IL
sw itch ed o ff.
V
G
lo g ic g ate,
o r p o rt b it
o u tp u t
t
on
Closed In general:
Switch
Open
ton VS
T
I Iave = ___
x _____
Iave
T R
Sensors and Embedded Systems – David Wilson 103
A Simple PWM Structure.
clock Counter
Comparator Output
data bus
• In ECCP modules, the Enhanced PWM function has either full-bridge or half-
bridge PWM output. Full-bridge ECCP modules have four available I/O pins while
half-bridge ECCP modules only have two available I/O pins.
• PRx registers
• TxCON registers
• CCPRxL registers
• CCPxCON registers
An 8-bit register,
whose value you
can set in your
program
CCPRIL value
Timer 2 and PR2 values changed in program
equal, Timer is reset and
PWM goes high
CCPRIL value transferred
PR2 Value to CCPRIH as Timer clears
Timer 2 Value
PWM Output
T ton
Timer 2, free-
running
CCPR = 750 = 1 0 1 1 1 0 1 1 1 0
CCPR1L = 0xBB
DC1B =0x2
void TMR2_Initialise(void)
{
PR2 = 0xF9; // PR2 249;
TMR2 = 0x00;
PIR1bits.TMR2IF = 0; // Clearing IF flag
T2CON = 0x06; // T2CKPS 1:16; T2OUTPS 1:1; TMR2 on
}
Sensors and Embedded Systems – David Wilson 120
Solution (5) The Code
void main(void)
{
PIC_Initialise();
EPWM1_Initialise();
TMR2_Initialise();
while(1)
{
//sit here and do nothing
}
The ECCP modules have the following additional PWM registers which control
Auto-shutdown, Auto-restart, Dead-band Delay and PWM Steering modes:
• ECCPxAS registers
• PSTRxCON registers
• PWMxCON registers
Not to worry too much about these as we will be using standard PWM.
Timer 0
void Timer0_Init(void)
{
T0CON=0x88 // Clock Fosc/4,no prescale,16bit, timer on
TMR1H=0xB1; // Preload timer 1 so overflows every 20ms
TMR1L=0xE0; // 65535 -20000+1 ( 1 for ISR overhead))
}
V
S
"freewheeling" diode In general:
L (with resistance R) ton VS
d ecay in g cu rren t flo w s
in th is d irectio n , w h en
Iave = ___
x _____
IL
sw itch ed o ff. T R
t
V on
G Closed
Switch
Open
lo g ic g ate, T
I
o r p o rt b it
Iave
o u tp u t
VBAT
9V
R6 R5
PNP PNP
R3 R4
Clockwise on Q3 Q4
off
INPUT 1
R1 off Q1 Q2
on R2
INPUT 2
NPN NPN
Enable1
VBAT
9V
R6 R5
PNP PNP
R3 R4
Q3 Q4
off on
Anti clockwise
Output1 M Output2
Direction
R1 on off R2
INPUT 1 Q1 Q2
INPUT 2
NPN NPN
Equivalent single
half bridge
Enable1
VBAT
9V
R6 R5
PNP PNP
R3 R4
Q3 Q4
Output1 M Output2
R1 R2
INPUT 1 Q1 Q2
INPUT 2
NPN NPN
VBAT
9V
R6 R5
PNP PNP
R3 R4
Q3 Q4
Output1 M Output2
R1 R2
INPUT 1 Q1 Q2
INPUT 2
NPN NPN
PWM
PWM Signal
When the shaft of the motor is at the desired position, the motor is stopped.
The motor's speed is proportional to the difference between its actual position and
desired position.
PWM needs to be sent several times for system to move to desired position ( see
datasheet)
The angle is determined by the duration of the pulse (PWM) that is applied to the
control wire.
Min Pulse 0
Pulse Width 1ms
90
Neutral
Pulse Width 1.5ms
void Timer1_Init(void)
{
T1CON=0x3; // ClockFosc/4,no prescale,16bit, timer on
TMR1H=0xB1; // Preload timer 1 so overflows every 20ms
TMR1L=0xE0; // 65535 -20000+1 ( 1 for ISR overhead))
}
• Configure CCP5 to compare Timer 1 value with data passed from main code.
void CCP5_Init(void)
{
CCP5CON = 0x0A; // CCP5 Compare mode: generate
// software interrupt on compare match
// (CCP5 pin is unaffected, CCP5IF is set)
CCPTMRS1 = 0x0; // Timer 1 used.
}
• Pass data to compare register
CCPR5 = duty;
Sensors and Embedded Systems – David Wilson 141
Servo Motors – ISR Code, Worth a
Look?
void __interrupt(high_priority) Timer1_ISR(void)
{
if (TMR1IF && TMR1IE) // if Timer 1's interrupt flag and enable set
{
TMR1H=0xB1; // Reload timer values Reload Timer 1
TMR1L=0xE0; with 45536
TMR1IF = 0; // clear the interrupt flag
LATAbits.LATA5 = 0;
}
if (CCP5IF && CCP5IE) // if CCP5's interrupt flag and enable set
Port pins
{ toggled
LATAbits.LATA5 = 1;
CCP5IF=0; // clear the interrupt flag
}
}
A A
B B B B
A A
A A
B B B B
A A
A A A A
B B B B B B B B
A A A A
A A A A
B B B B B B B B
A A A A
Sensors and Embedded Systems – David Wilson 145
Stepper Motors – Micro-stepping
A
With maximum power in phase A,
phase B is at zero. The rotor will line
B B
up with phase A. As the current to
stepper motor energised coil phase A decreases, it increases to
sequence
A
phase B. The rotor will take small
A steps towards phase B until phase B
is at its maximum and phase A is at
B B zero. The process continues around
the other phases.
A
• Individually switch on and off ports to match the desired drive pattern.
• Use CCP module to generate PWM ‘sine wave’ for micro-stepping.
• Use a dedicated stepper driver module eg TMC2130.
3
8
{
__delay_ms(15); // Power on delay as per datasheet
LCD_Command(0x38); // Uses 2 line and initialise 5*7 matrix
__delay_ms(5); // Additional delay as per datasheet
LCD_Command(0x01); // Clear display screen
LCD_Command(0x0c); // Display on cursor off
LCD_Command(0x06); // Increment cursor (shift cursor to right)
}
Full function set used and 5ms delay routine incorporated into
LCD_Command routine rather than 3 function set commands
Sensors and Embedded Systems – David Wilson 156
LCD Command – 8 bit mode
• The higher nibble is sent first and then the lower nibble.
• Only 4 data (D4 - D7) pins of the LCD are connected to the microcontroller
along with Register select, Read/write and Enable.
• Cons – slower and coding slightly more complex ( AND, OR and bit shifting
operations).
}
Sensors and Embedded Systems – David Wilson 163
Logical vs Bitwise Operations (1)
• Logical operations assume that the entire variable represents either true or false.
• Combining two integer values using a logical operator produces one result,
with the variable representing true or false.
int x = 0000FFFF;
int y = FFFF00FF;
int z = x && y;
• Bitwise operations assume that each bit in the variable’s value represents a
separate true or false.
• An example of ANDing:
0011 1011
0000 1111
cleared 0000 1011 unchanged
• An example of ORing:
0011 1011
0000 1111
unchanged 0011 1111 set
void LCD_Init()
{
LCD_Command(0x03);
LCD_Command(0x02); //send for initialisation of LCD
//for nibble (4-bit) mode
LCD_Command(0x28); //use 2 line and initialise 5*7 dots
LCD_Command(0x01); //clear display screen
LCD_Command(0x0c); //display on cursor off
LCD_Command(0x06); //increment cursor (shift cursor to right)
}
• The HD44780 type controller chip is used with a wide variety of Liquid Crystal
Displays.
• These LCDs come in many configurations and can be arranged in 1, 2, or 4 rows.
• The controller operates exactly the same way for all displays and it is up to the
programmer to deal with this situation.
• The controller contains 80 bytes of Display Data Random Access Memory which is
usually referred to as DDRAM.
80 1 0 0 0 0 0 0 0
C0 1 1 0 0 0 0 0 0
16 x 2
80 81 82 83 84 85 86 87 88 89 8A 8B 8C 8D 8E 8F
C0 C1 C2 C3 C4 C5 C6 C7 C8 C9 CA CB CC CD CE CF
20 x 4
80 81 82 83 84 85 86 87 88 89 8A 8B 8C 8D 8E 8F 90 91 92 93
C0 C1 C2 C3 C4 C5 C6 C7 C8 C9 CA CB CC CD CE CF D0 D1 D2 D3
94 95 96 97 98 99 9A 9B 9C 9D 9E 9F A0 A1 A2 A3 A4 A5 A6 A7
D4 D5 D6 D7 D8 D9 DA DB DC DD DE DF E0 E1 E2 E3 E4 E5 E6 E7
NUMBERS:
Numbers can be more difficult !
Example 1:
Your micro-controller measures a sensor and returns the value of 5.
Issue:
The value of five and the character “5” are two different things. The
ASCII number for the character “5” is 53.
Solution:
Add 48 to any number that you are sending to the display.
Sensors and Embedded Systems – David Wilson 175
ASCII
American Standard Code for Information Interchange
DEC HEX Char/Control DEC HEX Char/Control DEC HEX Char DEC HEX Char DEC HEX Char DEC HEX Char DEC HEX Char/Control DEC HEX Char DEC
0 00 Null 20 14 DC4 (Device Control 4) 40 28 ( 60 3C < 80 50 P 100 64 d 120 78 x 140 8C Œ 160
1 01 SOH (Start of Header) 21 15 NAK (Negative Acknowledge) 41 29 ) 61 3D = 81 51 Q 101 65 e 121 79 y 141 8D 161
2 02 STX (Start of Text) 22 16 SYN Synchronize 42 2A * 62 3E > 82 52 R 102 66 f 122 7A z 142 8E Ž 162
3 03 ETX (End of Text) 23 17 ETB (End Transmission Block) 43 2B + 63 3F ? 83 53 S 103 67 g 123 7B { 143 8F unused 163
4 04 EOT ( End of Transmission) 24 18 CAN Cancel 44 2C , 64 40 @ 84 54 T 104 68 h 124 7C | 144 90 unused 164
5 05 ENQ (Enquiry) 25 19 EM (End of Medium) 45 2D - 65 41 A 85 55 U 105 69 i 125 7D } 145 91 ‘ 165
6 06 ACK (Acknowledge) 26 1A SUB (Substitute) 46 2E . 66 42 B 86 56 V 106 6A j 126 7E ~ 146 92 ’ 166
7 07 BELL (Ring) 27 1B ESC ( Escape) 47 2F / 67 43 C 87 57 W 107 6B k 127 7F DEL (Delete) 147 93 “ 167
8 08 BS (Backspace) 28 1C FS (File Separator) 48 30 0 68 44 D 88 58 X 108 6C l 128 80 € 148 94 ” 168
9 09 HT (Horizontal Tab) 29 1D GS (Group Separator) 49 31 1 69 45 E 89 59 Y 109 6D m 129 81 unused 149 95 • 169
10 0A LF (Line Feed) 30 1E RS (Record Separator) 50 32 2 70 46 F 90 5A Z 110 6E n 130 82 ‚ 150 96 – 170
11 0B VT (Vertical Tab) 31 1F US (Unit Separator) 51 33 3 71 47 G 91 5B [ 111 6F o 131 83 ƒ 151 97 — 171
12 0C FF (Form feed) 32 20 SPACE 52 34 4 72 48 H 92 5C \ 112 70 p 132 84 „ 152 98 ˜ 172
13 0D CR (Carriage Retrun) 33 21 ! 53 35 5 73 49 I 93 5D ] 113 71 q 133 85 … 153 99 ™ 173
14 0E SO ( Shift Out) 34 22 " 54 36 6 74 4A J 94 5E ^ 114 72 r 134 86 † 154 9A š 174
15 0F SI (Shift In) 35 23 # 55 37 7 75 4B K 95 5F _ 115 73 s 135 87 ‡ 155 9B › 175
16 10 DLE ( Data Link Escape) 36 24 $ 56 38 8 76 4C L 96 60 ` 116 74 t 136 88 ˆ 156 9C œ 176
17 11 DC1 (Device Control 1) XON 37 25 % 57 39 9 77 4D M 97 61 a 117 75 u 137 89 ‰ 157 9D unused 177
18 12 DC2 (Device Control 2) 38 26 & 58 3A : 78 4E N 98 62 b 118 76 v 138 8A Š 158 9E ž 178
19 13 DC3 (Device Control 3) XOFF 39 27 ' 59 3B ; 79 4F O 99 63 c 119 77 w 139 8B ‹ 159 9F Ÿ 179
The original ASCII specification encodes 128 characters into numbers (see table below). These include the numbers 0 to 9, lowercase a-z, uppercase A-Z, and punctuation.
The first 31 characters are non-printing "control codes", most of which are no longer used, with the exception of the carriage return (13), line feed (10), and tab (9).
(LCD_Data& 0x0F) 0 0 0 0 0 0 0 0
OR (0xF0 & letter) 0 1 1 1 0 0 0 0
LCD_Data 0 1 1 1 0 0 0 0
(LCD_Data& 0x0F) 0 0 0 0 0 0 0 0
OR (letter<<4) 0 1 1 0 0 0 0 0 Send Low Nibble
LCD_Data 0 1 1 0 0 0 0 0
Issue:
First 139 must be chopped into “1”, “3”, and “9” then add 48 to each to convert the
value into ASCII then send individually to the display.
C has no native string data type, so strings must always be treated as character
arrays.
Strings:
•Are enclosed in double quotes "string"
•Are terminated by a null character '\0’ (ASCII value of 0).
•Must be manipulated as arrays of characters
•(treated element by element)
sprintf( data, "%2.1f“ ,flaps); // sprintf with float 2 sig figs and 1 decimal place
String to be formatted
Destination string
The conversion type field is where a single character specifies that the
argument is interpreted as a character, string, number, or pointer.
Sensors and Embedded Systems – David Wilson 183
Sprintf() Arguments
Conversion type Argument Type Output Format
d int Signed decimal number
u unsigned int Unsigned decimal number
o unsigned int Unsigned octal number
x unsigned int Unsigned hexadecimal number using 0123456789abcdef
X unsigned int Unsigned hexadecimal number using 0123456789ABCEDF
f double Floating-point number using the format [-][Link]
e double Floating-point number using the format [-][Link][-]dd
E double Floating-point number using the format [-][Link][-]dd
g double Floating-point number using either e or f format.
c int int is converted to unsigned char, and the resulting character is written
s char * String with a terminating null character
p void * Pointer value, the X format is used
% <none> A % is written. No argument is converted.
while(1)
{
Read_ADC();
LCD_Command(0xC4); //move cursor to row 2 column 4
sprintf(data, "%d", result);
LCD_String(data);
__delay_ms(200);
}
186
Next Lecture
Digital to Analogue Conversion
Resistor ladder
MUX signal
selection bits
MUX, selects
between several
signals and forwards
the selected one to a
single output line.
Let’s look at an 8 to 1
MUX as an example.
Source Select
Sensors and Embedded Systems – David Wilson 190
8 to 1 MUX (Input 0)
Each MUX setting selects one output from the internal resistor ladder and hence
generates an output voltage dependent on the MUX input setting.
Selectable
reference
voltages
Output
Enable
𝑉 𝑜𝑢𝑡 =¿
Sensors and Embedded Systems – David Wilson 209
DAC Initialisation
Analogue Digital
voltage value
temperature
pressure
light Transducer Signal ADC Controller
weight Conditioning
(optional)
airflow
humidity
A Basic System
Usually, more than one input is required and the signal needs processing
before it can be converted. In most cases, therefore, it is necessary to build
up a complete data acquisition system.
D-A converter
• Must assume the worst output
clock
Advantages Disadvantages
• Simplest in terms of operational • Expensive
theory
• For each additional output bit, the
• Most efficient in terms of speed, number of comparators is doubled
very fast • i.e. for 8 bits, 256
• limited only in terms of comparators needed
comparator and gate propagation
delays
• The register monitors the comparators output to see if the binary count is
greater or less than the analog signal input and adjusts the bits accordingly
• Vin =0.425V
MSB LSB
0.425<0.5 bit 7 =0
0.425>0.25 bit 6 =1
MSB LSB
0 1
0.425>0.375 bit 5 =1
MSB LSB
0 1 1
0.425<0.4375 bit 4 =0
MSB LSB
0 1 1 0
0.425>0.40625 bit 3 =1
MSB LSB
0 1 1 0 1
0.425>0.40625 bit 3 =1
MSB LSB
0 1 1 0 1 1
=0.4296875V 01101110
Vref =0.25V
0.425<0.4296875
bit 1 =0
MSB LSB
0 1 1 0 1 1 0
01101101
=0.42578125V
Vref =0.25V
0.425< 0.42578125V
bit 0 =0
MSB LSB
0 1 1 0 1 1 0 0
=0.421875V
0.5
0.5
0.4375 0.4296875 0.42578125
0.421875 0.421875
0.40625
0.4 0.375
0.3
0.25
0.2
0.1
0
1 2 3 4 5 6 7 8 9
– separate the ADC earth and supply from that of the microcontroller
– minimise interference during conversion - by synchronising
conversion to internal clock transitions and minimising all other
switching at this time.
• Ultimately, you will only find ADCs of limited resolution (generally 8- or 10-
bit) integrated onto a microcontroller.
Input multiplexer
AN 5-7 and AN20-27 Incorporates
not on PIC18F25K22 Sample and Hold
Need to look at acquisition and conversion times before this SFR can be set.
Sensors and Embedded Systems – David Wilson 245
Typical Timing Requirements.
Configure & enable ADC
Start Conversion
Read Data
From datasheet
TACQ = 6.1μs
Sensors and Embedded Systems – David Wilson 249
ADC Clock Source
The A/D conversion time per bit is defined as TAD. The A/D conversion
requires 11 TAD per 10-bit conversion.
The source of the A/D conversion clock is software selectable. There are
seven possible options for TAD:
• 2 TOSC
• 4 TOSC
• 8 TOSC
• 16 TOSC
• 32 TOSC
• 64 TOSC
• Internal RC Oscillator Sensors and Embedded Systems – David Wilson 250
Conversion Time
1
𝑇 𝑂𝑆𝐶=
𝐹𝑂𝑆𝐶 Minimum
conversion
time from
datasheet
So divide 1.0µs by 250ns = 4
For TAD to be a minimum time it needs to be at least
4xTOSC
𝐹 𝑂𝑆𝐶 Can only be
∴ 𝐹𝐴𝐷= divide by
4 2,4,8,16,32
Sensors and Embedded Systems – David Wilson 253
How to Calculate Acquisition Time
while (1)
{
__delay_us(7); //Wait the acquisition time ( 6.1us).
[Link] = 1; //start the conversion
while([Link]==1)
{
} //wait for the conversion to end
result = (ADRESH<<8)+ADRESL; //combine the 10 bits
//of the conversion
PORTB=result>>2; // use only the 8 most significant bits
}
This is aliasing
This is aliasing
262
Next Lecture
Using Interrupts .
Interrupt Polling
• An interrupt is like being • The polling method is like
a shopkeeper. If someone a salesperson. The salesperson
needs a service or product, goes from door to door checking
they go to the shopkeeper to see if a product is required.
and state their needs. • Similarly, the controller keeps
• In case of interrupts, when monitoring the flags or signals
the flags are received, they one by one for all devices and
notify the controller that they provides service to whichever
need to be serviced. component that needs its service.
Sensors and Embedded Systems – David Wilson 265
Interrupt Overview.
• Temporary memory
storage space used during Stack
the execution of a
program.
Timers 0, 1, 2, 3
Pins RB0, RB1, RB2 for external hardware interrupts INT0, INT1,
INT2
PORTB-Change interrupt (any one of the upper four Port B pins)
Serial communication’s USART interrupts: receive and transmit,
respectively
ADC (analog-to-digital converter)
CCP (compare capture pulse-width-modulation)
RBPU
Contains the
individual flag bits
for the peripheral
interrupts.
There are two interrupt vectors, high priority and low. All interrupts but
one can be allocated to high or low priority.
The one source that is not prioritised is the external interrupt 0. This is
always high priority and is labeled as such in the diagram.
It is possible to enable or disable the whole prioritisation process. This
is done by IPEN, the Interrupt Priority Enable bit. IPEN is the msb in the
RCON register.
Other
replicated for all other
maskable
maskable interrupts
interrupts Global Interrupt Enable*
Interrupt X Enable*
Interrupt X
S Q Interrupt
inputs to
(reset by CPU R Interrupt CPU
or program) Flag*
Non-maskable
Interrupt
* bits in a Special Function Register
Generalised
Pattern for High Activates
Priority Peripheral High
Interrupt Priority
Vector
Priority
Steering
Logic
Activates
Low Priority
Vector
Generalised
Pattern for Low
Priority Peripheral
Interrupt
The delay between the interrupt occurring, and the CPU response.
Initialisation
Routine task B
Timer-based Interrupt(s)
Routine task C
Timed task X
Timed task Y
Timed task Y
Sensors and Embedded Systems – David Wilson 287
Do I Need Interrupts?
void main(void)
{
TMR0ON = 1; // enable Timer 0
TMR0IE = 1; // enable Timer 0's interrupt
GIE = 1; // enable general interrupts
while (1) { } // main loop
}
Sensors and Embedded Systems – David Wilson 289
Simple XC8 Code with Interrupts (2)