Saturday, July 25, 2020

ATMega32 interfaces to TC72 SPI temperature-to-digital converter

TC72 SPI Thermometer 

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.

SPI Serial Peripheral interface AVR ATMega32 SSD TC72 temperature sensor display project Proteus Multiplexing common cathode Microchip thermometer
a sample of this program
The temperature reading is -32.00 degree Celsius.

Programming For TC72

The overall programming interface lists below:

  1. Set up the SPI to master mode
  2. Select SPI clock and data sampling mode
  3. Set up digital output for display
  4. Send the command to TC72
  5. Read temperature from TC72
  6. Display the result
Atmel Studio 7 C source code lists here.

Schematic Diagram

Schematic Diagram
Schematic Diagram of this example






Thursday, July 23, 2020

PIC16F818 interfaces to TC72 SPI Digital Temperature Sensor using CCS PICC

TC72 SPI Temperature Sensor At Brief

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.


Sample program of TC72 temperature reading sensor negative Proteus CCS PICC PIC PIC16F display LCD microcontroller Microchip digital thermometer
A program screen shot reading the temperature of -16.25 degree Celsius.

The device comes with 8-pin SMD package.


TC72 SMD Package
Pins diagram of this device lists below.

TC72 Pins Diagram

The supply voltage is between 2.65 V to 5.5 V DC. All pins description are list below.

  1. NC - No Connection
  2. CE - Chip Enable (active high)
  3. SCK - Serial Clock Input
  4. GND - Ground
  5. SDO - Serial Data Out
  6. SDI - Serial Data In
  7. NC - No Connection
  8. 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.


TC72 SPI Read/Write Operation
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.

TC72 Address

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.

  1. One-shot = 0 and Shut-Down = 0 - Continuous temperature conversion
  2. One-shot = 0 and Shut-Down = 1 - Shut Down
  3. One-shot = 1 and Shut-Down = 0 - Continuous temperature conversion
  4. 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 for this program
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*/
   setup_spi(SPI_MASTER | SPI_L_TO_H |SPI_CLK_DIV_64);

   output_b(0x00);
   set_tris_b(0x02);
   setup_adc_ports(no_analogs);
   lcd_init();
   lcd_gotoxy(1,1);
   
   printf(LCD_PUTC,"Temperature");
   /*Configure the TC72, Set the control
   register to continuous temperature reading*/
   output_high(pin_b0);
   /*0x80 is the control register address*/
   spi_write(0x80);
   /*0x04 means continuous temperature conversion*/
   spi_write(0x04);
   output_low(pin_b0);
   /*Wait for device to be ready*/
   delay_ms(250);
   
   while(1){
      
      /*Reading the LSB fraction number*/
      output_high(pin_b0);
      /*0x01 is the LSB temperature fraction number*/
      spi_write(0x01);
      /*output one more clock frame*/
      spi_write(0x00);
      output_low(pin_b0);    
      if( spi_data_is_in() ) sspL=spi_read();   
      
      /*Reading the MSB decimal number*/
      output_high(pin_b0);
      /*0x02 is MSB temperature address*/
      spi_write(0x02);
      /*output one more clock frame*/
      spi_write(0x00);    
      output_low(pin_b0);  
      if( spi_data_is_in() ) sspH=spi_read();
      
      lcd_gotoxy(1,2);
      
      /*Processing the fraction number*/
      sspL>>=6;
      fraction=sspL*25;
      
      /*Check wether temperature is negative*/
      if(sspH&0x80){
         sspH=~sspH;
         signing='-';
         fraction=fractionN[sspL];
      }
      else {
         signing=' ';
      }
      
      printf(LCD_PUTC,"%c%d.%d %cC   ",signing,sspH,fraction,c1);    
      delay_ms(1000);
   }
}

The overall program resource usage needs 86% of flash memory and 24% of program memory.

CCS PICC completed compilation


If you want a standard PCB for ATMega32 micro-controller, you can order my AVR Microcontroller project from PCBWay with a reasonable price. Click here to get a free $5 credit for new account.

Interfacing ATMega32 to 74HC595 shift register
ATMega16 ATMega32 Experiment Board PCB from PCBWay

 

 Watch it on YouTube


Saturday, July 18, 2020

Interfacing ATMega32 SPI to MCP4922 Dual 12-bit Digital To Analog Converter

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.

Interfacing ATMega32 SPI to MCP4922 Dual 12-bit Digital To Analog Converter DAC AVR ATMega Serial Peripheral Interface Microchip
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.


Interfacing ATMega32 SPI to MCP4922 Dual 12-bit Digital To Analog Converter
MCP4922 14-pin DIP Package

Interfacing ATMega32 SPI to MCP4922 Dual 12-bit Digital To Analog Converter
MCP4922 14-pin DIP pin diagram

These are the pins description of this IC:
  1. VDD  is positive supply voltage for internal device working (2.7 V to 5.5 V).
  2. NC - No Connection
  3. CS - Chip Select Input
  4. SCK - Serial Clock Input
  5. SDI - Serial Data Input
  6. NC - No Connection
  7. NC - No Connection
  8. LDAC - is active low. It transfers DAC setting from serial latches to output latches.
  9. SHDN - Hardware shut down input
  10. VOUTB - DAC_B Output
  11. VREFB - DAC_B Voltage Input (AVSS To VDD)
  12. AVSS - Analog ground
  13. VREFA - DAC_A Voltage Input (AVSS to VDD)
  14. 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:
Interfacing ATMega32 SPI to MCP4922 Dual 12-bit Digital To Analog Converter
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.

Interfacing ATMega32 SPI to MCP4922 Dual 12-bit Digital To Analog Converter
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.

Interfacing ATMega32 SPI to MCP4922 Dual 12-bit Digital To Analog Converter
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 SPI to MCP4922 Dual 12-bit Digital To Analog Converter DAC AVR ATMega Serial Peripheral Interface Microchip
A sample of this programming example

C source code:


Schematic Diagram

Interfacing ATMega32 SPI to MCP4922 Dual 12-bit Digital To Analog Converter
Schematic Diagram for simulation 



Thursday, July 16, 2020

Interfacing ATMega32 to MAX7221 LED display driver

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.

Interfacing ATMega32 to MAX7221 LED display driver
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.

Interfacing ATMega32 to MAX7221 LED display driver
MAX7221 28-DIP Package
These are its pins description:

Interfacing ATMega32 to MAX7221 LED display driver
Pin description of MAX7221

The data for format for MAX7221 reception is 16-bit wise, and device into two 8-bit registers.

Interfacing ATMega32 to MAX7221 LED display driver
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.

Interfacing ATMega32 to MAX7221 LED display driver
MAX7221 Register Map


These commands with value I selected:
  1. The 0x09 command register loads with 0xFF to enable BCD decoding for all digits.
  2. The 0x0B command register loads with 0x01 to limit the scanning for only digit 0 and 1.
  3. The 0x0C command register loads with 0x01 to turn on the device (called normal operation).
  4. The 0x0A command register loads with 0x09 to set the intensity to 10/16.
  5. 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.

Interfacing ATMega32 to MAX7221 LED display driver
A sample of program

ATMega32 C Source Code In Atmel Studio 7

The Atmel Studio 7 C source code is here.

A full schematic diagram lists below.

Interfacing ATMega32 to MAX7221 LED display driver
Schematic Diagram




Tuesday, July 14, 2020

ATMega32 SPI master and slave mode

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:
  1. Set MISO pin to output
  2. Since it's in slave mode by default, we just enable the SPI module to make it work.
  3. For the data reception, we must wait until the SPIF of SPSR register is cleared.
  4. Finally, we just read the data buffer from the  SPDR register.
ATMega32 SPI master and slave mode
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.

ATMega32 SPI master and slave mode
SPI Master and Slave Solution

ATMega32 Master Mode In C

For master source code in the master project:

#include <avr/io.h>

void masterInit(void){
/*Set MOSI, SCK and SS Output*/
DDRB=(1<<5)|(1<<7)|(1<<4);
/*Enable SPI Master set clock rate fck/4*/
SPCR=(1<<SPE)|(1<<MSTR);
}

void masterTransmit(char spiData){
/*Start the transmission*/
SPDR=spiData;
/*Wait for completion*/
while(!(SPSR&(1<<SPIF)));
}

int main(void)
{
    unsigned char ssd[16]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,
    0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
char cnt=0;
masterInit();
/*PIND2 inputs counting*/
DDRD&=~(1<<2);
/*TURN ON PULLUPS FOR PIND2*/
PORTD=0x04;

/*Initiate a starting transmission*/
PORTB&=~(1<<4);
masterTransmit(ssd[cnt]);
PORTB|=(1<<4);
    while (1) 
    {
if((PIND&0x04)==0){
while((PIND&0x04)==0);
cnt+=1;
if(cnt>15) cnt=0;
PORTB&=~(1<<4);
masterTransmit(ssd[cnt]);
PORTB|=(1<<4);
}
    }
}

ATMega32 Slave Mode In C

For Slave source code in the Slave project:

#include <avr/io.h>

void slaveInit(void){
/*Set MISO to input*/
DDRB=(1<<6);
/*Turn On SPI*/
SPCR=(1<<SPE);
}

char spiReceive(void){
/*Wait for completed received data*/
while(!(SPSR&(1<<SPIF)));
/*Return the SPI Buffer*/
return SPDR;
}

The full schematic is here.

ATMega32 SPI master and slave mode
Schematic Diagram




If you want a standard PCB for ATMega32 micro-controller, you can order my AVR Microcontroller project from PCBWay with a reasonable price. Click here to get a free $5 credit for new account.


Monday, July 13, 2020

Introduction to ATMega32 SPI Application With SN74HC595N

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). 

Introduction to ATMega32 SPI
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.

Introduction to ATMega32 SPI
The connection between a master and a slave device

For a master data transferring, it needs three wires:
  1. Master Out Slave In (MOSI), a master microcontroller send data out to the slave device.
  2. Serial Clock (SCK), a master microcontroller outputs
    synchronous clock while sending data out.
  3. 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.

Introduction to ATMega32 SPI
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.

Introduction to ATMega32 SPI
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.

Introduction to ATMega32 SPI
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.

Introduction to ATMega32 SPI
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.

Introduction to ATMega32 SPI
A sample of program, show the text "SPI" on the SSD.

Source Code:


Introduction to ATMega32 SPI
Schematic diagram





We can also use the SN74HC164 serial to parallel shift registers chip to drive 7-Segment display in individual or multiplexing mode

ATMega32 SN74HC164 Three-Digit Multiplexing Display
Simulating Program

Friday, July 10, 2020

Using analog comparator module of ATMega32

Analog Comparator Module Of ATMega32 AVR

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,
  1. a positive input AIN0
  2. and a negative input AIN1. 
Using analog comparator module of ATMega32 AVR ATmega LM358 LM311 Proteus Atmel Studio 7 C Microchip Atmel POT ADC Analog To Digital Converter
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). 

Using analog comparator module of ATMega32
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.

Using analog comparator module of ATMega32
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);
      }

Using analog comparator module of ATMega32
AIN0 varies the positive voltage while AIN1 hold a fixed voltage of 2.5 V. PC5 is set
whenever AIN0 is greater than AIN1.



Search This Blog

Labels

25AA010A (1) 8051 (7) 93AA46B (1) ADC (30) Analog Comparator (1) Arduino (15) ARM (6) AT89C52 (7) ATMega32 (56) AVR (57) CCS PICC (28) DAC (1) DHT11 (2) Display (105) Distance Sensor (3) DS18B20 (3) dsPIC (2) dsPIC30F1010 (2) EEPROM (5) Environment Sensor (4) esp8266 (1) I2C (29) Input/Output (67) Interrupt (19) Keil (5) Keypad (10) LCD (47) Master/Slave (1) MAX7221 (1) MCP23017 (5) MCP23S17 (4) Meter (3) MikroC (2) Motor (15) MPLABX (71) Nokia 5110 LCD (3) OLED (2) One-Wire (6) Oscillator (8) PCB (6) PCD8544 (3) PCF8574 (5) PIC (107) PIC12F (2) PIC16F628A (2) PIC16F630 (1) PIC16F716 (3) PIC16F818 (10) PIC16F818/819 (2) PIC16F84A (15) PIC16F876A (1) PIC16F877A (9) PIC16F88 (1) PIC16F887 (60) PIC18 (19) PIC18F1220 (4) PIC18F2550 (3) PIC18F4550 (12) PWM (11) RTC (8) Sensor (10) SH1106 (1) Shift Register (11) Shift Registers (3) SPI (24) STM32 (6) STM32 Blue Pill (6) STM32CubeIDE (6) STM32F103C8T6 (6) SysTick (3) temperature sensor (11) Thermometer (21) Timer/Counter (31) TM1637 (2) UART (7) Ultrasonic (4) Voltmeter (7) WDT (1) XC16 (2) XC8 (94)