cx
This commit is contained in:
1
.pio/build/arduino-esp32/idedata.json
Normal file
1
.pio/build/arduino-esp32/idedata.json
Normal file
File diff suppressed because one or more lines are too long
@ -1 +1 @@
|
||||
1176a7a80d642a21cd899acea7cb2a45730d0da1
|
||||
8d79aeea971473aa751f00d5a535f75f1c674577
|
||||
@ -8,15 +8,21 @@
|
||||
; Please visit documentation for the other options and examples
|
||||
; https://docs.platformio.org/page/projectconf.html
|
||||
|
||||
[env:arduino-esp32]
|
||||
platform = https://github.com/platformio/platform-espressif32.git
|
||||
board = esp32dev
|
||||
framework = arduino
|
||||
monitor_speed=115200
|
||||
platform_packages =
|
||||
framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32
|
||||
lib_deps = arkhipenko/TM1650@^1.1.0
|
||||
;[env:arduino-esp32]
|
||||
;platform = https://github.com/platformio/platform-espressif32.git
|
||||
;board = esp32dev
|
||||
;framework = arduino
|
||||
;monitor_speed=115200
|
||||
;platform_packages =
|
||||
; framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32
|
||||
;lib_deps = arkhipenko/TM1650@^1.1.0
|
||||
;upload_protocol = jlink
|
||||
;debug_tool = jlink
|
||||
;upload_protocol = esp-bridge
|
||||
;debug_tool = esp-bridge
|
||||
[env:arduino-esp32]
|
||||
platform = espressif32
|
||||
board = esp32dev
|
||||
framework = arduino
|
||||
monitor_speed = 115200
|
||||
lib_deps = arkhipenko/TM1650@^1.1.0
|
||||
190
src/main.cpp
190
src/main.cpp
@ -1,15 +1,113 @@
|
||||
#include <Wire.h>
|
||||
#include <TM1650.h>
|
||||
#include "FS.h"
|
||||
#include "SPIFFS.h"
|
||||
#include <SPI.h>
|
||||
int iiii=10;
|
||||
size_t TimeTotle =0;
|
||||
size_t timebegin;
|
||||
TM1650 d;
|
||||
File timefile;
|
||||
#define FRAM_CS 5
|
||||
#define FRAM_ADDR_TIME 0x0000 // 时间存储地址,0x0000 – 0x1FFF
|
||||
#define FRAM_WREN 0x06 // 使能
|
||||
#define FRAM_READ 0x03 // 读操作
|
||||
#define FRAM_WRITE 0x02 // 写操作
|
||||
#define FRAM_WRDI 0x04 //失能
|
||||
#define FRAM_RDSR 0x05 //读状态
|
||||
#define FRAM_WRSR 0x01 //写状态
|
||||
|
||||
//片选拉低,开始SPI
|
||||
inline void framSelect()
|
||||
{
|
||||
SPI.beginTransaction(SPISettings(8000000, MSBFIRST, SPI_MODE0));
|
||||
digitalWrite(FRAM_CS, LOW);
|
||||
}
|
||||
//片选拉高,结束SPI
|
||||
inline void framDeselect()
|
||||
{
|
||||
digitalWrite(FRAM_CS, HIGH);
|
||||
SPI.endTransaction();
|
||||
}
|
||||
//写使能,在执行写操作前必须调用
|
||||
inline void framWriteEnable()
|
||||
{
|
||||
framSelect();
|
||||
SPI.transfer(FRAM_WREN);
|
||||
framDeselect();
|
||||
}
|
||||
//读状态寄存器,其返回值是状态寄存器的内容
|
||||
uint8_t framReadStatus()
|
||||
{
|
||||
framSelect();
|
||||
SPI.transfer(FRAM_RDSR);
|
||||
uint8_t s = SPI.transfer(0x00);
|
||||
framDeselect();
|
||||
return s;
|
||||
}
|
||||
//写状态寄存器,将s写入状态寄存器
|
||||
void framWriteStatus(uint8_t s)
|
||||
{
|
||||
framWriteEnable();
|
||||
framSelect();
|
||||
SPI.transfer(FRAM_WRSR);
|
||||
SPI.transfer(s);
|
||||
framDeselect();
|
||||
}
|
||||
void framWriteDisable()
|
||||
{
|
||||
framSelect();
|
||||
SPI.transfer(FRAM_WRDI);
|
||||
framDeselect();
|
||||
}
|
||||
// 确保可写(不需要保护时清零SR),放在 SPI.begin 与片选初始化之后调用一次 ensureFramWritable() ,即可确认未被保护。
|
||||
void ensureFramWritable() {
|
||||
uint8_t sr = framReadStatus();
|
||||
// 若不需要任何块保护,统一清零
|
||||
if (sr != 0x00) {
|
||||
framWriteStatus(0x00);
|
||||
}
|
||||
framWriteDisable();
|
||||
}
|
||||
|
||||
|
||||
//读操作
|
||||
//addr:读取地址(16位)
|
||||
//返回:读取的32位数据(大端序)
|
||||
uint32_t framRead32(uint16_t addr)
|
||||
{
|
||||
framSelect(); // 片选拉低,开始SPI
|
||||
SPI.transfer(FRAM_READ); // 发送读操作指令
|
||||
SPI.transfer((addr >> 8) & 0xFF); // 发送高8位地址
|
||||
SPI.transfer(addr & 0xFF); // 发送低8位地址
|
||||
uint32_t v = 0;
|
||||
v |= ((uint32_t)SPI.transfer(0x00)) << 24; // 接收高8位数据
|
||||
v |= ((uint32_t)SPI.transfer(0x00)) << 16; // 接收次高8位数据
|
||||
v |= ((uint32_t)SPI.transfer(0x00)) << 8; // 接收次低8位数据
|
||||
v |= ((uint32_t)SPI.transfer(0x00)); // 接收低8位数据
|
||||
framDeselect(); // 片选拉高,结束SPI
|
||||
return v; // 返回读取到的32位数据(大端序)
|
||||
}
|
||||
//写操作
|
||||
//addr:写入地址(16位)
|
||||
//val:写入的数据(32位)
|
||||
void framWrite32(uint16_t addr, uint32_t val)
|
||||
{
|
||||
framWriteEnable();
|
||||
framSelect(); // 片选拉低,开始SPI
|
||||
SPI.transfer(FRAM_WRITE); // 发送写操作指令
|
||||
SPI.transfer((addr >> 8) & 0xFF); // 发送高8位地址
|
||||
SPI.transfer(addr & 0xFF); // 发送低8位地址
|
||||
SPI.transfer((val >> 24) & 0xFF); // 发送高8位数据
|
||||
SPI.transfer((val >> 16) & 0xFF); // 发送次高8位数据
|
||||
SPI.transfer((val >> 8) & 0xFF); // 发送次低8位数据
|
||||
SPI.transfer(val & 0xFF); // 发送低8位数据
|
||||
framDeselect(); // 片选拉高,结束SPI
|
||||
}
|
||||
|
||||
TickType_t xLastWakeTime;
|
||||
const TickType_t xFrequency = 1000;
|
||||
bool flashdot=true;
|
||||
//显示自定义4字符
|
||||
//a:长度为4的字符数组,按从右到左显示
|
||||
//dot:小数点位置索引
|
||||
void showtime(char a[4],int dot=1)
|
||||
{
|
||||
char b[4];
|
||||
@ -23,6 +121,8 @@ TickType_t xLastWakeTime;
|
||||
flashdot=!flashdot;
|
||||
|
||||
}
|
||||
//显示累计秒数
|
||||
//second:累计秒数,内部按小时格式化为4位显示
|
||||
void showtime(double second)
|
||||
{
|
||||
char bb[4];
|
||||
@ -48,51 +148,19 @@ void showtime(double second)
|
||||
//Serial.println(bb);
|
||||
|
||||
}
|
||||
int iiii=10;
|
||||
|
||||
void setup()
|
||||
{
|
||||
delay(500);
|
||||
pinMode(34,INPUT);
|
||||
//pinMode(34,INPUT);
|
||||
pinMode(4,INPUT_PULLUP);
|
||||
timebegin=millis();
|
||||
|
||||
bool ok = SPIFFS.begin();
|
||||
if(!ok)
|
||||
{
|
||||
SPIFFS.format();
|
||||
ok = SPIFFS.begin();
|
||||
|
||||
}
|
||||
|
||||
bool exist = SPIFFS.exists("/time.txt");
|
||||
if (exist)
|
||||
{
|
||||
// Serial.println("The file exists!");
|
||||
|
||||
File f = SPIFFS.open("/time.txt", "r");
|
||||
if (!f)
|
||||
{
|
||||
// 在打弢<E68993>过程中出现问题f就会为空
|
||||
// Serial.println("Some thing went wrong trying to open the file...");
|
||||
}
|
||||
else
|
||||
{
|
||||
int s = f.size();
|
||||
// Serial.printf("Size=%d\r\n", s);
|
||||
|
||||
//读取index.html的文本内宄1<E5AE84>7
|
||||
String data = f.readString();
|
||||
TimeTotle=data.toInt();
|
||||
// Serial.println(data);
|
||||
|
||||
//关闭文件
|
||||
f.close();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Serial.println("No such file found.");
|
||||
}
|
||||
SPI.begin(18, 19, 23, FRAM_CS);
|
||||
pinMode(FRAM_CS, OUTPUT);
|
||||
digitalWrite(FRAM_CS, HIGH);
|
||||
ensureFramWritable();
|
||||
TimeTotle = framRead32(FRAM_ADDR_TIME);
|
||||
Wire.setPins(26,25);
|
||||
Wire.begin(); //Join the bus as master
|
||||
|
||||
@ -118,21 +186,17 @@ void loop()
|
||||
|
||||
vTaskDelayUntil(&xLastWakeTime, xFrequency);
|
||||
|
||||
if (/* condition */digitalRead(4)==0)
|
||||
framWrite32(FRAM_ADDR_TIME, (uint32_t)TimeTotle); //写入当前时间到FRAM
|
||||
|
||||
if (digitalRead(4)==0) //GPIO4 ---- put 10s to reset time
|
||||
{
|
||||
Serial.println("you have put the butten reset");
|
||||
iiii--;
|
||||
|
||||
if (iiii==0)
|
||||
{ TimeTotle=0;
|
||||
timefile= SPIFFS.open("/time.txt", "w");
|
||||
timefile.println(TimeTotle);
|
||||
timefile.flush();
|
||||
timefile.close();
|
||||
/* code */
|
||||
framWrite32(FRAM_ADDR_TIME, (uint32_t)TimeTotle); //写入0
|
||||
}
|
||||
|
||||
/* code */
|
||||
}else
|
||||
{
|
||||
iiii=10;
|
||||
@ -150,26 +214,18 @@ void loop()
|
||||
timebegin=timenow;
|
||||
}
|
||||
|
||||
|
||||
int a=digitalRead(34);
|
||||
if (a!=0)
|
||||
{
|
||||
timefile= SPIFFS.open("/time.txt", "w");
|
||||
timefile.println(TimeTotle);
|
||||
timefile.flush();
|
||||
timefile.close();
|
||||
/* code */
|
||||
}
|
||||
else{
|
||||
showtime(0.0);
|
||||
delay(10000);
|
||||
}
|
||||
|
||||
// int a=digitalRead(34);
|
||||
// if (a!=0) //GPIO34 为高电平时每秒写入一次 FRAM
|
||||
// {
|
||||
// framWrite32(FRAM_ADDR_TIME, (uint32_t)TimeTotle);
|
||||
// }
|
||||
// else{
|
||||
// showtime(0.0);
|
||||
// delay(10000);
|
||||
// }
|
||||
|
||||
Serial.println(String(TimeTotle*1.0/1000));
|
||||
showtime(TimeTotle*1.0/1000);
|
||||
// delay(1000);
|
||||
timefile.close();
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user