Toll-Free Customer Support 24/7

PIC Development Board

/*
* Project name:
PIC Development Board
* Copyright
(c) Researchdesignlab.com
* Test configuration:
MCU: PIC16F877A
Dev.Board: PIC
Oscillator: 20.0 MHz
Software: MPLAB IDE v8.92(HI-TECH_C)
*/

/*ADC*/


#include <htc.h>
#include<string.h>
#define _XTAL_FREQ 20000000

/*LCD code */
#define EN RD7
#define RS RD6
void LCD_Delay()
{
//int Count=1000;
// while(Count--);
__delay_ms(1);
}

 

void LCD_Cmd(unsigned char cmd)
{


PORTB=cmd;
RS=0;
EN=1;
LCD_Delay();
EN=0;
LCD_Delay();
}

void LCD_Init()
{
unsigned char cmd[5]={0X38,0X06,0X0F,0X01,0X80},Count;
for(Count=0;Count<5;Count++)
LCD_Cmd(cmd[Count]);
}

void LCD_SendDataByte(unsigned char data)
{
PORTB=data;
RS=1;
EN=1;
LCD_Delay();
EN=0;
LCD_Delay();
}
void LCD_Display( char *addr)
{
while(*addr)
{
LCD_SendDataByte(*addr);
addr++;
}
}


void ADC_Init()
{
ADCON0 = 0x41; //A/D control register0
ADCON1 = 0xC0; //A/D control register1
}

unsigned int ADC_Read(unsigned char channel)
{
if(channel > 7)
return 0;

ADCON0 &= 0xC5;
ADCON0 |= channel<<3;
__delay_ms(2);
GO_nDONE = 1;
while(GO_nDONE);
return ((ADRESH<<8)+ADRESL); //left shift the higherorder bits and add the lower order bits
}

void display(unsigned int number) //for (0-1024)A/D conversion
{
unsigned char digit1,digit2,digit3,digit4,digit[4];
unsigned char x;

unsigned char temp;

digit1 = number / 1000u ; // extract thousands digit

digit2 = (number / 100u) % 10u; // extract hundreds digit

digit3 = (number / 10u) % 10u; // extract tens digit

digit4 = number % 10u; // extract ones digit
digit[3]=digit4;
digit[2]=digit3;
digit[1]=digit2;
digit[0]=digit1;

for(x=0;x<4;x++)
{
temp=digit[x]|0x30; //convert to ACII

LCD_SendDataByte(temp);
}
}


void main()
{
unsigned int value;
unsigned int a;
TRISB = 0x00; //Set register as output
TRISC = 0x00; //Set register as outut
TRISD=0x00; //set register as output
LCD_Init(); //initialize the LCD
__delay_ms(1000); //delay
ADC_Init(); //ADC initialisation

do
{

a = ADC_Read(0); //read port (A0)
__delay_ms(2000); //delay
LCD_Cmd(0x80); //1st row
LCD_ display(a); //display the value on LCD
__delay_ms(1000); //provide delay
}while(1);
}

Back to top