EmbDev.net

Forum: µC & Digital Electronics Problem with atmega -8


von Ajay R. (ajay_r)


Rate this post
useful
not useful
Hey..I am trying my hands on lcd 16x2 and somehow i got it to work.I'm 
trying to display an output on 16x2 which is dependent on sum input on 
other pins but cant get it to work.Here is my code..
1
 #define F_CPU 1000000UL
2
3
#include <avr/io.h>
4
#include <util/delay.h>
5
6
#define LCD_DATA PORTB                //LCD data port
7
#define ctrl PORTD
8
#define en PD2                         // enable signal
9
#define rw PD1                       // read/write signal
10
#define rs PD0                     // register select signal
11
12
13
14
int main()
15
{
16
DDRB=0xff;                                  // setting the port B
17
DDRD=0x07;                                // setting for port D
18
DDRC=0;
19
PORTC=0b0000000;
20
21
init_LCD();                                 // initialization of LCD
22
_delay_ms(50);                        // delay of 50 mili seconds
23
LCD_write_string("“hello world”");                      // function to print string on LCD
24
if(PINC==0b0000001){
25
_delay_ms(50);
26
LCD_cmd(0xc0);
27
_delay_ms(1);
28
LCD_write_string("THIS IS WORKING ");}
29
return 0;
30
}
31
32
void init_LCD(void)
33
{
34
LCD_cmd(0x38);                            // initialization of 16X2 LCD in 8bit mode
35
_delay_ms(1);
36
LCD_cmd(0x01);                                 // clear LCD
37
_delay_ms(1);
38
LCD_cmd(0x0E);                        // cursor ON
39
_delay_ms(1);
40
LCD_cmd(0x80);                     // —8 go to first line and –0 is for 0th position
41
_delay_ms(1);
42
return;
43
}
44
45
void LCD_cmd(unsigned char cmd)
46
{
47
LCD_DATA=cmd;
48
ctrl =(0<<rs)|(0<<rw)|(1<<en);
49
_delay_ms(1);
50
ctrl =(0<<rs)|(0<<rw)|(0<<en);
51
_delay_ms(50);
52
return;
53
}
54
55
void LCD_write(unsigned char data)
56
{
57
LCD_DATA= data;
58
ctrl = (1<<rs)|(0<<rw)|(1<<en);
59
_delay_ms(1);
60
ctrl = (1<<rs)|(0<<rw)|(0<<en);
61
_delay_ms(50);
62
return ;
63
}
64
65
void LCD_write_string(unsigned char *str)             //store address value of the string in pointer *str
66
{
67
int i=0;
68
while(str[i]!='\0')                               // loop will go on till the NULL character in the string
69
{
70
LCD_write(str[i]);                            // sending data on LCD byte by byte
71
i++;
72
}
73
return;
74
}
The first line ie HELLO WORLD is working fine but there is no display in 
the second line.

von Ralf (Guest)


Rate this post
useful
not useful
Ajay R. wrote:
> if(PINC==0b0000001){
> _delay_ms(50);
> LCD_cmd(0xc0);
> _delay_ms(1);
> LCD_write_string("THIS IS WORKING ");}

The program doesn't wait for PINC -> 1

von Ajay R. (ajay_r)


Rate this post
useful
not useful
Ralf wrote:
> Ajay R. wrote:
>> if(PINC==0b0000001){
>> _delay_ms(50);
>> LCD_cmd(0xc0);
>> _delay_ms(1);
>> LCD_write_string("THIS IS WORKING ");}
>
> The program doesn't wait for PINC -> 1

I didnt get can you elaborate a bit please.

von sepp1985 (Guest)


Rate this post
useful
not useful
1
#define F_CPU 1000000UL
2
3
#include <avr/io.h>
4
#include <util/delay.h>
5
6
#define LCD_DATA PORTB                //LCD data port
7
#define ctrl PORTD
8
#define en PD2                         // enable signal
9
#define rw PD1                       // read/write signal
10
#define rs PD0                     // register select signal
11
12
13
14
int main()
15
{
16
DDRB=0xff;                                  // setting the port B
17
DDRD=0x07;                                // setting for port D
18
DDRC=0;
19
PORTC=0b0000001;  //Sink mode??? 
20
21
init_LCD();                                 // initialization of LCD
22
_delay_ms(50);                        // delay of 50 mili seconds
23
LCD_write_string("“hello world”");                      // function to print string on LCD
24
25
while(1) //do it ever and ever and ever
26
{
27
if(PINC==0b0000001){
28
_delay_ms(50);
29
LCD_cmd(0xc0);
30
_delay_ms(1);
31
LCD_write_string("THIS IS WORKING ");}
32
}
33
return 0;
34
}
35
36
void init_LCD(void)
37
{
38
LCD_cmd(0x38);                            // initialization of 16X2 LCD in 8bit mode
39
_delay_ms(1);
40
LCD_cmd(0x01);                                 // clear LCD
41
_delay_ms(1);
42
LCD_cmd(0x0E);                        // cursor ON
43
_delay_ms(1);
44
LCD_cmd(0x80);                     // —8 go to first line and –0 is for 0th position
45
_delay_ms(1);
46
return;
47
}
48
49
void LCD_cmd(unsigned char cmd)
50
{
51
LCD_DATA=cmd;
52
ctrl =(0<<rs)|(0<<rw)|(1<<en);
53
_delay_ms(1);
54
ctrl =(0<<rs)|(0<<rw)|(0<<en);
55
_delay_ms(50);
56
return;
57
}
58
59
void LCD_write(unsigned char data)
60
{
61
LCD_DATA= data;
62
ctrl = (1<<rs)|(0<<rw)|(1<<en);
63
_delay_ms(1);
64
ctrl = (1<<rs)|(0<<rw)|(0<<en);
65
_delay_ms(50);
66
return ;
67
}
68
69
void LCD_write_string(unsigned char *str)             //store address value of the string in pointer *str
70
{
71
int i=0;
72
while(str[i]!='\0')                               // loop will go on till the NULL character in the string
73
{
74
LCD_write(str[i]);                            // sending data on LCD byte by byte
75
i++;
76
}
77
return;
78
}

: Edited by Admin
von Route_66 (Guest)


Rate this post
useful
not useful
Ralf wrote:
> The program doesn't wait for PINC -> 1

What is connected to PORT C, especialli at bit 0 of it?

von Route_66 (Guest)


Rate this post
useful
not useful
sorry, bit 1 of port C.

von B.A. (Guest)


Rate this post
useful
not useful
This linie is possiply wrong.
> if(PINC==0b0000001){

You want to do something if PC0 (Pin0 from Port C) is high?

try this:
if(PINC & 0b0000001){

von Route_66 (Guest)


Rate this post
useful
not useful
Route_66 wrote:
> sorry, bit 0 of port C.

von Ajay R. (ajay_r)


Rate this post
useful
not useful
Route_66 wrote:
> Ralf wrote:
>> The program doesn't wait for PINC -> 1
>
> What is connected to PORT C, especialli at bit 0 of it?

just a constant voltage source. I was trying to test thats why

von Ajay R. (ajay_r)


Rate this post
useful
not useful
I tried this code and it worked fine for a simple led
1
#define F_CPU 1000000UL
2
3
 #include <avr/io.h>
4
 #include <util/delay.h>
5
6
7
int main(void) {
8
9
DDRB=0;
10
DDRC=0b11111111;
11
PORTC=0b00000000;
12
PORTB=0b00000000;
13
if(PINB==0b00000001)
14
{PORTC=0b11111111;}
15
return 0;
16
}
but when i interchanged PORTC AND PORTB ie interchanged their function 
it didnt worked.So i guess there in something wrong with input pins at 
PORTC.By the way im using proteus for simulating

von Ralf (Guest)


Rate this post
useful
not useful
sepp1985 wrote:
> while(1) //do it ever and ever and ever
> {
> if(PINC==0b0000001){
> _delay_ms(50);
> LCD_cmd(0xc0);
> _delay_ms(1);
> LCD_write_string("THIS IS WORKING ");}
> }

thereabouts.

But what changes the bit? I think, you would better wait (e.g. if it is 
a button)

von Ralf (Guest)


Rate this post
useful
not useful
Ralf wrote:
> But what changes the bit?

Sorry, to slow.

von Ajay R. (ajay_r)


Rate this post
useful
not useful
Ralf wrote:
> sepp1985 wrote:
>> while(1) //do it ever and ever and ever
>> {
>> if(PINC==0b0000001){
>> _delay_ms(50);
>> LCD_cmd(0xc0);
>> _delay_ms(1);
>> LCD_write_string("THIS IS WORKING ");}
>> }
>
> thereabouts.
>
> But what changes the bit? I think, you would better wait (e.g. if it is
> a button)

I understand what you are saying but im not using a switch pinc is 
connected to vcc from the beginning no switch is there

von B.A. (Guest)


Rate this post
useful
not useful
Ajay R. wrote:
> I understand what you are saying but im not using a switch pinc is
> connected to vcc from the beginning no switch is there

>> if(PINC==0b0000001){
PC0 = high
PC1 ... PC7 = low

Do you have connect the other Pins (Pin 1 to Pin 7) from Port C to 
Ground?

von Ajay R. (ajay_r)


Rate this post
useful
not useful
B.A. wrote:
> Ajay R. wrote:
>> I understand what you are saying but im not using a switch pinc is
>> connected to vcc from the beginning no switch is there
>
>>> if(PINC==0b0000001){
> PC0 = high
> PC1 ... PC7 = low
>
> Do you have connect the other Pins (Pin 1 to Pin 7) from Port C to
> Ground?

Yes...Done that

von Ajay R. (ajay_r)


Rate this post
useful
not useful
TAKE A LOOK AT THIS

I tried this code and it worked fine for a simple led

#define F_CPU 1000000UL

 #include <avr/io.h>
 #include <util/delay.h>


int main(void) {

DDRB=0;
DDRC=0b11111111;
PORTC=0b00000000;
PORTB=0b00000000;
if(PINB==0b00000001)
{PORTC=0b11111111;}
return 0;
}

but when i interchanged PORTC AND PORTB ie interchanged their function
it didnt worked.So i guess there in something wrong with input pins at
PORTC.By the way im using proteus for simulating

von B.A. (Guest)


Rate this post
useful
not useful
Ajay R. wrote:
>> Do you have connect the other Pins (Pin 1 to Pin 7) from Port C to
>> Ground?
>
> Yes...Done that

Realy?

PC6 is the Reset-Pin of the ISP-Port ... this Pin must be high, if it is 
Low the AVR is in Programming-Mode.

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.