Railway Foundation - Sheffield Hallam University

Download Report

Transcript Railway Foundation - Sheffield Hallam University

Railway Foundation
Electronic, Electrical and
Processor Engineering
Microprocessor Systems
• Four main components
– Microprocessor
– Memory
– Inputs
– Outputs
• Memory
– ROM types – program and fixed data
– RAM (Read & Write) – Data variables
1
Microprocessor
• Circuit is driven by a clock signal
• The microprocessor has internal registers.
• The action performed is determined by a
set of binary instructions stored in ROM
• A reset starts the microprocessor at a
predetermined point in the program
(usually location 0)
2
Inputs & Outputs
• Normally Digital I/O ( two levels ‘0’ & ‘1’)
• Normally parallel i.e grouped – 8bit Ports
What about analogue signals?
– Analogue to Digital Converter (ADC)
– Digital to Analogue Converter (DAC)
• Other devices include hardware timers
and counters
• Digital data can also be in a serial format
(e.g. RS232, RS 485 are serial standards)
3
Microcontrollers
• Integration of all required components
onto one chip.
• Many manufacturers – Microchip,
Freescale, Intel, Infineon, Philips, ARM
etc. producing different microprocessors
• Many microcontrollers with same
microprocessor but differ in other
components.
• Used in embedded products.
4
Programming
• Can be done different levels
– Object (machine) binary code
– Assembly language
– High level language ( e.g. ‘C’ language)
• Internal architecture
• Memory Map
• Programmers Model – different for
programming at different levels
5
An example machine instruction
EXAMPLES
Machine code –
0110111100001000
means move the value
from W reg. to file
register 00001000 i.e 8
Assembly –
Count
EQU
8
MOVWF Count
A program called an
assembler converts it to
the binary object code.
6
An Assembler program
Program
Line
Numbers
000000
000000
000000
000002
000004
000004
000006
000008
9693
9681
8681
9681
D7FD
00050
00051
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
;Constants
LED
equ 3
;LED bit 3 on PORTB
;Reset vector
; This code will start executing when a reset occurs.
ORG
0x0000
;Start of main program
Start:
bcf TRISB,LED
bcf PORTB,LED
Loop:
bsf PORTB,LED
bcf PORTB,LED
bra Loop
ROM
location
Object
code in
HEX
format
Labels
Assembly
instructions
;Set PortB bit 3 as an o/p
;set LED off
;while(1)
; turn led on
; turn led off
;endwhile
Comments
Begin with ;
7
C Programming
• Portable
• High level – Abstract
• Standard constructs
– Variables – various data types
– Selection ( if statements)
– Loops (while, for, do)
– Standard operations (+-*/)
– Logical and bit-wise operations (AND OR
XOR etc.)
8
Simple ‘C’ outline
Selection - two types
if (comparison is true )
{
Do this once;
}
if (comparison is true )
{
Do this once;
}
else
{
do this once;
}
Example: if (x == 9)
Defining variables
unsigned char i; //8 bit value
int x;
// 16 bit signed
unsigned int y; // 16 bit value
Assignment(=) and operators(+-*/)
x = 9;
i = 7;
y = x/4 + (5*i); //integer division!
Loops
while (comparison is true )
{
KEEP Doing this;
}
Example: while ( y < 10)
Comparisons:== is equal to
!= is not equal to
> is greater than
< is less than
>= is greater or equal to
<= is less than or equal to
Misc.
// starts a comment
i++;
// increment by one
i--;
// decrement by one
&&
// logical AND
||
// logical OR
9
Practical approach
• Treat as a programmable digital device
• Choose device based on number and
types of input and outputs
• Write program:– Define inputs and outputs
– Read input data, process data and generate
outputs
• Requires ‘C’ language and knowledge of
microcontroller specific features.
10
Microchip PIC 18F452 & C18 ‘C’
Compiler
• Access to Special Function Registers by
#include <p18f452.h>
• Defines names for all component parts.
• Example : PORTB an 8 bit digital I/O Port
 TRISB - Port B direction register (0 = output, 1
= input)
 PORTB - all 8 bits
 PORTBbits.RBx - individual bits where x is bit
number (numbering starts at 0)
11
What does this do?
//PortA is 7 bits
TRISA = 15;
TRISB = 0;
PORTBbits.RB0 = 0;
while(1)
{
while( PORTAbits.RA4
{
}
while( PORTAbits.RA4
{
}
PORTBbits.RB0 = 1;
while( PORTAbits.RA4
{
}
while( PORTAbits.RA4
{
}
PORTBbits.RB0 = 0;
}
== 0)
== 1)
== 0)
== 1)
12
Analogue to Digital Converter(ADC)
• n bits – determines resolution
• Reference voltage sets the input range
• Often have an analogue multiplexer to
allow several input channels to a single
ADC
n bits
Analogue
voltage
ADC
Reference voltages (one is
usually analogue ground)
13
Example: 8 bit ADC
•
•
•
•
•
Reference voltages of 0v and +5v
Input voltage range = 5v – 0v = 5v
Number of digital values (steps) = 2n = 28 = 256
NOTE: Values range from 0 to 255
Resolution = Reference voltage range
Number of digital values
= 5 / 256
= 0.01953125 volts = 19.53125mV
14
Remember max. digital value = 255.
Digital value
Max. convertible input = 255 x resolution in volts
= 255 x 0.01953125
0.01953125 = 4.98046875 V
255
Input
voltage
0
0
4.98046875
Volts
15
Digital Conversion
Digital value
3
2
0.05859375
1
0.0390625
0
Analogue
Input voltage
0.01953125
16