Go Down

Topic: Ch-11 Peripheral Controllers and Devices (Read 2718 times) previous topic - next topic

GolamMostafa

Jul 09, 2020, 06:33 pm Last Edit: Jul 13, 2020, 05:55 pm by GolamMostafa
11.1  Introduction
In microcontroller applications, the MCU drives peripheral devices (the IO devices) like 7-segment display unit, LCD, Servo Motor, LM35 Temp Sensor, Water Pump, Fluid Injection System in the hospital, and the like for various purposes. These IO devices are always connected with the MCU through some kind of "interface controllers/peripheral controllers/programmable peripheral interface controllers (PPI)". These interface controllers could be embedded within the MCU or be lying outside the MCU. A peripheral controller solves the incompatibility problem (between MCU and IO device) which exists due to mismatch in logic level, protocol level, and power ratings (voltage/current).

MCU like ATmega328P of the Arduino UNO Board is a low voltage (5v) and low power (an IO pin can deliver only 20 mA current) device; but in the industrial field, it might drive/control high power device like a 3-phase 3 kW induction motor (415 V/ line-to-line voltage and about 9 A line current at 0.8 pf). This example tells us how important it is (particularly for the EE Engineering students) to study the interfacing issues.  

1.  Peripheral Device (CC7SD Display Unit)  and Embedded Peripheral Controller

Figure-11.1: Interfacing of multiplexed type "CC7SD display  unit"  

(1)  In the diagram of Fig-11.1, the "LM35 Temp Sensor" is an "input device/peripheral device" and the 4-digit "cc-type multiplexed 7-segment display unit (CC7SD)" is an "output device/peripheral device".

(2)  In Fig-11.1, the peripheral controllers are embedded within the MCU and these are:
(a)  PORTB, PORTC, and PORTD.
The CC7SD unit is connected with MCU via these PPIs. The IO lines of these PPIs drive/control the ON/OFF conditions of the segments of the display unit.

(b)  ADC.
The LM35 Temp Sensor is connected with MCU with the help of this PPI. This PPI helps the MCU to acquire the analog value of the temperature sensor and then convert it into digital form.    

(3)  Limitations of the CC7SD Unit of Fig-11.1.
(a)  Because it is a multiplexed type display unit, the MCU must refresh it frequently; otherwise, the display will freeze to a single-digit. Here, the MCU is spending huge amount time which could be used in some other useful tasks.  

(b)  because the port pins have limited current source/sink capability, the brightness of the display unit is usually below standard.

(c)  Because the display unit is being driven directly by the IO lines of the internal peripheral controllers, there are about 12 IO lines engaged with the display unit which is not affordable in the practical case as the MCU has to handle other peripheral devices using IO lines.

2.  Peripheral Device (CC7SD Display Unit)  and External Peripheral Controller

Figure-11.2:  External peripheral controller MAX7219 based multiplexed display unit

(1)  It is possible to overcome the limitations of the display system of Fig-11.1 with the help of the display system of Fig-11.2; where, an external peripheral controller (M2 : MAX7219) drives the "4-digit multiplexed CC7SD Unit".

(2)  Now, the MAX7219 chip (U2 in Fig-11.2) has taken over the "display refreshing" job from the MCU. The MCU is free and it can do some other useful tasks. Whenever needed, the MCU just writes new data into the registers of the MAX7219 chip for the digits of the display unit. The MAX7219 has its own scanning circuit by virtue of which it refreshes the multiplexed display unit.  

(3)  In Fig-11.1, the MCU needed 12 IO lines to drive the display unit; whereas, in Fig-11.2, the MCU needs only 3 IO lines to drive the same display unit. There is a saving of 9 IO lines which could be used to drive some other IO devices.

(4)  An IO line of MAX7219 chip has 100 mA regulate-able current driving capability than the IO line of the MCU which is only 20 mA. Thus, the brightness of the display unit could be adjusted at any desired level.

(5)  The MCU uses standard SPI protocol to transfer data to MAX7219 chip in Fig-11.2; whereas, the MCU uses "bit-bang" tricks to send data to the display unit via the internal Ports/PPIs.

3.  Peripheral Device (Parallel IO driven LCD)  and Embedded Peripheral Controller

Figure-11.3:  Embedded (internal) parallel IO (peripheral controller) driven LCD (PIOLCD)

4.  Peripheral Device (I2C Bus driven LCD)  and Embedded Peripheral Controller

Figure-11.4:  External I2C based  peripheral controller driven LCD (I2CLCD)

5.  Peripheral Device (Bluetooth/Mobile Phone) and Embedded Peripheral Controller

Figure-11.5: Interfacing Mobile phone with UNO using "HC-05 Bluetooth" external controller

... to be continued

GolamMostafa

#1
Jul 09, 2020, 06:34 pm Last Edit: Jul 16, 2020, 11:04 am by GolamMostafa
11.2  Architecture and Programming of MAX7219 Controller
1.  (1)  Internal Architecture refers to the organization of various electronics modules that are present within MAX7219 chip, their interconnections and functions. Detailed description could be found in Section-11.4/Fig-11.8.

(2)  External Architecture refers to the organization of the physical pins, signals attached with the pins, and the functions of the pins/signals.  Detailed picture of the pin signals could be found in Fig-11.2.

(3)  Programming refers to the "art and science" of writing codes to transfer data from MCU to MAX7219 chip. Why is it an art -- because you have to practice the programming task again and again until it comes to the closeness of the desired working shape? Why is it a science -- because you have to learn/accept few standard rules to do programming?

2.  Procedures to show 2 on DP0 position of the display unit of Fig-11.2.
(1)  DP0 is a cc-type display device. To see 2 on this device, we need to send the following bits into the segments (a to p) of the display device.
p   g   f   e   d   c   b  a        Hex Format
0   1   0  1   1   0   1  1   =  0x5B   //later on, 0x5B will be changed to 0x6D to comply with Fig-11.6.

(2)  But for MAX7219, the cc-codes for the digits are computed as per diagram of Fig-11.6.

Figure-11.6: Template of MAX7219 to compute cc-code for digits

(3)  According to Fig-11.6, the cc-code of 2 appears as (this is known as no-decode cc-code):
D7    D6    D5    D4    D3    D2    D1    D0      Hex Format
dp     A      B     C      D      E       F      G        
0       1      1     0      1      1       0       1     = 0x6D

(4)   Following Step-3, we may calculate the cc-codes for the digits 0 - F and declare it as an array like this:
Code: [Select]
byte lupTable[] = {0x7E, 0x30, 0x6D, 0x79, 0x33, 0x5B, 0x5F, 0x70, 0x7F,
0x7B, 0x77, 0x1F, 0x4E, 0x3D, 0x4F, 0x47}; //0, 1, ..., E, F ; No-decode cc-codes

(5)  In Fig-11.2, we observe that the MAX7219 chip can handle eight (DP0 - DP7) cc-type 7-segment display devices though only four (DP0 - DP3) are shown in the diagram.

(6)  In practical situation, each display device of the display unit of Fig-11.2 will show different digits/values. Therefore, we need eight different registers/storage locations within the MAX7219 to hold data for these display devices. These registers are called "Digit Registers" and are named as "Digit-0 Register", Digit-1 Register", and so on. Digit-0 Register will hold the "8-bit cc-code" for the digit to be shown at DP0 position and so on.

(7)  Each "Digit Register" has its own 8-bit address. According to Fig-11.7, the address for "Digit-0 Register" is 0x01 ( comes from 0xX1; where X is don't care and is taken as 0), address of "Digit-1 Register" is 0x02, and so on.

Figure-11.7: Register address of MAX7219  

(8)  Now, we have the following two 8-bit data items that are to be sent to the MAX7219 chip. These are:
(a)  8-bit address (0x01) of Digit-0 Register.
(b)  8-bit cc-code (0x6D) for the digit 2.

(9)  In Fig-11.2, it is observed that the MCU will use SPI Port to send the above two bytes to MAX7219 chip. Because SPI is a byte-oriented protocol (SPI Port sends/receives 1-byte data at a time), the MCU will require the following two bus transactions to transfer the said two bytes data:
Code: [Select]
SPI.transfer(0x01);  //address of Digit-0 Register is sent
SPI.transfer(0x6D); //cc-code of 2 is sent;

(10)  When 0x01 and 0x6D of Step-9 are palced side-by-side, we get this 16-bit data: 0x016D. The said 16-bit data item has gone into the MAX7219 chip, and now it is lying in the "Temporary Data Buffer" (Fig-11.8).


Figure-11.8: Internal temporary data buffer

(11)   To move and latch the 16-bit data of Step-(10) into active/destination register (the Digit-0 Register), the MCU issues LOH-HIGH-LOW signal on the "LOAD" pin of MAX7219 (Fig-11.2). To create this L-H-L pulse at the LOAD-pin, the following codes could be executed:
Code: [Select]
pinMode(10, OUTPUT);    //DPin-10 will work as output
digitalWrite(10, LOW);              //LOW
digitalWrite(10, HIGH);             //HIGH
digitalWrite(10, LOW);              //LOW

  
(12) Now, the "scanning circuit" of MAX7219 will transfer 0x6D (cc-code of 2) onto DP0 position; as a result, 2 will appear on DP0 position.

3. Complete sketch:
(1)  Set "No-decode cc-code Mode (cc-codes are made as per Fig-11.6)" by putting 0x00 into "Decode Mode Register (address: 0x09)".
Code: [Select]
SPI.transfer(0x09);   //address of Decode Mode RegisterRegister
SPI.transfer(0x00);  //data to be placed into the above register
//-------------------------
digitalWrite(LOAD, LOW);    //assert L-H-L signal at LOAD pin of MAX7219 chip
digitalWrite(LOAD, HIGH);
digitalWrite(LOAD, LOW);    //0x00 has entered into the target register

(2)  Set "Brightness level of display devices" at normal level by putting 0x01 into "Intensity Register (address: 0x0A)".
Code: [Select]
SPI.transfer(0x0A);   //address of Intensity Register
SPI.transfer(0x01);  //data to be placed into the above register
//-------------------------
digitalWrite(LOAD, LOW);
digitalWrite(LOAD, HIGH);
digitalWrite(LOAD, LOW);    //0x00 has entered into the target register

(3)  Set "Number pf digits to be scanned" by putting 0x00 (for 1-digit), 0x01 (for 2-digit), and son into "Scan Limit Register (address: 0x0B)".
Code: [Select]
SPI.transfer(0x0B);   //address of Scan Limit Register
SPI.transfer(0x00);  //data to be placed into the above register for 1-digit
//-------------------------
digitalWrite(LOAD, LOW);
digitalWrite(LOAD, HIGH);
digitalWrite(LOAD, LOW);    //0x00 has entered into the target register

(4)  Set "Normal Mode Operation" of MAX7219 by putting 0x01 into "Shutdown Register (address: 0x0C)". The MAX7219 chip can also be put into sleep mode (also called shutdown mode).
Code: [Select]
SPI.transfer(0x0C);   //address of Register
SPI.transfer(0x01);  //data to be placed into the above register
//-------------------------
digitalWrite(LOAD, LOW);
digitalWrite(LOAD, HIGH);
digitalWrite(LOAD, LOW);    //0x00 has entered into the target register

(5)  Send cc-code of 2  (0x6D made as per Fig-11.6) into "Digit-0 Register (address: 0x01)".
Code: [Select]
SPI.transfer(0x01);   //address of Register
SPI.transfer(0x6D);  //data to be placed into the above register
//-------------------------
digitalWrite(LOAD, LOW);
digitalWrite(LOAD, HIGH);
digitalWrite(LOAD, LOW);    //0x00 has entered into the target register

(6)  All the code lines of the above Step-1 to 4 could be reduced to the following block of codes using for() loop:
Code: [Select]
byte regAddress = 0x09;
byte regData[] = {0x00, 0x01, 0x00, 0x01};  //4 data bytes for 4 registers
for(int i = regAddress, i=0; i<0x0D; regAddress++, i++)
{
    SPI.transfer(regAddress);
    delayMicroseconds(100);     //byte transfer delay = 64 + 36 (margin) = 100 us at speed: 125 kbits/s
    SPI.transfer(regData[i]);
    delayMicroseconds(100);
    dataLoad();
}

void dataLoad()    ///to issus LOW-HIGH-LOW signal on the LOAD pin of MAX7219
{
    digitalWrite(LOAD, LOW);
    digitalWrite(LOAD, HIGH);
    digitalWrite(LOAD, LOW);    //0x00 has entered into the target register
}

GolamMostafa

#2
Jul 09, 2020, 06:34 pm Last Edit: Jul 14, 2020, 06:41 pm by GolamMostafa
11.3  Architecture and Programming of PCF8574A Controller

...to be continued.




GolamMostafa

#3
Jul 09, 2020, 06:35 pm Last Edit: Jul 17, 2020, 05:32 pm by GolamMostafa
11.4  Questions and Answers

Figure-11.8: Internal circuit modules of MAX7219




1.  MAX7219 (Fig-11.2) has SPI communication port by which it receives new data from MCU for the and then they are correctly routed/distributed among the digits of the multiplexed display unit.

2.  The "display refreshing" happens automatically without any intervention from the MCU side with the help of its own "scanning circuit".

3.   One MAX7219 chip can handle 1 to 8 CC-type display devices. the chip can be cascaded with many more MAX7219 chips with the help of the DOUT-pin to expand the number of display digits in the display unit.

4.  The MAX7219 chip contains registers, storage space, scanning circuit, and power driver for the management of the multiplexed display unit.

5.  To ensure uniform current flow through each segment of each digit of the display unit, an external resistor (Rex1, Fig-11.2) of about 9.9k is connected across Pins-18 and 19.

6.  (1)  The host MCU/UNO delivers data to the MAX7219 chip using SPI Port; where, the "unit of data item" (data size) is "16-bit" (Fig-11.6). However, data from MCU to MAX7219 is transferred 1-byte at a time. So, thee is a need of "two transactions" to transfer 16-bit data from MCU to MAX7219.

(2)  The data item is composed of "8-bit register address" and "8-bit register data".
(3)  The MSBit is transmitted first (Fig-11.6) and shifted-in into the buffer register at the rising edge of SCK signal.

(4)  When shifting of all 16 bits is done, the bits are latched into their respective register at the rising edge of LAOD signal.

(5) Example: To set operating mode of MAX7219 into "Normal Mode'" by storing 0x01 into Shutdown Register (address 0x0C), the following steps are carried out:
(a)  Transfer Register Address (0x0C = 00001100) by executing SPI.transfer(0x0C);. As a result, 0x0C occupies D7-D0 positions of shift register of Fig-11.6.  

(b)  Transfer Register Data (0x01 = 0000 0001) by executing SPI.transfer(0x01);. As a result, data bits of Step-(a) goes to D15-D8 positions and 0x01 occupies D7-D0 positions of Fig-11.6.

(c)  Activate LOAD Signal by executing LOW-HIGH-LOW on PB2-bit of Fig-11.2. As a result, Shutdown Register is selected and the data byte 0x01 enters into it; the operating mode of MAX7219 is set to "Normal Mode".

(d)  Codes for the Step-a,b, c:
Code: [Select]
SPI.transfer(0x0C); //address of Shutdown Register; 8 SCK pulses are generated
SPI.transfer(0x01); //data for Shutdown Register; 8 SCK pulses are generated
Activate L-H-L on LOAD line via DPin-10 //data is latched into Shutdown Register of 1st chip.

7.  Register Map of MAX7219


(1)  Digit 0/Digit 7 Register:
(a)  When 0x00 is written into "Decode Mode Register", then "8-bit data of Digit 0 Register" (to be written by user) control the ON/OFF conditions of the 8 segments of the cc-type display device (DP0 of Fig-11.2) as per Fig-11.8.

(b) Example: To show 2 at DP0 position of Fig-11.2, what value should we put into Digit 0 Register?
Ans: The cc-code of 2 is: (p g f e d c b a = 0 1 0 1 1 0 1 1 = 0x5B). But, to comply with Fig-11.8, the cc-code of 2 (p g f e d c b a = 0 1 0 1 1 0 1 1) has to be re-arranged this way: p a b c d e f g = 0 1 1 0 1 1 0 1  = 0x6D. Now, we have to send 0x6D into Digit 0 Register. This way, we can manually make all the "No-decode CC-codes" for the digits 0 - F and declare them by the following Lookup Table (LUT). To bring the decimal point into ON state, we may just put HIGH at DP position of Fig-11.8 (or of the re-arranged cc-code).
Code: [Select]
byte lupTable[] = {0x7E, 0x30, 0x6D, 0x79, 0x33, 0x5B, 0x5F, 0x70, 0x7F,
0x7B, 0x77, 0x1F, 0x4E, 0x3D, 0x4F, 0x47}; //0, 1, ..., E, F ; No-decode cc-codes

(2)  Decode Mode Register: When 0x00 (according to data sheets) is written into "Decode Mode Register", then the role of this register is as explained in Step-a.

(3)  Intensity Register: It controls the intensity of the brightness of the display digits. A value of 0x01 is good enough.

(4)  Scan Limit Register:  This register decides the number of display digits to be scanned (0 for 1-digit, DP0; 1 for 2-digit, DP0-DP1; …, 7 for 8-digit DP0-DP7).

(5)  Shutdown Register:  A value of 0x01 configures the "Normal Mode" operation of the MAX7219 chip.

(6) No-op (No Operation) Register: This register plays its role when 2 or more MAX7219 chips are operated in cascade.  Say, we have 2 chips in cascade and we want to store 0x01 into their respective Shutdown Registers. The procedures/codes to initialize Shutdown Register of 1st MAX7219 chip are:
Code: [Select]
SPI.transfer(0x0C); //address of Shutdown Register; 8 SCK pulses are generated
SPI.transfer(0x01); //data for Shutdown Register; 8 SCK pulses are generated
Activate L-H-L on LOAD //data is latched into Shutdown Register of 1st chip.

//------------------------------------------------------------------------------------------------------
To initialize the Shutdown Register of the 2nd MAX7219 chip, we need (8+8) + (8+8) SCK pulses of which 1st (8+8) SCK pulses are generated using SPI.transfer(0x0C) and SPI.transfer(0x01) instructions and the next (8+8) SCK pulses are generated using SPI.transfer(0x00 = address of No-op Register) and SPI.transfer(0xFF=any data for No-op Register) instructions. As a result, the 16-bit value 0C01 are shifted out via DOUT-pin into the shift register of the 2nd MAX7219 chip. After that LOAD pulse is activated; 0xFF enters into No-op Register of 1st chip (no harm) and 0x01 enters into Shutdown Register of 2nd chip.



8.  Example Sketch: (to show 27.65 on Display Unit of Fig-11.2)
Code: [Select]
#include<SPI.h>
byte registerAddress[] = {0x09, 0x0A, 0x0B, 0x0C};//Registers of MAX7219 to be initialized
byte registerData[] = {0x00, 0x01, 0x03, 0x01};    //data for registers of AMX7219
byte dataArray[4] = {0x6D, 0x70, 0x5F, 0x5B}; //cc-code for 2 7. 65 (no-decode format)
byte digitAddress[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};//digit address

void setup()
{
    pinMode(10, OUTPUT);  //LOAD Pin of MAX7219
    //-------------------
    SPI.begin();
    bitSet(SPCR, 4);              //UNO is Master SPI
    SPI.setBitOrder(MSBFIRST);    //MSB_bit will be transferred first
    SPI.setClockDivider(SPI_CLOCK_DIV128); //TX rate = 16MHz/128 = 125 kbit
    SPI.setDataMode(SPI_MODE1);//MOSI is sampled at the rising edge of CLK
    //------------------------------------------------
    digitalWrite(10, LOW);      //Low at LOAD pin
    //---- computation----------------------------------
    byte z = 0x12 + 0x13;   //z = 3C
    dataArray[0] = lupTable[z>>4];
    dataArray[1] = lupTable[z&0x0F];
    //-------------------------------------------------
    
    //---keep intializing the Mode of Operation------------
    for(int i=0; i<4; i++)
    {
      SPI.transfer(registerAddress[i]);
      SPI.transfer(registerData[i]);
      digitalWrite(10, LOW);
      digitalWrite(10, HIGH); //assert LH/LL on LOAD pin
      digitalWrite(10, LOW);
    }
  
    //--keep transferring the result/data----------------------
    for(int i=0; i<4; i++)
    {
      SPI.transfer(digitAddress[i]); //DPX position
      SPI.transfer(dataArray[i]);   //shows 2 on DP0-position
      digitalWrite(10, LOW);
      digitalWrite(10, HIGH);       //assert LH/LL on LOAD pin
      digitalWrite(10, LOW);
    }
 }

 void loop()
 {
  
 }




...to be continued.

GolamMostafa


(deleted)

#5
Jul 15, 2020, 06:55 am Last Edit: Jul 15, 2020, 06:58 am by smarts-jb
(deleted)

(deleted)

#6
Jul 15, 2020, 07:19 am Last Edit: Jul 15, 2020, 08:13 am by smarts-jb
(deleted)

GolamMostafa

#7
Jul 15, 2020, 09:29 am Last Edit: Jul 16, 2020, 06:42 am by GolamMostafa
@smarts-jb

It is my humble apology to you and others for creating un-intentional confusions having given the same Chapter Nos to different topics I have posted in Education and Teaching Section. I have removed the chapter serials from the titles of my all posts beyond June/2020.

There are no specific target groups for the materials that I have so far posted as Ch-5, 6, 7, 8, 9, 10, 11 in this Section; but, these materials could be used by any UNO users. However, I use these materials as online "Lecture Notes" for my hundreds of pandemic affected varsity pupils who have taken this subject: "Microcontroller Interfacing and System Design" using "Arduino UNO" as course work, but they can not physically attend the on-site classes which are officially postponed due to COVID-19  outbreak.

Ch-1 to 4 (Fundamentals, Architecture, DIO Controller, ADC Module) were covered in on-site classes before the pandemic. These materials might have postings (in future) in this Section should requests come from the pupils as part of review.

Ch-12 to 15 (Sensors, System Design, C++ Arduino and Library Functions, Arduino UNO Learning Kit) are parts of the course. The materials of these Chapters might appear in this Section should luck favors that "no objection" goes to the Moderator to shut-off my activities.

Thanks for your concern.  


GolamMostafa

If I were you I'd create an index in a new post and ask the mods to sticky it for you
Actually, I have not yet fully understood the "Management System of the Arduino Forum" though I am here since 2017.


GolamMostafa

For Five reasons, I have not posted these materials at my varsity site:

1.  As the students are conducting this course using UNO, they should consult the Arduino Forum for the answers of their various hardware/software queries/questions. If the materials are posted here, they have to access the Forum to get them and thus they might be (probably) interested to get involved with the Arduino Forum activities.

2.  The materials could be useful for some other UNO users.

3.  My varsity internet suffers from erratic disruption due to low speed.

4.  The Forum page and its online contents are eye-cooling when compared with pdf file.

5.  This is the "Education and Teaching" Section on Arduino; it may support promoting materials that are in line with the spirit of the Section.


GolamMostafa

#13
Jul 15, 2020, 12:46 pm Last Edit: Jul 15, 2020, 12:51 pm by GolamMostafa
In this Arduino Forum, there are always queries/questions from the school/college/varsity going students (even from the parents on behalf of their children) on their on-going projects and they are extended help from the veteran members of the Forum.


Go Up