├── README.md ├── LICENSE └── LCD ├── lcd.h ├── README.md └── lcd.c /README.md: -------------------------------------------------------------------------------- 1 | # PIC-Librares 2 | This libraries can be used nearly all 8-bit PIC microcontrollers with small changes. 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Trionium 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /LCD/lcd.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File: lcd.h 3 | * Author: Trion Projects 4 | * Comments: 5 | * Revision history: v1.0 6 | */ 7 | 8 | #ifndef LCD_H 9 | #define LCD_H 10 | 11 | // ***************************************************************************** 12 | // ************************** Edit Befor Use Library *************************** 13 | // Write which PORT and TRIS is LCD uses. 14 | volatile unsigned *LCD_PORT = &PORTB; 15 | volatile unsigned *LCD_TRIS = &TRISB; 16 | 17 | // Adjust pins in define section. 18 | #define LCD_RW 6 // The RW bit of the PORT 19 | #define LCD_RS 4 // The RS bit of the PORT 20 | #define LCD_EN 5 // The EN bit of the PORT 21 | #define LCD_D4 0 // The D4 bit of the PORT 22 | #define LCD_D5 1 // The D5 bit of the PORT 23 | #define LCD_D6 2 // The D6 bit of the PORT 24 | #define LCD_D7 3 // The D7 bit of the PORT 25 | 26 | // ***************************************************************************** 27 | 28 | #define ClearDisplay 0b00000001 29 | #define ReturnHome 0b00000010 30 | #define DisplayOn 0b00000100 31 | #define DisplayOff 0b00000100 32 | #define CursorOn 0b00000010 33 | #define CursorOff 0b00000010 34 | #define CursorBlinkOn 0b00000001 35 | #define CursorBlinkOff 0b00000001 36 | #define ShiftToLeft 0b00000100 37 | #define ShiftToRight 0b00000100 38 | #define ShiftCursor 0b00001000 39 | #define ShiftDisplay 0b00001000 40 | #define FirstLine 0b10000000 41 | #define SecondLine 0b11000000 42 | 43 | uint8_t lcdEntryMode = 0b00000111; 44 | uint8_t lcdDisplayControl = 0b00001100; 45 | uint8_t lcdCursorDisplayShift = 0b00010000; 46 | 47 | void LCDInitialize(); 48 | void LCDSendByte(uint8_t reg, uint8_t byte); 49 | void LCDSendNibble(uint8_t nibble); 50 | void LCDCommand(uint8_t byte); 51 | void LCDClearDisplay(void); 52 | void LCDReturnHome(void); 53 | void LCDDisplayToggle(uint8_t time, uint8_t n); 54 | void LCDDisplayOn(void); 55 | void LCDDisplayOff(void); 56 | void LCDCursorOn(void); 57 | void LCDCursorOff(void); 58 | void LCDCursorBlinkOn(void); 59 | void LCDCursorBlinkOff(void); 60 | void LCDShiftDisplayRight(void); 61 | void LCDShiftDisplayLeft(void); 62 | void LCDShiftCursorRight(void); 63 | void LCDShiftCursorLeft(void); 64 | void LCDSetPos(uint8_t x, uint8_t y); 65 | void LCDPrintChar(uint8_t ch, uint8_t y, uint8_t x); 66 | void LCDPrintString(uint8_t *string, uint8_t y, uint8_t x); 67 | 68 | #endif 69 | -------------------------------------------------------------------------------- /LCD/README.md: -------------------------------------------------------------------------------- 1 | # LCD Library for 8-bit PIC 2 | 3 | This is a powerful library for 8-bit **PIC** microcontrollers written in **XC8** language. You can use it in an 8-bit microcontroller with small changes. Create a project in **MPLAB IDE** and follow the steps below. 4 | 5 | [For a reading post about this visit our site.](https://trion-projects.blogspot.com/2020/01/lcd-interfacing-with-pic-using-xc8.html) 6 | 7 | ## Step 1 8 | Firstly, you must create `config.h` file in **Header Files** and include it in `main.c` file. Write your **Configuration Bit Settings** and `#define _XTAL_FREQ FrequencyInHertz` in `config.h` file. After that add include statements. For LCD library you need; 9 | - `#include ` 10 | - `#include ` 11 | - `#include ` 12 | - `#include <"lcd.h">` 13 | In this file you should include all of used libraries. Example (`config.h` file) is shown for PIC16F628A microcontroller. 14 | 15 | ## Step 2 16 | Install `lcd.c` and `lcd.h` files and move them to your `*.X` directory. Then in **Projects** panel right-click to `Header Files` and click `Add Existing Item...`. Select `lcd.h` file and click **Select**. Do the same thing for `lcd.c` file but add it to **Source Files**. 17 | 18 | ## Step 3 19 | Finally, go into `lcd.h` file and edit section between stars for your microcontroller. You must use one PORT for LCD. 20 | 21 | ## Usage of Functions 22 | 23 | ### LCDInitialize() 24 | This function initializes the LCD. Before use any function make sure that `LCDInitialize()` function is called once like code below. 25 | ``` c 26 | void main(void) { 27 | LCDInitialize(); 28 | while(1) { 29 | LCDPrintString("Trion Projects", 1, 1); 30 | } 31 | } 32 | ``` 33 | ### LCDClearDisplay() 34 | To clear display use this function. To execute this function you need about 2ms. 35 | 36 | ### LCDReturnHome() 37 | To move curser home use this function. To execute this function you need about 2ms. 38 | 39 | ### LCDDisplayToggle(time, n) 40 | When you need display toggle to use this function. `time` is between 0 and 256. `n` is toggle number. Each time multiplied by 100ms. See the example below. 41 | 42 | ``` c 43 | void main(void) { 44 | LCDInitialize(); 45 | while(1) { 46 | LCDPrintString("Trion Projects", 1, 1); 47 | // Toggle display 3 times with 500ms time. 48 | LCDDisplayToggle(5, 3); 49 | } 50 | } 51 | ``` 52 | 53 | ### LCDDisplayOn() 54 | Turn display on when display is off or stay display at on state. This function can be used for toggle display with **LCDDisplayOff()** function. 55 | 56 | ### LCDDisplayOff() 57 | Turn display off when the display is on or stay display at off state. 58 | 59 | ### LCDCursorOn() 60 | To turn on cursor use this function. 61 | 62 | ### LCDCursorOff() 63 | To turn off cursor use this function. 64 | 65 | ### LCDCursorBlinkOn() 66 | To turn on cursor blink use this function. 67 | 68 | ### LCDCursorBlinkOff() 69 | To turn off cursor blink use this function. 70 | 71 | ### LCDShiftDisplayRight() 72 | To shift display right use this function. 73 | 74 | ### LCDShiftDisplayLeft() 75 | To shift display left use this function. 76 | 77 | ### LCDShiftCursorRight() 78 | To shift cursor right use this function. 79 | 80 | ### LCDShiftCursorLeft() 81 | To shift cursor left use this function. 82 | 83 | ### LCDPrintChar(ch, y, x) 84 | If you want to print one character to a specific position use this function. `ch` is char which you want to print. `y` is line number. The first line is 1, the second line is 2. `x` is the position of the line. `x` must be between 0 and 17. Which can be a minimum of 1 and a maximum of 16. 85 | 86 | ### LCDPrintString(string, y, x) 87 | If you want to print the string to a specific position use this function. `string` is data which you want to print. `y` is line number. The first line is 1, the second line is 2. `x` is the position of the line. `x` must be between 0 and 17. Which can be a minimum of 1 and a maximum of 16. To center string, you could write 0 to x position and string will be centered. See examples below: 88 | 89 | You can write string directly to function. 90 | 91 | ``` c 92 | void main(void) { 93 | LCDInitialize(); 94 | while(1) { 95 | // Right justified string 96 | LCDPrintString("Trion Projects", 1, 1); 97 | // Centered string 98 | LCDPrintString("Trion Projects", 2, 0); 99 | } 100 | } 101 | ``` 102 | Or you can define char string and pass it to function. 103 | 104 | ``` c 105 | void main(void) { 106 | LCDInitialize(); 107 | char str[15] = "Trion Projects"; 108 | while(1) { 109 | LCDPrintString(str, 1, 1); 110 | } 111 | } 112 | ``` 113 | 114 | ## Print Numbers 115 | If you want to print some numbers, firstly, you need to convert it a string and then pass it to function. Let see the example below: 116 | 117 | This example shows how to print variable integer numbers to LCD. 118 | 119 | ``` c 120 | void main(void) { 121 | LCDInitialize(); 122 | char str[10]; 123 | uint8_t num = 0; 124 | while(1) { 125 | sprintf(str, "%d", num); 126 | LCDPrintString(str, 1, 1); 127 | __delay_ms(1000); 128 | num++; 129 | } 130 | } 131 | ``` 132 | 133 | This example shows how to print variable double numbers to LCD. 134 | 135 | ``` c 136 | void main(void) { 137 | LCDInitialize(); 138 | char str[10]; 139 | double num = 0.15; 140 | while(1) { 141 | sprintf(str, "%.2f", num); 142 | LCDPrintString(str, 1, 1); 143 | __delay_ms(1000); 144 | num += 0.05; 145 | } 146 | } 147 | ``` 148 | -------------------------------------------------------------------------------- /LCD/lcd.c: -------------------------------------------------------------------------------- 1 | /* 2 | * File: lcd.c 3 | * Author: Trion Projects 4 | * 5 | * LCD Library for 16x2 LCDs. 6 | * 7 | */ 8 | 9 | #include "config.h" 10 | 11 | // --- 12 | void LCDInitialize() { 13 | // Set TRIS as output and clear PORT 14 | *LCD_TRIS = 0; 15 | *LCD_PORT = 0; 16 | 17 | /******************************************* 18 | If needed clear analog pins which LCD uses 19 | *******************************************/ 20 | 21 | // Wait for initialize 22 | __delay_ms(40); 23 | 24 | // Return home 25 | LCDSendNibble(0x03); 26 | __delay_us(4500); 27 | LCDSendNibble(0x03); 28 | __delay_us(4500); 29 | LCDSendNibble(0x03); 30 | __delay_us(150); 31 | 32 | 33 | LCDSendNibble(ReturnHome); 34 | // Function Set 35 | // DL: 4-bit, N: 2-line, F: 5x8 dots 36 | LCDSendByte(0, 0x28); 37 | // Display ON/OFF control 38 | // D: Display ON, C: Cursor OFF, B: Cursor Blink OFF 39 | LCDSendByte(0, lcdDisplayControl); 40 | LCDSendByte(0, ClearDisplay); 41 | // Entry Mode Set 42 | // I/D: Cursor/blink moves to right and DDRAM address is increased by 1 43 | // SH: Shifting entire display is performed 44 | LCDSendByte(0, lcdEntryMode); 45 | } 46 | 47 | // Clear display 48 | void LCDClearDisplay(void) { 49 | LCDSendByte(0, ClearDisplay); 50 | __delay_ms(2); 51 | } 52 | 53 | // Return cursor to home 54 | void LCDReturnHome(void) { 55 | LCDSendByte(0, ReturnHome); 56 | __delay_ms(2); 57 | } 58 | 59 | // Toggle display 60 | /* 1 < time < 256 61 | * 1 < n < 256 62 | */ 63 | void LCDDisplayToggle(uint8_t time, uint8_t n) { 64 | // Blink LCD n times 65 | for(n; n > 0; n--) { 66 | LCDDisplayOff(); 67 | for(uint8_t i = time; i > 0; i--) { 68 | __delay_ms(100); 69 | } 70 | LCDDisplayOn(); 71 | for(uint8_t i = time; i > 0; i--) { 72 | __delay_ms(100); 73 | } 74 | } 75 | } 76 | 77 | // Display lcd on 78 | void LCDDisplayOn(void) { 79 | lcdDisplayControl |= DisplayOn; 80 | LCDSendByte(0, lcdDisplayControl); 81 | } 82 | 83 | // Display lcd off 84 | void LCDDisplayOff(void) { 85 | lcdDisplayControl &= ~DisplayOff; 86 | LCDSendByte(0, lcdDisplayControl); 87 | } 88 | 89 | // Cursor on 90 | void LCDCursorOn(void) { 91 | lcdDisplayControl |= CursorOn; 92 | LCDSendByte(0, lcdDisplayControl); 93 | } 94 | 95 | // Cursor off 96 | void LCDCursorOff(void) { 97 | lcdDisplayControl &= ~CursorOff; 98 | LCDSendByte(0, lcdDisplayControl); 99 | } 100 | 101 | // Cursor blink on 102 | void LCDCursorBlinkOn(void) { 103 | lcdDisplayControl |= CursorBlinkOn; 104 | LCDSendByte(0, lcdDisplayControl); 105 | } 106 | 107 | // Cursor blink off 108 | void LCDCursorBlinkOff(void) { 109 | lcdDisplayControl &= ~CursorBlinkOff; 110 | LCDSendByte(0, lcdDisplayControl); 111 | } 112 | 113 | // Shift display right 114 | void LCDShiftDisplayRight(void) { 115 | lcdCursorDisplayShift |= ShiftDisplay; 116 | lcdCursorDisplayShift |= ShiftToRight; 117 | LCDSendByte(0, lcdCursorDisplayShift); 118 | } 119 | 120 | // Shift display left 121 | void LCDShiftDisplayLeft(void) { 122 | lcdCursorDisplayShift |= ShiftDisplay; 123 | lcdCursorDisplayShift &= ~ShiftToLeft; 124 | LCDSendByte(0, lcdCursorDisplayShift); 125 | } 126 | 127 | // Shift cursor right 128 | void LCDShiftCursorRight(void) { 129 | lcdCursorDisplayShift &= ~ShiftCursor; 130 | lcdCursorDisplayShift |= ShiftToRight; 131 | LCDSendByte(0, lcdCursorDisplayShift); 132 | } 133 | 134 | // Shift cursor left 135 | void LCDShiftCursorLeft(void) { 136 | lcdCursorDisplayShift &= ~ShiftCursor; 137 | lcdCursorDisplayShift &= ~ShiftToLeft; 138 | LCDSendByte(0, lcdCursorDisplayShift); 139 | } 140 | 141 | // Send nibble to lcd 142 | void LCDSendNibble(uint8_t nibble) { 143 | // Check nibbles bits and set PORT. 144 | *LCD_PORT = (unsigned)((nibble & 0b00000001) >> 0) ? (*LCD_PORT | (1 << LCD_D4)) : (*LCD_PORT & ~(1 << LCD_D4)); 145 | *LCD_PORT = (unsigned)((nibble & 0b00000010) >> 1) ? (*LCD_PORT | (1 << LCD_D5)) : (*LCD_PORT & ~(1 << LCD_D5)); 146 | *LCD_PORT = (unsigned)((nibble & 0b00000100) >> 2) ? (*LCD_PORT | (1 << LCD_D6)) : (*LCD_PORT & ~(1 << LCD_D6)); 147 | *LCD_PORT = (unsigned)((nibble & 0b00001000) >> 3) ? (*LCD_PORT | (1 << LCD_D7)) : (*LCD_PORT & ~(1 << LCD_D7)); 148 | 149 | // Send nibble to LCD. 150 | *LCD_PORT |= 1 << LCD_EN; // E pin - LCD Enable 151 | __delay_us(1); 152 | *LCD_PORT &= ~(1 << LCD_EN); // E pin - LCD Disable 153 | __delay_us(50); // min. 37us 154 | } 155 | 156 | // Send byte to lcd 157 | void LCDSendByte(uint8_t reg, uint8_t byte) { 158 | *LCD_PORT = reg ? (*LCD_PORT | (1 << LCD_RS)) : (*LCD_PORT & ~(1 << LCD_RS)); // RS pin - Register Select 159 | 160 | *LCD_PORT &= ~(1 << LCD_RW); // RW pin to write mode 161 | 162 | LCDSendNibble(byte >> 4); 163 | LCDSendNibble(byte & 0x0f); 164 | } 165 | 166 | /* 167 | ************* Display Position *************** 168 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 169 | ----------------------------------------------- 170 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 171 | 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 172 | */ 173 | 174 | // Set position 175 | void LCDSetPos(uint8_t x, uint8_t y) { 176 | uint8_t addr; 177 | 178 | if(y == 2) 179 | addr = 0x40; 180 | else 181 | addr = 0; 182 | 183 | addr += x-1; 184 | 185 | LCDSendByte(0, 0x80 | addr); 186 | } 187 | 188 | // Print one character to lcd 189 | void LCDPrintChar(uint8_t ch, uint8_t y, uint8_t x) { 190 | LCDSetPos(x, y); 191 | LCDSendByte(1, ch); 192 | } 193 | 194 | // Print string to lcd 195 | void LCDPrintString(uint8_t *string, uint8_t y, uint8_t x) { 196 | // Set position 197 | // If x = 0, then print string to center 198 | if(x == 0) { 199 | for(uint8_t i = 0; i < 17; i++, x++) { 200 | if(string[i] == '\0') break; 201 | } 202 | LCDSetPos((18-x)/2, y); 203 | } else { 204 | LCDSetPos(x, y); 205 | } 206 | 207 | // Write each char 208 | for(uint8_t i = 0; i < 17; i++) { 209 | if(string[i] == '\0') break; 210 | LCDSendByte(1, string[i]); 211 | } 212 | } 213 | 214 | --------------------------------------------------------------------------------