Thông báo

Collapse
No announcement yet.

Cho mình hỏi 1 chút về đoạn mã lập trình C hiển thị LCD

Collapse
X
 
  • Lọc
  • Giờ
  • Show
Clear All
new posts

  • Cho mình hỏi 1 chút về đoạn mã lập trình C hiển thị LCD

    Mình đang cố đọc hiểu cách lập trình LCD qua C, nhưng mình không hiểu đoạn này lắm, rất mong được giải đáp:
    Code:
     /* line1 to display on LCD */
     code	unsigned char  *line1 = " noi dung 1 ";
     code	unsigned char  *line2 = "    noi dung 2    ";
     code	unsigned char  *line3 = "   noi dung 3   ";
     code	unsigned char  *line4 = "     noi dung 4     ";
    mã lệnh "code" dùng để làm gì? và dấu "*" để diễn đạt điều gì?
    Đoạn 2,
    Code:
    void LCD_PutCmd(unsigned char cmd)
    {
    	WaitReady(); 		// Wait for LCD ready
    	WriteCommand;		// set LCD to send mode
    	LCD_PORT = cmd; 	// Prepare to send
    	LatchData;			// latch data out
    }
    Nhất là cái câu LCD_PORT=cmd;
    Đoạn 3:
    Code:
    void LCD_PrString(char *str)
    {
    	while(*str!='\0')
    	{
    		LCD_PutChar(*str);
    		++str;
    	}
    }
    Đặt tên biến kiểu *str là như thế nào?
    Cả bài đó đây:
    Code:
    /****************************************************************************/
    /*																		    */
    /*	File:	lcd.c															*/
    /*	Author: Vu Ha Linh,	Automatic Control Deparment							*/
    /*			Ha Noi university of technology									*/
    /*																			*/
    /****************************************************************************/
    /*																			*/
    /*	Function:	This module is a standard LCD (16x2) functions				*/
    /*																			*/
    /*			Input and Output parameters of those subroutines:				*/
    /*			void LCD_Init(void);											*/
    /*			void LCD_Position(unsigned char, unsigned char);				*/
    /*		 	void LCD_PutCmd(unsigned char);									*/
    /*			void LCD_PutChar(unsigned char);								*/
    /*			void LCD_PrString(char*);										*/
    /*			void LCD_Clear(void);											*/
    /*			void LCD_PrInteger(int);										*/
    /*																			*/
    /*	Note:	Must be call LCD_Init() first at the begining of program		*/
    /*																			*/	
    /****************************************************************************/
    /*																			*/
    /*	History:																*/	
    /*	Version:																*/
    /*			Date			Writer			Remark							*/
    /*																			*/
    /*			30-07-2006		Vu Ha Linh		Creating the first version		*/
    /*																			*/	
    /*																			*/		
    /****************************************************************************/
    
    #include <REGX51.H>
    #include "lcd.h"
    
    /*****************************************************************************
    	define Hardware address
    	it can changed by user
    *****************************************************************************/
    #define LCD_PORT	P1
    
    sbit LCD_RS = P3^2;
    sbit LCD_RW	= P3^3;
    sbit LCD_EN = P3^4;
    sbit LCD_D7 = P1^7;
    
    #define ReadCommand		{LCD_RS = 0; LCD_RW = 1;}
    #define WriteCommand	{LCD_RS = 0; LCD_RW = 0;}
    #define WriteData		{LCD_RS = 1; LCD_RW = 0;}
    #define LatchData		{LCD_EN = 1; LCD_EN = 0;}
    
    /****************************************************************************
    	WaitReady function
    	Wait while LCD in execution time
    *****************************************************************************/
    void WaitReady(void)
    {
    	ReadCommand; 			// Set Read command mode
    	LCD_D7 = 1;
    	while(LCD_D7){
    		LCD_EN = 0;
    		LCD_EN = 1; 		// Read bit D7
    	}
    }
    
    /****************************************************************************
    	LCD_PutCmd function
    	Write a control byte to LCD
    	Input : control byte
    *****************************************************************************/
    void LCD_PutCmd(unsigned char cmd)
    {
    	WaitReady(); 		// Wait for LCD ready
    	WriteCommand;		// set LCD to send mode
    	LCD_PORT = cmd; 	// Prepare to send
    	LatchData;			// latch data out
    }
    
    /****************************************************************************
    	LCD_PutChar function
    	Write a ASCII symbol to LCD
    	Input: ASCII symbol
    *****************************************************************************/
    void LCD_PutChar(unsigned char ch)
    {
    	WaitReady();
    	WriteData;
    	LCD_PORT = ch;
    	LatchData;		
    }
    
    /*****************************************************************************
    	LCD_PrString function
    	Write a string to LCD
    	Input: string
    ******************************************************************************/
    void LCD_PrString(char *str)
    {
    	while(*str!='\0')
    	{
    		LCD_PutChar(*str);
    		++str;
    	}
    }
    
    /*****************************************************************************
    	LCD_Position function
    	Set the position of cursor on LCD
    	Input: row, column
    ******************************************************************************/
    void LCD_Position(unsigned char row, unsigned char col)
    {
    	LCD_PutCmd( (1<<7)|(row<<6)|col );
    }
    
    /*****************************************************************************
    	LCD_Clear function
    	Clear LCD
    ******************************************************************************/
    void LCD_Clear()
    {
    	LCD_PutCmd(LCD_CLEAR);	
    }
    
    /*****************************************************************************
    	LCD_Init function
    	Prepare for display content. Must be called at first
    ******************************************************************************/
    void LCD_Init()
    {
    	LCD_PutCmd(0x30);				// Data length setting, 8 bits
    	LCD_PutCmd(0x30);				// Data length setting, 8 bits
    	LCD_PutCmd(0x30);				// Data length setting, 8bits
    
    	LCD_PutCmd(LCD_FONT);			// Function set: 8 bits, 2 lines, font 5x7		
    	LCD_PutCmd(LCD_OFF);			// Display and cursor OFF
    	LCD_PutCmd(LCD_CLEAR);			// Clear display
    	LCD_PutCmd(LCD_MOVE_CURSOR);	// Entry mode set: Increment mode, not shift
    	LCD_PutCmd(LCD_DISPLAY_ON);		// Display ON
    //	LCD_PutCmd(LCD_BLINK_CURSOR);	// Blink cursor
    }
    
    /****************************************************************************
    	LCD_PrInteger function
    	Display a integer decimal number on LCD
    	Input: a integer number
    *****************************************************************************/
    void LCD_PrInteger(int num)
    {
    	int temp;
    	unsigned char i, c[5];
    		
    	temp = num;
    	if (temp != 0) {
    		if (temp < 0) {
    			LCD_PutChar('-');
    			temp = -temp;			
    		}
    		i = 0;
    		while(temp){
    			c[i++] = temp%10; 
    			temp /= 10;
    		}
    		while(i) LCD_PutChar(c[--i] + '0');
    	}
    	else LCD_PutChar('0');
    }
    
    /************************* END OF FILE *******************************/

  • #2
    Nguyên văn bởi firefoxvn Xem bài viết
    Mình đang cố đọc hiểu cách lập trình LCD qua C, nhưng mình không hiểu đoạn này lắm, rất mong được giải đáp:
    Code:
     /* line1 to display on LCD */
     code	unsigned char  *line1 = " noi dung 1 ";
     code	unsigned char  *line2 = "    noi dung 2    ";
     code	unsigned char  *line3 = "   noi dung 3   ";
     code	unsigned char  *line4 = "     noi dung 4     ";
    mã lệnh "code" dùng để làm gì? và dấu "*" để diễn đạt điều gì?
    Đoạn 2,
    Code:
    void LCD_PutCmd(unsigned char cmd)
    {
    	WaitReady(); 		// Wait for LCD ready
    	WriteCommand;		// set LCD to send mode
    	LCD_PORT = cmd; 	// Prepare to send
    	LatchData;			// latch data out
    }
    Nhất là cái câu LCD_PORT=cmd;
    Đoạn 3:
    Code:
    void LCD_PrString(char *str)
    {
    	while(*str!='\0')
    	{
    		LCD_PutChar(*str);
    		++str;
    	}
    }
    Đặt tên biến kiểu *str là như thế nào?
    Cả bài đó đây:
    Code:
    /****************************************************************************/
    /*																		    */
    /*	File:	lcd.c															*/
    /*	Author: Vu Ha Linh,	Automatic Control Deparment							*/
    /*			Ha Noi university of technology									*/
    /*																			*/
    /****************************************************************************/
    /*																			*/
    /*	Function:	This module is a standard LCD (16x2) functions				*/
    /*																			*/
    /*			Input and Output parameters of those subroutines:				*/
    /*			void LCD_Init(void);											*/
    /*			void LCD_Position(unsigned char, unsigned char);				*/
    /*		 	void LCD_PutCmd(unsigned char);									*/
    /*			void LCD_PutChar(unsigned char);								*/
    /*			void LCD_PrString(char*);										*/
    /*			void LCD_Clear(void);											*/
    /*			void LCD_PrInteger(int);										*/
    /*																			*/
    /*	Note:	Must be call LCD_Init() first at the begining of program		*/
    /*																			*/	
    /****************************************************************************/
    /*																			*/
    /*	History:																*/	
    /*	Version:																*/
    /*			Date			Writer			Remark							*/
    /*																			*/
    /*			30-07-2006		Vu Ha Linh		Creating the first version		*/
    /*																			*/	
    /*																			*/		
    /****************************************************************************/
    
    #include <REGX51.H>
    #include "lcd.h"
    
    /*****************************************************************************
    	define Hardware address
    	it can changed by user
    *****************************************************************************/
    #define LCD_PORT	P1
    
    sbit LCD_RS = P3^2;
    sbit LCD_RW	= P3^3;
    sbit LCD_EN = P3^4;
    sbit LCD_D7 = P1^7;
    
    #define ReadCommand		{LCD_RS = 0; LCD_RW = 1;}
    #define WriteCommand	{LCD_RS = 0; LCD_RW = 0;}
    #define WriteData		{LCD_RS = 1; LCD_RW = 0;}
    #define LatchData		{LCD_EN = 1; LCD_EN = 0;}
    
    /****************************************************************************
    	WaitReady function
    	Wait while LCD in execution time
    *****************************************************************************/
    void WaitReady(void)
    {
    	ReadCommand; 			// Set Read command mode
    	LCD_D7 = 1;
    	while(LCD_D7){
    		LCD_EN = 0;
    		LCD_EN = 1; 		// Read bit D7
    	}
    }
    
    /****************************************************************************
    	LCD_PutCmd function
    	Write a control byte to LCD
    	Input : control byte
    *****************************************************************************/
    void LCD_PutCmd(unsigned char cmd)
    {
    	WaitReady(); 		// Wait for LCD ready
    	WriteCommand;		// set LCD to send mode
    	LCD_PORT = cmd; 	// Prepare to send
    	LatchData;			// latch data out
    }
    
    /****************************************************************************
    	LCD_PutChar function
    	Write a ASCII symbol to LCD
    	Input: ASCII symbol
    *****************************************************************************/
    void LCD_PutChar(unsigned char ch)
    {
    	WaitReady();
    	WriteData;
    	LCD_PORT = ch;
    	LatchData;		
    }
    
    /*****************************************************************************
    	LCD_PrString function
    	Write a string to LCD
    	Input: string
    ******************************************************************************/
    void LCD_PrString(char *str)
    {
    	while(*str!='\0')
    	{
    		LCD_PutChar(*str);
    		++str;
    	}
    }
    
    /*****************************************************************************
    	LCD_Position function
    	Set the position of cursor on LCD
    	Input: row, column
    ******************************************************************************/
    void LCD_Position(unsigned char row, unsigned char col)
    {
    	LCD_PutCmd( (1<<7)|(row<<6)|col );
    }
    
    /*****************************************************************************
    	LCD_Clear function
    	Clear LCD
    ******************************************************************************/
    void LCD_Clear()
    {
    	LCD_PutCmd(LCD_CLEAR);	
    }
    
    /*****************************************************************************
    	LCD_Init function
    	Prepare for display content. Must be called at first
    ******************************************************************************/
    void LCD_Init()
    {
    	LCD_PutCmd(0x30);				// Data length setting, 8 bits
    	LCD_PutCmd(0x30);				// Data length setting, 8 bits
    	LCD_PutCmd(0x30);				// Data length setting, 8bits
    
    	LCD_PutCmd(LCD_FONT);			// Function set: 8 bits, 2 lines, font 5x7		
    	LCD_PutCmd(LCD_OFF);			// Display and cursor OFF
    	LCD_PutCmd(LCD_CLEAR);			// Clear display
    	LCD_PutCmd(LCD_MOVE_CURSOR);	// Entry mode set: Increment mode, not shift
    	LCD_PutCmd(LCD_DISPLAY_ON);		// Display ON
    //	LCD_PutCmd(LCD_BLINK_CURSOR);	// Blink cursor
    }
    
    /****************************************************************************
    	LCD_PrInteger function
    	Display a integer decimal number on LCD
    	Input: a integer number
    *****************************************************************************/
    void LCD_PrInteger(int num)
    {
    	int temp;
    	unsigned char i, c[5];
    		
    	temp = num;
    	if (temp != 0) {
    		if (temp < 0) {
    			LCD_PutChar('-');
    			temp = -temp;			
    		}
    		i = 0;
    		while(temp){
    			c[i++] = temp%10; 
    			temp /= 10;
    		}
    		while(i) LCD_PutChar(c[--i] + '0');
    	}
    	else LCD_PutChar('0');
    }
    
    /************************* END OF FILE *******************************/
    *str chỉ kiểu dữ liệu là xâu ký tự (chuỗi ký tự)
    LCD_PORT=cmd; để ra lệnh chọn chế độ làm việc hoặc các lệnh điều khiển khác của LCD. Các lệnh này cần tìm hiểu kỹ ở bảng lệnh LCD như xóa màn hiển thị, đặt chế độ dịch chuyển con trỏ, dịch chuyển hiển thị sang phải hay trái...
    Muốn làm việc với LCD bạn phải đọc kỹ các lệnh của LCD đã.Chúc thành công!

    Comment

    Về tác giả

    Collapse

    firefoxvn Tìm hiểu thêm về firefoxvn

    Bài viết mới nhất

    Collapse

    Đang tải...
    X