娇小w搡bbbb搡bbb,《第一次の人妻》,中国成熟妇女毛茸茸,边啃奶头边躁狠狠躁视频免费观看

(16)趣味單片機新玩法-自己動手做個簡單計算器

發(fā)布者:心靈律動最新更新時間:2025-03-25 來源: jianshu關(guān)鍵字:簡單計算器 手機看文章 掃描二維碼
隨時隨地手機看文章

電子愛好者的樂趣,想啥做啥,????????;不受拘束的想象力加上強大的動手能力,我們幾乎能做絕大部分東西,下面我們來一起簡單的”小試牛刀“,做個計算器耍耍。


使用到的東西元器件: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 #include #include LiquidCrystal lcd(13, 12, 11, 10, 9, 8);long first = 0;long second = 0;double total = 0;char customKey;const byte ROWS = 4;const byte COLS = 4;char keys[ROWS][COLS] = {

  {'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 和按鍵庫#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)。



關(guān)鍵字:簡單計算器 引用地址:(16)趣味單片機新玩法-自己動手做個簡單計算器

上一篇:(17)單片機仿真還可以這樣玩?Keil聯(lián)合Proteus實現(xiàn)51單片機
下一篇:(15)趣味單片機新玩法-單片機的“感知器官”(光敏電阻)

小廣播
設(shè)計資源 培訓(xùn) 開發(fā)板 精華推薦

最新單片機文章

 
EEWorld訂閱號

 
EEWorld服務(wù)號

 
汽車開發(fā)圈

 
機器人開發(fā)圈

電子工程世界版權(quán)所有 京ICP證060456號 京ICP備10001474號-1 電信業(yè)務(wù)審批[2006]字第258號函 京公網(wǎng)安備 11010802033920號 Copyright ? 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
主站蜘蛛池模板: 筠连县| 虹口区| 镶黄旗| 佛山市| 会昌县| 库伦旗| 英德市| 寻甸| 塘沽区| 上蔡县| 新乐市| 东至县| 西林县| 新巴尔虎左旗| 金乡县| 麦盖提县| 宜昌市| 阿克陶县| 徐水县| 绥化市| 胶南市| 广元市| 嵩明县| 航空| 闵行区| 宝清县| 莆田市| 福州市| 和龙市| 鄂州市| 临沂市| 辽宁省| 东宁县| 微博| 山阳县| 两当县| 同心县| 边坝县| 玉溪市| 滨州市| 阳泉市|