Clock working, I2C communication to be proven
This commit is contained in:
18
Core/Inc/clock.h
Normal file
18
Core/Inc/clock.h
Normal file
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
* clock.h
|
||||
*
|
||||
* Created on: Mar 26, 2026
|
||||
* Author: ewars
|
||||
*/
|
||||
|
||||
#ifndef INC_CLOCK_H_
|
||||
#define INC_CLOCK_H_
|
||||
|
||||
#include <stdint-gcc.h>
|
||||
|
||||
uint8_t clock_convert_hours(uint8_t hours);
|
||||
uint8_t clock_convert_min_sec(uint8_t min_sec);
|
||||
|
||||
|
||||
|
||||
#endif /* INC_CLOCK_H_ */
|
||||
@@ -8,11 +8,17 @@
|
||||
#ifndef INC_WS2812_EFFECT_H_
|
||||
#define INC_WS2812_EFFECT_H_
|
||||
|
||||
#include "stdint-gcc.h"
|
||||
|
||||
|
||||
void ws_Effect_RoudTheClock(void);
|
||||
void ws_Effect_RandRound(void);
|
||||
void ws_Effect_RandRand(void);
|
||||
|
||||
void ws_effect_display_time(uint8_t hour, uint8_t minute, uint8_t second);
|
||||
|
||||
void ws_effect_display_hour(uint8_t hour);
|
||||
|
||||
|
||||
|
||||
#endif /* INC_WS2812_EFFECT_H_ */
|
||||
|
||||
55
Core/Src/clock.c
Normal file
55
Core/Src/clock.c
Normal file
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* clock.c
|
||||
*
|
||||
* Created on: Mar 26, 2026
|
||||
* Author: ewars
|
||||
*/
|
||||
|
||||
|
||||
#include "clock.h"
|
||||
|
||||
// define LED marking 12:00 or 00:00
|
||||
#define LED_ZERO_OFFSET 10
|
||||
|
||||
|
||||
uint8_t clock_convert_hours(uint8_t hours){
|
||||
int8_t h = 0;
|
||||
uint8_t led = 0;
|
||||
|
||||
// input check
|
||||
if(hours < 24){
|
||||
|
||||
if(hours > 12){
|
||||
h = hours - 12;
|
||||
}
|
||||
else{
|
||||
h = hours;
|
||||
}
|
||||
|
||||
led = 2 * h + LED_ZERO_OFFSET;
|
||||
if(led > 23){
|
||||
led -= 24;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return led;
|
||||
|
||||
}
|
||||
|
||||
uint8_t clock_convert_min_sec(uint8_t min_sec){
|
||||
|
||||
float led = 0;
|
||||
|
||||
if(min_sec < 60){
|
||||
led = (float) min_sec/(60/24);
|
||||
|
||||
led += LED_ZERO_OFFSET;
|
||||
if(led > 23){
|
||||
led -= 24;
|
||||
}
|
||||
}
|
||||
|
||||
return (uint8_t) led;
|
||||
}
|
||||
|
||||
@@ -65,11 +65,17 @@ static ds1307_err_t ds1307_write_byte(uint8_t ds1307_reg_addres, uint8_t data){
|
||||
*/
|
||||
static uint8_t ds1307_read_byte(uint8_t ds1307_reg_addres){
|
||||
|
||||
HAL_StatusTypeDef status = HAL_ERROR;
|
||||
uint8_t data;
|
||||
HAL_I2C_Master_Transmit(&DS1307_HANDLER, DS1307_ADDRES << 1, &ds1307_reg_addres, 1, DS1307_TIMEOUT);
|
||||
HAL_I2C_Master_Receive(&DS1307_HANDLER, DS1307_ADDRES << 1, &data, 1, DS1307_TIMEOUT);
|
||||
status = HAL_I2C_Master_Transmit(&DS1307_HANDLER, DS1307_ADDRES << 1, &ds1307_reg_addres, 1, DS1307_TIMEOUT);
|
||||
status = HAL_I2C_Master_Receive(&DS1307_HANDLER, DS1307_ADDRES << 1, &data, 1, DS1307_TIMEOUT);
|
||||
if(HAL_OK == status){
|
||||
return data;
|
||||
}
|
||||
else{
|
||||
return 255;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @brief BCD decode
|
||||
*
|
||||
@@ -113,7 +119,10 @@ void ds1307_set_clock_halt(uint8_t halt){
|
||||
* @return uint8_t
|
||||
*/
|
||||
uint8_t ds1307_get_clock_halt(void){
|
||||
return (ds1307_read_byte(DS1307_SECONDS) & 0x80) >> 7;
|
||||
uint8_t read = ds1307_read_byte(DS1307_SECONDS);
|
||||
if(read != 255){
|
||||
return (read & 0x80) >> 7;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -150,7 +159,13 @@ void ds1307_set_second(uint8_t second){
|
||||
* @return uint8_t
|
||||
*/
|
||||
uint8_t ds1307_get_second(void){
|
||||
return ds1307_bcd_decode(ds1307_read_byte(DS1307_SECONDS) & 0x7F);
|
||||
uint8_t decode = ds1307_read_byte(DS1307_SECONDS);
|
||||
if(decode != 255){
|
||||
return ds1307_bcd_decode(decode & 0x7F);
|
||||
}
|
||||
else{
|
||||
return 255;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @brief ds1307_set_minutes
|
||||
@@ -273,9 +288,17 @@ int8_t ds1307_get_time_zone_min(void){
|
||||
* @param dev: ds1307 pointer
|
||||
*/
|
||||
void ds1307_update(ds1307_dev_t *ds1307_dev){
|
||||
uint8_t val = 0;
|
||||
|
||||
ds1307_dev->seconds = ds1307_get_second();
|
||||
ds1307_dev->minutes = ds1307_get_minutes();
|
||||
val = ds1307_get_second();
|
||||
if(val != 255){
|
||||
ds1307_dev->seconds = val;
|
||||
}
|
||||
|
||||
val = ds1307_get_minutes();
|
||||
if(val != 255){
|
||||
ds1307_dev->minutes = val;
|
||||
}
|
||||
ds1307_dev->hours = ds1307_get_hour();
|
||||
ds1307_dev->day = ds1307_get_day();
|
||||
ds1307_dev->date = ds1307_get_date();
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
#include "ws2812_drv.h"
|
||||
#include "ws2812_effect.h"
|
||||
#include "ds1307.h"
|
||||
#include "clock.h"
|
||||
/* USER CODE END Includes */
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
@@ -108,7 +109,7 @@ int main(void)
|
||||
// RTC config
|
||||
ds1307_init();
|
||||
ds1307_dev_t my_rtc;
|
||||
ds1307_config(0, 44, 12, SATURDAY, 18, SEPTEMBER, 2021, +8, 00);
|
||||
ds1307_config(0, 10, 19, THURSDAY, 26, MARCH, 2026, +1, 33);
|
||||
|
||||
|
||||
/* USER CODE END 2 */
|
||||
@@ -126,28 +127,26 @@ int main(void)
|
||||
#define EFFECT_TIME 10000
|
||||
|
||||
|
||||
// ds1307_update(&my_rtc);
|
||||
ds1307_update(&my_rtc);
|
||||
|
||||
mytimer = HAL_GetTick();
|
||||
while(mytimer + EFFECT_TIME > HAL_GetTick()){
|
||||
ws_Effect_RoudTheClock();
|
||||
}
|
||||
uint8_t hour = my_rtc.hours;
|
||||
uint8_t minutes = my_rtc.minutes;
|
||||
uint8_t seconds = my_rtc.seconds;
|
||||
|
||||
hour = clock_convert_hours(my_rtc.hours);
|
||||
minutes = clock_convert_min_sec(my_rtc.minutes);
|
||||
seconds = clock_convert_min_sec(my_rtc.seconds);
|
||||
|
||||
mytimer = HAL_GetTick();
|
||||
while(mytimer + EFFECT_TIME > HAL_GetTick()){
|
||||
ws_Effect_RandRound();
|
||||
}
|
||||
ws_effect_display_time(hour, (uint8_t) minutes, (uint8_t) seconds);
|
||||
|
||||
// ws_Effect_RoudTheClock();
|
||||
|
||||
mytimer = HAL_GetTick();
|
||||
while(mytimer + EFFECT_TIME > HAL_GetTick()){
|
||||
ws_Effect_RandRand();
|
||||
}
|
||||
|
||||
|
||||
HAL_GPIO_TogglePin(LED_Green_GPIO_Port, LED_Green_Pin);
|
||||
|
||||
HAL_Delay(1000);
|
||||
|
||||
}
|
||||
/* USER CODE END 3 */
|
||||
}
|
||||
|
||||
@@ -10,6 +10,17 @@
|
||||
#include "ws2812_drv.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct {
|
||||
uint8_t r;
|
||||
uint8_t g;
|
||||
uint8_t b;
|
||||
} colour_s;
|
||||
|
||||
static colour_s colour_hours = {250, 0, 0};
|
||||
static colour_s colour_minutes = {0, 250, 0};
|
||||
static colour_s colour_seconds = {0, 0, 250};
|
||||
|
||||
|
||||
void ws_Effect_RoudTheClock(void){
|
||||
|
||||
#define RAND_MAX 25
|
||||
@@ -46,6 +57,26 @@ void ws_Effect_RandRound(void){
|
||||
}
|
||||
}
|
||||
|
||||
void ws_effect_display_time(uint8_t hour, uint8_t minute, uint8_t second){
|
||||
|
||||
ws2812_reset();
|
||||
|
||||
ws2812_set_colour(hour, colour_hours.r, colour_hours.g, colour_hours.b);
|
||||
ws2812_update();
|
||||
ws2812_set_colour(minute, colour_minutes.r, colour_minutes.g, colour_minutes.b);
|
||||
ws2812_update();
|
||||
ws2812_set_colour(second, colour_seconds.r, colour_seconds.g, colour_seconds.b);
|
||||
ws2812_update();
|
||||
|
||||
}
|
||||
|
||||
void ws_effect_display_hour(uint8_t hour){
|
||||
|
||||
ws2812_reset();
|
||||
|
||||
ws2812_set_colour(hour, colour_hours.r, colour_hours.g, colour_hours.b);
|
||||
}
|
||||
|
||||
|
||||
void ws_Effect_RandRand(void){
|
||||
|
||||
|
||||
Reference in New Issue
Block a user