Digital Voltmeter measure voltage between two point in circuit . Now we are going to make a digital voltmeter which can measure 50 volt maximum . We will use pic18f2550 microcontroller and it's ADC pin . Remember that an ADC channel of pic18f2550 can count from 0 to 1023 . Because it's ADC channel register is 10bits. As we like to measure 50 volt , we need to apply a simple technique . So we have to convert 50 volt to 5 volt and we get reading 1023 at adc channel for 50 volt .
For converting we will connect 68k ohm, 22k ohm and 10k ohm resistors in serial so that during applying 50 volt it makes 5 volt drop at 10k ohm resistor . It's the main theme .
ADC(Analog to Digital Converter ) :
We need a basic knowledge about ADC . Let's take a look at here :
ADCON1 Register
Basically ADC is like as voltage divider . According to voltage It produce output .
bit 5 : VCFG0: Voltage Reference Configuration bit (VREF- source) 1 = VREF- (AN2) 0 = VSS or 0 volt
bit 4 :VCFG0: Voltage Reference Configuration bit (VREF+ source) 1 = VREF+ (AN3) 0 = VDD or 5volt
We
will set VCFG0[bit 5]=0and VCFG0[bit4]=0 . So we will get highest
value 5volt[1023] and lowest value 0volt[0].The ADCON1 is a 10 bit
register that means 2 to the power 10 is
it's highest counting capacity and result is 1024 . So this register can
count from 0 to 1023 . When 0 volt , we get reading at RA0 pin 0 .When
5 volt , we get reading at RA0 pin 1023. It means 5volt equivalent to
1023 .
If 1023 reading value equal to 50 volt.
So 1 reading value equal to 50/1023 volt
So 'read_val' reading value equal to (50/1023)*read_val .
Source Code :
sbit LCD_RS at LATB7_bit;
sbit LCD_EN at LATB6_bit;
sbit LCD_D4 at LATB5_bit;
sbit LCD_D5 at LATB4_bit;
sbit LCD_D6 at LATB3_bit;
sbit LCD_D7 at LATB2_bit;
sbit LCD_RS_Direction at TRISB7_bit;
sbit LCD_EN_Direction at TRISB6_bit;
sbit LCD_D4_Direction at TRISB5_bit;
sbit LCD_D5_Direction at TRISB4_bit;
sbit LCD_D6_Direction at TRISB3_bit;
sbit LCD_D7_Direction at TRISB2_bit;
char volt[]=" ";
char mv[]="v";
void main() {
double source=0;
float calv=0;
float sum=0;
float mult=0;
ADCON1=0x0E; // Configure RA) pin as input
CMCON=7;
ADC_Init(); // Initialize ADC
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(2,15,mv);
while(1){
source=ADC_Read(0);
calv=(source*50.50)/1023;
floattostr(calv,volt);
Lcd_Out(1,1,"Volt Meter");
Lcd_Out(2,1,"V=");
Lcd_Out(2,4,volt);
}
}
This tutorial contains description of microcontroller based digital clock and DS1307. We know microcontroller is a computer and specific time required to complete each task. So we cannot get real time and that's why we need a real-time clock IC. We will use DS1307 IC in this tutorial.
Digital Clock [Real Time] Using DS1307 and PIC Microcontroller [step by step ]
DS1307 IC:
DS1307 is a real-time clock IC which basically works with low power and returnsBCD(Binary Coded Decimal) number. We will describe later about BCD.
Through this IC we can get year, month, date, hour, minute and second. The months, days, second can be incremented automatically and It can operate either in 24-hour format or 12-hour format with AM/ PM.DS1307 has built-in power sensing circuit which can backup the IC automatically during the failure of power supply.Crystal (1000KHz) is required to operate this IC and don't need any external capacitor connection.
DS1307
At this stage, we have got basic information about DS1307. In this tutorial, we need to use I2C communication with Microcontroller. Description of I2C communication is given below.
I2C Communication with Microcontroller using MikroC
The I2C(I Squared C) communication means " Inter-Integrated Circuit " communication. A microcontroller can communicate with low power peripherals through I2C.To make a successful I2C communication we just need two ends or pins[ SCL and SDA ].Here SCL for clock selection and SDA for data. Both SCL and SDA are Open Drain drives that means the chip can drive it's low output and cannot drive the high output. It 's required to connect two pull up resistors in such way that one end of both resistors with +5v and others end with SCL, SDA respectively so that the higher output can be driven. The pull-up resistors value will be according to the I2C bus frequency.We are using 2k Ohm pull up resistor in this Tutorial.
MikroC Library for I2C :
void I2C1_Init(const unsigned long clock); this function is needed to initialize and returns nothing .We have to provide frequency as it's arguments . Example :I2C1_Init(100000);
unsigned short I2C1_Start(void) ; this function is needed to start communication . Example: I2C1_Start();
void I2C1_Repeated_Start(void); for starting again .
unsigned short I2C1_Is_Idle(void);
Returns 1 if I²C bus is free, otherwise returns 0.
unsigned short I2C1_Rd(unsigned short ack); Returns one byte from the slave.
unsigned short I2C1_Wr(unsigned short data_); Sends data byte (parameter data) via I²C bus.
void I2C1_Stop(void);To stop the signal .
The Time Keeper Register of DS1307:
The Time Keeper Register of DS1307
Here we can see the address of each function ,data bits and data range .First we have to initialize the I2C using 10kHz frequency and start the communication using start function .Now if we like to write , it's required to send out 0xD0 [D0=0x68+0]through write function so that the IC can be enabled for writing to us.Instructions given below :
I2C1_Wr(0xD0); [D0=0x68+0]
I2C1_Wr(address);
During reading the data comes from DS1307 , we need to send out 0xD1 [D1=0x68+1]through write functiono that the IC can be enabled for reading to us .Instructions given in below :
I2C1_Wr(0xD1);[D1=0x68+1]
data = I2C1_Rd(0);
Here in I2C1_Rd(0); To read data we will use Readdata(adrs) function and it is a user defined function. In "Readdata(0);" function we will write 0 as argument and the 0 means address of Seconds Register.This function returns short type data in BCD . I this way we can read data from each address .An important thing needed to notice that bit 6 of hour register is defined as the 24-hour or 12-hour mode selection bit.If we select 1 the bit 5 of hour used for representing AM/PM and if we select 0 the bit 5 used for representing hour .
BCD Number:
BCD number means Binary Coded Decimal number where each binary number is in 4bit .If we consider a decimal number 21 ,the BCD conversion will be 00100001 .Here blue represents the 2 and red represents 1. In that way we get BCD .Now the question is how can we get character from BCD ?Cause if we like to show in LCD , we need character .Look at the code given below :
First Char from BCD
char firstcharofbcd(char bcddata)
{
char tireturn;
tireturn= (bcddata >> 4) +0x30; Here we are shifting 4 bits to right hand side .
return tireturn;
}
If we consider 21 we can get 2 in character format .Because in short format data we added 0x30 , which is character '0' .So the sum of '0'+0010 = '2' .
Second Char from BCD
char secondcharofbcd(char bcddata)
{
char toret;
toret=(bcddata & 0x0F) + 0x30;
return toret;
} .
0x0F means '00001111' .We know in an AND gate if any input is low or 0 and the gate output is 0.If all input is 1 and we can get output 1.So by applying AND operation between 0x0F and BCD , we can make first 0000 bit as 0 .Look at the example given below :
00100001
00001111 &
-------------
00000001= 1
I think we get all required basic information .Now lets create a project in Proteus .
Proteus Project :
Please follow the instructions given below :
Pick all the required parts and Complete the circuit as given below :
Note : In practical , you have to connect 12MHz crystal betweenpin9 and pin10 . Also remember that1 & 20will be connected withVDDand8 & 19 will be connected withGND .
Digital Clock [Real Time] Using DS1307 and PIC Microcontroller [step by step ]
Now we need the .hex file .So lets Create a project in MikroC .Please follow the steps given below :