Using LM35 and Adc on the same port.(ADC is not working) :(

Status
Not open for further replies.

hamza1992

Newbie level 2
Joined
Aug 5, 2013
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
29

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include<avr/io.h>
#include<avr/delay.h>
#include<inttypes.h>
#define rs PA0
#define rw PA1
#define en PA2
void lcd_init();
void ADC_init();
unsigned int ADC_read(unsigned char);
void dis_cmd(char);
void dis_data(char);
void lcdcmd(char);
void lcddata(char);
 
int main(void)
{
    DDRA=0xEF;
    ADC_init();
    unsigned char data0[]="Temperatue=";
    unsigned char data1,adc_val;
    lcd_init();
    LCD_print(data0);
    adc_val=ADC_read(3);
    data1=adc_val;
    LCD_print(data1);
    while(1);
}
void lcd_init()     // Function for initialize
{
    dis_cmd(0x02);  // To initialize LCD in 4-bit mode.
    dis_cmd(0x28);  // To initialize LCD in 2 lines, 5X7 dots in 4bit mode.
    dis_cmd(0x0F);  // Blinking Cursor
    dis_cmd(0x06);  // Entry mode
    dis_cmd(0x80);  // This command gives the location of first letter  ** 80 + 'x' ** shifts the display 'x' units right.
    _delay_ms(500);
}
void dis_cmd(char cmd_value)
{
    char cmd_value1;
    cmd_value1 = cmd_value & 0xF0;              // Mask lower nibble because PA4-PA7 pins are used (Extracting Upper Four bits)
    // Masking means clearing bits
    // Upper nibble is sent first and then the lower nibble
    lcdcmd(cmd_value1);                         // Send to LCD
    cmd_value1 = ((cmd_value<<4) & 0xF0);       // Shift 4-bit and mask (Extracting Upper Four Bits)
    lcdcmd(cmd_value1);                         // Send to LCD
}
void dis_data(char data_value)
{
    char data_value1;
    data_value1=data_value & 0xF0;
    lcddata(data_value1);
    data_value1=((data_value<<4)&0xF0);
    lcddata(data_value1);
}
void lcdcmd(char cmdout)
{
    //Commands on LCD is initialized by having a negative transition on the enable pin.
    // 1) RS=RW=0 & EN=1;
    // 2) RS=RW=0 & EN=0;
    PORTA=cmdout;
    PORTA&=~(1<<rs);
    PORTA&=~(1<<rw);
    PORTA|=(1<<en);
    _delay_ms(1);
    PORTA&=~(1<<en);
}
void lcddata(char dataout)
{
    //Letters on LCD is displayed by having a negative transition on the enable pin.
    // 1) RS=RW=1 & EN=1;
    // 2) RS=RW=1 & EN=0;
    PORTA=dataout;
    PORTA|=(1<<rs);
    PORTA&=~(1<<rw);
    PORTA|=(1<<en);
    _delay_ms(1);
    PORTA&=~(1<<en);
}
 
void ADC_init()
{
    ADMUX=(1<<REFS0);
    ADCSRA=(1<ADEN)|(1<<ADPS2)|(1<<ADPS0);
}
unsigned int ADC_read(unsigned char ch)
{
    ch= ch & 0b00000111;        // channel must be b/w 0 to 7
    ADMUX |= ch;                // selecting channel
    
    ADCSRA|=(1<<ADSC);              // start conversion
    while(!(ADCSRA & (1<<ADIF)));   // waiting for ADIF, conversion complete
    ADCSRA|=(1<<ADIF);              // clearing of ADIF, it is done by writing 1 to it
    return (ADC);
    
}
void LCD_print(char * str)
{
    unsigned char i=0;
    while(str[i]!=0)
    {
        dis_data(str[i]);
        i++;
    }
}






adc is not converting
i dont know the problem can anyone tell me what is the problem in my code.
 
Last edited by a moderator:

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…