测试代码是否高亮

xiaoxiao2021-02-28  33

转载自ReCclayhttp://blog.csdn.net/recclay/article/details/79332846 LCD的攻略中要玩起指针了,指针不是C语言的摆设,更不是51的天敌!


/* ******************************************************************************* * 文件名:main.c * 描 述:51之指针初探 * 作 者:CLAY * 版本号:v1.0.0 * 日 期: 2018年2月17日 * 备 注:对应指令,显示对应数组 * ******************************************************************************* */ #include <stc15.h> #define u8 unsigned char #define u16 unsigned int #define u32 unsigned long bit CmdArrived = 0; u8 CmdIndex = 0; u8 *PtrTxd; u8 CntTxd = 0; u8 arr1[] = {1,2}; u8 arr2[] = {1,2,3,4}; u8 arr3[] = {1,2,3,4,5,6}; u8 arr4[] = {1,2,3,4,5,6,7,8}; void ConfigUART(u32 baud); void CloseFucker(); void main() { EA = 1; ConfigUART(9600); CloseFucker(); while(1) { if(CmdArrived) { CmdArrived = 0;//别忘了清零 switch(CmdIndex) { case 1: PtrTxd = arr1; CntTxd = sizeof(arr1); TI = 1; //注意这里是手动置位!!! break; case 2: PtrTxd = arr2; CntTxd = sizeof(arr2); TI = 1; break; case 3: PtrTxd = arr3; CntTxd = sizeof(arr3); TI = 1; break; case 4: PtrTxd = arr4; CntTxd = sizeof(arr4); TI = 1; break; default: break; } } } } void CloseFucker() { P2 = (P2 & 0x1F) | 0xA0; P0 = 0xAF; P2 = 0x00; } void ConfigUART(u32 baud) { PCON &= 0x7F; SCON = 0x50; AUXR &= 0xBF; AUXR &= 0xFE; TMOD &= 0x0F; TMOD |= 0x20; TH1 = 256 - (11059200/12/32)/baud; // TH1 = 0xFD; TL1 = TH1; ET1 = 0; ES = 1; TR1 = 1; } void InterruptUART() interrupt 4 { if(RI) { RI = 0; CmdIndex = SBUF; CmdArrived = 1; //置位标志位 } if(TI) { TI = 0; if(CntTxd > 0) { SBUF = *PtrTxd; CntTxd--; PtrTxd++; } } }

其实,与指针操作更像个的是字符串,也就是字符数组。假如,我们把上面的arr四个数组换成。。。

u8 arr1[] = "1-Hello!\r\n"; u8 arr2[] = {'2', '-', 'H', 'e', 'l', 'l', 'o', '!', '\r', '\n'}; u8 arr3[] = {51, 45, 72, 101, 108, 108, 111, 33, 13, 10}; u8 arr4[] = "4-Hello!\r\n";

这时就要特别注意一个东西了,就是sizeof和strlen的区别,具体内容可以看这篇博文: http://blog.csdn.net/recclay/article/details/78947296。

简记就是:sizeof是所占内存是包括 ' \0 ' 的!可能有的地方需要注意,这里特别声明下!

/* ******************************************************************************* * 文件名:main.c * 描 述:LCD1602时序初探 * 作 者:CLAY * 版本号:v1.0.0 * 日 期: 2018年2月17日 * 备 注:LCD底层。显示屏幕两行。 * ******************************************************************************* */ #include <stc15.h> #define u8 unsigned char #define u16 unsigned int #define u32 unsigned long #define LCD1602_DB P0 sbit LCD1602_RS = P2^0; sbit LCD1602_RW = P2^1; sbit LCD1602_E = P1^2; void InitLcd1602(); void LcdShowStr(u8 x, u8 y, u8 *str); void main() { u8 str[] = "CLAY"; InitLcd1602(); LcdShowStr(0, 1, str); LcdShowStr(1, 1, "Bo -- bo"); while(1); } void LcdWaitReady() { u8 sta; LCD1602_DB = 0xFF; LCD1602_RS = 0; LCD1602_RW = 1; do{ LCD1602_E = 1; sta = LCD1602_DB; LCD1602_E = 0; }while(sta & 0x80); } void LcdWriteCmd(u8 cmd) { LcdWaitReady(); LCD1602_RS = 0; LCD1602_RW = 0; LCD1602_DB = cmd; LCD1602_E = 1; LCD1602_E = 0; } void LcdWriteDat(u8 dat) { LcdWaitReady(); LCD1602_RS = 1; LCD1602_RW = 0; LCD1602_DB = dat; LCD1602_E = 1; LCD1602_E = 0; } void LcdSetCursor(u8 x, u8 y) { u8 addr; if(y == 0) addr = 0x00 + x; else addr = 0x40 + x; LcdWriteCmd(addr | 0x80); } void LcdShowStr(u8 x, u8 y, u8 *str) { LcdSetCursor(x, y); while(*str != '\0') { LcdWriteDat(*str++); } } void InitLcd1602() { LcdWriteCmd(0x38);//1602固定指令 LcdWriteCmd(0x0C);//开显示,关光标 LcdWriteCmd(0x06);//文字不动,指针加1 LcdWriteCmd(0x01);//清屏 }

整屏移动

/* ******************************************************************************* * 文件名:main.c * 描 述:1602的左移操作 * 作 者:CLAY * 版本号:v1.0.0 * 日 期: 2018年2月17日 * 备 注:整体左移实现 * ******************************************************************************* */ #include <stc15.h> #define u8 unsigned char #define u16 unsigned int #define u32 unsigned long #define LCD1602_DB P0 sbit LCD1602_RS = P2^0; sbit LCD1602_RW = P2^1; sbit LCD1602_E = P1^2; bit flag500ms = 0; u8 T0RH = 0; u8 T0RL = 0; u8 code str1[] = "Test..."; u8 code str2[] = "Move..."; void ConfigTimer0(u16 ms); void InitLcd1602(); void LcdShowStr(u8 x, u8 y, u8 *str, u8 len); void main() { u8 i = 0; u8 index = 0; u8 bufMove1[16+sizeof(str1)+16]; u8 bufMove2[16+sizeof(str2)+16]; EA = 1; ConfigTimer0(10); InitLcd1602(); for(i=0; i<16; i++) { bufMove1[i] = ' '; bufMove2[i] = ' '; } for(i=0; i<sizeof(str1)-1; i++) { bufMove1[16+i] = str1[i]; bufMove2[16+i] = str2[i]; } for(i=(16+sizeof(str1)-1); i<sizeof(bufMove1); i++) { bufMove1[i] = ' '; bufMove2[i] = ' '; } while(1) { if(flag500ms) { flag500ms = 0; LcdShowStr(0, 0, bufMove1+index, 16); LcdShowStr(1, 0, bufMove2+index, 16); index ++; if(index >= (16+sizeof(str1) - 1)) { index = 0; } } } } void ConfigTimer0(u16 ms) { u32 tmp = 0; tmp = 11059200 / 12; tmp = (tmp * ms) / 1000; tmp = 65536 - tmp; T0RH = (u8)(tmp>>8); T0RL = (u8)tmp; TMOD &= 0xF0; TMOD |= 0x01; TH0 = T0RH; TL0 = T0RL; ET0 = 1; TR0 = 1; } void LcdWaitReady() { u8 sta; LCD1602_DB = 0xFF; LCD1602_RS = 0; LCD1602_RW = 1; do{ LCD1602_E = 1; sta = LCD1602_DB; LCD1602_E = 0; }while(sta & 0x80); } void LcdWriteCmd(u8 cmd) { LcdWaitReady(); LCD1602_RS = 0; LCD1602_RW = 0; LCD1602_DB = cmd; LCD1602_E = 1; LCD1602_E = 0; } void LcdWriteDat(u8 dat) { LcdWaitReady(); LCD1602_RS = 1; LCD1602_RW = 0; LCD1602_DB = dat; LCD1602_E = 1; LCD1602_E = 0; } void LcdSetCursor(u8 x, u8 y) { u8 addr = 0; if(y == 0) { addr = 0x00 + x; } else { addr = 0x40 + x; } LcdWriteCmd(addr | 0x80); } void LcdShowStr(u8 x, u8 y, u8 *str, u8 len) { LcdSetCursor(x, y); while(len--) { LcdWriteDat(*str); } } void InitLcd1602() { LcdWriteCmd(0x38); LcdWriteCmd(0x0C); LcdWriteCmd(0x06); LcdWriteCmd(0x01); } void interrupTimer0() interrupt 1 { static u8 tmr500ms = 0; TH0 = T0RH; TL0 = T0RL; tmr500ms++; if(tmr500ms == 50) { tmr500ms = 0; flag500ms = 1; } }

多文件

/* ******************************************************************************* * 文件名:main.c * 描 述:整屏移动 * 作 者:CLAY * 版本号:v1.0.1 * 日 期: 2018年2月17日 * 备 注:采用多文件 * ******************************************************************************* */ #include <stc15.h> #include <Lcd1602.h> bit flag500ms = 0; u8 T0RH = 0; u8 T0RL = 0; u8 code str1[] = "TEST..."; u8 code str2[] = "MOVE..."; void ConfigTimer0(u16 ms); void main() { u8 i = 0; u8 index = 0; u8 bufMove1[16+sizeof(str1)+16]; u8 bufMove2[16+sizeof(str2)+16]; EA = 1; ConfigTimer0(10); InitLcd1602(); for(i=0; i<16; i++) { bufMove1[i] = ' '; bufMove2[i] = ' '; } for(i=0; i<(sizeof(str1)-1); i++) { bufMove1[16+i] = str1[i]; bufMove2[16+i] = str2[i]; } for(i=(16+sizeof(str1)-1); i<sizeof(bufMove1); i++) { bufMove1[i] = ' '; bufMove2[i] = ' '; } while(1) { if(flag500ms) { flag500ms = 0; LcdShowStr(0, 0, bufMove1+index, 16); LcdShowStr(1, 0, bufMove2+index, 16); index++; if(index >= (16+sizeof(str1)-1)) { index = 0; } } } } void ConfigTimer0(u16 ms) { u32 tmp; tmp = 11059200/12; tmp = (tmp*ms)/1000; tmp = 65536 - tmp; T0RH = (u8)(tmp>>8); T0RL = (u8)tmp; TMOD &= 0xF0; TMOD |= 0x01; TH0 = T0RH; TL0 = T0RL; ET0 = 1; TR0 = 1; } void InterruptTimer0() interrupt 1 { static u8 tmr500ms = 0; TH0 = T0RH; TL0 = T0RL; tmr500ms++; if(tmr500ms == 50) { tmr500ms = 0; flag500ms = 1; } } /* ******************************************************************************* * 文件名:1602.c * 描 述:1602底层驱动 * 作 者:CLAY * 版本号:v1.0.1 * 日 期: 2018年2月17日 * 备 注: * ******************************************************************************* */ #include <stc15.h> #include <Lcd1602.h> #define LCD1602_DB P0 sbit LCD1602_RS = P2^0; sbit LCD1602_RW = P2^1; sbit LCD1602_E = P1^2; void LcdWaitReady() { u8 sta; LCD1602_DB = 0xFF; LCD1602_RS = 0; LCD1602_RW = 1; do{ LCD1602_E = 1; sta = LCD1602_DB; LCD1602_E = 0; }while(sta & 0x80); } void LcdWriteCmd(u8 cmd) { LcdWaitReady(); LCD1602_RS = 0; LCD1602_RW = 0; LCD1602_DB = cmd; LCD1602_E = 1; LCD1602_E = 0; } void LcdWriteDat(u8 dat) { LcdWaitReady(); LCD1602_RS = 1; LCD1602_RW = 0; LCD1602_DB = dat; LCD1602_E = 1; LCD1602_E = 0; } void LcdSetCursor(u8 x, u8 y) { u8 addr; if(y == 0) { addr = 0x00 + x; } else { addr = 0x40 + x; } LcdWriteCmd(addr | 0x80); } void LcdShowStr(u8 x, u8 y, u8 *str, u8 len) { LcdSetCursor(x, y); while(len--) { LcdWriteDat(*str++); } } void InitLcd1602() { LcdWriteCmd(0x38); LcdWriteCmd(0x0C); LcdWriteCmd(0x06); LcdWriteCmd(0x01); } /* ******************************************************************************* * 文件名:Lcd1602.h * 描 述: * 作 者:CLAY * 版本号:v1.0.0 * 日 期: * 备 注: * ******************************************************************************* */ #ifndef LCD1602_H #define LCD1602_H #define u8 unsigned char #define u16 unsigned int #define u32 unsigned long void InitLcd1602(); void LcdShowStr(u8 x, u8 y, u8 *str, u8 len); #endif

附上一个LCD1602相关的实战: http://blog.csdn.net/recclay/article/details/79334658

转载请注明原文地址: https://www.6miu.com/read-2800243.html

最新回复(0)