sâmbătă, 13 octombrie 2012

Interfacing LM35 Temperature Sensor with PIC Microcontroller.

The are many cool sensors available now a days, ranging from IR distance sensor modules, accelerometers, humidity sensors, temperature sensors and many many more(gas sensors, alcohol sensor, motion sensors, touch screens). Many of these are analog in nature. That means they give a voltage output that varies directly (and linearly) with the sensed quantity. For example in LM35 temperature sensor, the output voltage is 10mV per degree centigrade. That means if output is 300mV then the temperature is 30 degrees. In this tutorial we will learn how to interface LM35 temperature sensor with PIC18F4520 microcontroller and display its output on the LCD module.
First I recommend you to go and read the following tutorial as they are the base of this small project.
After reading the ADC tutorial given above you will note the the PIC MCU's ADC gives us the value between 0-1023 for input voltage of 0 to 5v provided it is configured exactly as in the above tutorial. So if the reading is 0 then input is 0v, if reading is 1023 then input is 5v. So in general form if the adc read out is val then voltage is.
unsigned int val;
val=ADCRead(0); //Read Channel 0
voltage= ((val)/1023.0)*5;
The above formula give voltage in Volts, to get Voltage in mili Volts (mV) we must multiply it with 1000, so
voltage=((val)/1023.0)*5*1000); //Voltage is in mV
since 10mV = 1 degree, to get temperature we must divide it by 10, so
t=((val)/1023.0)*5*100); //t is in degree centigrade
simplifying further we get
t=((val/1023.0)*500);
t=(val*0.48876);
we round off this value, so
t=round(val*0.48876);
remember round() is a standard c library function

Hardware for LM35 based thermometer.

You will need a PIC18F4520 chip running at 20MHz attached with a standard 16x2 LCD Module and LM35 on AN0 pin. LM35 is a 3 pin device as show below.
lm35 temperature sensor pinout

Fig.: LM35 Temperature Sensor Pinout


connect the +Vs Pin to 5v and GND to GND. The output must be connected to the analog input pin 0 of the PIC18F4520 MCU. It is labeled AN0 in the datasheet. It is pin number 2 on the 40 pin package. It is also called RA0 because it is shared with PORTA0.
We will use our 40 PIN PIC Development board to realize the project. The base board has all the basic circuit to run the PIC. The extra part required for this project like LCD and the LM35 temperature sensor are installed in the expansion board.
LCD Interface with PIC

Fig.: LCD Expansion Board.

The supply for LM35 can be taken from the onboard extra power supply. See the image below.
5v and GND points on PIC Development Board

Fig.: 5v and GND points on PIC Development Board.

Just use single PIN female to female wire to connect with the leads of LM35 temperature sensor. Now plug the LCD Expansion board into the expansion slot and burn the hex file to the board using a PIC ISCP Programmer. Your are now all ready to run.
lm35

Fig.: LM35

C Source Code For PIC Thermometer Project.


/********************************************************************

LM35 Temperature Sensor INTERFACING TEST PROGRAM

---------------------------------------------------------
Simple Program to connect with LM temperature sensor using the
internal ADC of PIC MCU.

The program displays the current environment temperature on
LCD Module.

MCU: PIC18FXXXX Series from Microchip.
Compiler: HI-TECH C Compiler for PIC18 MCUs (http://www.htsoft.com/)

Copyrights 2008-2010 Avinash Gupta
eXtreme Electronics, India

For More Info visit
http://www.eXtremeElectronics.co.in

Mail: me@avinashgupta.com

********************************************************************/
#include <htc.h>

#include <math.h>

#include "lcd.h"

//Chip Settings
__CONFIG(1,0x0200);
__CONFIG(2,0X1E1F);
__CONFIG(3,0X8100);
__CONFIG(4,0X00C1);
__CONFIG(5,0XC00F);


//Simple Delay Routine
void Wait(unsigned int delay)
{
   for(;delay;delay--)
      __delay_us(100);
}

//Function to Initialise the ADC Module
void ADCInit()
{
   //We use default value for +/- Vref

   //VCFG0=0,VCFG1=0
   //That means +Vref = Vdd (5v) and -Vref=GEN

   //Port Configuration
   //We also use default value here too
   //All ANx channels are Analog

   /*
      ADCON2

      *ADC Result Right Justified.
      *Acquisition Time = 2TAD
      *Conversion Clock = 32 Tosc
   */

   ADCON2=0b10001010;
}

//Function to Read given ADC channel (0-13)
unsigned int ADCRead(unsigned char ch)
{
   if(ch>13) return 0;  //Invalid Channel

   ADCON0=0x00;

   ADCON0=(ch<<2);   //Select ADC Channel

   ADON=1;  //switch on the adc module

   GODONE=1;//Start conversion

   while(GODONE); //wait for the conversion to finish

   ADON=0;  //switch off adc

   return ADRES;
}
void main()
{
   //Let the LCD Module start up
   Wait(100);

   //Initialize the LCD Module
   LCDInit(LS_BLINK);

   //Initialize the ADC Module

   ADCInit();

   //Clear the Module
   LCDClear();

   //Write a string at current cursor pos
   LCDWriteString("LM35 Test");
   LCDWriteStringXY(4,1,"Degree Celcius");

   while(1)
   {
      unsigned int val; //ADC Value

      unsigned int t;      //Temperature


      val=ADCRead(0);   //Read Channel 0

      t=round(val*0.48876);//Convert to Degree Celcius

      LCDWriteIntXY(0,1,t,3);//Prit IT!


      Wait(1000);
   }

}


To compile the above code, lcd.c file must be added to the poject. While the lcd.h, myutils.h must be present in the same project folder. More instruction is available in following articles.

Testing The LM35 Based Thermometer.

Turn on the power supply, the screen should show the current temperature readings. Bring a Hot soldering iron tip near the LM35's pins, don't touch it keep it 1 or 2mm away. The screen should update with the rising temperature. Now finally touch the pins of LM35 with the tip of iron, the temperature should rise quickly. Keep it there until temperature rise to 80 degrees, then remove the iron. You can now blow some air by your mouth on the sensor to cool it down.
lm 35 temperature sensor demo

Fig.: LM35 Temperature Sensor Demo.

LM35 Temperature Sensor Demo

Fig.: LM35 Temperature Sensor Demo Hardware Setup.

PIC18F4520 based Thermometer using LM35 Schematic

lm35 temperrature sensor demo schematic

PIC18F4520 based Thermometer using LM35 Schematic

General Notes

  • For proper working use the components of exact values as shown above.
  • Wherever possible use new components.
  • Solder everything in a clean way. Major problems arises due to improper soldering,solder jumps and loose joints.
  • Use the exact value crystal shown in schematic.
  • Only burning the HEX file to the MCU is NOT enough. PIC18 devices are fairly complex MCU and can be configured in various ways. Chip is configured using the CONFIG Bytes. Although all hex file given in our site comes with embedded CONFIG bytes. But the user must ensure they are programmed to the chip. Any good programmer has the capability to read the configuration information from the hex file and transfer it to the MCU. Programs will not run without proper configuration byte programming. One major job of configuration is to setup proper oscillator and PLL modes without which the MCU won't execute a single instruction.
  • To compile the above code you need the HI-TECH C and MPLAB IDE. They must be properly set up and a project with correct settings must be created in order to compile the code. So I request you to read the following articles to become familiar with the built steps.
  • To understand the code you must have good knowledge of core C language. Please don't be confused with the basic concept of the language.
  • You must be familier with project concept and multi source file concept that used used in most professional languages like C.
  • You need Proteus VSM if you want to develop or debug the project without any hardware setup.


Downloads


marți, 16 august 2011

How to :

Here I gone to learn you how to download  from torrents without your download to get counted.

Step 1:

Download a torrent like  usually  :



Step 2:

 When the DW started (meaning that your DW speed is greater than 0) go to Options - > Preferences -> Connections 
 Change Proxy Server type from none to HTTP, in proxy field put localhost2 and at Port put 8118 as below , and press OK :



When the DW finished stop the torrent , change back the settings and start the torrent again . The upload the  torrent will make from now will get count .
Do the same thing for  each torrent for which you don't want that the DW to get counted .

This thing is 100% and you will not be detected by the tracker .










duminică, 24 aprilie 2011

Hello World Project With PIC Microcontroller – Part I

Hello friends, welcome to this exciting tutorial were we will begin our journey with latest PIC18F micros from Microchip Technologies. This tutorial will give you information on what software/hardware you will require and basic steps on how to get, install, configure and use them. After going through this tutorial you will have a complete setup and knowledge to experiment with these powerful chips !




What you will learn ?

  • MPLab as a powerful IDE.
  • HI-TECH C for PIC18 MCUs as a powerful C Compiler.
  • Creating a new HI-TECH C project in MPLab.
  • Write a C Code to blink LED and compile it to get a HEX code.
  • Configure the MCU for proper oscillator selection.
  • Burn the HEX code to MCU using eXtreme Burner PIC from eXtreme Electronics.
  • Use the programmed MCU to actually blink the LED !
So lets get started !!!

First get these stuffs

  • Microchip's MPLab IDE or Integrated development Environment. This is the central tool from where you can access most of other tools, like the C Compiler. This also lets you create and edit program files and projects. Download this from Microchips Web site and Install it in your computer.
  • HI-TECH C Pro for PIC18 MCUs - This is will compile the high level human readable programs (in C Language) to Machine Language Instructions (contained in a HEX file). You will not interact directly with this software instead the MPLAB will use it internally. Actually this is a commercial software but a fully functional demo is available. The demo has only restriction that codes are not highly optimized. Not a problem for startup hobbyist!
  • eXtreme Burner PIC - This hardware + software tool is used for burning(uploading) the HEX file to the microcontroller chip. This is a USB Based programmer with GUI interface so it is very easy to install and use.
  • If you have made a Simple Serial Port Based PIC Programmer as discussed here then you need this nice program from Mr Christian Stadler. This is similar to eXtreme Burner PIC but serial port based and the hardware can be made easily.

Creating your first project with MPLAB + HI-TECH C.

After installing the above tools open MPLAB IDE from desktop Icon or Start Menu. You will get a main screen similar to this.

mplab tutorial

Fig.: Microchip MPLAB IDE Main Screen.

From the Project Menu Select Project Wizard.
The Project Wizard will come up that will help you easily create a C Project. Click Next.
On the next screen select the Microcontroller you want to use. in our case select "PIC18F4550" and Click Next.

mplab tutorial

Fig.: Microchip MPLAB IDE - Processor Selection.

After that select what tool suit you want to use this project. There are many C Compilers for PIC18 (from different vendors) and also many people use assembly language, so this step is required.
As we will be working will HI-TECH C, select that from the list. Do not touch any other fields in this dialog. Just select Active Tool Suit = HI-TECH Universal ToolSuit. as shown in image below.

microchip mplab tutorial pic18

Fig.: Microchip MPLAB IDE - ToolSuit Selection.


Now Select select a suitable folder for your new project. I recommend create a Folder Named MPLAB (To save all MPLAB project in one place) and then create subdirectories in this folder for each new project. Like use C:\Your Name\MPLAB\LED\ for this led blinky demo. Now show the project wizard the path to this folder so it can create a project file here. See the next image.

Fig.: Microchip MPLAB IDE - Project Location.

The next screen asks to add existing files to project. This is useful when you have some ready made and tested code that you can add to this project. Like if you have got a tested LCD interfacing library (which you have got from Net or made yourself) then you can add that now. But since this a simple "hello world" project you can skip this step. So click Next

microchip mplab tutorial pic18

Fig.: Microchip MPLAB IDE - Adding existing files.

The final screen shows the project summary, review that and click Finish.

microchip mplab tutorial pic18

Fig.: Microchip MPLAB IDE - Project Summary.

Now you will be presented with the WORKSPACE. Now our Workspace is ready we need to add some source files. As this is a very simple project we will have as single source file. To add a new source file click File>New from menu or Select New File from toolbar. We need to save this file and also "add" this to our project. Select File>Save from Menu or Save File from Toolbar or Ctrl+S from keyboard. Give a proper name to file like LED.c and save in your project folder.
Make Sure that Add File To Project is checked. As shown below.

microchip mplab tutorial pic18

Fig.: Microchip MPLAB IDE - Saving a C File.

Now your new file is ready, lets do some coding!!! Hey I mean just copy paste the following code.

#include <htc.h>

__CONFIG(1,0x0F24);
__CONFIG(2,0X0000);

void Wait()
{
   unsigned char i;
   for(i=0;i<100;i++)
      _delay(60000);
}


void main()
{
   //Initialize PORTD
   //PD0 as Output
   TRISD=0b11111110;

   //Now loop forever blinking the LED.
   while(1)
   {
      LATD=0B00000001;  //PORTD0 = HIGH

      Wait();

      LATD=0B00000000;  //PORTD0 = LOW
      Wait();
   }
}
And don't scratch your head now if you don't get the code, just copy paste, everything will be explained in latter tutorials. So don't forget to register for updates (see Feedburner Sidebar).
Now time to convert that source code to machine code. So Select Rebuild form Project Menu.

pic18 c tutorial

Fig.: Microchip MPLAB IDE - Rebuild from Project Menu.

If everything is ok you will get a message "Build Successful". The the compiler has compiled and linked your project and a HEX file is generated. You have to transfer this HEX file to your MCU using a Programmer. The HEX file ready to burn is located in the project folder.

pic micro c tutorial

Fig.: HEX file Ready to burn to PIC MCU !

That's it for Part I of this tutorial the rest will be covered in next tutorial

luni, 28 martie 2011

Programming in C – Tips for Embedded Development.

Here I will highlight some features of C language commonly used in 8 bit embedded platforms like 8051, AVR and PICs. While programming microcontrollers in C most of the time we have to deal with registers. Most common tasks are setting and clearing bits in a register and check whether a bit is 0 or 1 in a given register. So here I will give detail on those topics, it will help you if you are new to embedded programming in C and if you get confused when you see some codes.


A Register

A register is simply a collection of some bits (mostly 8 bits in case of 8bit MCUs). Either each different bit in a register has some purpose or the register as a whole holds a value. Registers serves as connection between a CPU and a Peripheral device (like ADC or TIMER). By modifying the register the CPU is actually instructing the PERIPHERAL to do something or it is configuring it in some way. And by reading a register, the CPU can know the state of peripheral or read associated data.


Fig.: CPU writing to Peripheral Register


 Fig.: CPU Reading from Peripheral Register

 

Binary Numbers in C

When you write a=110; in C it means you are setting the value of variable"a" to "one hundred and ten" (in decimal). Many time in embedded programming we are not interested in the value of a variable but the state of each bits in the variable. Like when you want to set the bits of a register (MYREG) to a bit pattern like 10010111 (binary). Then you cannot write MYREG=10010111. Because compiler will interpret 10010111 as decimal. To specify a binary number in C program you have to prefix it with 0b (zero followed by b). So if you write
MYREG=0b10010111;
it assigns the bit pattern 10010111 to the bits of Register MYREG.

HEX Numbers in C

In same way if you prefix a number by 0x (a zero followed by x) then compiler interpret it like a HEX number. So
MYREG=0x10; (10 in HEX is 16 in decimal)
MYREG=0xFF;(Set all bits to 11111111 or decimal 255)

Setting a BIT in Register

Here our aim is to set (set to logical 1) any given bit (say bit 5) of a given register (say MYREG). The syntax is
MYREG=MYREG | 0b00100000;
The above code will SET bit 5 to 1 leaving all other bits unchanged. What the above code does is that it ORs each Bit of MYREG with each bit of 0b00100000 and store the value back in MYREG. If you know how logical OR works then you will get it.
In short you can write the same code as
MYREG|=0b00100000;
Now lets come to practical usage. In practice each bit has got a name according to its work/function. Say our BIT (the 5th bit) has got name ENABLE, and what it does is clear by its name,when we set it to 1 it enables the peripheral and when cleared (0) it disables it. So the right way to set it is.
MYREG|=(1<<ENABLE);
The << is called left shift operator. It shifts the bits of LHS variable left by the amount on its RHS variable. If you write
b=1<<3;
then, 1 whose binary value is 00000001 is shifted 3 places to left which results in 00001000
So if ENABLE is defined as 5 (as enable is 5th bit) then
MYREG|=(1<<ENABLE);
will result in
MYREG|=(1<<5);
which again result in
MYREG|=(0b00100000);
Now a beginner would ask "What's the Advantage ?". And once you know it you would realize that advantage is immense!
  1. Readability of code: MYREG|=(1<<ENABLE); gives a clue that we are enabling the peripheral while MYREG|=0b00100000; does not give any clue what it is doing, we have to go to data sheet and find out which bit actually ENABLEs the peripheral. While ENABLE=5 is already defined in header files by the developer of compiler by carefully studying the datasheets of device.
  2. Easier Portability: Suppose you use this code many times in your program (and your program is reasonably large and uses other register also) and you now want the same code to run on some other MCU model. The new MCU is of similar family but has slightly different bit scheme, say ENABLE is bit 2 instead of bit 5. Then you have to find all occurrence of MYREG|=(0b00100000); and change that to MYREG|=(0b00000100); But if you have used the other method then you simply need to inform the compiler (by its setting options) that you are going to use the other MCU and compiler will automatically get the definitions for the new device. And in this definition ENABLE=2 will already be defined by the compiler developer. So it will be lot easier.

Clearing a BIT in Register

For clearing a bit logical AND(symbol &) operator is used in place of logical OR (symbol |). The syntax is as follows
MYREG&=~(1<<ENABLE);


Fig.: How to clear (0) a bit in C language.


This will clear (i.e. set to value 0) a given bit (identified by name ENABLE) in a register called MYREG. This operation will not affect any other bits of register except ENABLE.
Let us see how it works with the help of following diagram.

Fig.: "Clearing a BIT" how it works?

So now you know how you can selectively clear any bit in any given register. If you want to clear more than one bit at a time you can write like this
//This will clear bits ENABLE,FAST_MODE and BUSY, leaving all other bits untouched
MYREG&=(~((1<<ENABLE)|(1<<FAST_MODE)|(1<<BUSY))); 
Similarly the syntax for setting(set to 1) multiple bits at a time is as follows
//This will set bits ENABLE,FAST_MODE and BUSY, leaving all other bits untouched
MYREG|=((1<<ENABLE)|(1<<FAST_MODE)|(1<<BUSY)); 

Testing The Status of a Bit.

Till now we were modifying the registers either setting or clearing bits. Now we will learn how can be know that a specific bit is 0 or 1. To Know if a bit is 0 or 1 we AND it with a AND MASK. Suppose if we want to check bit 5 of a register MYREG then the AND MASK would be 0b00100000. If we AND this value with the current value of MYREG then result will be non-zero only if the 5th bit in MYREG is '1' else the result will be '0'.
The syntax would be like this.



if(MYREG & (1<<ENABLE))
{
 //ENABLE is '1' in MYREG
 ...
}
else
{
 //ENABLE is '0' in MYREG
}
So now you know the basic operation on bits, they are widely used in firmware 
  programming and will help you understand other codes on my web site. And Please 
  don't forget to post your comment regarding any doubts, or reporting errors 
  in the above article, or simply to tell how you liked the stuff.