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

📄 lcd_mcb2300.c

📁 Show hou use semaphores in RTL-ARM
💻 C
字号:
/* 
 ********************************************************************
 * Project:     GNU-Port TCP/IP-Stack
 * File:    	lcd_mcb2300.c
 *
 * System:   	ARM7TDMI-S 32 Bit
 * Compiler:  	GCC 4.0.3
 *
 * Date:      	2006-08-31
 * Author:    	Wenz
 *
 * Rights:    	Hitex Development Tools GmbH
 *            	Greschbachstr. 12
 *            	D-76229 Karlsruhe
 ********************************************************************
 * Description:
 *	
 * driver for LCD module (A162) 2x16 on board MCB2300 
 ********************************************************************
 * History:
 *
 *  Revision 1.0    2006/09/27      We
 *     Initial revision 
 ********************************************************************
 * This software is provided by the author 'AS IS' without any 
 * warranties. Hitex Development Tools GmbH shall not be held 
 * liable for any direct, indirect or consequential damages with
 * respect to any claims arising from the content of such software.
 ********************************************************************/



#include "peripherals_lpc23xx.h"          /* LPC23xx definitions  */
#include "lcd_mcb2300.h"
#include "FreeRTOS.h"
#include "task.h"


/* LCD  definitions */
#define LCD_E     0x40000000              /* Enable control pin  */
#define LCD_RW    0x20000000              /* Read/Write control pin  */
#define LCD_RS    0x10000000              /* Data/Instruction control  */
#define LCD_CTRL  0x70000000              /* Control lines mask */
#define LCD_DATA  0x0F000000              /* Data lines mask  */

/* Local variables */
static uint32_t 	lcd_ptr;

/* 8 user defined characters to be loaded into CGRAM (used for bargraph) */
static const uint8_t UserFont[8][8] = 
{
   { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
   { 0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10 },
   { 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18 },
   { 0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C },
   { 0x1E,0x1E,0x1E,0x1E,0x1E,0x1E,0x1E,0x1E },
   { 0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F },
   { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
   { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }
};

/* Local Function Prototypes */
static void 		prvDelay (uint32_t cnt);
static void 		prvLCDWrite (uint32_t c);
static void 		prvLCDWrite4Bit (uint32_t c);
static uint32_t 	prvLCDReadStat(void);
static void 		prvLCDWriteCommand (uint32_t c);
static void 		prvLCDWriteData (uint32_t d);
static void 		prvLCDWaitForBusy (void);
void vStartLCDTasks( unsigned portBASE_TYPE uxPriority );
static void vLCDTask( void *pvParameters );

#define lcdSTACK_SIZE				configMINIMAL_STACK_SIZE

/****************************************************************************
* Name:		vLCDInit
*                  	
* Description:	Initialize the LCD controller to 4-bit mode.
*						
* Parameters:	none
*					
* Return value:	none
****************************************************************************
*/
void vLCDInit (void)
{
   GPIO1_IODIR |= LCD_CTRL | LCD_DATA;
   GPIO1_IOCLR  = LCD_RW   | LCD_RS   | LCD_DATA;
   prvLCDWrite4Bit(0x3);                  /* Select 4-bit interface            */
   prvDelay (100000);                     /* */
   prvLCDWrite4Bit (0x3);                 /* */
   prvDelay (10000);                      /* */
   prvLCDWrite4Bit (0x3);                 /* */
   prvLCDWrite4Bit (0x2);                 /* */
   prvLCDWriteCommand (0x28);             /* 2 lines, 5x8 character matrix     */
   prvLCDWriteCommand (0x0e);             /* Display ctrl:Disp/Curs/Blnk=ON    */
   prvLCDWriteCommand (0x06);             /* Entry mode: Move right, no shift  */
   vLCDLoad((uint8_t*)&UserFont, sizeof (UserFont));
   vLCDCls();                             /* */
}
/****************************************************************************
* Name:			prvDelay
*                  	
* Description:	a cycle delay function
*						
* Parameters:	aCount:
*					Counter for while - loop
*					
* Return value:	none
****************************************************************************
*/
static void prvDelay(uint32_t aCount) 
{
   while (aCount--);
}

/****************************************************************************
* Name:			prvLCDWrite
*                  	
* Description:	Write a 4-bit command to LCD controller.
*						
* Parameters:	cmdData:
*					write command or data to output to LCD-controller
*					
* Return value:	none
****************************************************************************
*/
static void prvLCDWrite4Bit(uint32_t cmdData)
{
   GPIO1_IODIR |= LCD_DATA | LCD_CTRL;    /* */
   GPIO1_IOCLR  = LCD_RW   | LCD_DATA;    /* */
   GPIO1_IOSET  = (cmdData & 0xF) << 24;  /* */
   GPIO1_IOSET  = LCD_E;                  /* */
   prvDelay (10);                         /* */
   GPIO1_IOCLR  = LCD_E;                  /* */
   prvDelay (10);                         /* */
}
/****************************************************************************
* Name:			prvLCDWrite
*                  	
* Description:	Write data/command to LCD controller.
*						
* Parameters:	cmdData:
*					command or data to output to LCD-controller
*					
* Return value:	none
****************************************************************************
*/
static void prvLCDWrite (uint32_t cmdData)
{
   /* write high nibble */
   prvLCDWrite4Bit(cmdData >> 4);
   /* write low nibble */
   prvLCDWrite4Bit(cmdData);
}

/****************************************************************************
* Name:			prvLCDReadStatus
*                  	
* Description:	Read status of LCD controller
*						
* Parameters:	none
*					
* Return value:	LCD-status
*
****************************************************************************
*/
static uint32_t prvLCDReadStat (void)
{
   uint32_t stat;                         /* */
   GPIO1_IODIR &= ~LCD_DATA;	            /* direction input */
   GPIO1_IOCLR  = LCD_RS;                 /* */
   GPIO1_IOSET  = LCD_RW;                 /* */
   prvDelay (10);                         /* */
   GPIO1_IOSET  = LCD_E;                  /* */
   prvDelay (10);                         /* */
   /* read status high nibble */
   stat    = (GPIO1_IOPIN >> 20) & 0xF0;
   GPIO1_IOCLR  = LCD_E;
   prvDelay (10);                         /* */
   /* read status low nibble   */
   GPIO1_IOSET  = LCD_E;                  /* */
   prvDelay (10);
   stat   |= (GPIO1_IOPIN >> 24) & 0xF;   /* */
   GPIO1_IOCLR  = LCD_E;
   return (stat);
}
/****************************************************************************
* Name:		prvLCDWaitForBusy
*                  	
* Description:	Wait until LCD controller is busy.
*						
* Parameters:	none
*					
* Return value:	none
****************************************************************************
*/
static void prvLCDWaitForBusy (void) 
{
   uint32_t stat;

   do
   {
      stat = prvLCDReadStat();            /* */
   }
   while (stat & 0x80);               /* Wait for busy flag                */
}
/****************************************************************************
* Name:		prvLCDWriteCommand
*                  	
* Description:	Write command to LCD controller.
*						
* Parameters:	cmd:* command
*					
* Return value:	none
****************************************************************************
*/
static void prvLCDWriteCommand (uint32_t cmd) 
{
	/* wait until ready */
   prvLCDWaitForBusy ();
   
   /* configure to command */
   GPIO1_IOCLR = LCD_RS;
   
   /* write command */
   prvLCDWrite(cmd);
}
/****************************************************************************
* Name:		prvLCDWriteData
*                  	
* Description:	Write data to LCD controller.
*						
* Parameters:	data to LCD
*					
* Return value:	none
****************************************************************************
*/
static void prvLCDWriteData (uint32_t data)
{
	/* wait until read */
   	prvLCDWaitForBusy ();
   	
   	/* configure to data out */
   	GPIO1_IOSET = LCD_RS;
   	
   	/* output data */
   	prvLCDWrite(data);
}

/****************************************************************************
* Name:		vLCDLoad
*                  	
* Description:	Load user-specific characters into CGRAM
*						
* Parameters:	         fp* pointer to characters
*                               cnt* number of characters	
*					
* Return value:	none
****************************************************************************
*/
void vLCDLoad (uint8_t *fp, uint32_t cnt)
{
   uint32_t i;

   prvLCDWriteCommand (0x40);                   /* Set CGRAM address counter to 0    */

   for (i = 0; i < cnt; i++, fp++)        /* */
      prvLCDWriteData (*fp);              /* */
}
/****************************************************************************
* Name:		vLCDGoto
*                  	
* Description:	Set cursor position on LCD display. Left corner: 1,1, right: 16,2 
*						
* Parameters:	         x,y* new position
*					
* Return value:	none
****************************************************************************
*/
void vLCDGoto (uint32_t x, uint32_t y)
{
   uint32_t c;

   c = --x;
   if (--y)
   {
      c |= 0x40;
   }
   
   prvLCDWriteCommand (c | 0x80);         /* */
   lcd_ptr = y*16 + x;                    /* */
}
/****************************************************************************
* Name:		vLCDCls
*                  	
* Description:	Clear LCD display, move cursor to home position.  
*						
* Parameters:	none
*					
* Return value:	none
****************************************************************************
*/
void vLCDCls (void)
{
   prvLCDWriteCommand (0x01);
   vLCDGoto(1,1);
}
/****************************************************************************
* Name:		vLCDCursorOff
*                  	
* Description:	Switch off LCD cursor. 
*						
* Parameters:	none
*					
* Return value:	none
****************************************************************************
*/
void vLCDCursorOff(void)
{
   prvLCDWriteCommand (0x0c);
}
/****************************************************************************
* Name:		vLCDsSwitchOn
*                  	
* Description:	Switch on LCD and enable cursor. 
*						
* Parameters:	none
*					
* Return value:	none
****************************************************************************
*/
void vLCDsSwitchOn(void)
{
   prvLCDWriteCommand (0x0e);
}
/****************************************************************************
* Name:		vLCDPutc
*                  	
* Description:	Print a character to LCD at current cursor position. 
*						
* Parameters:	   aChar* character to write
*					
* Return value:	none
****************************************************************************
*/
void vLCDPutc (int8_t aChar)
{ 
   if (lcd_ptr == 16)
   {
      prvLCDWriteCommand (0xc0);
   }
   prvLCDWriteData (aChar);
   lcd_ptr++;
}
/****************************************************************************
* Name:		vLCDPuts
*                  	
* Description:	Print a string to LCD at current cursor position. 
*						
* Parameters:	aString
*					Ponter to a string
*					
* Return value:	none
****************************************************************************
*/
void vLCDPuts (int8_t *aString) 
{
   while (*aString)
   {
      vLCDPutc (*aString++);
   }
}
/****************************************************************************
* Name:		vLCDBargraph
*                  	
* Description:	Print a bargraph to LCD display.
*                                  
* Parameters:	- val:  value 0..100 %
*               - size: size of bargraph 1..16
*					
* Return value:	none
****************************************************************************
*/
void vLCDBargraph (uint32_t val, uint32_t size)
{
   uint32_t i;

   val = val * size / 20;               /* Display matrix 5 x 8 pixels       */
   for (i = 0; i < size; i++)
   {
      if (val > 5)
      {
         vLCDPutc (5);
         val -= 5;
      }
      else
      {
         vLCDPutc ( (int8_t)val);
         break;
      }
   }
}
/* --------------------------------------------------------------------*/
/* RTOS implementation */
/* Structure used to pass parameters to the LED tasks. */
typedef struct LCD_PARAMETER
{
	portTickType FlashRate;	/*< The rate at which the LCD should work. */
} LCDParameter;

void vStartLCDTasks( unsigned portBASE_TYPE uxPriority )
{
LCDParameter *pxLCDParameter;
const portTickType FlashRate = 125;
/* Create and complete the structure used to pass parameter to the created task. */
	pxLCDParameter = ( LCDParameter * ) pvPortMalloc( sizeof( LCDParameter ) );
	pxLCDParameter->FlashRate = ( FlashRate );
	pxLCDParameter->FlashRate /= portTICK_RATE_halfMS;

/* Spawn the task. */
	xTaskCreate( vLCDTask, "LCD", lcdSTACK_SIZE, ( void * ) pxLCDParameter, uxPriority, NULL );
}

static void vLCDTask( void *pvParameter )
{
    LCDParameter *pxParameter;
    uint16_t Max_Count = 1;
    uint8_t* Line1 = "Counting";
    uint16_t DelayCounter = 0;
    uint16_t TheCount = 0;
    /* Stop warnings. */
	pxParameter = ( LCDParameter * ) pvParameter;
    for( ;; )
        {
        vTaskDelay( pxParameter->FlashRate / ( portTickType ) 2 );
        
        DelayCounter++;
        if (DelayCounter > Max_Count)
            {
            DelayCounter = 0;
            TheCount++;
            if (TheCount > 100)
                TheCount = 0;
            vLCDCls();
            vLCDPuts(Line1);
            vLCDGoto(1,2);
            vLCDBargraph(TheCount,16);
            }
        }
        
}


/*----------------------------------------------------------------------------
 * end of file
 *---------------------------------------------------------------------------*/

⌨️ 快捷键说明

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