View Single Post
Old 12-11-2011, 12:00 AM   #7
delta21
Đệ tử 5 túi
 
delta21's Avatar
 
Tham gia ngày: Mar 2010
Bài gửi: 95
:
Send a message via Yahoo to delta21
em vẫn chưa biết làm thế nào để xuất là 1 biến (không phải kiểu string)
trước em có dùng trình dịch HTPIC cho 16f887 nó có 2 file thư viện LCD như này(cái này là em sưu tầm trên mạng)
lcd.h:
Code:
#ifndef _LCD_H_
#define _LCD_H_

#include <htc.h>

#ifndef _XTAL_FREQ
#define _XTAL_FREQ	4000000
#endif


/* Display ON/OFF Control defines */
#define DON         0b00001111  /* Display on      */
#define DOFF        0b00001011  /* Display off     */
#define CURSOR_ON   0b00001111  /* Cursor on       */
#define CURSOR_OFF  0b00001101  /* Cursor off      */
#define BLINK_ON    0b00001111  /* Cursor Blink    */
#define BLINK_OFF   0b00001110  /* Cursor No Blink */

/* Cursor or Display Shift defines */
#define SHIFT_CUR_LEFT    0b00000100  /* Cursor shifts to the left   */
#define SHIFT_CUR_RIGHT   0b00000101  /* Cursor shifts to the right  */
#define SHIFT_DISP_LEFT   0b00000110  /* Display shifts to the left  */
#define SHIFT_DISP_RIGHT  0b00000111  /* Display shifts to the right */

/* Function Set defines */
#define FOUR_BIT   0b00101100  /* 4-bit Interface               */
#define EIGHT_BIT  0b00111100  /* 8-bit Interface               */
#define LINE_5X7   0b00110000  /* 5x7 characters, single line   */
#define LINE_5X10  0b00110100  /* 5x10 characters               */
#define LINES_5X7  0b00111000  /* 5x7 characters, multiple line */


/* Pins mapping */
#ifndef		LCD_RS
#define	LCD_RS		RD1
#define	LCD_EN		RD3
#define	LCD_RW		RD2
#define	LCD_DATA4	RD4
#define	LCD_DATA5	RD5
#define	LCD_DATA6	RD6
#define	LCD_DATA7	RD7

#define	LCD_RS_TRIS		TRISD1
#define	LCD_EN_TRIS		TRISD3
#define	LCD_RW_TRIS		TRISD2
#define	LCD_DATA4_TRIS	TRISD4
#define	LCD_DATA5_TRIS	TRISD5
#define	LCD_DATA6_TRIS	TRISD6
#define	LCD_DATA7_TRIS	TRISD7
#endif

//typedef unsigned char		unsigned char;				// 8-bit unsigned

typedef union _BYTE_VAL
{
    unsigned char Val;
    struct
    {
        unsigned char b0:1;
        unsigned char b1:1;
        unsigned char b2:1;
        unsigned char b3:1;
        unsigned char b4:1;
        unsigned char b5:1;
        unsigned char b6:1;
        unsigned char b7:1;
    } bits;
} BYTE_VAL;

void lcd_init();
unsigned char lcd_busy();
unsigned char lcd_get_byte(unsigned char rs);
void lcd_put_byte(unsigned char a,unsigned char b);
void lcd_gotoxy(unsigned char col, unsigned char row);
void lcd_putc(char c);
void lcd_puts(const char* s);
#endif

lcd.c
Code:
#include <stdlib.h>
#include <ctype.h>
#include "lcd.h"

void lcd_init(){
	unsigned char i;
	LCD_EN_TRIS = 0;
	LCD_RS_TRIS = 0;
	LCD_RW_TRIS = 0;
	LCD_DATA4_TRIS = 0;
	LCD_DATA5_TRIS = 0;
	LCD_DATA6_TRIS = 0;
	LCD_DATA7_TRIS = 0;
	LCD_EN = 0;
	LCD_RS = 0;
	LCD_RW = 0;

	__delay_ms(100);				// delay for power on
	
	// reset LCD
	lcd_put_byte(0,0x30);
	__delay_ms(50);
	lcd_put_byte(0,0x30);
	__delay_ms(50);
	lcd_put_byte(0,0x32);
	__delay_ms(100);				// delay for LCD reset
	__delay_ms(100);				// delay for LCD reset
	__delay_ms(100);				// delay for LCD reset
	
	while(lcd_busy());
	lcd_put_byte(0,FOUR_BIT & LINES_5X7);			// Set LCD type
	while(lcd_busy());
	
	lcd_put_byte(0,DOFF&CURSOR_OFF&BLINK_OFF);		// display off
	while(lcd_busy());
	lcd_put_byte(0,DON&CURSOR_OFF&BLINK_OFF);		// display on
	while(lcd_busy());
	
	lcd_put_byte(0,0x01);							// clear display and move cursor to home
	while(lcd_busy());
	lcd_put_byte(0,SHIFT_CUR_LEFT);				// cursor shift mode
	while(lcd_busy());
	lcd_put_byte(0,0x01);							// clear display and move cursor to home
	while(lcd_busy());
}
unsigned char lcd_busy()
{
	unsigned char busy;
	
	LCD_DATA4_TRIS = 1;
	LCD_DATA5_TRIS = 1;
	LCD_DATA6_TRIS = 1;
	LCD_DATA7_TRIS = 1;
	
	LCD_RW = 1;
	LCD_RS = 0;
	__delay_us(20);
	LCD_EN = 1;
	__delay_us(20);
	
	busy = LCD_DATA7;
	
	LCD_EN = 0;
	__delay_us(20);
	LCD_EN = 1;
	__delay_us(20);
	LCD_EN = 0;

	
	return busy;
}
unsigned char lcd_get_byte(unsigned char rs)
{
	BYTE_VAL b;
	
	LCD_DATA4_TRIS = 1;
	LCD_DATA5_TRIS = 1;
	LCD_DATA6_TRIS = 1;
	LCD_DATA7_TRIS = 1;
	
	LCD_RW = 1;
	LCD_RS = 0;
	if(rs) LCD_RS = 1;
	__delay_us(20);
	LCD_EN = 1;
	__delay_us(20);
	
	b.bits.b7 = LCD_DATA7;
	b.bits.b6 = LCD_DATA6;
	b.bits.b5 = LCD_DATA5;
	b.bits.b4 = LCD_DATA4;
	
	LCD_EN = 0;
	__delay_us(20);
	LCD_EN = 1;
	__delay_us(20);
	b.bits.b3 = LCD_DATA7;
	b.bits.b2 = LCD_DATA6;
	b.bits.b1 = LCD_DATA5;
	b.bits.b0 = LCD_DATA4;
	LCD_EN = 0;
	
	return b.Val;
}
void lcd_put_byte(unsigned char rs, unsigned char b)
{
	BYTE_VAL temp;
	
	LCD_DATA4_TRIS = 0;
	LCD_DATA5_TRIS = 0;
	LCD_DATA6_TRIS = 0;
	LCD_DATA7_TRIS = 0;
	
	LCD_RS = 0;
	if(rs) LCD_RS = 1;
	
	__delay_us(20);
	LCD_RW = 0;
	__delay_us(20);
	LCD_EN = 0;
	
	temp.Val = b;
	
	// send the high nibble
	LCD_DATA4 = temp.bits.b4;
	LCD_DATA5 = temp.bits.b5;
	LCD_DATA6 = temp.bits.b6;
	LCD_DATA7 = temp.bits.b7;
	__delay_us(20);
	LCD_EN =  1;
	__delay_us(20);
	LCD_EN = 0;
	// send the low nibble
	LCD_DATA4 = temp.bits.b0;
	LCD_DATA5 = temp.bits.b1;
	LCD_DATA6 = temp.bits.b2;
	LCD_DATA7 = temp.bits.b3;
	__delay_us(20);
	LCD_EN =  1;
	__delay_us(20);
	LCD_EN = 0;
}
void lcd_putc(char c){
	switch(c){
		case '\f':
			lcd_put_byte(0, 1);
			while(lcd_busy());
			break;
		case '\n':
			lcd_gotoxy(0, 1);
			break;
		default:
			if(isprint(c)){
				lcd_put_byte(1, c);
				while(lcd_busy());
			}
			break;
	}
}

void lcd_gotoxy(unsigned char col, unsigned char row)
{
	unsigned char address;
	
	if(row!=0)
		address=0x40;
	else
		address=0;
	
	address += col;
	lcd_put_byte(0,0x80|address);
	while(lcd_busy());
}
void lcd_puts(const char* s)
{
	while(*s){
		lcd_putc(*s++);
	}
}
với 2 file này em có thể dùng LCD khá dễ dang với lệnh printf ,như trong C chuẩn.
Anh R có thể chỉ cho em cách sửa 2 file này để dùng cho pic 18 dc ko ??
hoặc làm sao để dùng dc với thư viện Lcd của anh!!
thanks anh!!
__________________
Chỉ sợ thiếu tiền......
delta21 vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn