|
以51單片機(jī)為內(nèi)核,水位傳感器檢測(cè)水位,電機(jī)驅(qū)動(dòng)模塊驅(qū)動(dòng)水泵,1602顯示,矩陣鍵盤控制電機(jī)轉(zhuǎn)速
Cache_-6358ceb1d1da15c6..jpg (260.49 KB, 下載次數(shù): 30)
下載附件
2019-4-26 11:24 上傳
原理圖.jpg (52.2 KB, 下載次數(shù): 38)
下載附件
2019-4-20 23:54 上傳
單片機(jī)源程序如下:
- //#ifndef _Use_LCD1602B
- //# define _Use_LCD1602B
- /***********************
- * 系 統(tǒng) 宏 定 義 *
- ***********************/
- /*---------------------*
- * 常 數(shù) 宏 定 義 *
- *---------------------*/
- #ifndef True
- # define True 0x01
- #endif
- #ifndef False
- # define False 0x00
- #endif
- #ifndef Enable
- # define Enable 0x01
- #endif
- #ifndef Disable
- # define Disable 0x00
- #endif
- #ifndef RunStringSpeed
- # define RunStringSpeed 100
- #endif
- #ifndef FlashTimeOut
- # define FlashTimeOut 500
- #endif
- #ifndef FlashGIFTimeOut
- # define FlashGIFTimeOut 1000
- #endif
- # define LCD_Write 0x00
- # define LCD_Read 0x01
- # define LCD_Command 0x00
- # define LCD_Data 0x01
- # define LCD_CMD_Init 0x28
- # define LCD_CMD_DispCtr 0x0c
- # define LCD_CMD_CLS 0x01
- # define LCD_CMD_EnterSet 0x06
- # define LCD_CMD_IconShow 0x0f
- # define LCD_CMD_IconHide 0x0c
- # define LCD_CMD_NotMove 0b00010100
- /*---------------------*
- * 動(dòng) 作 宏 定 義 *
- *---------------------*/
- # define SetReadState LCD_SetReadData;LCD_RS = LCD_Command;LCD_RW = LCD_Read;
- # define SetRead LCD_SetReadData;LCD_RW = LCD_Read;
- # define SetWrite LCD_SetWriteData;LCD_RW = LCD_Write;
- # define SetCommand LCD_RS = LCD_Command;
- # define SetData LCD_RS = LCD_Data;
- # define Print(a) LCDDisplayString(a);
- # define Locate(x,y) LCDSetXY(x-1,y-1);
- # define CLS LCDWaitForReady();LCDSendCommand(LCD_CMD_CLS);
- # define PrintN(a,b) LCDDisplayNum((unsigned long)a,b);
- # define ShowIcon LCDWaitForReady();LCDSendCommand(LCD_CMD_IconShow);
- # define HideIcon LCDWaitForReady();LCDSendCommand(LCD_CMD_IconHide);
- /***********************
- * 全局變量聲明區(qū) *
- ***********************/
- const char CHR[16] = {'0','1','2','3','4','5','6','7','8'
- ,'9','a','b','c','d','e','f'};
- extern unsigned int FlashTimeCounter;
- extern unsigned int RunTimeCounter;
- extern unsigned int FlashGIFStringCounter;
- /***********************
- * 系統(tǒng)函數(shù)聲明區(qū) *
- ***********************/
- void LCDInit(void);
- void LCDSendCommand(char Command);
- void LCDSendData(char Data);
- void LCDWaitForReady(void);
- void LCDSetXY(char X,char Y);
- void LCDDisplayString(char *String);
- void LCDDisplayNum(unsigned long Num,char BitCount);
- void LCDDelay(unsigned int Time);
- void LCDDelayUs(unsigned int Time);
- void RunString(char *String,char Direction,char Y,char StartX,char EndX);
- void Flash(char *String,char Icon,char X,char Y);
- char StringLength(char *String);
- void FlashStringGroup(char String[][17],char StringCounter,char X,char Y);
- /********************************************************
- * 函數(shù)說(shuō)明:LCD驅(qū)動(dòng)類毫秒延時(shí)函數(shù) *
- * 輸入: 需要延時(shí)的大體毫秒數(shù) *
- ********************************************************/
- void LCDDelay(unsigned int Time)
- {
- unsigned int TimeCounter = 0;
-
- for (TimeCounter = 0;TimeCounter < Time;TimeCounter ++)
- {
- LCDDelayUs(255);
- }
- }
- /********************************************************
- * 函數(shù)說(shuō)明:LCD驅(qū)動(dòng)指令周期延時(shí)函數(shù) *
- * 輸入: 需要大體延時(shí)Us數(shù) *
- ********************************************************/
- void LCDDelayUs(unsigned int Time)
- {
- unsigned int TimeCounter = 0;
- for (TimeCounter = 0;TimeCounter < Time;TimeCounter ++)
- {
- asm("nop");
- }
- }
- /********************************************************
- * 函數(shù)說(shuō)明:LCD初始化函數(shù) *
- ********************************************************/
- void LCDInit(void)
- {
- LCDDelay(15);
-
- LCDWaitForReady();
- LCDSendCommand(LCD_CMD_Init);
-
- LCDWaitForReady();
- LCDSendCommand(LCD_CMD_DispCtr);
-
- LCDWaitForReady();
- LCDSendCommand(LCD_CMD_CLS);
- LCDDelay(2);
- LCDSendCommand(LCD_CMD_EnterSet);
-
- }
- /********************************************************
- * 函數(shù)說(shuō)明:向LCD發(fā)送指令函數(shù) *
- * 輸入: 需要發(fā)送的指令 *
- ********************************************************/
- void LCDSendCommand(char Command)
- {
- SetWrite;
- SetCommand;
- {
- LCD_E = Enable;
-
- LCD_SendHalfCharHigh(Command);
-
- LCD_E = Disable;
- }
- {
- LCD_E = Enable;
-
- LCD_SendHalfCharLow(Command);
-
- LCD_E = Disable;
- }
- SetRead;
- SetCommand;
- }
- /********************************************************
- * 函數(shù)說(shuō)明:向LCD發(fā)送數(shù)據(jù)函數(shù) *
- ********************************************************/
- void LCDSendData(char Data)
- {
- SetWrite;
- SetData;
- {
- LCD_E = Enable;
-
- LCD_SendHalfCharHigh(Data);
-
- LCD_E = Disable;
- }
- {
- LCD_E = Enable;
-
- LCD_SendHalfCharLow(Data);
-
- LCD_E = Disable;
- }
- SetRead;
- SetCommand;
- }
- /********************************************************
- * 函數(shù)說(shuō)明:等待LCD空閑狀態(tài)函數(shù) *
- ********************************************************/
- void LCDWaitForReady(void)
- {
- #ifdef _Use_LCDOnlyWrite
- LCDDelayUs(30);
- #else
-
- SetRead;
- SetCommand;
-
- LCD_E = Enable;
-
- while (LCD_BF == Enable); //RW=1,讀PD7,為0表示空閑;
-
- LCD_E = Disable;
-
- #endif
- }
- /********************************************************
- * 函數(shù)說(shuō)明:設(shè)置顯示坐標(biāo)函數(shù) *
- ********************************************************/
- void LCDSetXY(char X,char Y)
- {
- char Address;
- if (Y == 0)
- {
- Address = 0x80 + X;
- }
- else
- {
- Address = 0xc0 + X;
- }
-
- LCDWaitForReady();
- LCDSendCommand(Address);
- }
- /********************************************************
- * 函數(shù)說(shuō)明:LCD字符串顯示函數(shù) *
- ********************************************************/
- void LCDDisplayString(char *String)
- {
- while(*String)
- {
- LCDWaitForReady();
- LCDSendData(*String);
- String++;
- }
- }
- /********************************************************
- * 函數(shù)說(shuō)明:數(shù)值顯示函數(shù)(HEX) *
- * 輸入: 需要顯示的數(shù)字(無(wú)符號(hào)長(zhǎng)整形) *
- ********************************************************/
- void LCDDisplayNum(unsigned long Num,char BitCount)
- {
- char a = 0;
- for (a = 8-BitCount ;a<8;a++)
- {
- LCDSendData(CHR[(Num<<(a<<2))>>28]);
- }
- }
- /********************************************************
- * 函數(shù)說(shuō)明:滾屏字幕效果 *
- * 輸入: 需要滾屏的字符串 長(zhǎng)度 位置 *
- ********************************************************/
- void RunString(char *String,char Direction,char Y,char StartX,char EndX)
- {
- static char StringHead = 0;
- char SCREEN[17];
- char a = 0;
- char Point = StringHead;
- char StringLong = StringLength(String);
- static int RunTimeCounter = 0;
-
- for (a = 0;a<EndX - StartX + 1;a++)
- {
- SCREEN[a] = String[Point];
- Point ++;
- if (Point == StringLong)
- {
- Point = 0;
- }
- }
-
- for (;a < 17;a++)
- {
- SCREEN[a] =' ';
- }
-
- RunTimeCounter ++;
- if (RunTimeCounter >RunStringSpeed)
- {
- StringHead ++;
- RunTimeCounter = 0;
- if (StringHead == StringLong)
- {
- StringHead = 0;
- }
- }
-
- Locate(StartX,Y)
- Print(SCREEN)
- }
- /********************************************************
- * 函數(shù)說(shuō)明:字符串長(zhǎng)度測(cè)試函數(shù) *
- ********************************************************/
- char StringLength(char *String)
- {
- char n = 0;
- while (*String)
- {
- n++;
- String ++;
- }
-
- return n;
- }
- /********************************************************
- * 函數(shù)說(shuō)明:閃爍顯示函數(shù) *
- ********************************************************/
- void Flash(char *String,char Icon,char X,char Y)
- {
- char a = 0;
- char StringLong = StringLength(String);
-
- if (FlashTimeCounter % FlashTimeOut > (FlashTimeOut >> 1))
- {
- Locate(X,Y)
- Print(String)
- }
- else
- {
- for (a = X ;a < X+StringLong;a++)
- {
- Locate(a,Y)
- LCDWaitForReady();
- ……………………
- …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
源代碼.rar
(35.93 KB, 下載次數(shù): 37)
2019-4-15 23:41 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
|