Learn To Write Code For 8051, Arduino, AVR, dsPIC, PIC, STM32 ARM Microcontroller, etc.
Coding Embedded Controller With C/C++.
Printed Circuit Board (PCB) Project For Electronics Hobbyists.
TC72 is a SPI slave device reading and converting analog temperature to digital formats. The temperature reading ranges from -55 to +125 degree Celsius. With these temperature values, it also has a fraction temperature value. The fraction is 0.25 degree Celsius per step. Fraction reading has only 3 points, 0.25, 0.50 and 0.75 degree Celsius.
I don't cover its technical detail here. In the previous post, I put some detail of this device with programming interface to PIC16F818.
In this example, I use ATMega32 functioning as a master SPI reading temperature from TC72 and displays the temperature.
a sample of this program The temperature reading is -32.00 degree Celsius.
TC72 is a temperature-to-digital converter with serial peripheral interface (SPI). It could read the temperature from -55 to +125 degree Celsius. The temperature data is 10-bit format in which the upper 8-bit is the signed decimal value. The remaining two lower bits is the fraction value. The fraction is 0.25 degree Celsius step.
A program screen shot reading the temperature of -16.25 degree Celsius.
The device comes with 8-pin SMD package.
Pins diagram of this device lists below.
The supply voltage is between 2.65 V to 5.5 V DC. All pins description are list below.
NC - No Connection
CE - Chip Enable (active high)
SCK - Serial Clock Input
GND - Ground
SDO - Serial Data Out
SDI - Serial Data In
NC - No Connection
VDD - Positive Supply
For data communication, there are two read and write operations- SPI single byte and SPI multiple byte. But here, I implement only the SPI single byte implementation.
Ax Denotes the address of register. Dx is the data written or reading from the corresponding address.
Reading and writing need one 8-bit address and one 8-bit data. There are four addresses registers- Control, LSB Temperature, MSB Temperature and Manufacturer ID Register. Control register is read/write while others are read-only.
Control register is for initialize the operation of this device. There are three modes- one-shot, continuous conversion and shut down. At power on reset or brown out reset, it is in shut down mode. Control register setting lists below.
One-shot = 0 and Shut-Down = 0 - Continuous temperature conversion
One-shot = 0 and Shut-Down = 1 - Shut Down
One-shot = 1 and Shut-Down = 0 - Continuous temperature conversion
One-shot = 1 and Shut-Down = 1 - One Shot
LSB temperature is the two-bit fraction number. It is 0.25 degree Celsius per bit, and 0.75 degree Celsius maximum value. We can ignore this fraction LSB temperature number with 0.75 degree Celsius error.
MSB temperature is the signed decimal temperature data. Manufacturer ID identifies this device.
Programming with CCS PICC
PIC16F818 comes with and SPI communication module. The program memory sizes up to 2 kB, sufficient for this programming example.
In this example, the SPI port commands to read the temperature from TC72 in continuous mode. An 16x2 character LCD display the result with full formatting.
The MCU clocks at 4 MHz, yielding a 1 micro second instruction speed. SPI clock is divided by 64 to make a steady serial data reading.
Schematic diagram
CCS PICC program lists below.
#include<16F818.h>
#use delay(clock=4M)
#fuses NOWDT,INTRC_IO
#define LCD_ENABLE_PIN PIN_A2
#define LCD_RS_PIN PIN_A0
#define LCD_RW_PIN PIN_A1
#define LCD_DATA4 PIN_A4
#define LCD_DATA5 PIN_A3
#define LCD_DATA6 PIN_A6
#define LCD_DATA7 PIN_A7
/*use a built-in LCD driver*/
#include<lcd.c>
void main(){
int sspL,sspH,fraction;
/*223 is custom code for degree*/
char c1=223,signing;
int fractionN[3]={75,50,25};
/*SPI master mode, clock low to high divided by 64*/
Interfacing ATMega32 SPI to MCP4922 Dual 12-bit Digital To Analog Converter
Microchip MCP492X SPI Digital To Analog Converter
MCP492X is a 12-bit digital to analog converter (DAC) with SPI interface. MCP4921 is a single output DAC while MCP4922 has two configurable DAC. This SPI DAC could clock up to 20 MHz.
A sample of this programming example
In this post I use MCP4922 I possess. I bought this chip from Futurlec on-line store a few years ago. It comes with an easy to prototype 14-bit DIP.
MCP4922 14-pin DIP Package
MCP4922 14-pin DIP pin diagram
These are the pins description of this IC:
VDD is positive supply voltage for internal device working (2.7 V to 5.5 V).
NC - No Connection
CS - Chip Select Input
SCK - Serial Clock Input
SDI - Serial Data Input
NC - No Connection
NC - No Connection
LDAC - is active low. It transfers DAC setting from serial latches to output latches.
SHDN - Hardware shut down input
VOUTB - DAC_B Output
VREFB - DAC_B Voltage Input (AVSS To VDD)
AVSS - Analog ground
VREFA - DAC_A Voltage Input (AVSS to VDD)
VOUTA - DAC_A Output
The data format transfers to this device is 16-bit wide. One upper nibble is the DAC configuration bits while the remaining 12-bit is the DAC output value. The output value to DAC is up 4096 maximum.
DAC output voltage - VOUT lists as follow:
Where,
VREF is DAC voltage reference input. It could be DAC_A or DAC_B
G is output voltage gain, 1X or 2X.
DN is 12-bit DAC data input value
2^N is the DAC resolution. In this case, the resolution is 12-bit yield the decimal value to 2^12 = 4046
The 16-bit SPI data representation lists below.
MCP4922 Write Command Register
Each bit functions as below.
Bit 15 - A/B - Write '0' for DAC_A, otherwise DAC_B
Bit 14 - BUF - Write '0' for Unbuffered, otherwise Buffered.
Bit 13 - GA - Write '0' 1x Gain, otherwise 2x Gain.
Bit 12 - SHDN - Write '0' to disable output buffer, otherwise enable the output buffer.
Bit 11:0 - D11:D0 - 12-bit digital data DAC output ranges from 0 to 4095 (4096 in total).
Data transfer to this device is 16-bit, we need to send 8-bit twice with only on latching pulse.
Writing the command to MCP4922. External wiring is not include here.
Interfacing And Programming With Atmel Studio 7 In C
In this example, I use both output DAC. Both DAC outputs the maximum 12-bit value, 0xFFF or 4096. With 2X gain, DAC A generate the output voltage nearest to 10 V. Similarly, using 1X gain, DAC B generate output the voltage nearest to 5 V. The sample of this program lists below. The reference voltage for both DAC is 5 V.
Interfacing ATMega32 to MAX7221 LED display driver
MAX7221 SPI 8-Digit LED Display Driver
A master ATMega32 SPI could send data to many SPI slave devices on bus. MAX7221 is another example of an SPI slave device. MAX7221 is an 8-digit display driver. It could fit multiplexed seven-segment display, or a 8x8 dot matrix display. Both seven-segment and dot matrix display are common cathode. For more digits we can use a daisy-chain with preferred numbers of MAX7221.
A sample simulation of this example
This device work independently. The master MCU configures and send the data only once. Then MAX7221 periodically displays and refresh by itself.
MAX7221 28-DIP Package
These are its pins description:
Pin description of MAX7221
The data for format for MAX7221 reception is 16-bit wise, and device into two 8-bit registers.
Command register uses only 4 lower nibble.
I don't list all datasheet full specification here. I list only some needed technical details.
Every command register has its own address. We must load these address with any appropriate values.
MAX7221 Register Map
These commands with value I selected:
The 0x09 command register loads with 0xFF to enable BCD decoding for all digits.
The 0x0B command register loads with 0x01 to limit the scanning for only digit 0 and 1.
The 0x0C command register loads with 0x01 to turn on the device (called normal operation).
The 0x0A command register loads with 0x09 to set the intensity to 10/16.
All digits have its own command register ranges from 0x01 to 0x08. We load these command registers to display the value. The value could be a BCD format or anythings, up to the pre-setting.
In this example, I turn on only two digits. This two-digit display show the counting value at one second rate.
ATMega32 SPI master and slave mode Programming and interfacing
ATMega32 AVR Working In Slave Mode
In the previous post we introduced only the SPI master mode. In slave mode, SPI module is more easier to implement. In this example, I use one master ATMega32 to send a counting data to another slave ATMega32.
In slave mode, the ATMega32 must configured as follow:
Set MISO pin to output
Since it's in slave mode by default, we just enable the SPI module to make it work.
For the data reception, we must wait until the SPIF of SPSR register is cleared.
Finally, we just read the data buffer from the SPDR register.
At master, ATMega32 counts the input from a switch and send out the result to the slave
ATMega32. At the slave side, ATMega32 just receive the data and output to the SSD.
In Atmel Studio 7, I use one solution with two projects, a master project and a slave project.
Introduction to ATMega32 SPI serial peripheral interface
Serial Peripheral Interface Of Atmel AVR
The serial peripheral interface (SPI) is a high speed synchronous data transfer between a microcontroller (master device) and its peripheral device (slave).
A sample of program, show the text "SPI" on the SSD.
In some requirements, it's use to transfer data between a microcontroller (master) and others slave microcontrollers.
The connection between a master and a slave device
For a master data transferring, it needs three wires:
Master Out Slave In (MOSI), a master microcontroller send data out to the slave device.
Serial Clock (SCK), a master microcontroller outputs synchronous clock while sending data out.
Slave Select (SS), a master microcontroller issue a low logic level to force the slave device accepting the sent data. The SS pin could be any pin on the ATMega32 microcontroller when working as a master device.
In SPI communication, one different slave device requires it own slave select pin. So the number of slave devices increase the slave select pins.
A sample of master transfer data. Clock is activated
from low to high. Data is transfer at rising edge
of clock. Slave select pin activate from high to
low to select the slave device.
In ATMega32, there some register to set up before the SPI is ready-to-use.
SPI Control Register - SPCR
To use the SPI, set the SPE bit to '1' to turn on the SPI module.
The DODR is normally zero, allowing the data transfer the MSB first.
Setting the MSTR bit to '1', making the ATMega32 to work in master mode.
CPOL bit is the clock polarity select bit. It's zero by default and it's low in idle mode.
The CPHA is the clock phase setting bit. By default ( 0 ), the data is sample at the leading edge of the clock.
The clock rate select if Fosc/4 when it's not set.
SPI Status Register
The SPI interrupt flag - SPIF uses for polling the completion of SPI transfer. It's set when the transfer is completed. The other bits are not discussed here.
SPI Data Register
In master mode, writing data to the SPDR starts the transmission. In slave mode, reading this register when the reception is completed.
Programming Example In Atmel Studio 7 C
In this example I use the SPI module of ATMega32 to work in master mode. I use three slaves identical device, 74HC595 shift register. These three devices are the same in operation. So It requires only three wires. Activating the slave select pin once, these three shift register latch the data to its output register.
A sample of program, show the text "SPI" on the SSD.
An analog module is integrated in ATMega32. It's useful for comparing two analog voltage inputs. Just like any conventional analog comparator IC, the built-in analog comparator module work in the similar way. In the case of ATMega32 there are two standard analog voltage inputs pins,
a positive input AIN0
and a negative input AIN1.
An example of using analog comparator module
The module compares between the potential of these two inputs. If the voltage fed to AIN0 is greater than the AIN1, it set the analog comparator output (ACO).
A simple analog comparator module without its complex logic parts.
Actually the analog comparator module of this device is more complex than the figure listed above.
For more options, the negative input pin AIN1 could be replace with on of the ADC input pin from ADC0 to ADC7. It depends on the application we cop with.
The analog comparator module could generate interrupt on output rising, falling and toggling.
These are registers involves with the analog comparator module.
Analog Comparator Control and Status Register
There are two more registers to select an alternative negative voltage input pin. But it's stored in the ADC module register and special IO function register (SFIOR). By default, both analog voltage input pin are AIN0 and AIN1.
I don't want to list all description of these registers because some features are not used here. For more technical details, please check the datasheet.
By default the ACD of ACSR is '0'. Hence the analog comparator module is turned on. To test the output status, we must use the ACO bit.
Programming For Analog Comparator In Atmel Studio 7
In this example, I use a simple way to compare between AIN0 and AIN1. If the analog voltage fed to AIN0 is greater than 2.5 V, the output LED connects to PC5 will set.
Atmel Studio C source code implementation is very simple.
#include <avr/io.h>
int main(void)
{
//PC5 in output pin
DDRC|=(1<<5);
while (1)
{
/*Test the ACO bit*/
if(ACSR&0b00100000) PORTC|=(1<<5);
else PORTC&=~(1<<5);
}
AIN0 varies the positive voltage while AIN1 hold a fixed voltage of 2.5 V. PC5 is set
whenever AIN0 is greater than AIN1.