一乐电子

一乐电子百科

 找回密码
 请使用微信账号登录和注册会员

QQ登录

只需一步,快速开始

快捷登录

手机号码,快捷登录

搜索
查看: 1573|回复: 3
收起左侧

Lcd2usb改VFD遇到难题..请各位执教.

[复制链接]
发表于 2014-3-21 22:38 | 显示全部楼层 |阅读模式
Lcd2usb改VFD遇到难题..请各位执教.

1602用腻了.想改用三星20x2兼容hd44780的vfd.在lcd.h里改成 4位 模式..编译刷入..刷完能亮..掉电重新上电.不亮.必须复位一次才亮..是什么原因..求解答..
听说要延迟..怎么做呢?研究了两三天没搞明白.

#ifndef LCD_H
#define LCD_H
/*************************************************************************
Title :   C include file for the HD44780U LCD library (lcd.c)
Author:    Peter Fleury <pfleury@gmx.ch>  http://jump.to/fleury
File:     $Id: lcd.h,v 1.1.1.1 2007/01/14 11:37:56 harbaum Exp $
Software:  AVR-GCC 3.3
Hardware:  any AVR device, memory mapped mode only for AT90S4414/8515/Mega
***************************************************************************/
/**
@defgroup pfleury_lcd LCD library
@code #include <lcd.h> @endcode

@brief Basic routines for interfacing a HD44780U-based text LCD display
Originally based on Volker Oth's LCD library,
changed lcd_init(), added additional constants for lcd_command(),
added 4-bit I/O mode, improved and optimized code.
      
Memory mapped mode compatible with Kanda STK200, but supports also
generation of R/W signal through A8 address line.
      
@author Peter Fleury pfleury@gmx.ch http://jump.to/fleury

@see The chapter <a href="http://homepage.sunrise.ch/mysunrise/peterfleury/avr-lcd44780.html" target="_blank">Interfacing a HD44780 Based LCD to an AVR</a>
      on my home page.
*/
[url=]/*@{*/[/url]
#if (__GNUC__ * 100 + __GNUC_MINOR__) < 303
#error "This library requires AVR-GCC 3.3 or later, update to newer AVR-GCC compiler !"
#endif
#include <inttypes.h>
#include <avr/pgmspace.h>
/**
*  @name  Definitions for MCU Clock Frequency
*  Adapt the MCU clock frequency in Hz to your target.
*/
#define XTAL 12000000              /**< clock frequency in Hz, used to calculate delay timer */
/**
*  @name Definitions for 4-bit IO mode
*  Change LCD_PORT if you want to use a different port for the LCD pins.
*
*  The four LCD data lines and the three control lines RS, RW, E can be on the
*  same port or on different ports.
*  Change LCD_RS_PORT, LCD_RW_PORT, LCD_E_PORT if you want the control lines on
*  different ports.
*
*  Normally the four data lines should be mapped to bit 0..3 on one port, but it
*  is possible to connect these data lines in different order or even on different
*  ports by adapting the LCD_DATAx_PORT and LCD_DATAx_PIN definitions.
*  
*/
#define LCD_DATA_PORT    PORTD        /**< port for the LCD lines   */
#define LCD_RS_PORT      PORTD        /**< port for RS line         */
#define LCD_RS_PIN       3            /**< pin  for RS line         */
#define LCD_RW_PORT      PORTC        /**< port for RW line         */
#define LCD_RW_PIN       2            /**< pin  for RW line         */
#define LCD_E_PORT       PORTC        /**< port for Enable line     */
#define LCD_E0_PIN       3            /**< pin  for Enable line 0   */
#define LCD_E1_PIN       4            /**< pin  for Enable line 1   */
#define LCD_CTRL_0       (1<<0)
#define LCD_CTRL_1       (1<<1)
/**
*  @name Definitions for LCD command instructions
*  The constants define the various LCD controller instructions which can be passed to the
*  function lcd_command(), see HD44780 data sheet for a complete description.
*/
/* instruction register bit positions, see HD44780U data sheet */
#define LCD_CLR               0      /* DB0: clear display                  */
#define LCD_HOME              1      /* DB1: return to home position        */
#define LCD_ENTRY_MODE        2      /* DB2: set entry mode                 */
#define LCD_ENTRY_INC         1      /*   DB1: 1=increment, 0=decrement     */
#define LCD_ENTRY_SHIFT       0      /*   DB2: 1=display shift on           */
#define LCD_ON                3      /* DB3: turn lcd/cursor on             */
#define LCD_ON_DISPLAY        2      /*   DB2: turn display on              */
#define LCD_ON_CURSOR         1      /*   DB1: turn cursor on               */
#define LCD_ON_BLINK          0      /*     DB0: blinking cursor ?          */
#define LCD_MOVE              4      /* DB4: move cursor/display            */
#define LCD_MOVE_DISP         3      /*   DB3: move display (0-> cursor) ?  */
#define LCD_MOVE_RIGHT        2      /*   DB2: move right (0-> left) ?      */
#define LCD_FUNCTION          5      /* DB5: function set                   */
#define LCD_FUNCTION_8BIT     0      /*   DB4: set 8BIT mode (0->4BIT mode) */
#define LCD_FUNCTION_2LINES   3      /*   DB3: two lines (0->one line)      */
#define LCD_FUNCTION_10DOTS   2      /*   DB2: 5x10 font (0->5x7 font)      */
#define LCD_CGRAM             6      /* DB6: set CG RAM address             */
#define LCD_DDRAM             7      /* DB7: set DD RAM address             */
#define LCD_BUSY              7      /* DB7: LCD is busy                    */
/* set entry mode: display shift on/off, dec/inc cursor move direction */
#define LCD_ENTRY_DEC            0x04   /* display shift off, dec cursor move dir */
#define LCD_ENTRY_DEC_SHIFT      0x05   /* display shift on,  dec cursor move dir */
#define LCD_ENTRY_INC_           0x06   /* display shift off, inc cursor move dir */
#define LCD_ENTRY_INC_SHIFT      0x07   /* display shift on,  inc cursor move dir */
/* display on/off, cursor on/off, blinking char at cursor position */
#define LCD_DISP_OFF             0x08   /* display off                            */
#define LCD_DISP_ON              0x0C   /* display on, cursor off                 */
#define LCD_DISP_ON_BLINK        0x0D   /* display on, cursor off, blink char     */
#define LCD_DISP_ON_CURSOR       0x0E   /* display on, cursor on                  */
#define LCD_DISP_ON_CURSOR_BLINK 0x0F   /* display on, cursor on, blink char      */
/* move cursor/shift display */
#define LCD_MOVE_CURSOR_LEFT     0x10   /* move cursor left  (decrement)          */
#define LCD_MOVE_CURSOR_RIGHT    0x14   /* move cursor right (increment)          */
#define LCD_MOVE_DISP_LEFT       0x18   /* shift display left                     */
#define LCD_MOVE_DISP_RIGHT      0x1C   /* shift display right                    */
/* function set: set interface data length and number of display lines */
#define LCD_FUNCTION_4BIT_1LINE  0x20   /* 4-bit interface, single line, 5x7 dots */
#define LCD_FUNCTION_4BIT_2LINES 0x28   /* 4-bit interface, dual line,   5x7 dots */
#define LCD_FUNCTION_8BIT_1LINE  0x30   /* 8-bit interface, single line, 5x7 dots */
#define LCD_FUNCTION_8BIT_2LINES 0x38   /* 8-bit interface, dual line,   5x7 dots */

#define LCD_MODE_DEFAULT     ((1<<LCD_ENTRY_MODE) | (1<<LCD_ENTRY_INC) )

/**
*  @name Functions
*/

/**
@brief    Initialize display and select type of cursor
@param    dispAttr \b LCD_DISP_OFF display off\n
                    \b LCD_DISP_ON display on, cursor off\n
                    \b LCD_DISP_ON_CURSOR display on, cursor on\n
                    \b LCD_DISP_ON_CURSOR_BLINK display on, cursor on flashing            
@return  0 on failure, 1 on success
*/
extern uint8_t lcd_init(uint8_t ctrl);

/**
@brief    Clear display and set cursor to home position
@param    void                                       
@return   none
*/
extern void lcd_clrscr(uint8_t ctrl);
/**
@brief    Display string without auto linefeed
@param    s string to be displayed                                       
@return   none
*/
extern void lcd_puts(uint8_t ctrl, const char *s);
/**
@brief    Send LCD controller instruction command
@param    cmd instruction to send to LCD controller, see HD44780 data sheet
@return   none
*/
extern void lcd_command(uint8_t ctrl, uint8_t cmd);

/**
@brief    Send data byte to LCD controller

Similar to lcd_putc(), but without interpreting LF
@param    data byte to send to LCD controller, see HD44780 data sheet
@return   none
*/
extern void lcd_data(uint8_t ctrl, uint8_t data);
[url=]/*@}*/[/url]
#endif //LCD_H

发表于 2014-3-21 23:12 | 显示全部楼层
有人问怎么在开机冷启动状态下。设置成4bit模式。


//冷启动的时候从,6个Io从高阻全部到高电平。
void hd44780_4Bit_Reset( void )
{
unsigned  char i;
  DB7 = 0;
  DB6 = 0;
  DB5 = 0;
  DB4 = 0;
  Elcm = 0;
  RS = 0;

  DDR_RS = 1;
//  DDR_RW = 1;
  DDR_Elcm = 1;

  DDR_DB7 = 1;
  DDR_DB6 = 1;
  DDR_DB5 = 1;
  DDR_DB4 = 1;

  hd44780_4Bit_delay_ms(2000);  //预留时间给LCD模块启动时间

//预留2秒肯定够了,不够就是你的延时函数出了问题。




//折中刚好可以设置成4bit模式
//>>>
hd44780_4Bit_command(0x33);  
    hd44780_4Bit_delay_ms(100);
hd44780_4Bit_command(0x32);
    hd44780_4Bit_delay_ms(100);
///<<<<




#ifdef  LCD_4BIT
//设置为4bit模式
//相当于发送0b00100000 4bit模式命令
//4bit硬件下的LCD_FUNCTION_4BIT_2LINES命令的前半部分。
DB4=0;
DB5=1;
DB6=0;
DB7=0;
   hd44780_4Bit_lcd_e_toggle;
   hd44780_4Bit_delay_ms(100);
#endif



蓝字是一种【折中的办法】,
  00110000    0x30        //相当于发送#define LCD_FUNCTION_8BIT_1LINE  0x30   00110000
  00110000    0x30        //相当于发送#define LCD_FUNCTION_8BIT_1LINE  0x30   00110000

  00110000    0x30        //相当于发送#define LCD_FUNCTION_8BIT_1LINE  0x30   00110000
  00100000    0x20          //相当于发送0b00100000 4bit模式命令 LCD_FUNCTION_4BIT_1LINE
在切换模式之前,相当于发送了3次【LCD_FUNCTION_8BIT_1LINE】,一次【LCD_FUNCTION_4BIT_1LINE】



我喜欢用红字的方法,在冷启动的状态下,一次成功。
发表于 2014-3-21 23:17 | 显示全部楼层
#define LCD_DISP_OFF             0x08  // display off      
#define LCD_CLR               0x01      //clear display
#define LCD_HOME              0x03      // return to home position
#define LCD_DISP_ON              0x0C   // display on, cursor off
/* function set: set interface data length and number of display lines */
#define LCD_FUNCTION_4BIT_1LINE  0x20   /* 4-bit interface, single line, 5x7 dots */
#define LCD_FUNCTION_4BIT_2LINES 0x28   /* 4-bit interface, dual line,   5x7 dots */
#define LCD_FUNCTION_8BIT_1LINE  0x30   /* 8-bit interface, single line, 5x7 dots */
#define LCD_FUNCTION_8BIT_2LINES 0x38   /* 8-bit interface, dual line,   5x7 dots */




#define LCD_FUNCTION_4BIT_1LINE  0x20   00100000
#define LCD_FUNCTION_4BIT_2LINES 0x28   00101000
#define LCD_FUNCTION_8BIT_1LINE  0x30   00110000
#define LCD_FUNCTION_8BIT_2LINES 0x38   00111000

例如,你的程序是用我蓝色的代码,上电的时候,vfd接受了3次【LCD_FUNCTION_8BIT_1LINE】命令,一次【LCD_FUNCTION_8BIT_1LINE】命令,但是命令连接很快,不同的液晶vfd反应时间不同,你重启的是单片机,vfd并没有冷启动吧,
也就是,在你重启单片机所延时的时间后面,还要接收3次【LCD_FUNCTION_8BIT_1LINE】命令,一次【LCD_FUNCTION_8BIT_1LINE】命令。
蓝色的办法其实就是多发送几次【0x33,0x32】,直到vfd切换成4bit模式
发表于 2014-3-22 12:34 | 显示全部楼层
这个果断标记下,goodcode的白光改成vfd输出想尝试下~到时候有疑问再开贴~学习中...

本版积分规则

QQ|一淘宝店|手机版|商店|电子DIY套件|一乐电子 ( 粤ICP备09076165号 ) 公安备案粤公网安备 44522102000183号

GMT+8, 2024-4-26 05:05 , Processed in 0.058743 second(s), 36 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表