|
BlueNRG-1简易时钟,RTC模块,通过WS2812显示时间
[复制链接]
淘了一块WS2812的环形板子 想用它做一个模拟时钟 正常60颗灯珠是最理想的选择 因为价格高,尺寸大最后选择了24颗灯珠的板子 WS2812之前已经使用BlueNRG-1调试过了 这次直接把RTC代码添加进去 做些简单修改就可以 因为只有24颗灯珠,所以秒和分的精度都是2.5 通过RTC定时器将中断时间设置为2.5秒 每隔2.5秒更新一次 效果如下
RTC配置代码
- /* Includes ------------------------------------------------------------------*/
- #include "rtc.h"
- #include "BlueNRG_x_device.h"
- #include "BlueNRG1_conf.h"
- #include "SDK_EVAL_Config.h"
- #define RTC_PERIOD_2500ms ((uint32_t)((32768-1) * 2.5))
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- /* Private function prototypes -----------------------------------------------*/
- void RTC_Timer_Configuration(void);
- void RTC_Clockwatch_Configuration(void);
- /**
- * [url=home.php?mod=space&uid=159083]@brief[/url] RTC Configuration.
- * @param None
- * @retval None
- */
- void RTC_Timer_Configuration(void)
- {
- RTC_InitType RTC_Init_struct;
- NVIC_InitType NVIC_InitStructure;
-
- SysCtrl_PeripheralClockCmd(CLOCK_PERIPH_RTC, ENABLE);
-
- RTC_Init_struct.RTC_operatingMode = RTC_TIMER_PERIODIC; /**< Periodic RTC mode */
- RTC_Init_struct.RTC_PATTERN_SIZE = 1 - 1; /**< Pattern size set to 1 */
- RTC_Init_struct.RTC_TLR1 = RTC_PERIOD_2500ms; /**< Enable 0.5s timer period */
- RTC_Init_struct.RTC_PATTERN1 = 0x00; /**< RTC_TLR1 selected for time generation */
- RTC_Init(&RTC_Init_struct);
-
- /* Enable RTC Timer interrupt*/
- RTC_IT_Config(RTC_IT_TIMER, ENABLE);
- RTC_IT_Clear(RTC_IT_TIMER);
- /** Delay between two write in RTC0->TCR register has to be
- * at least 3 x 32k cycle + 2 CPU cycle. For that reason it
- * is neccessary to add the delay.
- */
- for (volatile uint16_t i=0; i<600; i++) {
- __asm("NOP");
- }
-
- /* Set the RTC_IRQn interrupt priority and enable it */
- NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = LOW_PRIORITY;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
- /* Enable RTC */
- RTC_Cmd(ENABLE);
- }
- /**
- * @brief RTC clockwatch configuration.
- * @param None
- * @retval None
- */
- void RTC_Clockwatch_Set(uint16_t year,uint8_t month,uint8_t day,uint8_t week,uint8_t hour,uint8_t minute,uint8_t second)
- {
- RTC_DateTimeType RTC_DateTime;
-
- /* Set the present time and date */
- RTC_DateTime.Second = second;
- RTC_DateTime.Minute = minute;
- RTC_DateTime.Hour = hour;
- RTC_DateTime.WeekDay = week;
- RTC_DateTime.MonthDay = day;
- RTC_DateTime.Month = month;
- RTC_DateTime.Year = year;
- RTC_SetTimeDate(&RTC_DateTime);
-
- /* Enable RTC clock watch interrupt */
- RTC_IT_Config( RTC_IT_CLOCKWATCH, ENABLE);
- RTC_IT_Clear(RTC_IT_CLOCKWATCH);
-
- /* Enable RTC clockwatch */
- RTC_ClockwatchCmd(ENABLE);
- }
- void RTC_Colckwath_Math(uint16_t year,uint8_t month,uint8_t day,uint8_t week,uint8_t hour,uint8_t minute,uint8_t second)
- {
- RTC_DateTimeType RTC_DateTime;
- /* Set the present time and date match */
- RTC_DateTime.Second = second;
- RTC_DateTime.Minute = minute;
- RTC_DateTime.Hour = hour;
- RTC_DateTime.WeekDay = week;
- RTC_DateTime.MonthDay = day;
- RTC_DateTime.Month = month;
- RTC_DateTime.Year = year;
- RTC_SetMatchTimeDate(&RTC_DateTime);
- }
- void rtc_init(uint16_t year,uint8_t month,uint8_t day,uint8_t week,uint8_t hour,uint8_t minute,uint8_t second)
- {
- RTC_Timer_Configuration();
- RTC_Clockwatch_Set(year,month,day,week,hour,minute,second);
- }
- void rtc_get_time(RTC_DateTimeType* RTC_DateTime)
- {
- RTC_GetTimeDate(RTC_DateTime);
- }
复制代码
主文件
- /* Includes ------------------------------------------------------------------*/
- #include "BlueNRG_x_device.h"
- #include <stdio.h>
- #include "BlueNRG1_conf.h"
- #include "BlueNRG1_it.h"
- #include "SDK_EVAL_Config.h"
- #include "ws2812b.h"
- #include "spi.h"
- #include "rtc.h"
- /** @addtogroup BlueNRG1_StdPeriph_Examples BlueNRG1 Standard Peripheral Examples
- * @{
- */
- /** @addtogroup SPI_Examples SPI Examples
- * @{
- */
- /** @addtogroup SPI_Master_DMA SPI Master DMA
- * @{
- */
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- #define WS2812B_COUNT 24
- uint32_t color_buffer1[WS2812B_COUNT];
- //RGB颜色缓冲转换为WS2812支持的SPI数据缓冲,一个RGB(24位)对应9个字节,WS2812B的一位需要3位,即110=1,100=0
- uint8_t ws2812b_buffer[WS2812B_COUNT * 9];
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- uint8_t index = 0;
- uint8_t ws2812b_flag = 0;
- /* Private function prototypes -----------------------------------------------*/
- /* Private functions ---------------------------------------------------------*/
- void ws2812b_clear(void)
- {
- uint16_t i;
- uint32_t colors[WS2812B_COUNT];
- for(i=0;i<WS2812B_COUNT;i++)
- {
- colors[i] = 0;
- }
- //填充完一列8个点后显示
- IntColorsToWS2812BBytes(ws2812b_buffer,colors,WS2812B_COUNT);
- DMASpi_Sending((uint32_t)ws2812b_buffer,WS2812B_COUNT * 9);
- }
- void ws2812b_display(void)
- {
- uint16_t i;
- uint32_t colors[WS2812B_COUNT];
- RTC_DateTimeType RTC_DateTime;
- rtc_get_time(&RTC_DateTime);
- //清除源数据,绘制刻度
- for(i=0;i<WS2812B_COUNT;i++)
- {
- if(i%6==0)
- {
- colors[i] = 0x301000;
- }
- else if(i% 2 == 0)
- {
- colors[i] = 0x060200;
- }
- else
- {
- colors[i] = 0;
- }
- }
- colors[(RTC_DateTime.Hour % 12) * 2] = 0x990ff;
- colors[(uint8_t)(RTC_DateTime.Minute * (24.00 / 60.00))] = 0xff0099;
- colors[index++] = 0x99ff00;
- if(index >= 24 || RTC_DateTime.Second == 0)index = 0;
- //填充完一列8个点后显示
- IntColorsToWS2812BBytes(ws2812b_buffer,colors,WS2812B_COUNT);
- DMASpi_Sending((uint32_t)ws2812b_buffer,WS2812B_COUNT * 9);
- }
- /**
- * @brief Main program code
- * @param None
- * @retval None
- */
- int main(void)
- {
- /* System initialization function */
- SystemInit();
-
- /* Identify BlueNRG1 platform */
- SdkEvalIdentification();
-
- /* Configure SysTick to generate interrupt with 25ms period */
- SysTick_Config(SYST_CLOCK/40 - 1);
-
- SPI_Master_Configuration();
- ws2812b_clear();
- rtc_init(2019,1,10,4,19,26,00);
- while(1)
- {
- if(ws2812b_flag)
- {
- ws2812b_flag = 0;
- ws2812b_display();
- }
- }
- }
复制代码
工程文件
Master_Dma.rar
(39.89 KB, 下载次数: 18)
|
赞赏
-
1
查看全部赞赏
-
|