找回密码
 注册会员
更新自动建库工具PCB Footprint Expert 2024.04 Pro / Library Expert 破解版

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

[复制链接]
xiaobai1284 发表于 2024-7-1 17:16:31 | 显示全部楼层 |阅读模式

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

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

×
#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;
}
*滑块验证:
您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

QQ|手机版|MCU资讯论坛 ( 京ICP备18035221号-2 )|网站地图

GMT+8, 2024-11-23 07:39 , Processed in 0.050561 second(s), 10 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表