lecture 4 i2 c bus & interrupts
DESCRIPTION
I²C bus & interrupts, Using the telemeter by polling, Interrupts,Using the telemeter with interrupts, The ultrasonic telemeter SRF08TRANSCRIPT
Workshop
Project Pedagogy approach ofMicrocontroller
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 1
Microcontroller
PTC Khadoury IUT Cachan
Palestinian Robotic Cup
4. I²C bus & interrupts
connecting and using an ultrasonic i²c sensor
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 2
PTC Khadoury IUT Cachan
Why do you need ultrasonic telemeter ?
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 3
Blanche neige accident.MOV
without telemeter with telemeter
valetpique.mov
jack &
Commands
Energy
I2C
PWMOutputs
Digital
communicationperipherals Chopper
+ Motors
ultrasonic
telemeter
UART
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 4
LCD
Display
jack &
buttons
line info
Micro-
controller
DigitalInput
Analog to digital
converter
Digitaloutputs
Fast counting
Inputs
Analog to digital
converter
Infrared
sensors
Motors
encoders
positioninfo
Potentio-
meter
jack &
Commands
Energy
I2C
PWMOutputs
Digital
communicationperipherals Chopper
+ Motors
ultrasonic
telemeter
UART
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 5
LCD
Display
jack &
buttons
line info
Micro-
controller
DigitalInput
Analog to digital
converter
Digitaloutputs
Fast counting
Inputs
Analog to digital
converter
Infrared
sensors
Motors
encoders
positioninfo
Potentio-
meter
4.1 The ultrasonic telemeter SRF08
4.2 I²C physical & protocol layers
4 I²C bus & interrupts
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 6
4.3 Using the telemeter by pooling
4.4 interrupts
4.5 Using the telemeter with interrupts
4.1 The ultrasonic telemeter SRF08
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 7
4.1 The ultrasonic telemeter SRF08
piezoelectricemitter
physics principle
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 8
piezoelectricreceiver
d = c.t
distance (m)
sound speedthrough the air (~340 m.s-1)
flight time (s)
4.1 The ultrasonic telemeter SRF08internals registers
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 9
initialization : 1 meter and result in centimeter
Measurement request
Waiting for 10 ms
Read result (between 0 and 100)
4.1 The ultrasonic telemeter SRF08
1234
I2C
Header 4
SCLSDAVDD
connections
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 10
Header 4
SRF08 sensor pins
Microcontroller boardI²C connector
for more details, see www.robot-electronics.co.uk
4.2 I²C physical & protocol layers
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 11
4.2 I²C physical and protocol layers
I²C overview
Inter Integrated Circuits bus
2-wires synchronous bus
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 12
Up to 15 Mbits/s
Multi-master mode is possible
Up to 127 devices on the bus
4.2 I²C physical and protocol layers
physical layer : 2-wires synchronous bus
Sensor(slave mode)
Memory(slave mode)
Microcontroller(Master Mode)
Real TimeClock
(slave mode)addr = 0xE0addr = 0xAC addr = 0xB4
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 13
GND
VCC = 5Vpull-up resistors
SCL (clock wire)
SDA (data wire)
4.2 I²C physical and protocol layers
frames
Transmit addressof Slave (W) Ack
Transmit registernumber to write
Transmit data StopStart
frame to write
Sensor(slave mode)address 0xE0
Microcontroller(Master Mode)
Ack Ack
Write address 0xE0Read address 0xE1
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 14
GND
pull-up resistors
SCL (clock wire)
SDA (data wire)
address 0xE0
4.2 I²C physical and protocol layers
framesTransmit address
of Slave (W)
Transmit registernumber to read
Receive registerdata from Slave
Start
frame to read
Transmit addressof Slave (R)
ReStart Stop
Sensor(slave mode)address 0xE0
Microcontroller(Master Mode)
Ack Ack
Ack NAck
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 15GND
pull-up resistors
SCL (clock wire)
SDA (data wire)
address 0xE0(Master Mode)
4.2 I²C physical and protocol layers
For more details,see I2C_BUS_SPECIFICATION_3.pdf
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 16
see I2C_BUS_SPECIFICATION_3.pdf
4.3 Using the telemeter by polling
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 17
4.3 Using the telemeter by polling
I²C configuration
See - Microchip PIC18F4550 datasheet (ch. 19)- Microchip C18 library
SSPADD = 118;OpenI2C(MASTER,SLEW_ON);
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 18
configure I²C peripheral(Master and 400 kHz mode)
initialization : 1 meter and result in centimeter
Measurement request
Waiting for 10 ms
Read result
4.3 Using telemeter by polling
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 19
Read result
void SRF_initialisation(char address);unsigned char SRF_read_byte( unsigned char address, unsigned char reg );unsigned char SRF_write_byte( unsigned char address, unsigned char reg,
unsigned char data );
library SRF08
Timer3
4.4 Interrupts
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 20
4.4 Interrupts
4.5 Interrupts
What is an interrupt ?
Measurement request
Waiting for 10 ms
Read result
initialization
main program
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 21
measurement
request
CP
U
occupation
CPU is reading
the value of the timer3
read
result
CPU is reading
the value of the timer3
measurement
request
read
result
…
4.5 Interrupts
What is an interrupt ?
initialization
Measurement request
Read result
main program interrupt subroutine
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 22
measurement
request
CP
U
occupation
read
result
measurement
request
read
result
…the CPU is free the CPU is free
4.5 Interrupts
interrupt in Microchip PIC18Fxxxx
different interrupt sources :- Timers- External pins- USART / USB / I2C- …
for each interrupt source, 2 bits :- One Flag bit
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 23
- One Flag bit- One Enable bit
and sometimes- One Priority bit
and, for all interrupts :Two bits : - GIE (Global interrupt enable)- PEIE (Peripherals Interrupt enable)
Peripheral Interrupt Enable registers (PIE1 and PIE2).
PIE1 = 0b00000000;
SPPIE ADIE TXIE TMR2IE TMR1IESSPIERCIE CCP1IE
4.5 Interrupts
interrupt in Microchip PIC18Fxxxx
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 24
PIE2 = 0b00000010;
OSCFIE CMIE EEIE TMR3IE CCP2IEBCLIE
SPPIE ADIE TXIE TMR2IE TMR1IESSPIERCIE CCP1IE
USBIE HLVDIE
Peripheral Interrupt Request (Flag) registers (PIR1 and PIR2).
PIR1 = 0b00000000;
SPPIF ADIF TXIF TMR2IF TMR1IFSSPIFRCIF CCP1IF
interrupt in Microchip PIC18Fxxxx
4.5 Interrupts
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 25
PIR2 = 0b00000000;
OSCFIF CMIF EEIF TMR3IF CCP2IFBCLIF
SPPIF ADIF TXIF TMR2IF TMR1IFSSPIFRCIF CCP1IF
USBIF HLVDIF
What happens when an interrupt occurs ?
interrupt in Microchip PIC18Fxxxx
4.5 Interrupts
The program executes the instruction which is at the address 0x08 in the program memory
Programcounter goto Interrupt funct
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 26
main program
Interrupt function
ProgramcounterProgramcounter
ProgramcounterProgramcounter
Programcounter
4.5 Using the telemeter with interrupts
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 27
Flow chart
initialization
Measurement request
Read result
main program interrupt subroutine
4.5 Using the telemeter with interrupts
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 28
measurement
request
CP
U
occupation
read
result
measurement
request
read
result
…the CPU is free the CPU is free
4.5 Using the telemeter with interrupts
1st : initialize the I²C bus and the sensor
3rd : unmask the interrupts you need (TMR3IE)
2nd : configure Timer3 (OpenTimer3(…))
4th : unmask interrupts (GIE and PEIE)
5th : write the infinite loop
main program
Interrupt subroutine
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 29
6th : write the interruption subroutine (Reading value & measurement request)
Interrupt subroutine
7th : write the declaration of this ISR on top of main file
8th : write the branch to go to this ISR
4.5 Using the telemeter with interrupts
Interrupt subroutine6th : write the interruption subroutine (Reading value & measurement request)
#pragma interrupt TMR3_Interrupt_function
void TMR3_Interrupt_function(void)
{
if (PIR2bits.TMR3IF == 1)
{... }//end if
)// end TMR3_Interrupt_function
it indicates that the following function is an ISR
it verifies if this is the timer3 which set on the interrupt
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 30
)// end TMR3_Interrupt_function
8th : write the branch to go to this ISR
#pragma code high_vector=0x08
void high_vector (void)
{ _asm goto TMR3_Interrupt_function _endasm }
#pragma code
it indicates that the nextcode is at the adress 0x08
it indicates that the nextcode is in the program memory
_asm and _endasm frame an asm instruction
on the interrupt