Operácie

Lcd.c

Zo stránky SensorWiki

#include "lcd.h"


// **************************************************************************
// *************************** PUBLIC FUNCTIONS *****************************
// **************************************************************************

void lcdInit4(void)
{ 	
    // Following two lines are a must when using BOOTLOADER:

    //UCSR0B = 0x00;                          // Disable RxD and TxD for UART0
                                            // Disable ALL interrupts (Global)
    SREG = SREG & 0x7F;                     // same as cli(); without Interrupts.h

                                            // initialize LCD control & data
										    // lines to output
   	PORTC = 0b00000000;						// pull-up on unused input
	DDRC  = 0b11111111;						// set ctrl & data as outputs
	PORTB = 0b00000000;						// pull-up on unused input
	DDRB  = 0b11111111;	


	_delay_ms(50);        					// -  wait 15ms or more
											// -------------------
	CLEARBIT(PORTB, LCD_CTRL_RS);			// set RS to "control"
	CLEARBIT(PORTB, LCD_CTRL_RW);			// set R/W to "write"
											// 4 bit write
	SETBIT(PORTB, LCD_CTRL_E);			    // set "E" line
	PORTC = (PORTC | 0x03) & 0xF3;          // output data, high 4 bits
	LCD_DELAY;							    // wait
	CLEARBIT(PORTB, LCD_CTRL_E);			// clear "E" line
											// -------------------
	_delay_ms(5); 				            // -  wait 4.1ms
	SETBIT(PORTB, LCD_CTRL_E);	 	        // set "E" line
	LCD_DELAY;								// wait
	CLEARBIT(PORTB, LCD_CTRL_E);			// clear "E" line
											// -------------------
    _delay_ms(1);	                        // -  wait 0.1ms
	SETBIT(PORTB, LCD_CTRL_E);			    // set "E" line
	LCD_DELAY;						        // wait
	CLEARBIT(PORTB, LCD_CTRL_E);			// clear "E" line
											// -------------------
    _delay_ms(1);	                        // -  wait 0.1ms
    										// -------------------
	PORTC = (PORTC | 0x02) & 0xF2;	        // output data, high 4 bits
	SETBIT(PORTB, LCD_CTRL_E);		        // set "E" line
	LCD_DELAY;								// wait
	CLEARBIT(PORTB, LCD_CTRL_E);		    // clear "E" line
											// -------------------
	lcdControlWrite(0x28);					// 0x28	- function set (2 rows, 5x8 font)
     _delay_ms(50);
	lcdControlWrite(0x0C);					// 0x0C - display ON, cursor OFF
	lcdControlWrite(0x01);					// 0x01 - clear display
	lcdControlWrite(0x06); 					// 0x06 - shift right

}


// ************************************************************
// ********************** LOCAL FUNCTIONS *********************
// ************************************************************


void lcdControlWrite(unsigned char c_data)  // write the CONTROL byte to the display controller
{
	while(lcdBusy()) /* wait here */ ;		// wait until LCD not busy  or timeout

	CLEARBIT(PORTB, LCD_CTRL_RS);		    // set RS to "control"
	CLEARBIT(PORTB, LCD_CTRL_RW);	    	// set R/W to "write"

	PORTC = (PORTC&0xF0)|(c_data>>4);	    // output data, high 4 bits
	SETBIT(PORTB, LCD_CTRL_E);		        // set "E" line
	LCD_DELAY;								// wait
	CLEARBIT(PORTB, LCD_CTRL_E);	        // clear "E" line

	PORTC = (PORTC&0xF0) | (c_data&0x0F);	    // output data, low 4 bits
	SETBIT(PORTB, LCD_CTRL_E);		        // set "E" line
	LCD_DELAY;								// wait
	CLEARBIT(PORTB, LCD_CTRL_E);	        // clear "E" line
}


void lcdDataWrite(unsigned char w_data)     // write a DATA byte to the display
{
	while(lcdBusy())	 /* wait */ ;		// wait until LCD not busy or timeout

	SETBIT(PORTB, LCD_CTRL_RS);		        // set RS to "data"
	CLEARBIT(PORTB, LCD_CTRL_RW);		    // set R/W to "write"

	PORTC = (PORTC&0xF0)|(w_data>>4);	// output data, high 4 bits
	SETBIT(PORTB, LCD_CTRL_E);	            // set "E" line
	LCD_DELAY;								// wait
	CLEARBIT(PORTB, LCD_CTRL_E);		    // clear "E" line

	PORTC = (PORTC&0xF0) | (w_data&0x0F);	    // output data, low 4 bits
	SETBIT(PORTB, LCD_CTRL_E);		        // set "E" line
	LCD_DELAY;								// wait
	CLEARBIT(PORTB, LCD_CTRL_E);		    // clear "E" line
}

/* ------------------------------------------------------- */
/* Outputs string to a LCD (at current position)           */
/*                                                         */
/* ------------------------------------------------------- */

  void lcdStringWrite(char* String)
   {
     register uint8_t i=0;

	// check to make sure we have a good pointer
	if (!String) return;

	// print data
	while (String[i])
	{
		lcdDataWrite(String[i]);
		i++;
	}
}


/* ------------------------------------------------------- */
/*  Read and debounce the LCD AppMod buttons               */
/*                                                         */
/*  Returns 0 if nothing  is pressed                       */
/*  Returns 1 if button A is pressed                       */
/*  Returns 2 if button B is pressed                       */
/*  Returns 4 if button C is pressed                       */
/*  Returns 8 if button D is pressed                       */
/*  Returns combination if more is pressed (e.g. 6 for B+C)*/
/*                                                         */
/* ------------------------------------------------------- */

/*unsigned char ReadButtons()
{
  DDRD  = 0b00001110;                // make LCD bus inputs
  unsigned char state = 0xFF;           // assume nothing pressed
  
  for(int scan = 1; scan<=10; scan++)
  {
   state = state & ((PIND&0xF0)>>4); // make sure button held
   _delay_ms(5);                     // debounce 10 x 5 ms
  }

  DDRD  = 0b11111110;				 // return bus to outputs
  
  return(state);
}*/


/* ------------------------------------------------------- */
/* Test whether LCD is BUSY or READY for next command      */
/*                                                         */
/* At the moment it is replaced by the fixed time delay    */
/*                                                         */
/* Return values: 1 - OK                                   */
/*                0 - timeout                              */
/* ------------------------------------------------------- */

volatile unsigned char t_out_LCD;  // timeout displeja (krok 1024 us)
                                   // krok 1ms
unsigned char lcdBusy(void)
{
   _delay_ms(1);
   return 0;

/*
unsigned char pom;
	t_out_LCD=4; // pockam cca 4ms
	
	// wait until LCD busy bit goes to zero

	// do a read from control register

   CLEARBIT(LCD_CTRL_PORT, LCD_CTRL_RS);				// set RS to "control"

   LCD_DATA_PORT |=  0xF0;	// set pull-ups to on (4bit)
   LCD_DATA_DDR  &=  0x0F;	// set data I/O lines to input (4bit)

   SETBIT(LCD_CTRL_PORT, LCD_CTRL_RW);				// set R/W to "read"
   SETBIT(LCD_CTRL_PORT, LCD_CTRL_E);				// set "E" line
   LCD_DELAY;										// wait
   pom= LCD_DATA_PIN&0xF0;		// input data, high 4 bits
   CLEARBIT(LCD_CTRL_PORT, LCD_CTRL_E);		// clear "E" line
   LCD_DELAY;										// wait
   SETBIT(LCD_CTRL_PORT, LCD_CTRL_E);				// set "E" line
   LCD_DELAY;										// wait
   pom|=(LCD_DATA_PIN>>4)&0x0F;		// input data, low 4 bits
   CLEARBIT(LCD_CTRL_PORT, LCD_CTRL_E);		// clear "E" line

   while ((pom & (1<<LCD_BUSY))  && t_out_LCD )
   {
    SETBIT(LCD_CTRL_PORT, LCD_CTRL_E);				// set "E" line
    LCD_DELAY;										// wait
    pom=LCD_DATA_PIN&0xF0;		// input data, high 4 bits
    CLEARBIT(LCD_CTRL_PORT, LCD_CTRL_E);		// clear "E" line
    LCD_DELAY;										// wait
    SETBIT(LCD_CTRL_PORT, LCD_CTRL_E);				// set "E" line
    LCD_DELAY;										// wait
    pom|=(LCD_DATA_PIN>>4)&0x0F;		// input data, low 4 bits
    CLEARBIT(LCD_CTRL_PORT, LCD_CTRL_E);		// clear "E" line
   }

	//	leave data lines in input mode so they can be most easily used for other purposes
	if(t_out_LCD)
		return 1;	// ak vrati 1 timeout OK
	else
		return 0;		// ak vrati 0 timeout notOK
*/

}



/*
unsigned char lcdControlRead(void)
{
	// read the control byte from the display controller
	register unsigned char r_data=0;

	if(lcdBusyWait())	// wait until LCD not busy or time out
	{
		outb(LCD_DATA_PORT, inb(LCD_DATA_PORT)|0xF0);	// set pull-ups to on (4bit)
		outb(LCD_DATA_DDR, inb(LCD_DATA_DDR)&0x0F);	// set data I/O lines to input (4bit)
		CLEARBIT(LCD_CTRL_PORT, LCD_CTRL_RS);				// set RS to "control"
		SETBIT(LCD_CTRL_PORT, LCD_CTRL_RW);				// set R/W to "read"

		SETBIT(LCD_CTRL_PORT, LCD_CTRL_E);	// set "E" line
		LCD_DELAY;							// wait
		r_data = inb(LCD_DATA_PIN)&0xF0;		// input data, high 4 bits
		CLEARBIT(LCD_CTRL_PORT, LCD_CTRL_E);	// clear "E" line
		LCD_DELAY;							// wait
		SETBIT(LCD_CTRL_PORT, LCD_CTRL_E);	// set "E" line
		LCD_DELAY;							// wait
		r_data |= inb(LCD_DATA_PIN)>>4;		// input data, low 4 bits
		CLEARBIT(LCD_CTRL_PORT, LCD_CTRL_E);	// clear "E" line

		//	leave data lines in input mode so they can be most easily used for other purposes
	}
	return r_data;
}

unsigned char lcdDataRead(void)
{
// read a data byte from the display
	register unsigned char r_data =0;

	if(lcdBusyWait())				// wait until LCD not busy or time out
	{
		outb(LCD_DATA_PORT, inb(LCD_DATA_PORT)|0xF0);	// set pull-ups to on (4bit)
		outb(LCD_DATA_DDR, inb(LCD_DATA_DDR)&0x0F);	// set data I/O lines to input (4bit)
		SETBIT(LCD_CTRL_PORT, LCD_CTRL_RS);				// set RS to "data"
		SETBIT(LCD_CTRL_PORT, LCD_CTRL_RW);				// set R/W to "read"

		SETBIT(LCD_CTRL_PORT, LCD_CTRL_E);	// set "E" line
		LCD_DELAY;							// wait
		r_data = inb(LCD_DATA_PIN)&0xF0;		// input data, high 4 bits
		CLEARBIT(LCD_CTRL_PORT, LCD_CTRL_E);	// clear "E" line
		LCD_DELAY;							// wait
		SETBIT(LCD_CTRL_PORT, LCD_CTRL_E);	// set "E" line
		LCD_DELAY;							// wait
		r_data |= inb(LCD_DATA_PIN)>>4;		// input data, low 4 bits
		CLEARBIT(LCD_CTRL_PORT, LCD_CTRL_E);	// clear "E" line

	}
	//	leave data lines in input mode so they can be most easily used for other purposes
	return r_data;
}





*/