Quantcast
Channel: interfacing | Battery Guide
Viewing all 111 articles
Browse latest View live

HD44780 16×2 Char LCD Interfacing with microcontroller

$
0
0

Project Description:-

In this project we are going to learn various things about this chip set and displaying text on this LCD. The HD44780 16×2 char LCD screen Use 8bit and 4 bit parallel interface with backlight.

This Primary Objective in this project are:-

1.  Displaying  “Hello Word!! LCD ” message on the scree.
2.   Interfacing The LCD to the Microcontroller Using 8bit  Mode and 4 Bit Mode.
3.  Generating and Displaying Custom Char on the LCD Screen. clik here for custom char

Operation 

as i have mentioned  before this type of lcd are connected to microcontroller using parallel 8bit or 4bit lines.
using 8 bit method is quite simple but take 8 lines (for data or command)+ 3 control signal total 11 line , i guess few small microcontrollers don’t even have that much of I/O lines ,so in 4 bit mode total 7 lines (sometimes 6 ) are required .  in this tutorial i will show you with both of the methods .

HD44780 16x2 Char LCD Interfacing with microcontroller Pin description

PIN NUMBER SYMBOL FUNCTION
1 Vss GND
2 Vdd  + 3V or + 5V
3 Vo Contrast Adjustment
4  RS H/L Register Select Signal
5 R/W H/L Read/Write Signal
6 E  H → L Enable Signal
7  DB0 H/L Data Bus Line
8  DB1 H/L Data Bus Line
9  DB2 H/L Data Bus Line
10  DB3 H/L Data Bus Line
11  DB4 H/L Data Bus Line
12  DB5 H/L Data Bus Line
13  DB6 H/L Data Bus Line
14  DB7 H/L Data Bus Line
15 A/Vee + 3.5V for LED/Negative Voltage Output
16 K K Power Supply for B/L (OV)

in 8 bit mode all the Data line DB0 to DB7 are being used for transferring the the data to lcd but in 4-bit mode only 4 line form DB4 to DB7 are being used to transfer the  8 bit wide data in two peaces one after another .

we can’t display any data on the lcd until all the required internal command register of the lcd are not being properly initialized.
to know every thing about this lcd controller .. you can go through it’s data sheet
click here to download HD44780 data sheet
so now we will learn how to initialize the lcd.

LCD Commands

Clear Display

clear  and place the cursor in the first position (address 0). The bit I / D to 1 by default.

RS R / W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
0 0 0 0 0 0 0 0 0 1

Return the cursor to Home

Place the cursor in the home position (address 0) and make the display starts to move from its original position. The contents of the RAM display data (DD RAM) remains unchanged. The address of the RAM for display data (DD RAM) is set to 0.

RS R / W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
0 0 0 0 0 0 0 0 1 X

Entry Mode in Set

Set cursor moving direction and specify that the display moves to the next position of the screen or not. These operations are performed during reading or writing of the DD RAM or CG RAM. To view usually set bit S = 0.

RS R / W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
0 0 0 0 0 0 0 1 I / D S

Display ON / OFF Control

Turn on or off by turning ON / OFF both the LCD (D) as the cursor (C) and whether or not this last flash (B).

RS R / W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
0 0 0 0 0 0 1 D C B

Cursor or Display Shift

Move the cursor to move the LCD without changing the memory contents of the display data DD RAM.

RS R / W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
0 0 0 0 0 1 S / C R / L X X

HD44780 16x2 Char LCD Interfacing with microcontroller SchematicFunction Set

Set the size of interface with the data bus (DL), number of lines in the LCD (N) and character type (F).

RS R / W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
0 0 0 0 1 DL N F X X

Set the CG RAM Address

The LCD module defined in addition to all the ASCII character set allows the user to define 4 or 8 characters. The composition of these characters is saved to a CG RAM memory called up to 64 bytes. Each user defined character consists of 16 or 8 bytes that are stored in successive positions of the CG RAM.

Using this instruction sets the CG RAM memory address from which the bytes will be stored that define a character. Running this command all the data that is subsequently read or write this memory made from CG RAM.

 

For more detail: HD44780 16×2 Char LCD Interfacing with microcontroller 

The post HD44780 16×2 Char LCD Interfacing with microcontroller appeared first on PIC Microcontroller.


1. Serial interfacing LCD with Pic Microcontroller

$
0
0
Parallel interfacing LCD with MCU at least need 6 I/O pins (4 bit mode) and maximun can up to 
11 I/O pins (8 bit mode). The I/O pin can be cut down to 3 pin by serial iterfacing using shift 
register. They were few shift register can be used such as 74HC164, 74HC595, CD4094 and any 
compatible 8 bit shift register. Before you attempt to do serial interfacing, it is good 
pratice to familiar with parallel interfacing. You can find many reference from internet. 
Following diagram show the serial interfacing Hitachi compatible 2 X 16 LCD modules with 
Pic16F84 or Pic16F628 MCU.

The Hitachi compatible 2 X 16 LCD modules drive by Pic16F84 MCU show on figure 1. The Hitachi compatible 2 X 20 LCD modules (optrex) drive by Pic16F84 MCU show on figure 2. When using this optrex 2 X 20 LCD modules, the Vref (pin 3 at LCD) need around -4.5v to -5.5V supply. The source code/hex file for Pic16f84 download here: F84SLCD.zip. The source code/hex file for Pic16f628 download here: 628SLCD.zip.

1. Serial interfacing LCD with Pic MicrocontrollerMore LCD reference can find here:

  1. www.myke.com
  2. www.eio.com
  3. www.repairfaq.org
  4. www.iaehv.nl
  5. www.shellyinc.com

2. Software SPI interfacing ADC with Pic16F84/Pic16F628

Communication with the MCP3028 ADC chip is done using a simple serial interface compatible
with the SPI protocol. The Pic16f84 or Pic16f628 didn't had hardware SPI peripheral. However, 
software implemeted SPI protocol can be done to communicate with 12 bit MCP3028 ADC device. 
The example of this communication shown as following circuit diagram. The ADC result shown
on LCD with hex value (conversion from binary code). Press the push button (pin 6, RB0/INT)
to select the ADC channel (8 channel from CH0 to CH7). Then, connect the potention meter to
the channel and turn it. You will see the hex value change accordingly. The SPI communication 
protocol need 4 line for interfacing. Using software implemeted SPI protocol enable 
communication with more than one ADC device. 
The hex file for Pic16f84 download here: F84S_L.zip.
The hex file for Pic16f628 download here: 628S_L.zip.

3. Simple multi channel digital voltmeter

The circuit diagram same as above 12 bit ADC interfacing. The only different is the source code
which the binary code has been change to ASCII code with decimen number. This need some math 
fuction such as divide function and floating point. The math fuction for assembler code can 
find from Microchip Website www.microchip.com. You can also find some documen of math fuction 
from www.piclist.com or use C compiler such as Piclite for PIC16f8X/c8x series, cc5x c, 
pacific c and so on. The following hex file are use Microchip Math Library (assembler code) for 
this simple multi channel digital voltmeter device (using 16 bit for calculation). The maximum 
hex value for the ADC channel is FFF equal to decimel number 4095. This will display on the LCD 
as 5.11 volt.

Download hex file for Pic16F84 here: F84dec.zip.
Download hex file for Pic16F628 here: 628dec.zip.
*Tip on source code migration from Pic16f8X series to Pic16f62X series*
  1. If using PORTA pins as interfacing I/O, you need to define/set the I/O fuction for PIC16f628. Generally, the initial PORTA setting as following for I/O fuction: clrf PORTA movlw 0x07 ;turn comparators off and movwf CMCON ;enable pins for I/O functions Note: Ensure the above setting done at Bank 0. The above setting especially important if you want to migrate the source code from Pic16f8X series to Pic16f62X series.
  2. Always refer to data memory MAP (on Pic datasheet) to ensure that the general purpose register used not out of range when migrate one type of MCU to another type of MCU. As example, general purpose register for Pic16f84 was 0x00c to 0x04f (at bank 0). For Pic16f628 was 0x020 to 0x07f (at bank 0), 0x0a0 to 0x0ef (at bank 1) and 0x120 to 0x14f (at bank 2). It is better to define the address follow sequence.If out of range, you can re-define it easly.
  3. When Using bit 4 (RB4) of PORT B of Pic16F62X series as a output pin, ensure that the MCU not programmed at low voltage program mode (see Microchip datsheet) if not it will set as Schmitt Trigger input and independence of TRISB regeister I/O direction setting.

Serial interfacing LCD with Pic Microcontroller SchematicStepper motor controller

The stepper motor driver circuit shown as following
The opto-isolator are important which prevent destroy of MCU by the feeback voltage from 
power transistor. Two adjust-able voltage regulator used to adjust the running voltage and 
stopping/holding voltage of the stepper motor. The running voltage on by
the step pulse input which also signal to rotate the motor. The simple source
code using interrupt method to change the step sequence (one wave) download here:
F628_stepper for Pic16F628.
Simple mathematics algorithm able to drive more complicate step sequence (such as half step)
even for half_step 5phase stepper. 
Another stepper motor circuit for X-Y table project click here:X-Y table project

More stepper motor information refer to:

1)www.cs.uiowa.edu
2)www.ams2000.com

5). 24 segments LED display module

The numerical LED display module was widely used in display application. The most common numerical LED display module consists of either 8 segmented or 
16 segmented LED units. However, this type of display module only limited to display numerical character (0 to 9) and a few type of alphabet character. 
Most of alphabet word message displayed using alphanumerical LED modules with dot matrix arrangement.
The 24 segments LED display module designed in such a way that enable it to display  alphabet (A to Z) and numerical (0 to 9) character. 
This will able it to display most of word message using A to Z alphabet and numerical character when combine a few units of this module. 
The display controlling method also more simple than the dot matrix type of alphanumerical LED module.

 

For more detail: 1. Serial interfacing LCD with Pic Microcontroller

The post 1. Serial interfacing LCD with Pic Microcontroller appeared first on PIC Microcontroller.

Rotary Encoder Interfacing with PIC Mirocontroller

$
0
0
i am currently working with some power supply design and i can say using conventional pots(potentiometer)  and rotary  switch  to adjust the voltage and other stuff is quite old school.
so i have decided to go for a bit high tech , actually bit digital.
so here is the solution
Incremental Rotary Encoder
first of all i would like to tell you , these type of rotary encoder is totally digital component so you can’t directly replace these  with you conventional pots. so lets start what are Incremental Rotary Encoder ,
Rotary Encoder Interfacing with PIC Mirocontroller
and hear the output wave form

Decoding with Microcontroller 


in this examples we will be decoding the rotary encoder with the help of sate machine.

view of state machine in the wave form
Schematic with PIC18F458
Source code and firmware

CLIK Here To download Source Code and Firmware. if you have any problem please leave in the comment section.

The post Rotary Encoder Interfacing with PIC Mirocontroller appeared first on PIC Microcontroller.

Interfacing LCD with PIC Microcontroller – CCS C

$
0
0

In this tutorial we will see How to interface a 16×2 character LCD Module with PIC 16F877A Microcontroller using CCS C Compiler. 16×2 character LCD is a very commonly used LCD module in electronic projects and products. 16×2 means it can display 2 rows of 16 characters. It is a very basic and low cost module. Its other variants such as 16×1, 20×4 are available in the market. In these displays each character is displayed using 5×8 or 5×10 dot matrix. These LCDs commonly uses HD44780 compliant controllers for their operation.

Interface between a microcontroller and LCD can be 4-bit or 8-bit. The difference between 4-bit and 8-bit is how data are send to the LCD. To write an 8-bit character to the LCD module in 8-bit mode, ASCII data is send through the data lines DB0- DB7 and data strobe is given through the E line.

Interfacing LCD with PIC Microcontroller – CCS CBut 4-bit mode uses only 4 data lines. In this mode the 8-bit ASCII data is divided into 2 parts which are send sequentially through data lines DB4 – DB7 with its own data strobe through the E line. The idea of 4-bit communication is to save as much pins that used to interface with LCD. The 4-bit communication is a bit slower when compared to 8-bit. The speed difference is only minimal, as LCDs are slow speed devices the tiny speed difference between these two modes is not significant. Remember that our microcontrollers works in the speed of MHz range. Thus the 4-bit mode data transmission is most commonly used.

CCS C provides a built in library file, “lcd.c” for interfacing LCDs having HD44780 compliant controllers using 4-bit mode communication. Just include this file in your program and enjoy.

CCS C LCD Library

LCD Connections

For the proper functioning of LCD Library, you should define the connections of below 7 pins used for LCD interfacing in the program.

  • Enable – E or EN
  • Register Select – RS
  • Read / Write – RW
  • Data 4 – DB4 or D4
  • Data 5 – DB5 or D5
  • Data 6 – DB6 or D6
  • Data 7 – DB7 or D7

These must be defined before including the header file, it can be done in two ways as given below.

PORT Access Method

This method requires the entire 7 bit interface connected to same GPIO port. It should be defined before including the header file as shown below.

#define LCD_DATA_PORT getenv("SFR:PORTD")

This defines that the entire 7 bit interface is connected to PORTD of PIC Microcontroller.

PIN Access Method

In this method you can connect those 7 bits to any GPIO pins and it should be defined before including the header file as shown below.

//LCD Module Connections
#define LCD_RS_PIN PIN_D1
#define LCD_RW_PIN PIN_D2
#define LCD_ENABLE_PIN PIN_D3
#define LCD_DATA4 PIN_D4
#define LCD_DATA5 PIN_D5
#define LCD_DATA6 PIN_D6
#define LCD_DATA7 PIN_D7
//End LCD Module Connections

Important Functions

lcd_init()

This function must be called before any other lcd functions. It initializes the LCD module with above defined connections.
Interfacing LCD with PIC Microcontroller – CCS C Schematiclcd_putc(c)

This function will display c on the next cursor position of the LCD. You can print strings and characters using this function. You can also use following backslash character constants for sending different commands to LCD.

  • \\a – To set cursor to the upper left
  • \\f – To clear display and set cursor to upper left
  • \\n – To go to start of next line
  • \\b – To move back one position

lcd_gotoxy(x, y)

This function can be used to set cursor position of the LCD, upper left position is (1,1).

lcd_getc(x, y)

This function returns the character at the position (x, y) on the LCD.

lcd_cursor_on(int1 on)

This function can be used to turn the cursor on or off.
Example :

lcd_cursor_on(TRUE); //Turns ON the cursor
lcd_cursor_on(FALSE); //Turns OFF the cursor

Note : For more details you can read the library file “lcd.c” in the location C:/Program Files/PICC/Drivers/.

 

For more detail: Interfacing LCD with PIC Microcontroller – CCS C

The post Interfacing LCD with PIC Microcontroller – CCS C appeared first on PIC Microcontroller.

Interfacing xbee with PIC microcontroller using MikroC

$
0
0

In this tutorial, I will show how to interface an xbee module with PIC microcontroller.Here MikroC Pro for PIC compiler is used to write the code. Xbee modules communicates with host devices using serial communication.Most of the PIC microcontroller have UART module to support serial communication.

First of all we have to understand the two operating mode of xbee.

  • Transparent Mode
  • API Mode

Interfacing xbee with PIC microcontroller using MikroCInterfacing xbee in Transparent Mode

In transparent mode two xbee module makes just simple bridge of  serial communication.This is like just remove the wire of serial port and place two xbee module.In this mode if you send ‘A’ then another module will receive ‘A’.To send data write this code

void main() {
UART1_Init(9600);   // Initialize UART module at 9600 bps
Delay_ms(100);      // Wait for UART module to stabilize
UART1_Write(‘A’);
}
To receive this data on the other side write this code
char receive;
void main() {
UART1_Init(9600);  // Initialize UART module at 9600 bps
Delay_ms(100);   //Wait for UART module to stabilize
// If data is ready, read it:
if (UART1_Data_Ready() == 1)

{receive = UART1_Read();}}

Interfacing xbee in API Mode

API mode is main thing of a xbee module.You can make mesh networking,use of analog to digital pins(IO pins).In this mode all communications take place as structured data frame.Here some examples of data frame are shown:

AT Commands

The following image shows the API frame exchange that takes place at the UART when sending an AT command request to read or set a module parameter. The response can be disabled by setting the frame ID to 0 in the request.

Interfacing xbee with PIC microcontroller using MikroC schematicTransmitting and Receiving RF Data

The following image shows the API exchanges that take place at the UART when sending RF data to another device. The transmit status frame is always sent at the end of a data transmission unless the frame ID is set to 0 in the transmit request. If the packet cannot be delivered to the destination, the transmit status frame will indicate the cause of failure. The received data frame (0x90 or 0x91) is set by the AP command.you can get all the frames structures in datasheet.So we don’t proceed to explain each data frame.Our concern is to send these frames or receive these frames using PIC microcontroller.

Sending API Frames

First we will learn how to send data frames from one xbee modules to other.Suppose you have two xbee module.First configure one xbee module as coordinator say it has id 0013 A200 4000 CF30.Another module has id 0013 A200 4000 CF31 is configured as router.Both xbee module is connected with two PIC microcontroller (say two PIC16F73) via uart (Tx and Rx pin).

Now we will send some API frame frame coordinator to router xbee.Before sending API Frame it will be good practice to learn the use of DIGI API Frame Maker.This is an HTML page to build API frames.You can select your desired Frame and it will show you the structure of the frame with its functions.You can also put the desired values in various fields and it will build frame packet that you can use to write code.Another way to make data frame is to read xbee data sheet,each data frame is explained in data sheet.But I highly recommend you to use DIGI API Frame Maker as it is easy to use.

 

Transmit Request Frame

This frame is used to send any data to remote module.Suppose we want send “txdata” from coordinator to the router.All will be sent as hex.So we have to convert it in hex format.From DIGI API Frame Maker the data frame will be

7E 00 14 10 01 00 13 A2 00 40 00 CF 31 00 00 00 00 74 78 64 61 74 61 73

7E is the delimiter.Every frame contains this hex value first.

00 14 is Number of bytes between length and checksum fields.

Next byte 10 is API id of Transmit Request frame.

00 13 A2 00 40 00 CF 31 is ou router’s id which is destination address where data will be sent.

Next look at the 74 78 64 61 74 61 is desired data to be sent. This is hex format of ”txdata”.

Now we will write code to PIC which is connected with coordinator xbee to send this frame to router module.

void main() {

UART1_Init(9600);   // Initialize UART module at 9600 bps
Delay_ms(100);      // Wait for UART module to stabilize

UART1_Write(0x7E);

UART1_Write(0x00);

UART1_Write(0x14);

UART1_Write(0x10);

UART1_Write(0x01);

UART1_Write(0x00);

UART1_Write(0x13);

UART1_Write(0xA2);

UART1_Write(0x00);

UART1_Write(0x40);

UART1_Write(0x00);

UART1_Write(0xCF);

UART1_Write(0x31);

UART1_Write(0x00);

UART1_Write(0x00);

UART1_Write(0x00);

UART1_Write(0x00);

UART1_Write(0x74);

UART1_Write(0x78);

UART1_Write(0x64);

UART1_Write(0x61);

UART1_Write(0x74);

UART1_Write(0x61);

UART1_Write(0x73);

}

This code simply sends a data frame with data “txdata” from coordinator  to router.Now  router module will receive this frame as Receive Packet Frame(frame id 0x90).We will discuss later how to write code to receive this data in the PIC microcontroller  connected with the router module.

Remote AT Command

This frame is used to change any setting of an xbee module from a remote xbee module.Suppose you want to change the state of a digital IO pin(from high to low or vice versa) of your router module from your coordinator module.Here a data frame is shown to make the DIO4 pin to high.

7E 00 10 17 01 00 13 A2 00 40 00 CF 31 00 00 00 44 34 05 75

Look at 44 34 05.

44 34 means D4 which is defined AT command DIO4 configuration.

05 stands for output high.If you want to make it output low then it will be 04.

17 is the Remote AT Command Frame ID.

00 13 A2 00 40 00 CF 31 is the ID of our router module.

Now write the following code in PIC which connected to the coordinator module

void main() {

UART1_Init(9600);   // Initialize UART module at 9600 bps
Delay_ms(100);      // Wait for UART module to stabilize

UART1_Write(0x7E);

UART1_Write(0x00);

UART1_Write(0x10);

UART1_Write(0x17);

UART1_Write(0x01);

UART1_Write(0x00);

UART1_Write(0x13);

UART1_Write(0xA2);

UART1_Write(0x00);

UART1_Write(0x40);

UART1_Write(0x00);

UART1_Write(0xCF);

UART1_Write(0x31);

UART1_Write(0x00);

UART1_Write(0x00);

UART1_Write(0x00);

UART1_Write(0x44);

UART1_Write(0x34);

UART1_Write(0x05);

UART1_Write(0x75);

}

If you place a led in DIO4 pin of router module then it will glow.

Receiving API Frames

Let’s talk about how to this frames we already sent.Recall the data frame Transmit Request.When you send this frame to router from coordinator,after receiving this frame router module send a frame to PIC microcontroller via uart called Receive Packet Frame(ID 0x90).The structure of the received frame will be

7E 00 12 90 00 13 A2 00 40 00 CF 30 00 00 00 74 78 64 61 74 61 F5

Look at the frame.

90 is the frame id.

00 13 A2 00 40 00 CF 30 is sender address(Our Coordinator address)

74 78 64 61 74 61 is desired data which is received(recall “tadata”).

Now we write code to receive our data from the frame.

MikroC gives a built in function called

void UARTx_Read_Text(char *Output, char *Delimiter, char Attempts);
It reads characters received via UART until the delimiter sequence is detected. The read sequence is stored in the parameter output; delimiter sequence is stored in the parameter delimiter.For example:
UART1_Read_Text(output, “OK”, 10); // reads text until OK is found

Here our delimeter is ‘~’ sign.So we will write the code

// LCD module connections

sbit LCD_RS at RB4_bit;

sbit LCD_EN at RB5_bit;

sbit LCD_D4 at RB0_bit;

sbit LCD_D5 at RB1_bit;

sbit LCD_D6 at RB2_bit;

sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;

sbit LCD_EN_Direction at TRISB5_bit;

sbit LCD_D4_Direction at TRISB0_bit;

sbit LCD_D5_Direction at TRISB1_bit;

sbit LCD_D6_Direction at TRISB2_bit

sbit LCD_D7_Direction at TRISB3_bit;

// End LCD module connections

char output[30];

void main() {

Lcd_Init(); // Initialize LCD

UART1_Init(9600);   // Initialize UART module at 9600 bps
Delay_ms(100);      // Wait for UART module to stabilize

while(UART_Data_Ready() == 1)

{

UART1_Read_Text(output, “~”, 35); // reads text until OK is found

Lcd_Chr(1, 1, output[14]);

Lcd_Chr(1, 2, output[15]);

Lcd_Chr(1, 3, output[16]);

Lcd_Chr(1, 4, output[17]);

Lcd_Chr(1, 5, output[18]);

Lcd_Chr(1, 6, output[19]);

}

When the frame is received output[] array will be filled with the frame data.Our frame was

7E 00 12 90 00 13 A2 00 40 00 CF 30 00 00 00 74 78 64 61 74 61 F5

So output[] sequence will be

output[o]=0x00

output[1]=0x12

output[2]=0x90

output[3]=0x00

output[4]=0x13

output[5]=0xA2

output[6]=0x00

output[7]=0x40

output[8]=0x00

output[9]=0xCF

output[10]=0x30

output[11]=0x00

output[12]=0x00

output[13]=0x00

output[14]=0x74

output[15]=0x78

output[16]=0x64

output[17]=0x61

output[18]=0x74

output[19]=0x61

output[20]=0xF5

So we just need

output[14]=0x74

output[15]=0x78

output[16]=0x64

output[17]=0x61

output[18]=0x74

output[19]=0x61

Which is printed in LCD in the above code.

 

Source  : Interfacing xbee with PIC microcontroller using MikroC

The post Interfacing xbee with PIC microcontroller using MikroC appeared first on PIC Microcontroller.

Interfacing LM35 Temperature Sensor with PIC Microcontroller.

$
0
0

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

Interfacing LM35 Temperature Sensor with PIC Microcontroller schematicHardware for LM35 based thermometer.

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

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.

Interfacing LM35 Temperature Sensor with PIC Microcontroller

The supply for LM35 can be taken from the onboard extra power supply. See the image below.

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.

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

Fig.: LM35 Temperature Sensor Demo.

Fig.: LM35 Temperature Sensor Demo Hardware Setup.

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.

Video

Source : Interfacing LM35 Temperature Sensor with PIC Microcontroller.

The post Interfacing LM35 Temperature Sensor with PIC Microcontroller. appeared first on PIC Microcontroller.

Interfacing Temperature Sensor with Microchip PIC16F876A

$
0
0

The tutorial aims at providing the necessary information for interfacing an analog type temperature sensor with a Microchip PIC Microcontroller. PIC (Peripheral Interface Controllers) was introduced in 1985. The PIC16F876A has 8K of Flash Program Memory, 368 bytes of Data Memory (RAM) and many other attractive features. Some features are ADC, USART, and 14 Interrupts all in 28 PDIP Package.

mperature Sensor with Microchip PIC16F876AThe Analog temperature sensor used is LM35. It has a transfer function of 10mv/’c. The output of LM35 is analog voltage which varies with changes in temperature. This analog voltage is digitized using the On-Chip 10bit A/D Converter and the value is displayed on a 2×16 LCD.

It is possible to switch On/Off an external application based on temperature value.

The LCD is based on HD44780 controller. The programming has been done using the MikroC compiler from Mikroelektronika (www.mikroe.com). The demo version has a 2KB Hex Output limit, fortunately it is more than enough for our requirement.

Program

int t1,temp;

char *text[6];

void main()

{adcon1=14;

lcd_init(&portb);

lcd_out(1,1,”Temperature”);

lcd_out(2,8,”‘C”);

while(1)

{t1=adc_read(0);

//temp=0.245*t1;          // For TMP37 Sensor 20mv/’c

temp=0.245*t1*2;        // For Lm35 Sensor 10mv/’c

inttostr(temp,text);

lcd_cmd(lcd_cursor_off);

lcd_out(2,1,text);

delay_ms(100);}}

Interfacing Temperature Sensor with Microchip PIC16F876A SchematicThe program is self explanatory, however let me explain you the calculation done. In a while loop. The Input channel 0 is read and the digitized value is obtained. Now the smallest digitized value is equal to Vref/((2^10)-1). Internal Vref is 4096mV but we will consider 5000mV for the ease of calculation. Multiplying the value obtained above with the digitized value will give us the analog voltage. Since the transfer function of Lm35 is 10mV/’c, we can obtain the temperature.

 

For more detail: Interfacing Temperature Sensor with Microchip PIC16F876A

The post Interfacing Temperature Sensor with Microchip PIC16F876A appeared first on PIC Microcontroller.

Interfacing DC Motor with PIC Microcontroller using L293D

$
0
0

DC Motor and L293D

We can’t drive a DC Motor (depends) directly with a Microcontroller, as DC Motors requires high current and high voltage than a Microcontroller can handle. Microcontrollers usually operates at +5 or +3.3V supply and it I/O pin can provide only up to 25mA current. Commonly used DC Motors requires 12V supply and 300mA current, moreover interfacing DC Motors directly with Microcontrollers may affect the working of Microcontroller due to the Back EMF of the DC Motor. Thus it is clear that, it not a good idea to interface DC Motor directly with Microcontrollers.Interfacing DC Motor with PIC Microcontroller using L293D

The solution to above problems is to use H-bridge circuit.

It is a special circuit, by using the 4 switches we can control the direction of DC Motor. Depending upon our power requirements we can make our own H-bridge using Transistors/MOSFETs as switches. It is better to use ready made ICs, instead of making our own H-bridge.

L293D and L293 are two such ICs. These are dual H-bridge motor drivers, ie by using one IC we can control two DC Motors in both clock wise and counter clockwise directions. The L293D can provide bidirectional drive currents of up to 600-mA at voltages from 4.5 V to 36 V while L293 can provide up to 1A at same voltages. Both ICs are designed to drive inductive loads such as dc motors, bipolar stepping motors, relays and solenoids as well as other high-current or high-voltage loads in positive-supply applications. Interfacing DC Motor with PIC Microcontroller using L293D schematicAll inputs of these ICs are TTL compatible and output clamp diodes for inductive transient suppression are also provided internally. These diodes protect our circuit from the Back EMF of DC Motor.

In both ICs, drivers are enabled in pairs, with drivers 1 and 2 are enabled by a high input to 1,2EN and drivers 3 and 4 are enabled by a high input to 3,4EN. When drivers are enabled, their outputs will be active and in phase with their inputs. When drivers are disabled, their outputs will be off and will be in the high-impedance state.

 

For more detail: Interfacing DC Motor with PIC Microcontroller using L293D

The post Interfacing DC Motor with PIC Microcontroller using L293D appeared first on PIC Microcontroller.


Interfacing 7-Segment Display With PIC Microcontroller – MikroC

$
0
0

The 7-segment display is the earliest type of an electronic display that uses 7 LEDs bars arranged in a way that can be used show the numbers 0 – 9. (actually 8 segments if you count the decimal point, but the generic name adopted is 7-segment display.) These devices are commonly used in digital clocks, electronic meters, counters, signalling, and other equipment for displaying numeric only data. 

Interfacing 7-Segment Display With PIC Microcontroller - MikroCIt is not different from an LED in terms of interfacing, by turning the appropriate segments ON and OFF we can display easily the numbers 0 to 9 and optionally the decimal point (DP). 

The segments of the displays are normally referred to by letters ‘a’ to ‘g’.
Figures 2 and 3 show how a 7-segment display can display digits. 
In figure 2, all the segments (LEDs) are switched on to display the digit “8” with the decimal point. On the other hand, in figure 3, segments a, b, c, d and g are switched on to display the digit “3”.  any combination can be used to display any desired digit.
The segments can also be used to display some letters, but this is limited. For example, the letter “b” can be displayed by switching on the segments  c, d, e and f and the letter “F” by switching on segments a, e, f and g.

In figure 4, the anode pins of all the segments are connected together and this pin is usually connected to the power supply. Individual segments are turned ON by grounding the required segment pin through the microcontroller by sending a “0” to the pin output. 
In figure 5 as well as in figure 1, all the cathodes of all the segments are connected together and this pin is usually connected to ground. Individual segments are turned ON by applying voltage to the required segment pin through the microcontroller by sending a “1” to the pin output. 

A PIC can source or sink 25mA of current per Input/Output pin. When designing an LED circuit, we have to know the typical voltage drop as we have learnt from the Blinking an LED Connected to a PIC microcontroller article. 
As with standard LEDs, it is required to use current limiting resistors in each segment of the display to limit the current as shown in figure 1. 
Interfacing 7-Segment Display With PIC Microcontroller - MikroC SchematicThe easiest way to display a number on the 7-segment is to find a way to determine or look up the pattern corresponding to the digit to be displayed . This can be something like a table showing the numbers and the corresponding segment that should be turned ON or OFF to display something and the required number (this can be in decimal, hexadecimal or in binary format) to be sent to the port where the display is connected to in order to display a specific number.

 

For more detail: Interfacing 7-Segment Display With PIC Microcontroller – MikroC

The post Interfacing 7-Segment Display With PIC Microcontroller – MikroC appeared first on PIC Microcontroller.

Interfacing DC Motor with 8051 Microcontroller

$
0
0

When we talk about controlling the robot, the first thing comes into the mind is controlling DC motors. Interfacing DC motor to the microcontroller is very important concept in Robotic applications. By interfacing DC motor to the microcontroller, we can do many things like controlling the direction of the motor, controlling the speed of the …

Interfacing DC Motor with 8051 Microcontroller Continue Reading

The post Interfacing DC Motor with 8051 Microcontroller appeared first on PIC Microcontroller.

Know the Procedure of GSM Modem Interfacing with 8051

$
0
0

The acronym of the GSM modem is Global System for Mobile Communications, it is a wireless communication module, used to  make a computer or any processor to communicate over a network. This modem uses a variation of TDMA(Time Division Multiple Access) and is the most commonly used of the digital wireless telephony technologies like TDMA, …

Know the Procedure of GSM Modem Interfacing with 8051 Continue Reading

The post Know the Procedure of GSM Modem Interfacing with 8051 appeared first on PIC Microcontroller.

Viewing all 111 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>