電子愛好者的樂趣,想啥做啥,????????;不受拘束的想象力加上強大的動手能力,我們幾乎能做絕大部分東西,下面我們來一起簡單的”小試牛刀“,做個計算器耍耍。
使用到的東西元器件:4*4矩陣鍵盤,1602液晶模塊,arduino uno開發(fā)板,220Ω電阻
1602液晶模塊連接注意的引腳:
VCC: 液晶模塊供電正極
LED+:背光燈正極連接電源正極VCC
LED-:背光燈負(fù)極通過220Ω限流電阻連接負(fù)極GND
VO:設(shè)置液晶偏置電壓連接至GND
RW:讀寫模式引腳,我們這里只要寫,所以R/W接GND
GND:液晶模塊供電負(fù)極
我們來看下實際運行效果:
矩陣鍵盤A,B,C,D分別代表 +,-,X,/; ' * '代表清除,' # '號代表' = '
代 碼 部 分:
#include {'1','2','3','+'}, {'4','5','6','-'}, {'7','8','9','*'}, {'C','0','=','/'}};byte rowPins[ROWS] = {7,6,5,4}; //connect to the row pinouts of the keypadbyte colPins[COLS] = {3,2,1,0}; //connect to the column pinouts of the keypad//initialize an instance of class NewKeypadKeypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS); void setup(){ lcd.begin(16, 2); // start lcd for(int i=0;i<=3;i++); lcd.setCursor(0,0); lcd.print('Hello'); lcd.setCursor(0,1); lcd.print('calc'); delay(4000); lcd.clear(); lcd.setCursor(0, 0);}void loop(){ customKey = customKeypad.getKey(); switch(customKey) { case '0' ... '9': // This keeps collecting the first value until a operator is pressed '+-*/' lcd.setCursor(0,0); first = first * 10 + (customKey - '0'); lcd.print(first); break; case '+': first = (total != 0 ? total : first); lcd.setCursor(0,1); lcd.print('+'); second = SecondNumber(); // get the collected the second number total = first + second; lcd.setCursor(0,3); lcd.print(total); first = 0, second = 0; // reset values back to zero for next use break; case '-': first = (total != 0 ? total : first); lcd.setCursor(0,1); lcd.print('-'); second = SecondNumber(); total = first - second; lcd.setCursor(0,3); lcd.print(total); first = 0, second = 0; break; case '*': first = (total != 0 ? total : first); lcd.setCursor(0,1); lcd.print('*'); second = SecondNumber(); total = first * second; lcd.setCursor(0,3); lcd.print(total); first = 0, second = 0; break; case '/': first = (total != 0 ? total : first); lcd.setCursor(0,1); lcd.print('/'); second = SecondNumber(); lcd.setCursor(0,3); second == 0 ? lcd.print('Invalid') : total = (float)first / (float)second; lcd.print(total); first = 0, second = 0; break; case 'C': total = 0; lcd.clear(); break; }}long SecondNumber(){ while( 1 ) { customKey = customKeypad.getKey(); if(customKey >= '0' && customKey <= '9') { second = second * 10 + (customKey - '0'); lcd.setCursor(0,2); lcd.print(second); } if(customKey == '=') break; //return second; } return second; } 代碼解釋: 我們這里用了arduino自帶的液晶庫#include 這樣我們只要將重心挪到計算器核心的算法開發(fā)上就好了。 定義四線驅(qū)動1602的引腳: LiquidCrystal lcd(13, 12, 11, 10, 9, 8); 按鍵行和列符號定義: char keys[ROWS][COLS] = { {'1','2','3','+'}, {'4','5','6','-'}, {'7','8','9','*'}, {'C','0','=','/'}}; 按鍵行和列引腳定義: byte rowPins[ROWS] = {7,6,5,4}; byte colPins[COLS] = {3,2,1,0}; 按鍵符號與行列引腳關(guān)聯(lián): Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS); 第一個部分運算字符輸入處理: case '0' ... '9': lcd.setCursor(0,0); first = first * 10 + (customKey - '0'); lcd.print(first); break; 加法處理部分: case '+': first = (total != 0 ? total : first); lcd.setCursor(0,1); lcd.print('+'); second = SecondNumber(); total = first + second; lcd.setCursor(0,3); lcd.print(total); first = 0, second = 0; break; 減法處理部分: case '-': first = (total != 0 ? total : first); lcd.setCursor(0,1); lcd.print('-'); second = SecondNumber(); total = first - second; lcd.setCursor(0,3); lcd.print(total); first = 0, second = 0; break; 乘法處理部分: case '*': first = (total != 0 ? total : first); lcd.setCursor(0,1); lcd.print('*'); second = SecondNumber(); total = first * second; lcd.setCursor(0,3); lcd.print(total); first = 0, second = 0; break; 除法處理部分:除法里面還做了除0非法操作 case '/': first = (total != 0 ? total : first); lcd.setCursor(0,1); lcd.print('/'); second = SecondNumber(); lcd.setCursor(0,3); second == 0 ? lcd.print('Invalid') : total = (float)first / (float)second; lcd.print(total); first = 0, second = 0; break; 清除計算處理: case 'C': total = 0; lcd.clear(); break; 另外還有第二個數(shù)據(jù)字符輸入處理: long SecondNumber(){ while( 1 ) { customKey = customKeypad.getKey(); if(customKey >= '0' && customKey <= '9') { second = second * 10 + (customKey - '0'); lcd.setCursor(0,2); lcd.print(second); } if(customKey == '=') break; //return second; } return second; } 我們輸入第一個數(shù)據(jù)字符串后,如果檢測到有輸入運算符,那么就直接進入到第二個數(shù)據(jù)字符串里面,一直到有輸入“=”符號,輸出計算結(jié)果,并跳出當(dāng)前計算;注意到每個數(shù)據(jù)字符串里面還做了數(shù)據(jù)轉(zhuǎn)換,將字符轉(zhuǎn)換為十進制數(shù)。 大家可以思考下? 我們看到里面的數(shù)據(jù)定義都是long型的,如果超過long型所能表達的最大數(shù)據(jù)會出現(xiàn)什么情況?又該如何處理?大家可以嘗試去完善下。 另外我們這里面基本上都是調(diào)用arduino的庫函數(shù)在處理,如果放到51單片機上或者其它單片機上,那么又該如何操作?這些函數(shù)大家都可以大膽的去嘗試實現(xiàn)。
上一篇:(17)單片機仿真還可以這樣玩?Keil聯(lián)合Proteus實現(xiàn)51單片機
下一篇:(15)趣味單片機新玩法-單片機的“感知器官”(光敏電阻)
設(shè)計資源 培訓(xùn) 開發(fā)板 精華推薦
- 迅為恩智浦IMX6Q開發(fā)板Buildroot系統(tǒng)wifi測試
- 迅為imx6ull開發(fā)板Linux I2C驅(qū)動實驗-應(yīng)用程序與I2C通信
- 迅為恩智浦IMX6Q開發(fā)板系統(tǒng)固件TF卡燒寫
- 迅為IMX6ULL開發(fā)板Linux驅(qū)動初探-最簡單的設(shè)備驅(qū)動-helloworld
- 迅為imx6ull開發(fā)板移植Debian文件系統(tǒng)
- 迅為imx6ull開發(fā)板Ubuntu文件系統(tǒng)測試
- 迅為imx6開發(fā)板QT系統(tǒng)移植FFmpeg-編譯FFmpeg
- 迅為IMX6ULL開發(fā)板Buildroot文件系統(tǒng)構(gòu)建-配置Busybox
- 迅為imx6開發(fā)板QT系統(tǒng)移植FFmpeg-編譯x264
- LTC1261CS8 低輸出電壓發(fā)生器的典型應(yīng)用電路
- LTC2946MPMS-1 6V 至 300V 高端電源、電荷和能量監(jiān)視器的典型應(yīng)用
- VL53L3CX-SATEL:帶有VL53L3CX多目標(biāo)檢測ToF測距傳感器的分線板,可輕松集成到客戶設(shè)備中
- 基于VL53L1X的STM32 Nucleo遠距離測距傳感器擴展板
- 使用 ON Semiconductor 的 FAN1537 的參考設(shè)計
- 用于首頁應(yīng)用的 LTC3374EFE 降壓穩(wěn)壓器的典型應(yīng)用電路
- MC78M12ACTG 12V 電流調(diào)節(jié)器的典型應(yīng)用
- LTC3621HMS8E-25 1.2V 輸出、同步至 600kHz、強制連續(xù)模式同步降壓型穩(wěn)壓器的典型應(yīng)用
- EN5394QI 具有集成電感器的功能豐富的 9A 電壓模式同步降壓 PWM DC-DC 轉(zhuǎn)換器的典型應(yīng)用
- REP015: 調(diào)諧雙通道CDMA PA實現(xiàn)高線性度
- 芯馳科技X9SP:單芯片艙泊一體方案技術(shù)解析
- 廣汽發(fā)布人形機器人核心部件,計劃年內(nèi)小批量生產(chǎn)
- 汽車圈這場AI生死戰(zhàn),如何讀懂?
- 降低研發(fā)門檻!具身機器人時空智能三體套件免費開放
- 智能駕駛:當(dāng)技術(shù)狂奔撞上安全圍墻
- 超快充能否讓固態(tài)電池變成雞肋,提升充電速度能實現(xiàn)彎道超車嗎?
- 【行業(yè)數(shù)據(jù)】為什么BMW在第六代車型上選用大圓柱電池?
- 我國固態(tài)電池相關(guān)專利申請量已達1.3萬項
- 工信部發(fā)布2025年標(biāo)準(zhǔn)工作要點:全固態(tài)電池標(biāo)準(zhǔn)體系建設(shè)加速
- “鋰王”固態(tài)電池獲重大突破!400Wh/kg級產(chǎn)品開啟商業(yè)化沖刺
- EEWorld邀你來拆解(第四期):熱門充電寶大拆解
- 再見2019,你好2020!寫下你的年終總結(jié)和新年計劃
- 有獎直播|保護嵌入式設(shè)備與系統(tǒng)的完整性和可靠性——英飛凌 OPTIGA™ TPM 安全解決方案
- 希望一月 愛上EEWORLD——論壇推廣月
- 【評論有禮!】Sleepace RestOn 智能睡眠監(jiān)測儀拆解
- 您的電路保護有足夠的空間嗎?Littelfuse的881系列保險絲迎接這一挑戰(zhàn)
- 【看電源研討會 抽好禮】 高密度電源系統(tǒng)的PCB布局與散熱設(shè)計系統(tǒng)
- 有獎直播:ST60 非接觸式連接器及應(yīng)用探索
- 有獎直播|魏德米勒產(chǎn)品在半導(dǎo)體行業(yè)的應(yīng)用 報名中