PIC Vietnam

Go Back   PIC Vietnam > Microchip PIC > Cơ bản về vi điều khiển và PIC

Tài trợ cho PIC Vietnam
Trang chủ Đăng Kí Hỏi/Ðáp Thành Viên Lịch Bài Trong Ngày Vi điều khiển

Cơ bản về vi điều khiển và PIC Những bài hướng dẫn cơ bản nhất để làm quen với vi điều khiển PIC

 
 
Ðiều Chỉnh Xếp Bài
Prev Previous Post   Next Post Next
Old 02-11-2012, 12:50 PM   #8
handsometran
Đệ tử 2 túi
 
Tham gia ngày: Feb 2009
Bài gửi: 35
:
Trích:
Nguyên văn bởi vietnhoc View Post
e tạo rồi mà anh.nhưng nó vẫn ko chạy cho.nên e chuyển sang 8bit rồi anh ạ.hơi tốn chân tí,nhưng mà cũng dc,
thanks anh.
nếu dc thì hôm sau e hỏi anh về đọc ADC tí.
Nếu em viết bằng MPLAP IDE v8.87 HT PIC 9.8 PRO, thì em chỉ cần #include file lcd.c vào chương trình main là ok.
//*******File lcd.h(file header) được #include trong file lcd.c******//
#ifndef _LCD_H_
#define _LCD_H_

#include <htc.h>

#ifndef _XTAL_FREQ
#define _XTAL_FREQ 8000000
#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 */


/* Chổ này thay đổi tùy ý không cần thiết là chung 1 Port */
#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

//*******File lcd.c được #include trong file main.c******//
//**Chứa các lệnh điều khiển lcd 16x2**************//
#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);
//lcd_put_byte(0,2);
while(lcd_busy());
break;
default:
if(isprint(c)){
lcd_put_byte(8, c);
while(lcd_busy());
}
break;
}
}
//================================================== ============//
void lcd_gotoxy( char x, char y)
{char address;
switch(y) {
case 0 : address=0x80;break;//row=1,col=0
case 1 : address=0xc0;break;//row=2,col=0
case 2 : address=0x94;break;//row=1.col=20
case 3 : address=0xd4;break;//row=2,col=84
}
address=address+x;//move col to expect position
lcd_put_byte(0,0x80|address);
}
//================================================== ============//
/*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++);
}
}
//================================================== ==
/*void LCD_ShiftLeft(void)
{
lcd_put_byte(0,0x18);
}
void LCD_ShiftRight(void)
{
lcd_put_byte(0,0x1C);
}*/

void lcd_moveright(unsigned char p){
char i;
for(i=0;i<p;i++){
lcd_put_byte(0,28);
__delay_ms(100);
}
}

void lcd_moveleft(unsigned char p){
char i;
for(i=0;i<p;i++){
lcd_put_byte(0,24);
__delay_ms(100);
}
}
//Chúc bạn thành công!
handsometran vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
 


Quyền Sử Dụng Ở Diễn Ðàn
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is Mở
Smilies đang Mở
[IMG] đang Mở
HTML đang Tắt

Chuyển đến


Múi giờ GMT. Hiện tại là 05:04 AM.


Được sáng lập bởi Đoàn Hiệp
Powered by vBulletin®
Page copy protected against web site content infringement by Copyscape
Copyright © PIC Vietnam