/*
#include <stdio.h>
#include "pico/stdlib.h"
#include <hardware/i2c.h>
/*
Configuração básica I2C no RP2040
Tarefa 2:
4.Comunicação I2C, iremos utilizar o DS1307, que é um Real Time Clock – RTC
disponível no simulador Wokwi. O endereço I2C do DS1307 é 0x68.
*/
/*
#define I2C_ADR 0x68 //ENDEREÇO DO DISPOSITIVO I2C
int valor;
int main() {
i2c_init(i2c0,100*1000); //Inicializa o I2C0 a 100kHz
gpio_set_function(4,GPIO_FUNC_I2C);//SDA
gpio_set_function(5,GPIO_FUNC_I2C);//SCL
gpio_pull_up(4);//Habilita pull-up para SDA
gpio_pull_up(5);//Habilita pull-up para SCL
uint8_t buffer[2];
while (true) {
i2c_read_blocking(i2c0,I2C_ADR,buffer,2,false);//Lê dois bytes do sensor
uint16_t valor = (buffer[0]<< 8) | buffer[1]; //Combina os dois bytes
printf("Valor lido: %d\n", valor);//Exibe o valor lido
scanf(" %d", &valor);
sleep_ms(1000);//Pausa d e 1 segundo.
}
}
*/
/*
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/i2c.h"
#define I2C_PORT i2c0
#define SDA_PIN 4
#define SCL_PIN 5
#define DS1307_ADDRESS 0x68
// Função para converter decimal para BCD
uint8_t dec_to_bcd(int val) {
return (val / 10 * 16) + (val % 10);
}
// Função para converter BCD para decimal
int bcd_to_dec(uint8_t val) {
return (val / 16 * 10) + (val % 16);
}
// Função para configurar a data e hora do RTC DS1307
void set_rtc(int year, int month, int day, int hour, int minute, int second) {
uint8_t data[7];
data[0x00] = dec_to_bcd(second);
data[0x01] = dec_to_bcd(minute);
data[0x02] = dec_to_bcd(hour);
//data[0x03] = 0; // Dia da semana, 0 (domingo) a 6 (sábado)
data[0x04] = dec_to_bcd(day);
data[0x05] = dec_to_bcd(month);
data[0x06] = dec_to_bcd(year - 2000); // DS1307 conta anos de 2000 a 2099
// Escrever data e hora no DS1307
i2c_write_blocking(I2C_PORT, DS1307_ADDRESS, data, 7, false);
}
// Função para ler a data e hora do RTC DS1307
void read_rtc() {
uint8_t data[7];
i2c_write_blocking(I2C_PORT, DS1307_ADDRESS, &data[0], 1, true);//troca 1 para 2
i2c_read_blocking(I2C_PORT, DS1307_ADDRESS, data, 7, false);
int second = bcd_to_dec(data[0x00]);
int minute = bcd_to_dec(data[0x01]);
int hour = bcd_to_dec(data[0x02]);
int day = bcd_to_dec(data[0x04]);
int month = bcd_to_dec(data[0x05]);
int year = bcd_to_dec(data[0x06]) + 2000;
printf("Data: %02d/%02d/%04d Hora: %02d:%02d:%02d\n", day, month, year, hour, minute, second);
}
int main() {
stdio_init_all();
// Inicializar I2C
i2c_init(I2C_PORT, 100 * 1000);
gpio_set_function(SDA_PIN, GPIO_FUNC_I2C);
gpio_set_function(SCL_PIN, GPIO_FUNC_I2C);
gpio_pull_up(SDA_PIN);
gpio_pull_up(SCL_PIN);
// Configurar data e hora (exemplo: 8 de fevereiro de 2025, 07:47:00)
set_rtc(0, 27, 13, 24, 9, 2024);//ano,mes,dia,hora,min,seg.
while (true) {
read_rtc();
sleep_ms(5000);
}
return 0;
}
*/
#include <stdio.h>
#include "hardware/i2c.h"
//#include "hardware/rtc.h"
#include "pico/stdlib.h"
#include "pico/util/datetime.h"
int main() {
stdio_init_all();
printf("Hello RTC!\n");
char datetime_buf[256];
char *datetime_str = &datetime_buf[0];
// Start on Friday 5th of June 2020 15:45:00
datetime_t t = {
.year = 2020,
.month = 06,
.day = 05,
.dotw = 5, // 0 is Sunday, so 5 is Friday
.hour = 15,
.min = 45,
.sec = 00
};
// Start the RTC
rtc_init();
rtc_set_datetime(&t);
// clk_sys is >2000x faster than clk_rtc, so datetime is not updated immediately when rtc_get_datetime() is called.
// The delay is up to 3 RTC clock cycles (which is 64us with the default clock settings)
sleep_us(64);
// Print the time
while (true) {
rtc_get_datetime(&t);
datetime_to_str(datetime_str, sizeof(datetime_buf), &t);
printf("\r%s ", datetime_str);
sleep_ms(100);
}
}