To do that, we open Proteus in our computer and we create the next electronic circuit:
Where we have the next parts:
-This is the scheme of an Arduino Uno, with an Atmega328P:
-This is a Virtual Terminal. This allows us to see what is there on the pins TXD and RXD of the arduino when we click on Play in the simulation.
In our case, we have written in our program that we will see this text:
Note: we will see al the written code later.
-Here, we can see the DS1621. This is the electronic device that measures the temperature and converts it into a digital signal, that is sended to the arduino using I2C. The pins A0, A1 and A2 are connected to the GND because they aren't used, SCL and SDA are the I2C terminals and TOUT it's a LED that it's switched ON if the temperature is over 30 degrees. If the temperature is over 30 degrees, the output becomes "0" and then, the LED is ON. We have programmed it on the code to switch it ON when it passes 30 degrees and it gets OFF if the temperature is under 0ºC.
R5 and R6 are two pull-up resistors that are needed because if they weren't there, SCL and SDA would be always at 5V (an 1 logic).
AD5 and AD4 are digital pins of the arduino that are connected to SCL and SDA.
R5 and R6 are two pull-up resistors that are needed because if they weren't there, SCL and SDA would be always at 5V (an 1 logic).
AD5 and AD4 are digital pins of the arduino that are connected to SCL and SDA.
The other device it's an I2C debugger. On it, we can see the information sended and received by the DS1621 using I2C. If we start the simulation, we can see on it this window:
On it, the first number that we can see, it's 96.846ms (or another number), that is the time that the DS1621 lasts to do all that operations. Then, we can see 97.178ms (or another number), that it's the time that is used in that part of the communication since the beginning of the program. Then, it is send a signal with some bits. They are represented with those letters:
S: bit of start
90: command to write
91: command to read
A: recognition bit
P: stop bit
AC: command to write in the register
EE: command to start the connection
AA: command to read the temperature
-Here, we have an LCD screen, that says the temperature in Celsius degrees and if the temperature is correct or not. Usually, it uses all it's pins, but in this case, we have used a library which allows us to use only the half and thanks to it we can control the text only with the inputs D4, D5, D6 and D7.
-Here, we can see in the left column the name of the variables that we are using, it's memory address and it's value.
Here, we can see the code of the program while it's paused, so we can see the orders of the processor on the left.
-Here, we can see a part of the library that we are using (Wire.h)
Code that we are using:
/* Main.ino file generated by New Project wizard
*
* Created: Thu Sep 5 2013
* Processor: ATmega328P
* Compiler: Arduino AVR
*/
//Here, we will declare our variables
#include <Wire.h> //We include the library Wire.h, that is used to use I2C with arduino
int tempC=0; //We initialize the initial temperature in Celsius to 0ºC
int tempF=0; //We initialize the initial temperature in Fahrenheit to 0ºF
int direccion=0x48; //Here, we set the direction of memory
//To introduce the number 90, we have to introduce the number 48 in binary, because then, it's introduced a 0 in it's right (48 in binary is 01001000) and the bit in the left is lost.
//We introduce the LCD library and it's parameters
#include <LiquidCrystal.h> //We include the library LCD
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
//LiquidCrystal lcd (rs, enable, d4, d5, d6, d7)
//rs -> register select
//enable -> allow the LCD to work
void setup()
{ // put your setup code here, to run once:
Serial.begin(9600); //We initializate the serial port at 9600 bauds
Wire.begin(); //We start the I2C in the arduino
Wire.beginTransmission(direccion); //We say where we want to directionate the slave
/*
http://stackoverflow.com/questions/16909175/arduino-i2c-ds1621-commands
*/
Wire.begin();
Wire.beginTransmission(direccion); //I am talking to you
Wire.write(0xA1); //We want to change TH
Wire.write(0x1E); // Value to switch on the LED at 30ºC
Wire.endTransmission();
/*
Wire.beginTransmission(direccion); //I am talking to you
Wire.write(0xA2); //We want to change TL
Wire.write(0x19); //Value of 25
Wire.endTransmission();
*/
Wire.beginTransmission(direccion);
Wire.write(0xAC); //We write on the control register
Wire.write(0x02); //We configurate it for a continuous adquisition
Wire.beginTransmission(direccion); //We introduce a repeated starting condition
Wire.write(0xEE); //We use a command to start the conversion
Wire.endTransmission(direccion); //Stop condition
//LCD:
lcd.begin(16, 2); //16 is the number of columns and 2 is the number of rows of the LCD
}
void loop()
{ // put your main code here, to run repeatedly:
delay(1000); //We repeat all the loop every 1 second
Wire.beginTransmission(direccion); //Starting condition
Wire.write(0xAA); //We read the last conversion
Wire.endTransmission(); //Stop
Wire.requestFrom(0x48,1); //We read the result
tempC=Wire.read(); //We save the result on the variable tempC
tempF=tempC*9/5+32; //We convert the previous result to Fahrenheit
//LCD:
lcd.clear(); //We clear the LCD
lcd.setCursor(1, 0); //We set the cursor of the LCD in the first position in the first row
lcd.print(tempC); //We write the temperature in celsius in the place previously decided
lcd.setCursor(0, 1); //We set the cursor of the LCD in the first position in the second row
if (tempC<=20) //If the temperature is under 20 degrees, we print "Mucho frio"
{
lcd.print("Mucho Frio");}
else if (tempC>=30) //If the temperature is over 30 degrees, we print "Overflow"
{lcd.print("Overflow");}
else
{lcd.print("Correcto");} //Else, we say that everything is correct
Serial.print("Temperatura: "); //We say through the serial port the temperature in celsius and fahrenheit
Serial.print(tempC);
Serial.print(" C/ ");
Serial.print(tempF);
Serial.println(" F");
}
You can download the program (and the pictures in case you don't see them correctly) from here:
No hay comentarios:
Publicar un comentario