⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lcd.c

📁 AVR Mega88 + nRF24L01 wireless 2.4GHz >> Driver nRF24L01 >> AVRSTUDIO project
💻 C
📖 第 1 页 / 共 2 页
字号:
Set cursor to home position
*************************************************************************/
void lcd_home(void)
{
    lcd_command(1<<LCD_HOME);
}

#ifdef LCD_SCROLL_FUNCTION
/*************************************************************************
Scroll up mtmt
*************************************************************************/
void lcd_scrollup(void)
{
	#if LCD_LINES==4
	uint8_t i,c;
	
	for (i=0;i<LCD_DISP_LENGTH;i++) {
		// line 2 to 1
		lcd_waitbusy();
		lcd_write((1<<LCD_DDRAM)+LCD_START_LINE2+i,0);
		lcd_waitbusy();
		c=lcd_read(1);
		lcd_waitbusy();
		lcd_write((1<<LCD_DDRAM)+LCD_START_LINE1+i,0);
		lcd_waitbusy();
		lcd_write(c, 1);
		// line 3 to 2
		lcd_waitbusy();
		lcd_write((1<<LCD_DDRAM)+LCD_START_LINE3+i,0);
		lcd_waitbusy();
		c=lcd_read(1);
		lcd_waitbusy();
		lcd_write((1<<LCD_DDRAM)+LCD_START_LINE2+i,0);
		lcd_waitbusy();
		lcd_write(c, 1);
		lcd_waitbusy();
		lcd_write((1<<LCD_DDRAM)+LCD_START_LINE4+i,0);
		lcd_waitbusy();
		c=lcd_read(1);
		lcd_waitbusy();
		lcd_write((1<<LCD_DDRAM)+LCD_START_LINE3+i,0);
		lcd_waitbusy();
		lcd_write(c, 1);
		lcd_waitbusy();
		lcd_write((1<<LCD_DDRAM)+LCD_START_LINE4+i,0);
		lcd_waitbusy();
		lcd_write(' ', 1);
	}
    #else
	#warning "Scroll up avail. only for 4-line displays"
	#endif	
}
#endif

/*************************************************************************
Display character at current cursor position 
Input:    character to be displayed                                       
Returns:  none
*************************************************************************/
void lcd_putc(char c)
{
    uint8_t pos;
	pos = lcd_waitbusy();   // read busy-flag and address counter
    
	if (c=='\n')
    {   
        lcd_newline(pos);
    }
    else
    {
		// mtmt changed order to fix autowrap first write to lcd then 
		// check position
		lcd_write(c, 1);
		pos = lcd_waitbusy();   // read busy-flag and address counter
#if LCD_WRAP_LINES==1
#if LCD_LINES==1
        if ( pos == LCD_START_LINE1+LCD_DISP_LENGTH )
            lcd_write((1<<LCD_DDRAM)+LCD_START_LINE1,0);
#elif LCD_LINES==2
        if ( pos == LCD_START_LINE1+LCD_DISP_LENGTH )
            lcd_write((1<<LCD_DDRAM)+LCD_START_LINE2,0);    
        else if ( pos == LCD_START_LINE2+LCD_DISP_LENGTH )
            lcd_write((1<<LCD_DDRAM)+LCD_START_LINE1,0);
#elif LCD_LINES==4
        if ( pos == LCD_START_LINE1+LCD_DISP_LENGTH )
            lcd_write((1<<LCD_DDRAM)+LCD_START_LINE2,0);    
		else if ( pos == LCD_START_LINE2+LCD_DISP_LENGTH )
            lcd_write((1<<LCD_DDRAM)+LCD_START_LINE3,0);
        else if ( pos == LCD_START_LINE3+LCD_DISP_LENGTH )
            lcd_write((1<<LCD_DDRAM)+LCD_START_LINE4,0);
        else if ( pos == LCD_START_LINE4+LCD_DISP_LENGTH ) {
			// mtmt call to autoscroll
			#ifdef LCD_AUTO_SCROLL
			lcd_scrollup();
			lcd_waitbusy();
			lcd_write((1<<LCD_DDRAM)+LCD_START_LINE4,0);
			#else
			lcd_write((1<<LCD_DDRAM)+LCD_START_LINE1,0);
			#endif
		}
#endif
        lcd_waitbusy();
#endif
        // lcd_write(c, 1);
    }

}/* lcd_putc */

/*************************************************************************
Display string without auto linefeed 
Input:    string to be displayed
Returns:  none
*************************************************************************/
void lcd_puts(const char *s)
/* print string on lcd (no auto linefeed) */
{
    register char c;

    while ( (c = *s++) ) {
        lcd_putc(c);
    }

}/* lcd_puts */


/*************************************************************************
Display string from program memory without auto linefeed 
Input:     string from program memory be be displayed                                        
Returns:   none
*************************************************************************/
void lcd_puts_p(const char *progmem_s)
/* print string from program memory on lcd (no auto linefeed) */
{
    register char c;

    while ( (c = pgm_read_byte(progmem_s++)) ) {
        lcd_putc(c);
    }

}/* lcd_puts_p */


/*************************************************************************
Initialize display and select type of cursor 
Input:    dispAttr LCD_DISP_OFF            display off
                   LCD_DISP_ON             display on, cursor off
                   LCD_DISP_ON_CURSOR      display on, cursor on
                   LCD_DISP_CURSOR_BLINK   display on, cursor on flashing
Returns:  none
*************************************************************************/
void lcd_init(uint8_t dispAttr)
{
#if LCD_IO_MODE
    /*
     *  Initialize LCD to 4 bit I/O mode
     */
     
    if ( ( &LCD_DATA0_PORT == &LCD_DATA1_PORT) && ( &LCD_DATA1_PORT == &LCD_DATA2_PORT ) && ( &LCD_DATA2_PORT == &LCD_DATA3_PORT )
      && ( &LCD_RS_PORT == &LCD_DATA0_PORT) && ( &LCD_RW_PORT == &LCD_DATA0_PORT) && (&LCD_E_PORT == &LCD_DATA0_PORT)
      && (LCD_DATA0_PIN == 0 ) && (LCD_DATA1_PIN == 1) && (LCD_DATA2_PIN == 2) && (LCD_DATA3_PIN == 3) 
      && (LCD_RS_PIN == 4 ) && (LCD_RW_PIN == 5) && (LCD_E_PIN == 6 ) )
    {
        /* configure all port bits as output (all LCD lines on same port) */
        DDR(LCD_DATA0_PORT) |= 0x7F;
    }
    else if ( ( &LCD_DATA0_PORT == &LCD_DATA1_PORT) && ( &LCD_DATA1_PORT == &LCD_DATA2_PORT ) && ( &LCD_DATA2_PORT == &LCD_DATA3_PORT )
           && (LCD_DATA0_PIN == 0 ) && (LCD_DATA1_PIN == 1) && (LCD_DATA2_PIN == 2) && (LCD_DATA3_PIN == 3) )
    {
        /* configure all port bits as output (all LCD data lines on same port, but control lines on different ports) */
        DDR(LCD_DATA0_PORT) |= 0x0F;
        DDR(LCD_RS_PORT)    |= _BV(LCD_RS_PIN);
        DDR(LCD_RW_PORT)    |= _BV(LCD_RW_PIN);
        DDR(LCD_E_PORT)     |= _BV(LCD_E_PIN);
    }
    else
    {
        /* configure all port bits as output (LCD data and control lines on different ports */
        DDR(LCD_RS_PORT)    |= _BV(LCD_RS_PIN);
        DDR(LCD_RW_PORT)    |= _BV(LCD_RW_PIN);
        DDR(LCD_E_PORT)     |= _BV(LCD_E_PIN);
        DDR(LCD_DATA0_PORT) |= _BV(LCD_DATA0_PIN);
        DDR(LCD_DATA1_PORT) |= _BV(LCD_DATA1_PIN);
        DDR(LCD_DATA2_PORT) |= _BV(LCD_DATA2_PIN);
        DDR(LCD_DATA3_PORT) |= _BV(LCD_DATA3_PIN);
    }
	
    delay_us(16000);        /* wait 16ms or more after power-on       */
    
    /* initial write to lcd is 8bit */
    LCD_DATA1_PORT |= _BV(LCD_DATA1_PIN);  // _BV(LCD_FUNCTION)>>4;
    LCD_DATA0_PORT |= _BV(LCD_DATA0_PIN);  // _BV(LCD_FUNCTION_8BIT)>>4;
    lcd_e_toggle();
    delay_us(4992);         /* delay, busy flag can't be checked here */
   
    /* repeat last command */ 
    lcd_e_toggle();      
    delay_us(64);           /* delay, busy flag can't be checked here */
    
    /* repeat last command a third time */
    lcd_e_toggle();      
    delay_us(64);           /* delay, busy flag can't be checked here */

    /* now configure for 4bit mode */
    LCD_DATA0_PORT &= ~_BV(LCD_DATA0_PIN);   // LCD_FUNCTION_4BIT_1LINE>>4
    lcd_e_toggle();
    delay_us(64);           /* some displays need this additional delay */
    
    /* from now the LCD only accepts 4 bit I/O, we can use lcd_command() */    
#else
    /*
     * Initialize LCD to 8 bit memory mapped mode
     */
    
    /* enable external SRAM (memory mapped lcd) and one wait state */        
    MCUCR = _BV(SRE) | _BV(SRW);

    /* reset LCD */
    delay_us(16000);                           /* wait 16ms after power-on     */
    lcd_write(LCD_FUNCTION_8BIT_1LINE,0);   /* function set: 8bit interface */                   
    delay_us(4992);                            /* wait 5ms                     */
    lcd_write(LCD_FUNCTION_8BIT_1LINE,0);   /* function set: 8bit interface */                 
    delay_us(64);                              /* wait 64us                    */
    lcd_write(LCD_FUNCTION_8BIT_1LINE,0);   /* function set: 8bit interface */                
    delay_us(64);                              /* wait 64us                    */
#endif
    lcd_command(LCD_FUNCTION_DEFAULT);      /* function set: display lines  */
    lcd_command(LCD_DISP_OFF);              /* display off                  */
    lcd_clrscr();                           /* display clear                */ 
    lcd_command(LCD_MODE_DEFAULT);          /* set entry mode               */
    lcd_command(dispAttr);                  /* display/cursor control       */

}


//-------------------------------------------------------
//   Display byte in binary '0' or '1' (1-byte)  8-digit
//   test=0xA6 = 0b10100110
//   lcd_SendBin(0xA6,'1','0'); -> 10100110
//	 lcd_SendBin(0xA6,'-','_'); -> -_-__--_
//	 lcd_SendBin(0xA6,'X','Y'); -> XYXYYXXY
//-------------------------------------------------------
void lcd_SendBin(unsigned char x, unsigned char ch0, unsigned char ch1)
{
  unsigned char i;
  for (i=128;i>0;i>>=1)
  {
    if ((x&i)==0) lcd_putc(ch0);
    else lcd_putc(ch1);
  }
}


//-----------------------------------------
//   Display 1-byte in hex (8bit)  2-digit
//   lcd_8bitHex(0xA6); -> A6
//-----------------------------------------
void lcd_8bitHex( unsigned char hexIn )
{
	unsigned char tempHex;


 	tempHex =  lcdhexDigits(hexIn >> 4);
	lcd_putc(tempHex);	
	tempHex =  lcdhexDigits(hexIn & 0x0F); 
	lcd_putc(tempHex);	  
}

//------------------------------------------
//   Display 2-byte in hex (16bit)  4-digit
//   lcd_16bitHex(0xA6);   -> 00A6 
//   lcd_16bitHex(0xF9A6); -> F9A6 
//------------------------------------------
void lcd_16bitHex(unsigned int hexIn)
{
	unsigned char tempHex;

	tempHex =  lcdhexDigits((hexIn & 0xF000) >> 12);
	lcd_putc(tempHex);	
	tempHex =  lcdhexDigits((hexIn & 0x0F00) >> 8); 
	lcd_putc(tempHex);	
	tempHex =  lcdhexDigits((hexIn & 0x00F0) >> 4);
	lcd_putc(tempHex);	
	tempHex =  lcdhexDigits((hexIn & 0x000F)); 
	lcd_putc(tempHex);	

}

//----------------------------------------------------
//  Display 0..65535 with or without leading zeroes
//	lcd_SendDec(12345,4,' ');  2345
//	lcd_SendDec(12345,5,' ');  12345
//	lcd_SendDec(12345,6,' ');   12345  
//	lcd_SendDec(12345,6,'0');  012345
//----------------------------------------------------
void lcd_SendDec(unsigned int x, unsigned char n, unsigned char fillch)
{
    unsigned char i;
    unsigned char s[10];
    for (i = 0; i < n; i++) {
        s[n - i - 1] = '0' + (x % 10);
        x /= 10;
    }
    for (i=0; i<(n - 1); i++) {
        if (s[i] == '0') s[i] = fillch; else break;
    }
    for (i=0; i<n; i++) lcd_putc(s[i]);
}

//----------------------------------------------------
//    Display floatung number
//    float1 = 4556.6312; 
//    lcd_SendFloat(float1,4,1); -> 4556.6
//    lcd_SendFloat(float1,4,2); -> 4556.63
//    lcd_SendFloat(float1,4,3); -> 4556.631
//    lcd_SendFloat(float1,4,4); -> 4556.6312
//----------------------------------------------------
void lcd_SendFloat( float lcd_value,unsigned char lcd_digit ,unsigned char lcd_precision)
{
	char lcd_temp[15];
	unsigned char i;

	dtostrf(lcd_value,lcd_digit,lcd_precision,lcd_temp);
	
    i=0;
	
	while(lcd_temp[i] != 0)
	{
		lcd_putc(lcd_temp[i]);
		i++;
	}		
	 
	for(unsigned char i=0;i<=15;i++)
	{
		lcd_temp[i]=0;
	}   
}

//----------------------------------------------------
//    Display unsigned int 0-32767 (16bit)
//    lcd_SendDecDigit(32767); -> 32767
//    lcd_SendDecDigit(4321);  -> 04321
//    lcd_SendDecDigit(321);   -> 00321
//    lcd_SendDecDigit(21);    -> 00021
//----------------------------------------------------
void lcd_SendDecDigit(unsigned long x)
{
  unsigned int y;
  if (x<65534)
  {
	y=x/10000;lcd_putc(y+0x30);x-=(y*10000);
    y=x/1000;lcd_putc(y+0x30);x-=(y*1000);
    y=x/100;lcd_putc(y+0x30);x-=(y*100);
    y=x/10;lcd_putc(y+0x30);x-=(y*10);
    lcd_putc(x+0x30);
  }
  else lcd_puts("Err\n");
}

//----------------------------------------------------
//    Display unsigned int 0-32767 (16bit)
//    lcd_SendInt(32767); -> 32767
//    lcd_SendInt(4321);  -> 4321
//    lcd_SendInt(321);   -> 321
//    lcd_SendInt(21);    -> 21
//----------------------------------------------------
void lcd_SendInt(unsigned int number )
{
    char temp[7];
    itoa(number,temp,10);
    lcd_puts(temp);
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -