1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
| #include "lcd1602.h" #include "runled.h"
void LCD_init() { LCD_write_command(LCD_WORK_MODE); LCD_write_command(LCD_DISP_MODE1); LCD_write_command(LCD_DISP_MODE2); LCD_write_command(LCD_DISP_MODE3); }
void LCD_write_data(unsigned char dat) { LCD_RS = 1; LCD_RW = 0; LCD_DATA_PORT = dat; LCD_EN = 1; delay(1); LCD_EN = 0; delay(1); }
void LCD_write_command(unsigned char cmd) { LCD_RS = 0; LCD_RW = 0; LCD_DATA_PORT = cmd; LCD_EN = 1; delay(1); LCD_EN = 0; delay(1); }
void LCD_set_address(unsigned char line,unsigned char col) { int db7 = 0x80; unsigned char address[2] = {0x00,0x40};
if (col>15) { line+=1; col-=16; }
LCD_write_command(db7+address[line]+col); }
void LCD_disp_char(unsigned char line,unsigned char col,unsigned char dat) { LCD_set_address(line,col); LCD_write_data(dat); }
void LCD_disp_str(unsigned char line,unsigned char col,const char *str) { unsigned char backup[16]; unsigned char i = 0; unsigned char len; for(; i<16; i++) if (str[i] != '\0') backup[i] = str[i]; else break; len = i; i = 0; for (; i<len; i++) { LCD_disp_char(line,col+i,str[i]); } }
void LCD_disp_num(unsigned char line,unsigned char col,int num) { unsigned char str[10]; unsigned char index = 0; unsigned char i = 0; while (num) { unsigned char base = '0'; unsigned char mod = num%10; str[index++] = (base + mod); num = num/10; }
for (; i < index / 2; i++) { unsigned char tmp = str[i]; str[i] = str[index - i - 1]; str[index - i - 1] = tmp; }
LCD_disp_str(line,col,str); }
void LCD_disp_hex(unsigned char line,unsigned char col,int num) { unsigned char str[10]; unsigned char index = 0; unsigned char i = 0; unsigned char base; while (num) { unsigned char mod = num % 16; if (mod >= 10) { base = 'A'; mod -= 10; } else base = '0'; str[index++] = (base + mod); num = num / 16; } str[index] = '\0';
for (; i<index/2; i++) { unsigned char tmp = str[i]; str[i] = str[index-1-i]; str[index-1-i] = tmp; }
LCD_disp_str(line,col,str); }
|