Operácie

Lcd.c

Z SensorWiki

Verzia z 11:37, 18. december 2012, ktorú vytvoril StudentDVPS (diskusia | príspevky)
/*
 *   Created: 12/12/2012 12:12:12
 *   Authors: Lukas Mrva (55716), Vladimir Reksak (55720)
 */

#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
   	PORTD = 0b00000001;						// pull-up on unused input
	DDRD  = 0b11111110;						// set ctrl & data as outputs

	_delay_ms(50);        					// -  wait 15ms or more
											// -------------------
	CLEARBIT(PORTD, LCD_CTRL_RS);			// set RS to "control"
	CLEARBIT(PORTD, LCD_CTRL_RW);			// set R/W to "write"
											// 4 bit write
	SETBIT(PORTD, LCD_CTRL_E);			    // set "E" line
	PORTD = (PORTD | 0x30) & 0x3F;          // output data, high 4 bits
	LCD_DELAY;							    // wait
	CLEARBIT(PORTD, LCD_CTRL_E);			// clear "E" line
											// -------------------
	_delay_ms(5); 				            // -  wait 4.1ms
	SETBIT(PORTD, LCD_CTRL_E);	 	        // set "E" line
	LCD_DELAY;								// wait
	CLEARBIT(PORTD, LCD_CTRL_E);			// clear "E" line
											// -------------------
    _delay_ms(1);	                        // -  wait 0.1ms
	SETBIT(PORTD, LCD_CTRL_E);			    // set "E" line
	LCD_DELAY;						        // wait
	CLEARBIT(PORTD, LCD_CTRL_E);			// clear "E" line
											// -------------------
    _delay_ms(1);	                        // -  wait 0.1ms
    										// -------------------
	PORTD = (PORTD | 0x20) & 0x2F;	        // output data, high 4 bits
	SETBIT(PORTD, LCD_CTRL_E);		        // set "E" line
	LCD_DELAY;								// wait
	CLEARBIT(PORTD, 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(PORTD, LCD_CTRL_RS);		    // set RS to "control"
	CLEARBIT(PORTD, LCD_CTRL_RW);	    	// set R/W to "write"

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

	PORTD = (PORTD&0x0F) | (c_data<<4);	    // output data, low 4 bits
	SETBIT(PORTD, LCD_CTRL_E);		        // set "E" line
	LCD_DELAY;								// wait
	CLEARBIT(PORTD, 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(PORTD, LCD_CTRL_RS);		        // set RS to "data"
	CLEARBIT(PORTD, LCD_CTRL_RW);		    // set R/W to "write"

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

	PORTD = (PORTD&0x0F) | (w_data<<4);	    // output data, low 4 bits
	SETBIT(PORTD, LCD_CTRL_E);		        // set "E" line
	LCD_DELAY;								// wait
	CLEARBIT(PORTD, 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;
  _delay_ms(20);
  unsigned char state=(PIND>>4)&0x0F;

  DDRD  = 0b11111110; // return bus to outputs
  
  //  return(state);
  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;
}

Na stiahnutie: Súbor:Lcd.c


Späť na zadanie