欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 能源 > esp32-c3 Supermini 驱动ds3121的问题

esp32-c3 Supermini 驱动ds3121的问题

2024/10/26 7:33:08 来源:https://blog.csdn.net/m0_59802969/article/details/143083227  浏览:    关键词:esp32-c3 Supermini 驱动ds3121的问题

c3 驱动ds3121 ,始终有问题,但把程序用esp32上,一点问题都没有,难道c3 的i2c库是另外的库,

下图只读取秒显示的 错误数据,更换了scl频率,针脚,还是错,但换成esp32 输出是正确连续秒数。折腾了一天,没有发现问题

在查错中发现一个问题,如果idf抱错显示jbk编码问题,有可能是数据线松动重插一下就排除了,这个现象很奇怪。

图片

95d1d9e66e9e4f6daa56df24458b780e.jpg

代码


#include <string.h>
#include "freertos/FreeRTOS.h"#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "esp_http_server.h"
#include "esp_timer.h"// WiFi 
#define WIFI_SSID "ChinaNet-AETP5V"
#define WIFI_PASS "wf123456"//=================================#include "driver/gpio.h"#include "freertos/task.h"
#include "string.h"
#include "driver/i2c.h"#include "sdkconfig.h"
#include <stdio.h>
#include "esp_system.h"
#include "esp_log.h"#define I2C_MASTER_SCL_IO           20
#define I2C_MASTER_SDA_IO           21
#define I2C_MASTER_NUM              I2C_NUM_0
#define I2C_MASTER_FREQ_HZ          100000
#define I2C_MASTER_TX_BUF_DISABLE   0
#define I2C_MASTER_RX_BUF_DISABLE   0
#define DS3231_ADDRESS              0x68  // 68// I2C 初始化
void i2c_master_init() {i2c_config_t conf = {.mode = I2C_MODE_MASTER,.sda_io_num = I2C_MASTER_SDA_IO,.scl_io_num = I2C_MASTER_SCL_IO,.sda_pullup_en = GPIO_PULLUP_ENABLE,.scl_pullup_en = GPIO_PULLUP_ENABLE,.master.clk_speed = I2C_MASTER_FREQ_HZ,};ESP_ERROR_CHECK(i2c_param_config(I2C_MASTER_NUM, &conf));ESP_ERROR_CHECK(i2c_driver_install(I2C_MASTER_NUM, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0));
}
//==========================static EventGroupHandle_t s_wifi_event_group;
static const int WIFI_CONNECTED_BIT = BIT0;
static const char *TAG = "WiFi_HTTP";
static  uint64_t n;
// 
static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {esp_wifi_connect();  // } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {esp_wifi_connect();  // ESP_LOGI(TAG, "...");} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;ESP_LOGI(TAG, "IP: " IPSTR, IP2STR(&event->ip_info.ip));xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);  // λ}
}// WiFi 
void wifi_init_sta(void) {s_wifi_event_group = xEventGroupCreate();  // //  NVSesp_err_t ret = nvs_flash_init();if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {ESP_ERROR_CHECK(nvs_flash_erase());ret = nvs_flash_init();}ESP_ERROR_CHECK(ret);//  WiFiESP_ERROR_CHECK(esp_netif_init());ESP_ERROR_CHECK(esp_event_loop_create_default());esp_netif_create_default_wifi_sta();wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();ESP_ERROR_CHECK(esp_wifi_init(&cfg));ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, NULL));ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL, NULL));//  WiFi wifi_config_t wifi_config = {.sta = {.ssid = WIFI_SSID,.password = WIFI_PASS,},};ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));  // ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));ESP_ERROR_CHECK(esp_wifi_start());  //  WiFiESP_LOGI(TAG, "WiFi ");
}// 浏览器向esp32 GET信息
esp_err_t hello_get_handler(httpd_req_t *req) {//   ESP_LOGI(TAG, "Requested URI: %s", req->uri);     //显示浏览器向esp32 http server 发送的信息   可以把uri的信息提取出来 控制esp32 如uri中有ds3231  则esp32控制ds3231//  ESP_LOGI(TAG, "Requested Method: %s", http_method_str(req->method));
//    ESP_LOGI(TAG, "Requested URI: %d", req->content_len);//   n=esp_timer_get_time();   //esp32 从开机到运行此命令的时间(微秒)i2c_cmd_handle_t cmd;uint8_t data[3]={0,0,0};uint8_t o;uint8_t om;uint8_t omh;uint8_t oml;cmd = i2c_cmd_link_create();i2c_master_start(cmd);	 i2c_master_write_byte(cmd, (DS3231_ADDRESS << 1) | I2C_MASTER_WRITE, true);i2c_master_write_byte(cmd, 0x00, true);i2c_master_start(cmd);i2c_master_write_byte(cmd, (DS3231_ADDRESS << 1) | I2C_MASTER_READ, true);i2c_master_read(cmd, data, 3, I2C_MASTER_ACK);i2c_master_stop(cmd);esp_err_t ret = i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 1000 / portTICK_PERIOD_MS);i2c_cmd_link_delete(cmd);om=data[0];omh=(om>>4)&0b00001111;oml=om&0b00001111;o=omh*10+oml;printf("%d\n",o);char resp_str[21]; // uint64_t 的最大长度是 20 位,加上结尾的 null 字符sprintf(resp_str, "%u", o); //  snprintf(resp_str, sizeof(resp_str), "%llu", n);    // 使用 snprintf 将 uint64_t 转换为字符串httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); // 允许所有来源,此条非常重要httpd_resp_set_type(req, "text/plain");httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);      //esp32 向浏览器发送return ESP_OK;
}//  URI 
httpd_uri_t hello = {.uri = "/time",.method = HTTP_GET,.handler = hello_get_handler,.user_ctx = NULL 
};//  HTTP 
static httpd_handle_t start_webserver(void) {httpd_config_t config = HTTPD_DEFAULT_CONFIG();httpd_handle_t server = NULL;if (httpd_start(&server, &config) == ESP_OK) {httpd_register_uri_handler(server, &hello);  // }return server;
}void app_main(void) {i2c_master_init();//  WiFi wifi_init_sta();//  WiFi EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group, WIFI_CONNECTED_BIT, pdFALSE, pdTRUE, portMAX_DELAY);if (bits & WIFI_CONNECTED_BIT) {ESP_LOGI(TAG, "WiFi ok");//  HTTP start_webserver();} else {ESP_LOGI(TAG, "WiFi no");}
}

 

 

 

 

 

 

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com