Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 sbit LCD_RS at RC2_bit; sbit LCD_EN at RC3_bit; sbit LCD_D4 at RC7_bit; sbit LCD_D5 at RC6_bit; sbit LCD_D6 at RC5_bit; sbit LCD_D7 at RC4_bit; sbit LCD_RS_Direction at TRISC2_bit; sbit LCD_EN_Direction at TRISC3_bit; sbit LCD_D4_Direction at TRISC7_bit; sbit LCD_D5_Direction at TRISC6_bit; sbit LCD_D6_Direction at TRISC5_bit; sbit LCD_D7_Direction at TRISC4_bit; // End LCD module connections void read_adc(void); void custchar(char x, char y); unsigned int result; float n; int temp,rs; char txt[7]; const char character[]={4,10,4,0,0,0,0,0}; //degree symbol void main(void) { TRISA.RA0=1; ADCON0=0b10000000; // select Fosc/2 ADCON1=0b11000000; // select left justify result. A/D port configuration 0 ADCON0.ADON=1; // turn on the A2D conversion module Delay_ms(300); LCD_init(); Lcd_Cmd(_LCD_CLEAR); // Clear LCD display Lcd_Cmd(_LCD_CURSOR_OFF); // Turn cursor off while(1){ Delay_ms(30); read_adc(); n=(result*4.883); n+=0.05; temp=n; n=(n-temp)*10; rs=n; InttoStr(temp,txt); Lcd_Out(2,3,txt); Lcd_chr(2,10,'.'); Lcd_chr(2,11,rs+48); custchar(2,12); Lcd_chr(2,13,'C'); Delay_ms(30); } } void read_adc(void) { unsigned short i; unsigned long result_temp=0; for(i=2000;i>0;i-=1) //looping 2000 times for getting average value { ADCON0.GO = 1; //ADGO is the bit 2 of the ADCON0 register while(ADCON0.GO==1); //ADC start, ADGO=0 after finish ADC progress result=ADRESH; result=result<<8; //shift to left for 8 bit result=result|ADRESL; //10 bit result from ADC result_temp+=result; } result = result_temp/2000; //getting average value } void custchar(char x, char y){ char i; Lcd_Cmd(64); for(i=0;i<=7;i++) Lcd_Chr_CP(character[i]); Lcd_Cmd(_LCD_RETURN_HOME); LCD_chr(x,y,0); }
ADCON0 = 0x91;
//Fosc/64 is Selected
//Channel-1 is Selected i.e RA1
//Analog-to-Digital Converter Module is Powered Up
ADCON1 = 0xC1;
HI,
i am currently trying to display a lm35 reading on my LCD. i am having problems using the ADC. I need to know what i should declare ADCON1 and ADCON0 and what ever i need to put in. i am bring in the volatage from the LM35 on RA1 and my Vref is on RA3 . I am using a pic16f877a TQFP. please help i am really confused
#include<htc.h>
#include<string.h>
#include<stdio.h>
#define _XTAL_FREQ 20000000
#define LCD_DATA PORTB
#define RS PORTCbits.RC0
#define RW PORTCbits.RC1
#define EN PORTCbits.RC2
void lcdcmd(unsigned char value);
void lcddata(unsigned char value);
void lcdstr(unsigned char msg[]);
void adc_display(unsigned int adc_data);
void main()
{
unsigned int adc_data,i;
TRISB = 0x00;
TRISC = 0x00;
TRISAbits.TRISA0 = 1; //Channel A0 as Input Port
EN = 0;
lcdcmd(0x38);
__delay_ms(10);
lcdcmd(0x0E);
__delay_ms(10);
lcdcmd(0x01);
__delay_ms(10);
lcdcmd(0x06);
__delay_ms(10);
lcdcmd(0x83);
lcdstr("PIC16F877A");
__delay_ms(100);
lcdcmd(0xC5);
lcdstr("Temp. Unit");
lcdcmd(0x01);
lcdcmd(0x83);
lcdstr("booting.....");
__delay_ms(100);
i = 0;
do
{
lcdcmd(0xC0+i);
lcddata(0xFF);
__delay_ms(100);
if(i>10)
{
__delay_ms(300);
}
i++;
}while(i<16);
lcdcmd(0x01);
ADCON0 = 0x81;
//Fosc/64 is Selected
//Channel-0 is Selected
//Analog-to-Digital Converter Module is Powered Up
ADCON1 = 0xCE;
//A/D Result Format Select Bit Right Justified
//and AN0 Channel as Analog Channel
while(1)
{
//__delay_ms(100);
ADCON0bits.GO = 1; //Start Conversion
while(ADCON0bits.GO == 1); //Wait Here Until Conversion Gets Over
//adc_data_l = ADRESL & 0x00FF;
adc_data = ADRESH & 0x00FF;
adc_data = adc_data<<8;
adc_data = adc_data | ADRESL;
adc_display(adc_data/2);
}
}
void lcdcmd(unsigned char value)
{
LCD_DATA = value;
RS = 0;
RW = 0;
EN = 1;
__delay_ms(10);
EN = 0;
}
void lcddata(unsigned char value)
{
LCD_DATA = value;
RS = 1;
RW = 0;
EN = 1;
__delay_ms(10);
EN = 0;
}
void lcdstr(unsigned char msg[])
{
unsigned int i,len;
len = strlen(msg);
for(i=0;i<len;i++)
{
lcddata(msg[i]);
__delay_ms(100);
}
}
void adc_display(unsigned int adc_data)
{
int hundreds, tens, ones;
unsigned char buffer[16];
ones = adc_data%10;
adc_data = adc_data/10;
tens = adc_data%10;
hundreds = adc_data/10;
sprintf(buffer," %d%d%d",hundreds,tens,ones);
lcdcmd(0x01);
lcdstr("Temperature");
lcdcmd(0xC0);
lcdstr(buffer);
}
Hi, Thanks everyone for your help But Can You Also Tell Me What Header Files I Need To Use For This??
#include<htc.h>
#include<string.h>
#include<stdio.h>
#define _XTAL_FREQ 8000000
#define LCD_DATA PORTD
#define RS PORTDbits.RD4
#define RW PORTDbits.RD6
#define EN PORTDbits.RD5
void lcdcmd(unsigned char value);
void lcddata(unsigned char value);
void lcdstr(unsigned char msg[]);
void adc_display(unsigned int adc_data);
void main()
{
unsigned int adc_data,i;
TRISD = 0x00;
TRISAbits.TRISA1 = 1; //Channel A1 as Input Port
EN = 0;
lcdcmd(0x38);
__delay_ms(10);
lcdcmd(0x0E);
__delay_ms(10);
lcdcmd(0x01);
__delay_ms(10);
lcdcmd(0x06);
__delay_ms(10);
lcdcmd(0x83);
lcdstr("PIC16F877A");
__delay_ms(100);
lcdcmd(0xC5);
lcdstr("Temp. Unit");
lcdcmd(0x01);
lcdcmd(0x83);
lcdstr("booting.....");
__delay_ms(100);
i = 0;
do
{
lcdcmd(0xC0+i);
lcddata(0xFF);
__delay_ms(100);
if(i>10)
{
__delay_ms(300);
}
i++;
}while(i<16);
lcdcmd(0x01);
ADCON0 = 0x48;
//Fosc/8 is Selected
//Channel-1 is Selected
//Analog-to-Digital Converter Module is Powered Up
ADCON1 = 0x81;
//A/D Result Format Select Bit Right Justified
//and AN0 Channel as Analog Channel
while(1)
{
//__delay_ms(100);
ADCON0bits.GO = 1; //Start Conversion
while(ADCON0bits.GO == 1); //Wait Here Until Conversion Gets Over
//adc_data_l = ADRESL & 0x00FF;
adc_data = ADRESH & 0x00FF;
adc_data = adc_data<<8;
adc_data = adc_data | ADRESL;
adc_display(adc_data/2);
}
}
void lcdcmd(unsigned char value)
{
LCD_DATA = value;
RS = 0;
RW = 0;
EN = 1;
__delay_ms(10);
EN = 0;
}
void lcddata(unsigned char value)
{
LCD_DATA = value;
RS = 1;
RW = 0;
EN = 1;
__delay_ms(10);
EN = 0;
}
void lcdstr(unsigned char msg[])
{
unsigned int i,len;
len = strlen(msg);
for(i=0;i<len;i++)
{
lcddata(msg[i]);
__delay_ms(100);
}
}
void adc_display(unsigned int adc_data)
{
int hundreds, tens, ones;
unsigned char buffer[16];
ones = adc_data%10;
adc_data = adc_data/10;
tens = adc_data%10;
hundreds = adc_data/10;
sprintf(buffer," %d%d%d",hundreds,tens,ones);
lcdcmd(0x01);
lcdstr("Temperature");
lcdcmd(0xC0);
lcdstr(buffer);
}
#define LCD_DATA PORTD
#define RS PORTDbits.RD4
#define RW PORTDbits.RD6
#define EN PORTDbits.RD5
Code C - [expand] 1 2 3 4 5 6 7 void adc_display(unsigned int adc_data); void main() { unsigned int adc_data,i; -------------------- -------------------------- }
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 while(1) { //__delay_ms(100); adc_data=0; //<------------------------- ADCON0bits.GO = 1; //Start Conversion while(ADCON0bits.GO == 1); //Wait Here Until Conversion Gets Over //adc_data_l = ADRESL & 0x00FF; adc_data = ADRESH & 0x00FF; adc_data = adc_data<<8; adc_data = adc_data | ADRESL; adc_display(adc_data/2); }
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 main() { unsigned int adc_data,i; //add delay of 250mS needed for LCD and ADC check datasheet for exact time. 250 for safe side checking //Place LCD initialization codes adc_data=123; adc_display(adc_data); while(1) { } }
//Last Modified: 11 January 2012
#define _XTAL_FREQ 8000000
int read_a2d(unsigned char channel);
int read_a2d(unsigned char channel)
{
ADCON0=0x48; //Turn on A/D module
ADCON1=0x81; //Right Justify
if(channel<=7) ADRESL=ADRESL|(0b00000001<<channel);
else ADRESH=ADRESH|(0b00000001<<(channel-8));
ADCON0=(ADCON0&0xC3)|(channel<<2); //select analog input channel
__delay_ms(2);
GO=1; //initiate conversion on the selected channel
while(GO==1) continue; //wait until conversion done
return(256*ADRESH+ADRESL);
}
Hi guys please look at this code i have to read_adc
My results keep resulting in 0 so am guessing the adc isn't reading from the right port.
I am using AN1 for the analogue input for the LM35 and also using a Vref at RA3 . i am using a 8MHZ crystal on a pic16f877a
Please if you have experience with this can you check my code and see if it is correct?
i have a constant voltage connected to RA3 on the micro controller of 2.5V this is used for Vref to give a good accuracy for the ADC.
This is so confusing, i hope i solve this i have 2 more weeks left for this project.
Help
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?