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

📄 freescale

📁 Freescale 系列单片机常用模块与综合系统设计
💻
字号:
#include "LCD1602.h"

#define DispMaxRows  2
#define DispMaxCols  16


const Uchar delay_cnt=80;//延时时间常数

/*延迟1 ms*/

static void Wait1ms(void) 
{
  int cnt = 0;
  while (cnt<delay_cnt) cnt++;
}

/*延迟n ms*/ 
void WaitNms(int n)
{
	Uchar i;
    for(i=1;i<=n;i++)
   		Wait1ms();
}

/*产生E信号的下降沿*/ 
void Toggle(void){             
   LCD_CONTROL_SetBit(2);/* E=1 */
   LCD_CONTROL_ClrBit(2) ; /* E=0 */     
}
/*读忙标志*/
void Lcd_Read_BF(void){ //读取BF的值,判断控制器当前是否可以接收MCU发来的数据
	Uchar temp;
	
	LCD_DATA_SetDir(0);
	while(1)
	{			
		LCD_CONTROL_ClrBit(0) ; //RS=0 
		LCD_CONTROL_SetBit(1);  //R/W=1
		LCD_CONTROL_SetBit(2);  //E=1
		temp=LCD_DATA_GetVal();
		LCD_CONTROL_ClrBit(2);			//E=0
		if ((temp&0x80)==0x00) break;
	}
}

/*写指令代码*/
void Lcd_Write_Command(Uchar command){	
 	Lcd_Read_BF();
 	LCD_DATA_SetDir(1); 
 	LCD_CONTROL_ClrBit(0);			//RS=0 	
 	LCD_CONTROL_ClrBit(1);			//R/W=0 	
 	LCD_DATA_PutVal(command);
 	Toggle(); 		
}
/*写显示数据*/
void Lcd_Write_Data(Uchar data){
	Lcd_Read_BF();
	LCD_DATA_SetDir(1); 	
	LCD_CONTROL_SetBit(0);			//RS=1
	LCD_CONTROL_ClrBit(1);			//R/W=0 	
	LCD_DATA_PutVal(data);
	Toggle();	
}

/* 初始化HD44780 LCD*/
void Lcd_Init(void)
{ 
    LCD_DATA_PutVal(0x30);
    LCD_CONTROL_ClrBit(0) ; //RS=0 
    LCD_CONTROL_ClrBit(1) ; //R/W=0
    
    Toggle();
    WaitNms(5);
    
    LCD_DATA_PutVal(0x30);
    LCD_CONTROL_ClrBit(0) ; //RS=0 
    LCD_CONTROL_ClrBit(1) ; //R/W=0
    Toggle();
    Wait1ms();
    
    LCD_DATA_PutVal(0x30);
    LCD_CONTROL_ClrBit(0) ; //RS=0 
    LCD_CONTROL_ClrBit(1) ; //R/W=0
    Toggle();
    WaitNms(2);
    
    Lcd_Write_Command(0x38);	//设置工作方式-8位总线 字符行数两行 5*7点阵        
    Lcd_Write_Command(0x08);	//设置显示状态-关显示 	
    Lcd_Write_Command(0x01);	//清屏
    WaitNms(20);    
    Lcd_Write_Command(0x06);	//设置输入方式-画面不动光标右移  
    Lcd_Write_Command(0x0F);	//设置显示状态-画面显示,光标、闪烁显示    
}

/*
*********************************************************************************************************
*                                     POSITION THE CURSOR (Internal)
*
* Description : This function positions the cursor into the LCD buffer
* Arguments   : 'row'   is the row    position of the cursor in the LCD Display
*                       'row' can be a value from 0 to 'DispMaxRows - 1'
*               'col'   is the column position of the cursor in the LCD Display
*                       'col' can be a value from 0 to 'DispMaxCols - 1'
* Returns     : none
*********************************************************************************************************
*/

static  void  Lcd_Disp_CursorSet (Uchar row, Uchar col)
{
    switch (row) {
        case 0:
             if (DispMaxRows == 1) {                       /* Handle special case when only one line   */
                 if (col < (DispMaxCols >> 1)) {
                     Lcd_Write_Data(0x80 + col);               /* First  half of the line starts at 0x80   */
                 } else {                                  /* Second half of the line starts at 0xC0   */
                     Lcd_Write_Data(0xC0 + col - (DispMaxCols >> 1));
                 }
             } else {
                 Lcd_Write_Data(0x80 + col);                   /* Select LCD's display line 1              */
             }
             break;

        case 1:
             Lcd_Write_Data(0xC0 + col);                       /* Select LCD's display line 2              */
             break;

        case 2:
             Lcd_Write_Data(0x80 + DispMaxCols + col);         /* Select LCD's display line 3              */
             break;

        case 3:
             Lcd_Write_Data(0xC0 + DispMaxCols + col);         /* Select LCD's display line 4              */
             break;
    }
}   
/*
*********************************************************************************************************
*                                         DISPLAY AN ASCII STRING
*
* Description : This function is used to display an ASCII string on a line of the LCD display
* Arguments   : 'row'   is the row    position of the cursor in the LCD Display
*                       'row' can be a value from 0 to 'DispMaxRows - 1'
*               'col'   is the column position of the cursor in the LCD Display
*                       'col' can be a value from 0 to 'DispMaxCols - 1'
*               's'     is a pointer to the string to write to the display at
*                       the desired row/col.
* Returns     : none
*********************************************************************************************************
*/

void  Lcd_Disp_Str (Uchar row, Uchar col, char *s)
{                                                       
    unsigned char i;

    if (row < DispMaxRows && col < DispMaxCols) 
    {
        Lcd_Disp_CursorSet(row, col);            /* Position cursor at ROW/COL                              */
        i = col;                            /* Set counter to limit column to maximum allowable column */
        while (i < DispMaxCols && *s) {     /* Write all chars within str + limit to DispMaxCols       */
            Lcd_Write_Data(*s++);               /* Send character to LCD display                           */
            i++;                            /* Increment limit counter                                 */
        }
        
    }
}

⌨️ 快捷键说明

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