全新论坛MCU智学网上线,欢迎访问新论坛!稀缺资源、技术干货、参考设计、原厂资料尽在MCU智学网
更新自动建库工具PCB Footprint Expert 2023.13 Pro / Library Expert 破解版

有大侠能在此基础代码加上读写功能么,用串口读取24C02数据。

[复制链接]
41 0

本文包含原理图、PCB、源代码、封装库、中英文PDF等资源

您需要 登录 才可以下载或查看,没有帐号?注册会员

x
#include <Keypad.h>//矩阵键盘扫描库
#include <LiquidCrystal.h>//LCD1604库
#include <Wire.h>//IIC总线
#include <Wire.h>
#include <EEPROM.h>

#define EEPROM_ADDR 0x50  

const byte ROWS = 4;
const byte COLS = 4;

char keys[ROWS][COLS] = {
  { '7', '8', '9', '/' },
  { '4', '5', '6', '*' },
  { '1', '2', '3', '-' },
  { 'C', '0', '=', '+' }
};

byte rowPins[ROWS] = { 2, 3, 4, 5 };
byte colPins[COLS] = { 6, 7, 8, 9 };


Keypad myKeypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// 初始化针脚
const int rs = A0, en = A1, d4 = 10, d5 = 11, d6 = 12, d7 = 13;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

bool firstNumState;
bool secondNumState;
bool resultState;
String firstNum = "";
String secondNum = "";
float result = 0.0;
char operatr = ' ';

void setup() {
  Wire.begin();        // 初始化I2C总线
  Serial.begin(9600);  // 初始化串口通信


  writeToEEPROM("123+321=444", 0);
  delay(100);
  writeToEEPROM("abc+edf=hijk", 12);
  delay(100);


  String data1 = readFromEEPROM(0, 12);
  String data2 = readFromEEPROM(12, 12);

  Serial.println("Data read from address 0: " + data1);
  Serial.println("Data read from address 12: " + data2);


  lcd.begin(16, 4);
  firstNumState = true;
  secondNumState = false;
  resultState = false;
  clr();

}

void loop() {
  writeToEEPROM("123", 24);
  delay(100);
  String data1 = readFromEEPROM(0, 25);  

  Serial.println("Data read from address 0: " + data1);
  delay(100);

  char newKey = myKeypad.getKey();
  if (newKey != NO_KEY && (newKey == '1' || newKey == '2' || newKey == '3' || newKey == '4' || newKey == '5' || newKey == '6' || newKey == '7' || newKey == '8' || newKey == '9' || newKey == '0')) {

    if (firstNumState == true) {
      firstNum = firstNum + newKey;
      lcd.print(newKey);
    }
    if (secondNumState == true) {
      secondNum = secondNum + newKey;
      lcd.print(newKey);
    }
    if (resultState == true) {
      firstNumState = true;
      secondNumState = false;
      resultState = false;
      clr();
      firstNum = firstNum + newKey;
      lcd.print(newKey);
    }
  }
  if (newKey != NO_KEY && (newKey == '+' || newKey == '-' || newKey == '*' || newKey == '/')) {
    if (firstNumState == true) {
      operatr = newKey;
      firstNumState = false;
      secondNumState = true;
      lcd.setCursor(15, 0);
      lcd.print(operatr);
      lcd.setCursor(5, 1);
    }
  }

  if (newKey != NO_KEY && newKey == '=') {
    if (operatr == '+') {
      result = firstNum.toFloat() + secondNum.toFloat();
    }

    if (operatr == '-') {
      result = firstNum.toFloat() - secondNum.toFloat();
    }

    if (operatr == '*') {
      result = firstNum.toFloat() * secondNum.toFloat();
    }

    if (operatr == '/') {
      result = firstNum.toFloat() / secondNum.toFloat();
    }
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(firstNum);
    lcd.print(operatr);
    lcd.print(secondNum);
    lcd.setCursor(0, 1);
    lcd.print("=");
    lcd.print(result);
    firstNumState = true;
    secondNumState = false;
    resultState = true;
  }

  if (newKey != NO_KEY && newKey == 'C') {
    clr();
  }
}

void clr() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("1st: ");
  lcd.setCursor(12, 0);
  lcd.print("op ");
  lcd.setCursor(0, 1);
  lcd.print("2nd: ");
  lcd.setCursor(5, 0);
  firstNum = "";
  secondNum = "";
  result = 0;
  operatr = ' ';
}

void writeToEEPROM(String data, int address) {
  for (int i = 0; i < data.length(); i++) {
    EEPROM.write(address + i, data[i]);  
  }
}

String readFromEEPROM(int address, int length) {
  String data;
  for (int i = 0; i < length; i++) {
    data += (char)EEPROM.read(address + i);  
  }
  return data;
}

举报

回复
*滑块验证:
您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

打开支付宝扫一扫,最高立得1212元红包
搜索

图文热点

更多

社区学堂

更多

客服中心

QQ:187196467 服务时间:周一至周日 8:30-20:30

关注我们

关于我们
关于我们
友情链接
联系我们
帮助中心
网友中心
购买须知
支付方式
服务支持
资源下载
售后服务
定制流程
关注我们
官方微博
官方空间
官方微信
快速回复 返回顶部 返回列表