EmbDev.net

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


Author: Ajay R. (ajay_r)
Posted on:

Rate this post
0 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..
 #define F_CPU 1000000UL

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

#define LCD_DATA PORTB                //LCD data port
#define ctrl PORTD
#define en PD2                         // enable signal
#define rw PD1                       // read/write signal
#define rs PD0                     // register select signal



int main()
{
DDRB=0xff;                                  // setting the port B
DDRD=0x07;                                // setting for port D
DDRC=0;
PORTC=0b0000000;

init_LCD();                                 // initialization of LCD
_delay_ms(50);                        // delay of 50 mili seconds
LCD_write_string("“hello world”");                      // function to print string on LCD
if(PINC==0b0000001){
_delay_ms(50);
LCD_cmd(0xc0);
_delay_ms(1);
LCD_write_string("THIS IS WORKING ");}
return 0;
}

void init_LCD(void)
{
LCD_cmd(0x38);                            // initialization of 16X2 LCD in 8bit mode
_delay_ms(1);
LCD_cmd(0x01);                                 // clear LCD
_delay_ms(1);
LCD_cmd(0x0E);                        // cursor ON
_delay_ms(1);
LCD_cmd(0x80);                     // —8 go to first line and –0 is for 0th position
_delay_ms(1);
return;
}

void LCD_cmd(unsigned char cmd)
{
LCD_DATA=cmd;
ctrl =(0<<rs)|(0<<rw)|(1<<en);
_delay_ms(1);
ctrl =(0<<rs)|(0<<rw)|(0<<en);
_delay_ms(50);
return;
}

void LCD_write(unsigned char data)
{
LCD_DATA= data;
ctrl = (1<<rs)|(0<<rw)|(1<<en);
_delay_ms(1);
ctrl = (1<<rs)|(0<<rw)|(0<<en);
_delay_ms(50);
return ;
}

void LCD_write_string(unsigned char *str)             //store address value of the string in pointer *str
{
int i=0;
while(str[i]!='\0')                               // loop will go on till the NULL character in the string
{
LCD_write(str[i]);                            // sending data on LCD byte by byte
i++;
}
return;
}
The first line ie HELLO WORLD is working fine but there is no display in 
the second line.

Author: Ralf (Guest)
Posted on:

Rate this post
0 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

Author: Ajay R. (ajay_r)
Posted on:

Rate this post
0 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.

Author: sepp1985 (Guest)
Posted on:

Rate this post
0 useful
not useful
#define F_CPU 1000000UL

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

#define LCD_DATA PORTB                //LCD data port
#define ctrl PORTD
#define en PD2                         // enable signal
#define rw PD1                       // read/write signal
#define rs PD0                     // register select signal



int main()
{
DDRB=0xff;                                  // setting the port B
DDRD=0x07;                                // setting for port D
DDRC=0;
PORTC=0b0000001;  //Sink mode??? 

init_LCD();                                 // initialization of LCD
_delay_ms(50);                        // delay of 50 mili seconds
LCD_write_string("“hello world”");                      // function to print string on LCD

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 ");}
}
return 0;
}

void init_LCD(void)
{
LCD_cmd(0x38);                            // initialization of 16X2 LCD in 8bit mode
_delay_ms(1);
LCD_cmd(0x01);                                 // clear LCD
_delay_ms(1);
LCD_cmd(0x0E);                        // cursor ON
_delay_ms(1);
LCD_cmd(0x80);                     // —8 go to first line and –0 is for 0th position
_delay_ms(1);
return;
}

void LCD_cmd(unsigned char cmd)
{
LCD_DATA=cmd;
ctrl =(0<<rs)|(0<<rw)|(1<<en);
_delay_ms(1);
ctrl =(0<<rs)|(0<<rw)|(0<<en);
_delay_ms(50);
return;
}

void LCD_write(unsigned char data)
{
LCD_DATA= data;
ctrl = (1<<rs)|(0<<rw)|(1<<en);
_delay_ms(1);
ctrl = (1<<rs)|(0<<rw)|(0<<en);
_delay_ms(50);
return ;
}

void LCD_write_string(unsigned char *str)             //store address value of the string in pointer *str
{
int i=0;
while(str[i]!='\0')                               // loop will go on till the NULL character in the string
{
LCD_write(str[i]);                            // sending data on LCD byte by byte
i++;
}
return;
}

: Edited by Admin
Author: Route_66 (Guest)
Posted on:

Rate this post
0 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?

Author: Route_66 (Guest)
Posted on:

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

Author: B.A. (Guest)
Posted on:

Rate this post
0 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){

Author: Route_66 (Guest)
Posted on:

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

Author: Ajay R. (ajay_r)
Posted on:

Rate this post
0 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

Author: Ajay R. (ajay_r)
Posted on:

Rate this post
0 useful
not useful
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

Author: Ralf (Guest)
Posted on:

Rate this post
0 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)

Author: Ralf (Guest)
Posted on:

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

Sorry, to slow.

Author: Ajay R. (ajay_r)
Posted on:

Rate this post
0 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

Author: B.A. (Guest)
Posted on:

Rate this post
0 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?

Author: Ajay R. (ajay_r)
Posted on:

Rate this post
0 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

Author: Ajay R. (ajay_r)
Posted on:

Rate this post
0 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

Author: B.A. (Guest)
Posted on:

Rate this post
0 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.

Reply

Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]




Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.