#include #include #include #include //sleep #include //nanosleep #include //u,mdelay #include #include #include #include "chrlcd.h" // #include #define INIT_MSG "* Hello saekisan ! *" //20chrs new #define LCD_NUM_LINE 4 //Number of lines #define LCD_NUM_CHAR 20 //characters per line // #define LCD_VIRTUAL_NUM_LINE 8 #define LCD_VIRTUAL_NUM_CHAR 40 #define POLL_INTERVAL (HZ/10) //100ms #define LC_READ 0xFF #define LC_OFF 0x00 #define LC_BLINK 0x01 #define LC_CURSOR 0x02 #define LC_POWER 0x04 #define DETECT_COUNT 10 static char lcd_buffer[LCD_VIRTUAL_NUM_LINE][LCD_VIRTUAL_NUM_CHAR]; static int curline = 0; static int active_line = LCD_VIRTUAL_NUM_LINE; static int lcd_control(int code); static void lcd_set_addr(unsigned long line, unsigned long addr); static int lcd_print_line(int pline, int vline); static int lcd_control(int code){ static int recode = 0; if(code == LC_READ){ return recode; } recode = code & 0x07; port_wr4bitD(0,0x00); port_wr4bitD(0,0x08 | recode); return 0x08; } static void lcd_clear(void){ port_wr4bitD(0,0x00); port_wr4bitD(0,0x01); usleep(10000); } static void lcd_on(void){ //Cursor off, Blink off lcd_control(LC_POWER); } static void lcd_off(void){ //Cursor off,Blink off lcd_control(LC_OFF); usleep(10000); } static void lcd_putchar(char c){ unsigned char tmp; tmp = ((c & 0xf0) >> 4); port_wr4bitD(1,tmp); tmp = (c & 0x0f); port_wr4bitD(1,tmp); } static void lcd_set_addr(unsigned long line, unsigned long addr){ unsigned long temp; switch(line){ case 1: temp = addr - 1;break; case 2: temp = addr -1 + 0x40;break; // 0x40,64d case 3: temp = addr -1 + 0x14;break; // 0x14 20d case 4: temp = addr -1 + 0x54;break; // 0x54 84d } port_wr4bitD(0,((temp & 0xf0) >> 4) | 0x08); port_wr4bitD(0,temp & 0x0f); } static void lcd_init(void){ usleep(30); port_wr4bitD(0, 0x03); //ファンクションセット 8ビット化 3回 パワーオンイニシャルの念押し usleep(10); port_wr4bitD(0, 0x03); usleep(1000); port_wr4bitD(0, 0x03); //4Bits port_wr4bitD(0, 0x02); // ここで 4ビット化 4Bits, 2lines, 5x7dots port_wr4bitD(0, 0x02); //ファンクションセット 4bitなので2回に分けて 上位4ビットが先 port_wr4bitD(0, 0x08); lcd_on(); lcd_clear(); wr_str(INIT_MSG); port_wr4bitD(0, 0x00); // Entry-mode,shift Inc,no port_wr4bitD(0, 0x06); } // ここで イニシャル終わり static char str_buf[21]; int wr_str(char *str_buf){ int ptr = 0; while (str_buf[ptr] != '\0'){ lcd_putchar(str_buf[ptr]); ++ptr; } } void wr_buf(){ unsigned char str[21]; FILE *infile; if ((infile = fopen("buf", "r")) == NULL) return -1; while (fgets(str,21,infile) != NULL) wr_str(str); fclose(infile); } int auto_loop(void){ unsigned int chrlcd_exist_flg = 0; unsigned char str[21]; unsigned int val = 0; FILE *infile, *outfile; chrlcd_exist_check: val = port_check(); switch(chrlcd_exist_flg){ case 1:if (val < 32) break; // normal chrlcd_exist_flg = 0; goto chrlcd_exist_check; // 外された goto _end/goto chrlcd_exist_checkのloop?かも default:if (val < 32) // { chrlcd_exist_flg = 1; lcd_init(); break;} sleep(1); goto chrlcd_exist_check; //chrlcd接続なし 1 sec polling } if ((outfile = fopen("bufrd", "w")) == NULL) return -1; // fprintf(outfile,"%2d: %d %d %d %d %d\n",val, (val>>4)&1, (val>>3)&1, (val>>2)&1,(val>>1)&1,val&1 ); fprintf(outfile,"%2d\n", val); fclose(outfile); lcd_set_addr(1, 1); if ((infile = fopen("buf1", "r")) == NULL) return -1; while (fgets(str,21,infile) != NULL) wr_str(str); fclose(infile); lcd_set_addr(2, 1); if ((infile = fopen("buf2", "r")) == NULL) return -1; while (fgets(str,21,infile) != NULL) wr_str(str); fclose(infile); lcd_set_addr(3, 1); if ((infile = fopen("buf3", "r")) == NULL) return -1; while (fgets(str,21,infile) != NULL) wr_str(str); fclose(infile); lcd_set_addr(4, 1); if ((infile = fopen("buf4", "r")) == NULL) return -1; while (fgets(str,21,infile) != NULL) wr_str(str); fclose(infile); usleep(300000); goto chrlcd_exist_check; //for test loop _end: return 0; } //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++// //+ main //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++// int main(int argc, char **argv){ extern int debug_flg; void h_debug(int action); unsigned long value = 0x0; //***** 0:low 1:high 0x0-f:4it unsigned char c; unsigned long line; unsigned long addr; extern unsigned char str_buf[21]; unsigned char str[21]; unsigned int i; switch(argc){ case 1: printf("Usage\(for debug): ./chrlcd \"str\", start, end, clear, pos n n\n"); printf(" rd_status, put chr loop, b_chk/(rtn:) \n\n");break; default: if(strcmp(argv[1], "start") == 0) lcd_init(); // for TEST else if(strcmp(argv[1], "end") == 0) lcd_off(); // for TEST // else if(strcmp(argv[1], "clear") == 0) lcd_clear(); // for TEST // else if(strcmp(argv[1], "rd_status") == 0) port_read(); // else if(strcmp(argv[1], "wrbuf") == 0) wr_buf(); // else if(strcmp(argv[1], "loop") == 0) auto_loop(); // for TEST // else if(strcmp(argv[1], "b_chk") == 0) { value = port_check(); printf("%d\n",value); return value; // for TEST // } else if(strcmp(argv[1], "put") == 0) { sscanf(argv[2], "%c", &c); lcd_putchar(c);} // else if((argc == 4) && (sscanf(argv[2], "%d", &line) == 1 ) && (sscanf(argv[3], "%d", &addr) == 1 )){ // printf("line: %d addr: %d\n",line,addr); //for test : posion lcd_set_addr(line, addr);} else if(sscanf(argv[1], "%s", &str_buf) == 1 ) { wr_str(str_buf); // 文字列表示 argc:2 argv[1] if(argc > 2){ for(i = 2; i != argc; ++i){ wr_str(" "); if(sscanf(argv[i], "%s", &str_buf) == 1 ) wr_str(str_buf); } } } else printf(" ** ? **\n"); break; } }