Sunday, June 3, 2012
CODE OF PIC 16F877A FOR MAKING DVM USING MICRO C PROGRAMMING IDE
NOTE: This is the programming code written for pic 16877 for making digital voltmeter.The IDE used for this program is micro c programming.we should also know that this code is written in C language whereas pic doesnot understand C,it only knows the Hex code so it must be first converted to hex code and then it is burned to the pic.
The design is performed in proteus
all the things within /.........../ are the comments.
/*************display in LCD using 8-bit, 2 line mode*******/
#include <htc.h>
#define data PORTB
#define RS RD0
#define EN RD1
float result,val;
int val1,val2;
__CONFIG(HS & WDTDIS & PWRTEN & BORDIS & LVPDIS);
void delay_ms(int n )
{
TMR1H=0xEC; // EC77H=60535d
TMR1L=0x77;
T1CKPS1=0;
T1CKPS0=0;
TMR1CS=0;
TMR1IF=0;
TMR1ON=1;
while(n>0)
{
while(!TMR1IF) { }
TMR1IF=0;
TMR1H=0xEC;
TMR1L=0x77;
n--;
}
}
void delay_100us(int n )
{
TMR1H=0xFE;
TMR1L=0x0C;
T1CKPS1=0;
T1CKPS0=0;
TMR1CS=0;
TMR1IF=0;
TMR1ON=1;
while(n>0)
{
while(!TMR1IF) { }
TMR1IF=0;
TMR1H=0xFE;
TMR1L=0x0C;
n--;
}
}
void LCD_Write(unsigned char value,int rs)
{
data = value;
RS = rs; //if rs=0; command is sent to LCD otherwise data to be displayed is sent
EN = 1;
delay_ms(1);
EN = 0;
}
void LCD_clear(void)
{
LCD_Write(0x01,0); //this clears LCD
}
void LCD_goto(unsigned char row,unsigned char column)
{
if(row==1){
LCD_Write(0x80+column,0); //
}
else if(row==2){
LCD_Write(0xC0+column,0);
}
}
void LCD_num(int n)
{
if(n<=9)
{
LCD_Write(48+n,1);
}
else if(n<=99)
{
LCD_Write((n/10)+48,1);
LCD_Write((n%10)+48,1);
}
else if(n<=999)
{
LCD_Write((n/100)+48,1);
LCD_Write(((n%10)/10)+48,1);
LCD_Write((n%10)%10+48,1);
}
}
void init_ADC()
{
PCFG3=0; PCFG2=0; PCFG2=0; PCFG0=0; //Configures all the pins of PORTA and PORTB as ANALOG pins.
/*For correct A/D conversions, the A/D conversion clock must be selected to ensure a minimum
TAD time of 1.6 ms as shown in parameter 130 of the “Electrical Specifications” section.*/
ADCS1=1; ADCS0=0; //this gives 32*Tosc=1.6 us
ADFM=1; //Left justified format viz 6 LSBs of ADRESL regsiter are read as ‘0’.
//Result will be held at ADRESH and ADRESL register
}
void initLCD(void)
{
//PORT configuration
TRISB = 0x00;
TRISD=0x00;
//////////////////
LCD_Write(0x30,0);
// data = 0x3; // 8 bit mode
EN=1; EN = 0;
LCD_Write(0x38,0);
delay_ms(1);
LCD_Write(0x0C,0); //dispaly on , no cursor blinking
delay_ms(1);
LCD_Write(0x01,0); // clear dispaly screen
delay_ms(1);
LCD_Write(0x06,0);
delay_ms(1);
LCD_clear();
LCD_goto(1,1);
LCD_goto(1,1); //1st row 1st column
LCD_Write('D',1);delay_ms(100); LCD_Write('I',1);delay_ms(100); LCD_Write('G',1);delay_ms(100); LCD_Write('I',1);delay_ms(100);
LCD_Write('T',1);delay_ms(100);LCD_Write('A',1);delay_ms(100);LCD_Write('L',1);delay_ms(100);LCD_Write(' ',1);delay_ms(100);
LCD_Write('V',1);delay_ms(100);LCD_Write(' ',1);delay_ms(100);LCD_Write('M',1);delay_ms(100);LCD_Write('E',1);delay_ms(100);
LCD_Write('T',1);delay_ms(100); LCD_Write('E',1);delay_ms(100); LCD_Write('R',1);
LCD_goto(2,1);
LCD_Write('D',1); LCD_Write('C',1); LCD_Write(' ',1); LCD_Write('V',1); LCD_Write('O',1); LCD_Write('L',1); LCD_Write('T',1); LCD_Write('=',1);
}
/************Analog to digital conversion will occur here**********/
void ADC()
{
ADON=1; //A/D converter module is powered up
/**********************channel one********************/
CHS2=0; CHS1=0; CHS0=0; // selecting pin A0 as input pin where analog signal will appear
delay_ms(3);
ADGO = 1; //ADGO WILL BE RESET BY SYSTEM AUTOMATICALLY ONCE ADC IS COMPLETE
while(ADGO);
result=(ADRESH<<8)+ADRESL;
LCD_goto(2,9);
if(result<511||result>512)
{
if(result>512.0)
{// LCD_goto(2,9);
LCD_Write(' ',1);
result=result-511.5;
}
else if(result<511.0)
{//LCD_goto(2,9);
LCD_Write('-',1); //displaying negative sign
result=result-511.5;
result=-(result);
}
result=result*30/511.5;
val1=(int)result;
LCD_num(val1);
LCD_Write('.',1); //displaying dot sign
val=result-val1;
val=val*1000;
val2=(int)(val);
LCD_num(val2);
}
else
{result=0;
LCD_Write(' ',1);
LCD_Write(' ',1);
result=result*30/511.5;
val1=(int)result;
LCD_num(val1);
LCD_Write('.',1); //displaying dot sign
val=result-val1;
val=val*1000;
val2=(int)(val);
LCD_num(val2);}
}
void main(void)
{ TRISB=0x00; //Configuring all pins of B port as output.
PORTB=0x00; //output at all pins will be low
TRISA=0xff; //Configuring all pins of A port as input
TRISD0=TRISD1=0;
RD0=RD1=0;
initLCD();
init_ADC();
while(1)
{
ADC(); //call ADC convert
}
}
Subscribe to:
Post Comments (Atom)
This is an informative and interesting post for I think it is very useful and knowledgeable. Thanks for sharing. . .
ReplyDeleteCar transportation in bangalore.