ATMega16l and Nokia5110 LCD issue

Status
Not open for further replies.

darkestdream

New Member
I made a simple program wich has to turn all the display black and...it doesn't work. The LCD seems dead. Just the backlight is on. Here's the code

Code:
#include <avr/io.h>
#define F_CPU 40000UL  //4 MHz
#include <math.h>
#include <avr/interrupt.h>
#include <avr/power.h>
#include <avr/sleep.h>


typedef struct
{
   unsigned int bit0:1;
   unsigned int bit1:1;
   unsigned int bit2:1;
   unsigned int bit3:1;
   unsigned int bit4:1;
   unsigned int bit5:1;
   unsigned int bit6:1;
   unsigned int bit7:1;
} _io_reg;

#define REGISTER_BIT(rg,bt)((volatile _io_reg*)&rg)->bit##bt


#define LCD_RST REGISTER_BIT(PORTB,0)
#define LCD_DC REGISTER_BIT(PORTB,1)
#define SS_LCD REGISTER_BIT(PORTB,2)



int main(void)
{
   ioinit();
   spiinit();
   lcdinit();
   lcdclear();
   lcdfill();

  
   while(1)
   {

     
   }
}

void sleep30ms()
{
  
   OCR1A=120;
   TCCR1B|=(1<<CS12)|(0<<CS11)|(1<<CS10)|(1<<WGM12);


   while(OCF1A==0)
   {
  
   }

   TCCR1B &=~(1<<CS12);
   TCCR1B &=~(1<<CS10);
   TCNT1=0;

}


void ioinit()
{
   DDRB|=(1<<0)|(1<<1)|(1<<2);
}

void spiinit()
{
         //SCK   MOSI   !SS
   DDRB |= (1<<7)|(1<<5)|(1<<4);
      //enable-master-fclk/128
   SPCR =0x53;


}

void lcdinit()
{
   LCD_RST=0;
   sleep30ms();
   LCD_RST=1;
   lcdcommand(0x21);   //extend command
   lcdcommand(0x0B);   //contrast
   lcdcommand(0x04);   //temp coeff
   lcdcommand(0x14);   //bias mode 1:48
   lcdcommand(0x0C);   //lcd in normal mode
   lcdcommand(0x20);   //normal mode
   lcdcommand(0x0C);   //normal display
}

void lcdcommand(char data)
{
   LCD_DC=0;
   SS_LCD=0;
   spiout(data);
   SS_LCD=1;
}

void lcddata(char data)
{
   LCD_DC=1;
   SS_LCD=0;
   spiout(data);
   SS_LCD=1;
}

void spiout(char data)
{
   SPDR=data;
   while(!(SPSR & (1<<SPIF)))
   ;

}

void lcdclear()
{
   unsigned int i;

   for(i = 0; i < 504; i++)
   {
      lcddata(0x00);
   }
}

void lcdfill()
{
   unsigned int i;

   for(i = 0; i < 504; i++)
   {
      lcddata(0xff);
   }
}
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…