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

📄 lcd_drivers.c

📁 关于无传感器无刷电机的源程序,其芯片小,功能还行.
💻 C
📖 第 1 页 / 共 2 页
字号:
        //DATA_PORT |= 0x0003;

        E_PIN = 1;                      // Clock the cmd in
        DelayFor18TCY();
        E_PIN = 0;

        // Delay for at least 100us
        DelayXLCD();

        // Setup interface to LCD
                                  
        DATA_PORT &= 0xfff0;            // Function set cmd(4-bit interface)
        DATA_PORT |= 0x0003;

        E_PIN = 1;                      // Clock cmd in
        DelayFor18TCY();
        E_PIN = 0;

		//only now set to 4 bit interface
		DATA_PORT &= 0xfff0;            // Function set cmd(4-bit interface)
        DATA_PORT |= 0x0002;

        E_PIN = 1;                      // Clock cmd in
        DelayFor18TCY();
        E_PIN = 0;

	    TRIS_DATA_PORT |= 0x000f;       // Make data nibble input


        // Set data interface width, # lines, font
        while(BusyXLCD());              // Wait if LCD busy
        WriteCmdXLCD(lcdtype);          // Function set cmd 
        
 		while(BusyXLCD());              // Wait if LCD busy
        WriteCmdXLCD(lcdtype);          // Function set cmd 

        while(BusyXLCD());              // Wait if LCD busy
        WriteCmdXLCD(0x08);        		// Display OFF/Blink OFF DOFF&CURSOR_OFF&BLINK_OFF
        
        while(BusyXLCD());              // Wait if LCD busy
        WriteCmdXLCD(0x01);          	 // Display ON/Blink ON DON&CURSOR_ON&BLINK_ON

        // Clear display
        while(BusyXLCD());              // Wait if LCD busy
        WriteCmdXLCD(0x06);             // Clear display ENTRY MODE

        // Set entry mode inc, no shift
        while(BusyXLCD());              // Wait if LCD busy
        WriteCmdXLCD(0x0e);   			// Entry Mode SHIFT_CUR_RIGHT

        // Set DD Ram address to 0
        while(BusyXLCD());              // Wait if LCD busy
        SetDDRamAddr(0x00);               // Set Display data ram address to 0

        return;
}

/********************************************************************
*       Function Name:  putsXLCD
*       Return Value:   void
*       Parameters:     buffer: pointer to string
*       Description:    This routine writes a string of bytes to the
*                       Hitachi HD44780 LCD controller. The user
*                       must check to see if the LCD controller is
*                       busy before calling this routine. The data
*                       is written to the character generator RAM or
*                       the display data RAM depending on what the
*                       previous SetxxRamAddr routine was called.
********************************************************************/
void putsXLCD(unsigned const char *buffer)
{
        while(*buffer)                  // Write data to LCD up to null
        {
                while(BusyXLCD());      // Wait while LCD is busy
                WriteDataXLCD(*buffer); // Write character to LCD
                buffer++;               // Increment buffer
        }
        return;
}

/********************************************************************
*       Function Name:  SetDDRamAddr                                *
*       Return Value:   void                                        *
*       Parameters:     CGaddr: display data address                *
*       Description:    This routine sets the display data address  *
*                       of the Hitachi HD44780 LCD controller. The  *
*                       user must check to see if the LCD controller*
*                       is busy before calling this routine.        *
********************************************************************/
void SetDDRamAddr(unsigned char DDaddr)
{
                                          // Lower nibble interface
        TRIS_DATA_PORT &= 0xfff0;               // Make port output
        DATA_PORT &= 0xfff0;                    // and write upper nibble
        DATA_PORT |= (((DDaddr | 0x80)>>4) & 0x0f);

        RW_PIN = 0;                             // Set control bits
        RS_PIN = 0;
        DelayFor18TCY();
        E_PIN = 1;                              // Clock the cmd and address in
        DelayFor18TCY();
        E_PIN = 0;
                                        // Lower nibble interface
        DATA_PORT &= 0xfff0;                    // Write lower nibble
        DATA_PORT |= (DDaddr&0x0f);

        DelayFor18TCY();
        E_PIN = 1;                              // Clock the cmd and address in
        DelayFor18TCY();
        E_PIN = 0;
                                        // Lower nibble interface
        TRIS_DATA_PORT |= 0x000f;                // Make port input

        return;
}
/********************************************************************
*       Function Name:  WriteCmdXLCD                                *
*       Return Value:   void                                        *
*       Parameters:     cmd: command to send to LCD                 *
*       Description:    This routine writes a command to the Hitachi*
*                       HD44780 LCD controller. The user must check *
*                       to see if the LCD controller is busy before *
*                       calling this routine.                       *
********************************************************************/
void WriteCmdXLCD(unsigned char cmd)
{
                           // Lower nibble interface
        TRIS_DATA_PORT &= 0xfff0;
        DATA_PORT &= 0xfff0;
        DATA_PORT |= (cmd>>4)&0x0f;

        RW_PIN = 0;                     // Set control signals for command
        RS_PIN = 0;
        DelayFor18TCY();
        E_PIN = 1;                      // Clock command in
        DelayFor18TCY();
        E_PIN = 0;
                                // Lower nibble interface
        DATA_PORT &= 0xfff0;
        DATA_PORT |= cmd&0x0f;

        DelayFor18TCY();
        E_PIN = 1;                      // Clock command in
        DelayFor18TCY();
        E_PIN = 0;

        TRIS_DATA_PORT |= 0x000f;		//return port to input

        return;
}

/********************************************************************
*       Function Name:  WriteDataXLCD                               *
*       Return Value:   void                                        *
*       Parameters:     data: data byte to be written to LCD        *
*       Description:    This routine writes a data byte to the      *
*                       Hitachi HD44780 LCD controller. The user    *
*                       must check to see if the LCD controller is  *
*                       busy before calling this routine. The data  *
*                       is written to the character generator RAM or*
*                       the display data RAM depending on what the  *
*                       previous SetxxRamAddr routine was called.   *
********************************************************************/
void WriteDataXLCD(unsigned char data)
{
#ifdef BIT8                             // 8-bit interface
        TRIS_DATA_PORT &= 0xff00;       // Make port output
        DATA_PORT |= data;              // Write data to port
        RS_PIN = 1;                     // Set control bits
        DelayFor18TCY();
        RW_PIN = 0;
        DelayFor18TCY();
        E_PIN = 1;                      // Clock data into LCD
        DelayFor18TCY();
        E_PIN = 0;
        DelayFor18TCY();
        RS_PIN = 0;                     // Reset control bits
        TRIS_DATA_PORT |= 0x00ff;       // Make port input
#else                                   // 4-bit interface
#ifdef UPPER                            // Upper nibble interface
        TRIS_DATA_PORT &= 0xff0f;
        DATA_PORT &= 0xff0f;
        DATA_PORT |= data&0xf0;
#else                                   // Lower nibble interface
        TRIS_DATA_PORT &= 0xfff0;
        DATA_PORT &= 0xfff0;
        DATA_PORT |= ((data>>4)&0x0f);
#endif
        RS_PIN = 1;                     // Set control bits
        DelayFor18TCY();
        RW_PIN = 0;
        DelayFor18TCY();
        E_PIN = 1;                      // Clock nibble into LCD
        DelayFor18TCY();
        E_PIN = 0;
#ifdef UPPER                            // Upper nibble interface
        DATA_PORT &= 0xff0f;
        DATA_PORT |= ((data<<4)&0xf0);
#else                                   // Lower nibble interface
        DATA_PORT &= 0xfff0;
        DATA_PORT |= (data&0x0f);
#endif
        DelayFor18TCY();
        E_PIN = 1;                      // Clock nibble into LCD
        DelayFor18TCY();
        E_PIN = 0;
#ifdef UPPER                            // Upper nibble interface
        TRIS_DATA_PORT |= 0x00f0;
#else                                   // Lower nibble interface
        TRIS_DATA_PORT |= 0x000f;
#endif
#endif
        return;
}





⌨️ 快捷键说明

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