fork download
  1. /*
  2. 运行:sudo ./12864
  3. 编译:gcc 12864.c -o 12864 -L lib -l wiringPi (需已安装wiringPi)
  4. by:WuSiYu
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <stdint.h>
  9. #include <wiringPi.h>
  10. #include <iconv.h>
  11. #include <sys/timeb.h>
  12.  
  13.  
  14. #define LCD_RS 4 //显示屏控制线
  15. #define LCD_RW 5
  16. #define LCD_EN 1
  17.  
  18. #define D1 30 //显示屏数据线
  19. #define D2 21
  20. #define D3 22
  21. #define D4 23
  22. #define D5 24
  23. #define D6 25
  24. #define D7 26
  25. #define D8 27
  26.  
  27. char u2g_out[255];
  28.  
  29. int this_line_data[768];
  30.  
  31. struct timeb tp;
  32.  
  33. char this_line[3200];
  34. char buf[20];
  35. int i,len,s,data_s,n;
  36.  
  37. FILE *data_fp;
  38. /*===================================================================
  39. 功能:编码转换
  40. 输入:UTF8
  41. 输出:GB2312
  42. ====================================================================*/
  43. int code_convert(char *from_charset,char *to_charset,char *inbuf,int inlen,char *outbuf,int outlen){
  44. iconv_t cd;
  45. int rc;
  46. char **pin = &inbuf;
  47. char **pout = &outbuf;
  48.  
  49. cd = iconv_open(to_charset,from_charset);
  50. if (cd==0) return -1;
  51. memset(outbuf,0,outlen);
  52. if (iconv(cd,pin,&inlen,pout,&outlen)==-1) return -1;
  53. iconv_close(cd);
  54. return 0;
  55. }
  56.  
  57. int u2g(char *inbuf,int inlen,char *outbuf,int outlen){
  58. return code_convert("utf-8","gb2312",inbuf,inlen,outbuf,outlen);
  59. }
  60.  
  61. /*===================================================================
  62. 功能:总线写入
  63. 输入:十六进制数据
  64. 输出:无
  65. ====================================================================*/
  66. void bus_write(unsigned char data){
  67. int t[10];
  68. int f=0,i=0,d=data;
  69.  
  70. //进制转换
  71. for(i=0;i<8;i++){
  72. t[i]=data%2;
  73. data=data/2;
  74. }
  75.  
  76. //输出
  77. digitalWrite(D1,t[0]);
  78. digitalWrite(D2,t[1]);
  79. digitalWrite(D3,t[2]);
  80. digitalWrite(D4,t[3]);
  81. digitalWrite(D5,t[4]);
  82. digitalWrite(D6,t[5]);
  83. digitalWrite(D7,t[6]);
  84. digitalWrite(D8,t[7]);
  85. }
  86. /*===================================================================
  87. 功能:检查LCD忙状态
  88. 输入:无
  89. 输出:lcd_busy为1时,忙,等待。lcd-busy为0时,闲,可写指令与数据。
  90. ====================================================================*/
  91. void chk_busy(){//检查忙位
  92. digitalWrite(LCD_RS,0);
  93. digitalWrite(LCD_RW,1);
  94. digitalWrite(LCD_EN,1);
  95. bus_write(0xff);
  96. pinMode(D8, INPUT);
  97. while(digitalRead(D8));
  98. pinMode(D8, OUTPUT);
  99. digitalWrite(LCD_EN,0);
  100. }
  101. /*====================================================================
  102. 功能:写命令
  103. 输入:8位数据
  104. 输出:无
  105. =====================================================================*/
  106. void WriteCmd_LCD12864(unsigned char cmdcode){
  107. chk_busy();
  108. digitalWrite(LCD_RS,0);
  109. digitalWrite(LCD_RW,0);
  110. digitalWrite(LCD_EN,1);
  111. // delay(5);
  112. bus_write(cmdcode);
  113. digitalWrite(LCD_EN,0);
  114. // delay(5);
  115. }
  116. /*====================================================================
  117. 功能:写数据
  118. 输入:8位数据
  119. 输出:无
  120. =====================================================================*/
  121. void WriteData_LCD12864(unsigned char Dispdata){
  122. chk_busy();
  123. digitalWrite(LCD_RS,1);
  124. digitalWrite(LCD_RW,0);
  125. digitalWrite(LCD_EN,1);
  126. // delay(5);
  127. bus_write(Dispdata);
  128. digitalWrite(LCD_EN,0);
  129. // delay(5);
  130. }
  131. /*==========================================================================
  132. 功能:发送字符串
  133. 输入:地址,字符串
  134. 输出:无
  135. ===========================================================================*/
  136. void WriteWord_LCD12864(unsigned char a,unsigned char *d){//向LCD指定位置发送一个字符串,长度64字符之内。
  137. unsigned char *s;
  138. u2g(d,strlen(d),u2g_out,255);
  139. s=u2g_out;
  140. WriteCmd_LCD12864(a);
  141. while(*s>0){
  142. WriteData_LCD12864(*s);
  143. s++;
  144. }
  145. }
  146. /*==========================================================================
  147. 功能:发送字符串2
  148. 输入:字符串
  149. 输出:无
  150. ===========================================================================*/
  151. void WriteWord_LCD12864_2(unsigned char *d){//向LCD发送一屏字符串,长度64字符之内。
  152. int i=0;
  153. unsigned char *s;
  154. u2g(d,strlen(d),u2g_out,255);
  155. s=u2g_out;
  156. WriteCmd_LCD12864(0x80);
  157. while(*s>0){
  158. WriteData_LCD12864(*s);
  159. s++;
  160. i++;
  161. if(i==16){
  162. WriteCmd_LCD12864(0x90);
  163. }
  164. if(i==32){
  165. WriteCmd_LCD12864(0x88);
  166. }
  167. if(i==48){
  168. WriteCmd_LCD12864(0x98);
  169. }
  170. }
  171. }
  172. void Clean_LCD12864(unsigned char c,unsigned char d){
  173. unsigned char l,r;
  174. WriteCmd_LCD12864(0x34);
  175. for(l=0;l<32;l++){
  176. WriteCmd_LCD12864(0x80+l);
  177. WriteCmd_LCD12864(0x80);
  178. for(r=0;r<16;r++){
  179. WriteData_LCD12864(c);
  180. WriteData_LCD12864(d);
  181. }
  182. }
  183. }
  184. void Show_LCD12864(void){
  185. unsigned char l,r;
  186. WriteCmd_LCD12864(0x36);
  187. for(l=0;l<32;l++){
  188. WriteCmd_LCD12864(0x80+l);
  189. WriteCmd_LCD12864(0x80+1);
  190. for(r=0;r<6;r++){
  191. WriteData_LCD12864(this_line_data[12*l+r*2]);
  192. WriteData_LCD12864(this_line_data[12*l+r*2+1]);
  193. }
  194.  
  195. for(r=0;r<4;r++){
  196. WriteData_LCD12864(0x00);
  197. }
  198.  
  199. for(r=0;r<6;r++){
  200. WriteData_LCD12864(this_line_data[12*32+12*l+r*2]);
  201. WriteData_LCD12864(this_line_data[12*32+12*l+r*2+1]);
  202. }
  203. }
  204. }
  205. /*==========================================================================
  206. 功能:初始化LCD
  207. 输入:无
  208. 输出:无
  209. ===========================================================================*/
  210. void Init_LCD12864(void){ //初始化LCD屏
  211. pinMode(D1, OUTPUT); //设置GPIO
  212. pinMode(D2, OUTPUT);
  213. pinMode(D3, OUTPUT);
  214. pinMode(D4, OUTPUT);
  215. pinMode(D5, OUTPUT);
  216. pinMode(D6, OUTPUT);
  217. pinMode(D7, OUTPUT);
  218. pinMode(D8, OUTPUT);
  219.  
  220. pinMode(LCD_RS, OUTPUT);
  221. pinMode(LCD_RW, OUTPUT);
  222. pinMode(LCD_EN, OUTPUT);
  223.  
  224. WriteCmd_LCD12864(0x38); //选择8bit数据流
  225. delay(20);
  226. WriteCmd_LCD12864(0x01); //清除显示,并且设定地址指针为00H
  227. delay(20);
  228. WriteCmd_LCD12864(0x0c); //开显示(无游标、不反白)
  229. delay(20);
  230. }
  231.  
  232. void do_it(void){
  233. fgets(this_line,3200,data_fp);
  234.  
  235. ftime(&tp);
  236. printf("%d--", tp.millitm);
  237.  
  238. printf("%d\n", n);
  239. n++;
  240. len = strlen(this_line);
  241.  
  242. i=0;
  243. data_s=0;
  244. s=0;
  245. for(i=0;i<len;i++){
  246. if(this_line[i]=='-'){
  247. this_line_data[data_s]=atoi(buf);
  248. buf[0]=' ';
  249. buf[1]=' ';
  250. buf[2]=' ';
  251. s=0;
  252. data_s++;
  253. }else{
  254. buf[s]=this_line[i];
  255. s++;
  256. }
  257. }
  258. WriteCmd_LCD12864(0x36);
  259. Show_LCD12864();
  260. WriteCmd_LCD12864(0x36);
  261. }
  262.  
  263. int main (int args, char *argv[]){
  264. int last=0,img;
  265.  
  266. wiringPiSetup();
  267. Init_LCD12864();
  268.  
  269. WriteCmd_LCD12864(0x01);
  270. WriteWord_LCD12864(0x80,"Hello LCD12864");
  271. delay(1000);
  272.  
  273.  
  274.  
  275. WriteCmd_LCD12864(0x01);
  276. WriteCmd_LCD12864(0x36);
  277. Clean_LCD12864(0x00,0x00);
  278. WriteCmd_LCD12864(0x36);
  279.  
  280. data_fp=fopen("all_in_one.h","r");
  281.  
  282. ftime(&tp);
  283. last=tp.millitm;
  284.  
  285. while(1){
  286.  
  287. ftime(&tp);
  288.  
  289. if(last>tp.millitm){
  290. if(1000-last+tp.millitm>=33){
  291. // ftime(&tp);
  292. // last=tp.millitm;
  293. last=43-(1000-last);
  294. printf("%d--", last);
  295. delay(10);
  296. do_it();
  297. }
  298. }else{
  299. if(tp.millitm-last>=33){
  300. // ftime(&tp);
  301. // last=tp.millitm;
  302. last+=33;
  303. printf("%d--", last);
  304. do_it();
  305. }
  306. }
  307. }
  308. }
  309.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:9:22: fatal error: wiringPi.h: No such file or directory
 #include <wiringPi.h>
                      ^
compilation terminated.
stdout
Standard output is empty