Thứ Bảy, 28 tháng 4, 2018

Published tháng 4 28, 2018 by Nặc danh with 0 comment

[PIC16F877A] Thư viện LCD 4 bit

Đây là bộ thư viện LCD 4 bit dùng cho PIC16f877a mới một số thao tác trên LCD đủ dùng

#include "stddef.h"
#define LCD_RS PIN_D2
#define LCD_EN PIN_D3
#define LCD_D4 PIN_D4
#define LCD_D5 PIN_D5
#define LCD_D6 PIN_D6
#define LCD_D7 PIN_D7
// misc display defines-
#define Line_1 0x80
#define Line_2 0xC0
#define Clear_Scr 0x01
// prototype statements
#separate void LCD_Init(void); //Ham khoi tao LCD
#separate void LCD_SetPosition(unsigned int cX);//Thiet lap vi tri con tro
//Ham viet 1ky tu hoac 1 chuoi len LCD
#separate void LCD_PutChar(unsigned int cX);
#separate void LCD_PutCmd(unsigned int cX) ; //Ham gui lenh len LCD
#separate void LCD_PulseEnable(void); //Xung kich hoat
#separate void LCD_SetData(unsigned int cX); //Dat du lieu len chan Data
#separate void WriteNum(unsigned char num);
#separate void HTsothapphan(Int32 num);
// Dinh nghia Cong
#use standard_io(B)
#use standard_io(A)
//Khoi tao LCD
#separate void LCD_Init (void)
{
LCD_SetData (0x00);
Delay_ms(20); // Cho cho LCD khoi tao >> 15ms
output_low(LCD_RS); // Che do gui lenh
LCD_SetData(0x03); // Khoi tao che do 4 bit
LCD_PulseEnable();
LCD_SetData(0x02); // Tao giao dien 4 bit
LCD_PulseEnable(); // send dual nibbles hereafter, MSN first
LCD_PutCmd(0x2C); // function set (all lines, 5x7 characters)
LCD_PutCmd(0x0C); // display ON, cursor off, no blink
LCD_PutCmd(0x06); // entry mode set, increment & scroll left
LCD_PutCmd(0x01); // clear display
}
#separate void LCD_SetPosition(unsigned int cX)
{
/* this subroutine works specifically for 4-bit Port A */
LCD_SetData(swap(cX)|0x08);
LCD_PulseEnable();
LCD_SetData(swap(cX));
LCD_PulseEnable();
}
#separate void LCD_PutChar(unsigned int cX)
{
/* this subroutine works specifically for 4-bit Port A */
output_high(LCD_RS);
LCD_PutCmd(cX);
output_low(LCD_RS);
}
#separate void LCD_PutCmd(unsigned int cX)
{
/* this subroutine works specifically for 4-bit Port A */
LCD_SetData(swap(cX)); /* send high nibble */
LCD_PulseEnable();
LCD_SetData(swap(cX)); /* send low nibble */
LCD_PulseEnable();
}
#separate void LCD_PulseEnable(void)
{
output_high(LCD_EN);
delay_us(5);
output_low(LCD_EN);
delay_ms(5);
}
#separate void LCD_SetData(unsigned int cX)
{
output_bit(LCD_D4, cX & 0x01);
output_bit(LCD_D5, cX & 0x02);
output_bit(LCD_D6, cX & 0x04);
output_bit(LCD_D7, cX & 0x08);
}
#separate void WriteNum(unsigned char num)
{
unsigned char digit;
digit = num/100;
LCD_PutChar(digit + '0'); //write tram+ma ASCII cua '0'
digit = num/10;
digit = digit % 10;
LCD_PutChar(digit + '0'); //write chuc
digit = num % 100;
digit = digit % 10;
LCD_PutChar(digit + '0'); //write dvi
}
#separate void HTsothapphan(Int32 num)
{
Int8 Integer, First, Second, Third;
Integer = num/1000 + 0x30; // Tach phan nguyen
num= num%1000;
First = num/100 + 0x30; // Tach phan le thu 1
num = num%100;
Second = num/10 + 0x30; // Tach phan le thu 2
Third = num%10 + 0x30; // Tach phan le thu 3
Lcd_putcmd(0xCA);
Lcd_putchar(Integer);
Lcd_putchar(".");
Lcd_putchar(First);
Lcd_putchar(Second);
Lcd_putchar(Third);

}
      edit

0 nhận xét:

Đăng nhận xét