Monday 19 October 2015

PIC16F877A ADC (POT HG VALUE ON LCD)


#include<pic.h>
#include<htc.h>
#include<stdio.h>
#define _PIC16F877A_H
__CONFIG(PWRTE_ON & FOSC_HS & LVP_OFF & WDTE_OFF);

void delay(unsigned int i)
{
while(i--);
}

void cmd(unsigned char c)
{
unsigned int l,h;
l=c &0X0F;
h=c>>4;


PORTD=h|0X40;
delay(20);
PORTD=h|0X00;

PORTD=l|0X40;
delay(20);
PORTD=l|0X00;
}

void dat (unsigned char c)
{
unsigned int l,h;
l=c &0X0F;
h=c>>4;

PORTD=h|0X50;
delay(20);
PORTD=h|0X10;

PORTD=l|0X50;
delay(20);
PORTD=l|0X10;
}


void lcd_init()
{
cmd(0x02);
delay(100);
cmd(0x28);
delay(100);
cmd(0x01);
delay(100);
cmd(0x80);
delay(100);
cmd(0x0e);
delay(100);
}


void num(unsigned int p)
{
unsigned int k,w,b=1;
k=p;
while(k>=10)
{
b=b*10;
k=k/10;
}
while(b>=1)
{
w=p/b;
p=p%b;
b=b/10;
dat(w+48);
}
}

void string(unsigned char *p)
{
while(*p!='\0')
{
dat(*p);
p++;
}
}


int adc()
{
unsigned int x;
GO=1;
while((ADCON0&0x04)==0x04);
x=(ADRESL+(ADRESH*255));
return x;
}
int main()

{
unsigned int a;
unsigned char z[40];
int i=0;
TRISD=0x00;
ADCON0=0xc1;
ADCON1=0Xc0;
lcd_init();
while(1)
{
a=adc();
sprintf(z,"adc=%-4d     ",a);
cmd(0x80);
i=0;
while(z[i]!='\0')
{
dat(z[i]);
i++;
}
}
}





No comments:

Post a Comment