Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Lecture 4 i2 c bus & interrupts
1. Workshop
Project Pedagogy approach of
Microcontroller
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 1
Microcontroller
PTC Khadoury IUT Cachan
Palestinian Robotic Cup
2. 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
3. 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
4. jack &
Commands
Energy
I2C
PWM
Outputs
Digital
communication
peripherals Chopper
+ Motors
ultrasonic
telemeter
UART
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 4
LCD
Display
jack &
buttons
line info
Micro-
controller
Digital
Input
Analog to
digital
converter
Digital
outputs
Fast
counting
Inputs
Analog to
digital
converter
Infrared
sensors
Motors
encoders
position
info
Potentio-
meter
5. jack &
Commands
Energy
I2C
PWM
Outputs
Digital
communication
peripherals Chopper
+ Motors
ultrasonic
telemeter
UART
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 5
LCD
Display
jack &
buttons
line info
Micro-
controller
Digital
Input
Analog to
digital
converter
Digital
outputs
Fast
counting
Inputs
Analog to
digital
converter
Infrared
sensors
Motors
encoders
position
info
Potentio-
meter
6. 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
7. 4.1 The ultrasonic telemeter SRF08
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 7
8. 4.1 The ultrasonic telemeter SRF08
piezoelectric
emitter
physics principle
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 8
piezoelectric
receiver
d = c.t
distance (m)
sound speed
through the air (~340 m.s-1)
flight time (s)
9. 4.1 The ultrasonic telemeter SRF08
internals 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)
10. 4.1 The ultrasonic telemeter SRF08
1
2
3
4
I2C
Header 4
SCL
SDA
VDD
connections
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 10
Header 4
SRF08 sensor pins
Microcontroller board
I²C connector
for more details, see www.robot-electronics.co.uk
11. 4.2 I²C physical & protocol layers
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 11
12. 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
13. 4.2 I²C physical and protocol layers
physical layer : 2-wires synchronous bus
Sensor
(slave mode)
Memory
(slave mode)
Microcontroller
(Master Mode)
Real Time
Clock
(slave mode)
addr = 0xE0addr = 0xAC addr = 0xB4
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 13
GND
VCC = 5V
pull-up resistors
SCL (clock wire)
SDA (data wire)
14. 4.2 I²C physical and protocol layers
frames
Transmit address
of Slave (W) Ack
Transmit register
number to write
Transmit data StopStart
frame to write
Sensor
(slave mode)
address 0xE0
Microcontroller
(Master Mode)
Ack Ack
Write address 0xE0
Read address 0xE1
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 14
GND
pull-up resistors
SCL (clock wire)
SDA (data wire)
address 0xE0
15. 4.2 I²C physical and protocol layers
frames
Transmit address
of Slave (W)
Transmit register
number to read
Receive register
data from Slave
Start
frame to read
Transmit address
of Slave (R)
ReStart Stop
Sensor
(slave mode)
address 0xE0
Microcontroller
(Master Mode)
Ack Ack
Ack NAck
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 15
GND
pull-up resistors
SCL (clock wire)
SDA (data wire)
address 0xE0
(Master Mode)
16. 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
17. 4.3 Using the telemeter by polling
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 17
18. 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)
19. 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
21. 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
CPU
occupation
CPU is reading
the value of the timer3
read
result
CPU is reading
the value of the timer3
measurement
request
read
result
…
22. 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
CPU
occupation
read
result
measurement
request
read
result
…the CPU is free the CPU is free
23. 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)
26. 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
Program
counter goto Interrupt funct
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 26
main program
Interrupt function
Program
counterProgram
counter
Program
counter
Program
counter
Program
counter
27. 4.5 Using the telemeter with interrupts
Project Pedagogy approach of Microcontroller – Palestinian Robotic Cup 27
28. 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
CPU
occupation
read
result
measurement
request
read
result
…the CPU is free the CPU is free
29. 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
30. 4.5 Using the telemeter with interrupts
Interrupt subroutine
6th : 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 next
code is at the adress 0x08
it indicates that the next
code is in the program memory
_asm and _endasm frame
an asm instruction
on the interrupt