Microcontrollers – Part 7: The I²C Interface, Interrupts, WD & Timers
It’s time to meet again with our little friend: CPU Monkey – the “manager” of all processes into our Virtual machine – The Microcontroller.
Today, the CPU Monkey will communicate using I²C (Inter-Integrated Circuit) interface and will be Interrupted … It will be a Heavy work … , or just a fun !
Our tireless little friend CPU Monkey again will communicate, using all new interface. In front of him are going to be 3 new CPU monkey’s interlocutors. All they are his relatives. To easily recognition them, we will apply indexes as follow: #1 for our manager CPU Monkey and 3 new #2 … #4 , i.e. CPU Monkey #1 and CPU Monkey #2.
Let we present you and new pet invited into our virtual “Zoo” – meet the supervising dog named “Watchdog”.
Have a FUN!
NOW, let’s try to answer the questions below?
What does the I²C Interface mean?
What does the Interrupt mean?
What does the WatchDog mean?
What does the Timer mean?
Is it necessary, and how to use it?
Today’s dialog between CPU Monkey #1: Named Master and its relatives CPU Monkey #2, #3 and #4 is using next useful interface after the previous part explained UART interface.
Theory:
DIAG. 1
In its deep basis and real practice the I²C or Inter-Integrated Circuit, also it ‘s often used the name : Multi-Slave communication interface. It has the main purpose connecting one Master MCU to the variety of targets or slaves peripheral integral circuits or devices. It is also slave’s address-oriented interface.
Let’s remember and clarify, The UART Interface is Point to Point interface – i.e. there is a potential conflict if are used more than two communicating devices.
The I²C interface circumnavigates such problem using the address of Slave, pointed to give the data.
Interrupts: Hierarchy ordered possibility to preferred input to detect an event of change it state, – By The Edge of this change or state of the input.
The Different MCUs are supporting sometimes limited functions of its I/O PORTS. Some MCUs can detect the only Falling or rising edge in the way of changing the Pin state. Other can detect Both states when toggling the pin state.
Watchdog: The MCU Timer based engine, used for preventing the “magically” loop descending of the CPU – based on the programmer’s state machine error. The Programmer need to reset the WD timer counter periodically, i.e. on each second. If the Program is sticking over 1 s. and there isn’t resetting the WD timer Counter register, Watchdog Reset is generated and the User program starts from 0000 h address.
Timers: 8, 16, 32 … bit Counters, adjusted for many program oriented activities, e.g. fast counting subroutine on 1 ms or 1 us, generating an interrupt. Generally, in the body of the timer function has a lot of variables, with the increment , i.e. by one in every entering of this routine (by Pragma Timer Interrupt Vector). This is the way to count the number or how many periods are passed, proportionally the set timer interrupt period.
The Goal of the program is:
CPU Monkey #1 or Master will dry some kind of herbs – it will start the heater, will check the humidity, the temperature, etc. check the values of some I2C Connected Sensors and I2C I/O extensions.
The main CPU Monkey#1 is using a cord to create a unique work atmosphere in its laboratory – The virtual MCU place.
All CPU Monkeys has an individual Working table. All tables are ordered in line – to represent the serial connection, as follow CPU Monkey#1: Table #1, CPU Monkey#2: Table #2…CPU Monkey#4 Table #4,
On each table has a fixed some spools connected with flags – they have the names as follow:
Table #1 CPU M#1 SDA SDA – Serial Data Master I/O, Resistive OR circuit, Using Pull-Up Resistor – In Our virtual design, the Pull-Up role played one spring. It Returns all spools in neutral state – with all flags up – The Idle State
Table #2 CPU M#2 SDA, SDA – Serial Data Slave I/O
Table #3 CPU M#3 SDA, SDA – Serial Data Slave I/O
Table #4 CPU M#4 SDA, SDA – Serial Data Slave I/O
There are also SCL – The Serial Clock Pulses By Master MCU to all interlocutors ( Peripheral Sensing Circuits )
Table #1 CPU M#1 SCL SCL – Serial Clock Master I/O, Although the absolutely logical is this pin to be an output only, there is a mechanism in some slaves to delay SCL pulse ( called Holding the SCL Pulse ) reporting the Master, that there isn’t completed the internal operation wet.
Table #2 CPU M#2 SCL, SCL – Serial Clock Slave I/O
Table #3 CPU M#3 SCL, SCL – Serial Clock Slave I/O
Table #4 CPU M#4 SCL, SCL – Serial Clock Slave I/O
The principle of work :
Between the Tables is routed the cord, with a single twist on every same named spool beginning from Table#1 and ending in Table #4.
One Cord Loop for SDA, another cord loop For SCL. The WatchDog don’t need the loop – it barks if the CPU monkey#1 aren’t throwing regularly a small piece of dry food.
Firstly, The CPU Monkey#1 Prepare the I/O Ports, configuring all necessary adjustments to Data direction and Input pull-ups, etc.
He is throwing a small piece of dry food to WD (resetting the Watchdog Counter ) – saturating the hunger of the Supervising Watchdog.
When the Master CPU Monkey#1 are pulling special sticks – moving the cords, All Spools, and corresponding flags are going down, then restoring up. See the Diag.1
CPU Monkey #2 Measures the Temperature of ambient air , The I²C Address 20h , 21h two bytes represent the Temperature sensor
CPU Monkey #3 Measures The Humidity of Ambient Air – First sensor, and second sensor The Humidity into the herbs 30h, 31h & 35h, 36h
CPU Monkey #4 Start the Blower and Heater. The I2C Address 40h , 41h
Beginning of the infinite main Program loop …
Consequently, CPU Monkey#1 is Sending Commands pulling the cords…
- Start Condition,
- Address 20h (is going to Read High Byte Of Ambient Temperature) Operation Read, Waiting for Acknowledge
- Address 21h (is going to Read Low Byte Of Ambient Temperature) Operation Read, Waiting for Acknowledge
- Address 30h (is going to Read High Byte Of Ambient Humidity) Operation Read, Waiting for Acknowledge
- Address 31h (is going to Read Low Byte Of Ambient Humidity) Operation Read, Waiting for Acknowledge
- Address 35h (is going to Read High Byte Of Herbs Humidity) Operation Read, Waiting for Acknowledge
- Address 36h (is going to Read Low Byte Of Herbs Humidity) Operation Read, Waiting for Acknowledge
If the Herbs Humidity is higher than the set limit, its need to start the heater.
If the AmbientTemperature is higher than the set limit, its need to stop the heater. Generate The interrupt.
If the Herbs Humidity is lower than the set limit, its need to start the Blower. Generate The Interrupt – The Task is completed.
- Address 40h (is going to start the Blower) Operation Write, Waiting for Acknowledge
- Address 41h (is going to start the Heater) Operation Write, Waiting for Acknowledge
- Stop Condition
Don’t forget to throw a small piece of dry food to WatchDog (resetting the Watchdog Counter)
Back to the main Program loop…
Don’t Stop to Reinvent Yourself, Dear Programmers!