23-06-2007, 07:55 PM | #1 |
Đệ tử 9 túi
|
HTPIC18 Các chương trình cho PIC18
Chào các bạn. Mình mới thi xong nên upload vài code về HTPIC18 cho các bạn.
Đầu tiên là thư viện LCD full cho 8 bit và 4 bit mode. Mình đã test thành công thư viện và chạy rất ok. Mình dùng PIC18F2620. Trình dịch : HTPIC18 V9.50 Sơ đồ chân kết nối với LCD. Đầu tiên là hàm LCD.h Code:
// LCD.h #ifndef LCD_H #define LCD_H # include "pic18.h" // Defines # define TRUE 1 # define FALSE 0 /*====================================================================================== Define LCD_PORT ======================================================================================*/ #define LCD_DATA_PORT PORTB // Port on which LCD data lines are connected #define LCD_TRIS_PORT TRISB // Need to specify the corresponding TRIS # define LCD_RS RB0 # define LCD_RW RB1 # define LCD_E RB2 # define LCD_TRIS_RS TRISB0 # define LCD_TRIS_RW TRISB1 # define LCD_TRIS_E TRISB2 //============== Using LCD - 2 line, 16 colum #define LCD_MODE_2x16 (TRUE) // HDM16216H-2, HDM16216H-4, HDM16216H-5, HDM16216H-B, HDM16216H-D, HDM16216L-2, HDM16216L-5, HDM16216L-7, HDM16216L-D, HDM16216L-B, HDM16216H-I // Define for LCD_DISPLAY_CONTROL #define LCD_DISPLAY_ON (TRUE) // Turn display on/off #define LCD_CURSOR_BLINK (FALSE) // Blink/Noblink cursor mode #define LCD_CURSOR_ON (TRUE) // Cursor visible // Define for LCD_ENTRY_MODE #define LCD_CURSOR_INCREMENT (TRUE) // Set left-to-right cursor movement #define LCD_CURSOR_SHIFT (FALSE) // Shift display on entry // Other user defines #define LCD_ALLOW_USER_CHARS (TRUE) // Controls whether display uses ASCII for control chars or uses user-defined chars in lcd_putc() #define LCD_ENABLE_GETC (TRUE) // Save code space by setting to FALSE #define LCD_ENABLE_GOTOXY (TRUE) // any functions which you will not #define LCD_ENABLE_PRINTF (TRUE) // need in your application. #define LCD_ENABLE_UNSCROLL (TRUE) #define LCD_ENABLE_SCROLL (TRUE) #define LCD_ENABLE_CLEAR (TRUE) #if LCD_MODE_2x16 #define LCD_MAXROWS ((unsigned char)(2)) #define LCD_MAXCOLS ((unsigned char)(16)) # define LCD_MULTI_LINE (TRUE) #endif // Define 4 Bit or 8Bit mode #define LCD_8_BIT_MODE (FALSE) #define LCD_4_BIT_MODE (!LCD_8_BIT_MODE) // Define LCD_TRIS_DATAMASK #if LCD_4_BIT_MODE #define LCD_TRIS_DATAMASK (0b11110000) // Define the bitmask used to read/write the data bits #define LCD_D4_BIT 4 // Attachment of D0 to data port bus - note #else #define LCD_TRIS_DATAMASK (0b11111111) // Define all bits used for 8-bit mode #endif // ======================= Define command code #define LCD_COMMAND_CLEAR ((unsigned char)(0x01)) //1.53 ms // Clear screen, home cursor, unshift display #define LCD_COMMAND_HOME ((unsigned char)(0x02)) //1.53 ms // Home cursor, unshift display #define LCD_COMMAND_BACKSPACE ((unsigned char)(0x10)) // Move cursor left one (destructive based on LCD_DESTRUCTIVE_BS) #define LCD_COMMAND_FWDSPACE ((unsigned char)(0x14)) // Move cursor right one #define LCD_COMMAND_PANLEFT ((unsigned char)(0x18)) // Move screen left one #define LCD_COMMAND_PANRIGHT ((unsigned char)(0x1C)) // Move screen right one // ==================== Declare some functions void delay_us (unsigned char us); void delay_ms(unsigned char ms); unsigned char LCD_getByte (void); void LCD_PrByte (unsigned char c); void LCD_Command(unsigned char c); void LCD_PrChar (unsigned char c); void LCD_gotoXY(unsigned char row, unsigned char col); unsigned char LCD_getChar (void); // Read character at cursor void LCD_Init(void); void LCD_PrString (const char* message); #if (LCD_4_BIT_MODE) void lcd_putnybble (unsigned char c); # endif #endif Code:
//LCD.c # include "lcd.h" # include "pic18.h" // ==== Some constants for LCD 16x2 const unsigned char const LCD_ROW_ADDRESS[] = // Row/Column information for LCD_gotoxy() { 0x00, // Line 1 0x40 // Line 2 }; const unsigned char const LCD_INIT_STRING [] = // LCD Init String on powerup { 0b00000001, // Clear display 0b00000010, // Home cursor 0b00000100 // Entry Mode #if LCD_CURSOR_INCREMENT | 0b00000010 // Increment cursor #endif #if LCD_CURSOR_SHIFT | 0b00000001 // Shift on cursor #endif , // end 0b00001000 // Display Control #if LCD_DISPLAY_ON | 0b00000100 // Display on #endif #if LCD_CURSOR_ON | 0b00000010 // Cursor on #endif #if LCD_CURSOR_BLINK | 0b00000001 // Blink on #endif , // end 0b00100000 // Function Set #if LCD_8_BIT_MODE | 0b00010000 // 8-bit data bus #endif #if LCD_MULTI_LINE | 0b00001000 // 2-line refreshing #endif #if LCD_DISPLAY_5x10 | 0b00000100 // 5x10 matrix #endif }; const unsigned char const LCD_ENTRY_MODE ={ 0b00000100 #if LCD_CURSOR_INCREMENT | 0b00000010 // Increment cursor #endif #if LCD_CURSOR_SHIFT | 0b00000001 // Shift on cursor #endif } ; const unsigned char const LCD_FUNCTION_SET = { 0b00100000 // Function Set #if LCD_8_BIT_MODE | 0b00010000 // 8-bit data bus #endif #if LCD_MULTI_LINE | 0b00001000 // 2-line refreshing #endif #if LCD_DISPLAY_5x10 | 0b00000100 // 5x10 matrix #endif }; const unsigned char const LCD_DISPLAY_CONTROL = { 0b00001000 // Display Control #if LCD_DISPLAY_ON | 0b00000100 // Display on #endif #if LCD_CURSOR_ON | 0b00000010 // Cursor on #endif #if LCD_CURSOR_BLINK | 0b00000001 // Blink on #endif }; //========================================================================================== void delay_us (unsigned char us){ while(us--){ asm("nop"); asm("nop"); }; } //========================================================================================== void delay_ms(unsigned char ms){ unsigned char i, j; while(ms--){ for (i = 0; i < 20; i++) for (j = 0; j < 100; j++) asm("nop"); }; } // ============================================================================================ unsigned char LCD_getByte (void){ unsigned char retval; // Return value #if (LCD_4_BIT_MODE) unsigned char highbits ; LCD_TRIS_PORT |= LCD_TRIS_DATAMASK; // Set port to read mode for data pins LCD_RW = 1; // Tell LCD we want to read delay_ms (1); LCD_E = 1; highbits = (((LCD_DATA_PORT & LCD_TRIS_DATAMASK) >> LCD_D4_BIT) << 4);// Grab high bits and shift to right place LCD_E = 0; delay_ms (1); LCD_E = 1; delay_ms (1); retval = ((LCD_DATA_PORT & LCD_TRIS_DATAMASK) >> LCD_D4_BIT); // Grab low bits LCD_E = 0; retval |= highbits; LCD_TRIS_PORT &= ~LCD_TRIS_DATAMASK; // Set port back to output mode # else LCD_TRIS_PORT = 0xFF; // Set port to all input LCD_RW = 1; // Tell LCD we want to read delay_ms (2); LCD_E = 1; // Do the read delay_ms (2); retval = LCD_DATA_PORT; LCD_E = 0; LCD_TRIS_PORT = 0x00; // Set port back to outputs # endif return (retval); } //==================================================================================================== void LCD_PrByte (unsigned char c){ // Write byte to port in current RS mode unsigned char RS_Status; RS_Status = LCD_RS; // Get old pin state LCD_RS = 0; // Force into command mode to read state while (LCD_getByte () & 0x80); // Wait for read state if (RS_Status) LCD_RS = 1; // Restore RS to old state delay_ms (1); LCD_RW = 0; // Set to write mode delay_us (1); #if LCD_4_BIT_MODE lcd_putnybble (c >> 4); // Send the character out lcd_putnybble (c); #else LCD_DATA_PORT = c; // Send the character out LCD_E = 1; delay_us (1); LCD_E = 0; #endif } //================================================================================================ /*Ham yeu cau goi lenh dieu khien LCD*/ void LCD_Command(unsigned char c){ // Send command to LCD port LCD_RS = 0; LCD_PrByte(c); } //================================================================================================ /*Ham yeu cau goi du lieu hien thi len LCD*/ void LCD_PrChar (unsigned char c){ // Write character to LCD LCD_RS = 1 ; LCD_PrByte(c); } //================================================================================================= void LCD_gotoXY(unsigned char Row, unsigned char Col){ if (Row > LCD_MAXROWS) // Range limit Row = LCD_MAXROWS; if (Col > LCD_MAXCOLS) Col = LCD_MAXCOLS; Row = LCD_ROW_ADDRESS[Row-1]; // Get address of first byte on desired row Row += Col - 1; LCD_Command (0x80 | Row); // Write new cursor address } // ===================================================================================== unsigned char LCD_getChar (void) // Read character at cursor { unsigned char retval; // Return value LCD_RS = 1; retval = LCD_getByte (); LCD_RS = 0; return (retval); } //======================================================================================= void LCD_Init(void){ unsigned char i; LCD_TRIS_PORT &= ~LCD_TRIS_DATAMASK; // Set data bus to output LCD_TRIS_E = 0; LCD_TRIS_RW = 0; LCD_TRIS_RS = 0; LCD_E = 0 ; LCD_RS = 0; LCD_RW = 0; delay_ms (15); // Tao tre 15ms cho LCD khoi dong #if LCD_4_BIT_MODE // Set LCD into 4-bit mode lcd_putnybble(0b0010); delay_ms(10); lcd_putnybble(0b0010); //delay_ms(10); LCD_PrByte (LCD_FUNCTION_SET); //Function set ; DL = 0(4 bits) , N = 1 (2 lines), F = 0 (5x8 dots) LCD_PrByte (LCD_DISPLAY_CONTROL); // Display on/off control LCD_PrByte (LCD_ENTRY_MODE); // Entry mode set LCD_PrByte(LCD_COMMAND_CLEAR); LCD_PrByte(LCD_COMMAND_HOME); #else LCD_PrByte (LCD_FUNCTION_SET); //Function set ; DL = 1(8 bits) , N = 1 (2 lines), F = 0 (5x8 dots) LCD_PrByte (LCD_DISPLAY_CONTROL); // Display on/off control LCD_PrByte (LCD_ENTRY_MODE); // Entry mode set LCD_PrByte(LCD_COMMAND_CLEAR); delay_ms(2); LCD_PrByte(LCD_COMMAND_HOME); delay_ms(2); #endif } // =================================================================================================== void LCD_PrString (const char* message){ // Write message to LCD (C string type) while (*message) // Look for end of string LCD_PrChar (*message++); // Show and bump } //================================================================================================ #if LCD_4_BIT_MODE void lcd_putnybble (unsigned char c) // Write nybble to port in current RS mode { c = c << LCD_D4_BIT; // Shift over to correct bit column c &= LCD_TRIS_DATAMASK; // Remove any extraneous bits LCD_DATA_PORT = (LCD_DATA_PORT & ~LCD_TRIS_DATAMASK) | c; // Write data bits to port delay_us (1); LCD_E = 1; // Start to write it delay_us (1); LCD_E = 0; // Finish write cycle } #endif Code:
/*;======================================================== /*; Ten chuong trinh : Test IO LCD ; Nguoi thuc hien : Ngo Hai Bac (NOHB) ; Ngay thuc hien : 23/06/07 ; Phien ban : 1.0 ; Mo ta phan cung : Dung PIC18F2620s - thach anh 20MHz ; Trinh dich : HTPIC 18V9.50 ; ;---------------------------------------------------------------- ; Ngay hoan thanh : ; Ngay kiem tra : ; Nguoi kiem tra : ;---------------------------------------------------------------- ; Chu thich : ;========================================================*/ // Include # include "pic18.h" # include "LCD.h" // configuration __CONFIG(1,HS & FCMDIS & IESODIS); __CONFIG(2,BORV21 & PWRTDIS & WDTDIS); __CONFIG(3,MCLREN & LPT1DIS & PBANDIS); __CONFIG(4,DEBUGDIS & LVPDIS & STVRDIS & XINSTDIS); __CONFIG(5,UNPROTECT); __CONFIG(6,UNPROTECT); __CONFIG(7,UNPROTECT); /*===================================================================================== Main function =====================================================================================*/ void main(){ unsigned char i; ADCON1 = 0x07; TRISA = 0x00; PORTA = 0xFF; TRISB = 0x00; PORTB = 0xFF; LCD_Init(); LCD_gotoXY(2,3); //LCD_PrChar('B'); //LCD_PrChar('B'); LCD_PrString("I Love You"); while(1); } Anh em nào dùng thấy có ích thì thanks cho mình cái . Chúc các bạn thành công. |
Ðiều Chỉnh | |
Xếp Bài | |
|
|