#include<pic.h>
#include<htc.h>
#include<stdio.h>
#define __PIC12F675_H //header file for ic
__CONFIG(FOSC_INTRCIO & WDTE_OFF & PWRTE_ON & MCLRE_OFF & BOREN_ON & CP_OFF & CPD_OFF);
#define _XLAT_FREQ 4000000 //define cpu frequency
// Define Pins
#define LCD_E GP0 // Enable pin for LCD
#define LCD_CLK GP1 // Serial clock pin
#define LCD_D GP2 // Serial data pin
// Define Pins direction registrers
#define LCD_E_Dir TRISIO0
#define LCD_CLK_Dir TRISIO1
#define LCD_D_Dir TRISIO2
void delay(unsigned int i)
{
while(i--);
}
void ToggE(void)
{
LCD_E = 1; // Give a pulse on E pin
delay(500); // so that LCD can latch the
LCD_E = 0; // data from data bus
delay(500);
}
void WriteByteToLCD(unsigned Byte)
{
unsigned char BitCount = 0;
for(BitCount=0;BitCount<8;BitCount++)
{
LCD_D = (((Byte>>BitCount)&0x1)!=0); // Write bit value
LCD_CLK = 1; // Toggle Clock pin to transfer it
delay(500);
LCD_CLK = 0;
delay(500);
}
}
void cmd(unsigned char c)
{
WriteByteToLCD((c&0xF0)>>4); // Write Upper nibble
ToggE();
WriteByteToLCD(c&0x0F); // Write Lower nibble
ToggE();
}
void data(unsigned char c)
{
WriteByteToLCD(((c&0xF0)>>4)|0x80); // Write Upper nibble
ToggE();
WriteByteToLCD((c&0x0F)|0x80); // Write Lower nibble
ToggE();
}
void lcd_init(void)
{
cmd(0x02);
delay(100);
cmd(0x28);
delay(100);
cmd(0x01);
delay(100);
cmd(0x80);
delay(100);
cmd(0x0e);
delay(100);
}
void string(unsigned char *p)
{
while(*p!='\0')
{
data(*p);
p++;
}
}
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;
data(w+48);
}
}
int adc()
{
unsigned int x;
GO_nDONE=1;
while((ADCON0&0x02)==0x02);
x=(ADRESL+(ADRESH*255));
return x;
}
int main()
{
unsigned int a;
unsigned char z[10];
int i=0;
CMCON = 0x07; // Shut off the Comparator
VRCON = 0x00; // Shut off the Voltage Reference
TRISIO = 0x10; // GP3 input, rest all output
ADCON0=0x8d;
ANSEL=0x33;
lcd_init();
// string ("SUMMIT SHARMA");
//num(12345);
while(1)
{
a=adc();
delay(5);
sprintf(z,"adc=%d ",a);
cmd(0x80);
string(z);
}
}
No comments:
Post a Comment