当前位置:网站首页>M-Arch(番外10)GD32L233评测-SPI驱动DS1302
M-Arch(番外10)GD32L233评测-SPI驱动DS1302
2022-06-12 10:33:00 【趣Python】
前言
架构做得好,高产似母猪。
接口改一改,效果就出来。
开工第二天,搞一搞SPI驱动DS1302。
关键字:GD32,SPI,DS1302,三线SPI,半双工SPI
SPI
串行外设接口(Serial Peripheral Interface,缩写为SPI)提供了基于SPI协议的数据发送和接收功能,可以工作于主机或从机模式。SPI接口支持具有硬件CRC计算和校验的全双工和单工模式。有些SPI口还支持SPI四线主机模式。
常规的SPI信号描述如下图:

DS1302
DS1302是由美国DALLAS公司推出的具有涓细电流充电能力的低功耗实时时钟芯片。它可以对年、月、日、周、时、分、秒进行计时,并且具有闰年补偿等多种功能。(百度百科)
DS1302的管脚配置:

DS1302的时序:

可以看到:除了CE高低电平不一样,这个时序跟半双工的SPI是一样的。

DS1302的寄存器配置:

DS1302的时钟频率:

三线SPI驱动DS1302
半双工SPI的基本初始化:
单线发送模式
数据8位
主机模式
时钟空闲低电平,第一个边沿读数据
NSS Soft
分频:根据DS1302的配置来,一般1M多比较合适。
LSB
发送流程:
设置为发送模式
CE使能
发送地址
发送数据
CE失能
接收流程:
设置为发送模式
CE使能
发送地址
设置为接收模式
等待数据接收完成
CE失能
切换为发送模式,掐断SPI时钟信号
读数据,等待SPI收工
模拟IO驱动DS1302
略(曾经写过,就不贴了)
SPI驱动DS1302
坑爹的小插曲:
SPI一般是AF5,结果用的SPI1管脚是PB13,PB14,PB15,他们是AF6,被坑了2个小时,真扯。
巨讨厌调试硬件,真希望芯片厂商设计硬件时能更多一些软件工程师的设计idea。
SPI初始化:
#define DS1302_USING_SPI
/* SPI0:PA5-SCK-SPI_SCK PA6-CE-SPI_MISO PA7-DATA-SPI_MOSI */
/* SPI1:PB13-SCK-SPI_SCK PB14-CE-SPI_MISO PB15-DATA-SPI_MOSI */
#define CURRENT_SPI SPI1
#define CURRENT_SPI_RCU RCU_SPI1
#define RTC_RCU RCU_GPIOB
#define RTC_GPIO GPIOB
#define RTC_SCK_PIN GPIO_PIN_13
#define RTC_CE_PIN GPIO_PIN_14
#define RTC_DATA_PIN GPIO_PIN_15
#ifdef DS1302_USING_SPI
#define READ_MODE spi_disable(CURRENT_SPI); \
spi_bidirectional_transfer_config(CURRENT_SPI, SPI_BIDIRECTIONAL_RECEIVE); \
spi_enable(CURRENT_SPI);
#define WRITE_MODE spi_disable(CURRENT_SPI); \
spi_bidirectional_transfer_config(CURRENT_SPI, SPI_BIDIRECTIONAL_TRANSMIT); \
spi_enable(CURRENT_SPI);
#define CE_HIGH gpio_bit_set(RTC_GPIO, RTC_CE_PIN);
#define CE_LOW gpio_bit_reset(RTC_GPIO, RTC_CE_PIN);
#else
#endif
void spi1_init(void)
{
spi_parameter_struct spi_init_struct;
spi_i2s_deinit(CURRENT_SPI);
rcu_periph_clock_enable(CURRENT_SPI_RCU);
/* SPI_MOSI */
gpio_init_af_mode(RTC_RCU, RTC_GPIO, RTC_DATA_PIN, GPIO_OSPEED_50MHZ, GPIO_AF_6);
/* SPI_SCK */
gpio_init_af_mode(RTC_RCU, RTC_GPIO, RTC_SCK_PIN, GPIO_OSPEED_50MHZ, GPIO_AF_6);
/* RTC CE */
gpio_init_output_mode(RTC_RCU, RTC_GPIO, RTC_CE_PIN, GPIO_OSPEED_50MHZ, 0);
CE_LOW;
/* SPI parameter config */
spi_init_struct.trans_mode = SPI_TRANSMODE_BDTRANSMIT;
spi_init_struct.device_mode = SPI_MASTER;
spi_init_struct.frame_size = SPI_FRAMESIZE_8BIT;
spi_init_struct.clock_polarity_phase = SPI_CK_PL_LOW_PH_1EDGE;
spi_init_struct.nss = SPI_NSS_SOFT;
spi_init_struct.prescale = SPI_PSC_16;
spi_init_struct.endian = SPI_ENDIAN_LSB;
spi_init(CURRENT_SPI, &spi_init_struct);
/* enable */
spi_enable(CURRENT_SPI);
}DS1302初始化:
#define DS1302_UNLOCK write_ds1302(0x8e, 0x00);
#define DS1302_LOCK write_ds1302(0x8e, 0x80);
void rtc_time_init(void)
{
uint8_t hour;
uint8_t hour_24 = 0;
if (read_ds1302(READ_FLAG_ADDR) != RTC_INIT_FLAG)
{
write_ds1302(0x84, hour_24);
set_rtc(init_time);
return;
}
hour = read_ds1302(0x85);
if (hour > 0x80)
{
// 12小时制转为24小时制
hour_24 = (hour > 0xA0)?(hour&0x1F)|DEC2BCD(12):(hour&0x1F);
write_ds1302(0x84, hour_24);
}
}读写接口:
static void spi_rtc_send_byte(uint8_t byte)
{
/* loop while data register in not empty */
while(RESET == spi_i2s_flag_get(CURRENT_SPI, SPI_FLAG_TBE));
/* send byte through the SPI peripheral */
spi_i2s_data_transmit(CURRENT_SPI, byte);
while(spi_i2s_flag_get(CURRENT_SPI, SPI_FLAG_TRANS));
}
static void write_ds1302(uint8_t address, uint8_t data)
{
WRITE_MODE;
CE_HIGH;
spi_rtc_send_byte(address);
spi_rtc_send_byte(data);
CE_LOW;
}
static uint8_t read_ds1302(uint8_t address)
{
uint8_t data;
/* send addr */
WRITE_MODE;
CE_HIGH;
spi_rtc_send_byte(address);
/* recive data */
READ_MODE;
while(RESET == spi_i2s_flag_get(CURRENT_SPI, SPI_FLAG_RBNE));
CE_LOW;
WRITE_MODE;
data = spi_i2s_data_receive(CURRENT_SPI);
while(spi_i2s_flag_get(CURRENT_SPI, SPI_FLAG_TRANS));
return data;
}
void set_rtc(time_t t)
{
uint8_t year = t.year - YEAR_BASE;
if(year >= 100) year = 0;
DS1302_UNLOCK;
/* 写入已经设置时间标记 */
write_ds1302(WRITE_FLAG_ADDR, RTC_INIT_FLAG);
write_ds1302(0x80, DEC2BCD(t.second));
write_ds1302(0x82, DEC2BCD(t.minute));
write_ds1302(0x84, DEC2BCD(t.hour));
write_ds1302(0x86, DEC2BCD(t.day));
write_ds1302(0x88, DEC2BCD(t.month));
write_ds1302(0x8c, DEC2BCD(year));
write_ds1302(0x8a, DEC2BCD(t.week));
DS1302_LOCK;
}
time_t get_rtc(void)
{
time_t t;
uint16_t year = read_ds1302(0x8d);
uint8_t month = read_ds1302(0x89);
uint8_t day = read_ds1302(0x87);
uint8_t hour = read_ds1302(0x85);
uint8_t minute = read_ds1302(0x83);
uint8_t second = read_ds1302(0x81);
uint8_t week = read_ds1302(0x8b);
t.year = BCD2DEC(year) + YEAR_BASE;
t.month = BCD2DEC(month);
t.day = BCD2DEC(day);
t.hour = BCD2DEC(hour);
t.minute = BCD2DEC(minute);
t.second = BCD2DEC(second);
t.week = BCD2DEC(week);
return t;
}测试结果
DS1302星期(寄存器0x8B)的SPI波形:

串口数据:




边栏推荐
- MQTT 协议中文版
- Get start date and end date for all quarters of the year
- CONDA install tensorflow test tensorflow
- Wechat payment, wechat refund, Alibaba payment
- ASP. Net core permission system practice (zero)
- 基于C#的安全聊天工具设计
- Class selectors and using pseudo class selectors with
- Pycharm view the current version of opencv
- The most detailed explanation of the top ten levels of sqli labs platform
- Malicious code analysis practice - lab03-01 Exe basic dynamic analysis
猜你喜欢

AcWing 132. 小组队列(队列模拟题)

2022 JD 618 Comment rembourser le dépôt de pré - vente? Le dépôt JD 618 peut - il être remboursé?

2022京东618预售定金怎么退?京东618定金能退吗?

Vite Basics

Remote desktop cannot copy and paste solution

Student management system

Chapter 3 search

性能指标的信仰危机

How the ArrayList collection implements ascending and descending order

Amélioration de la 3dsc par HSC
随机推荐
Chromebook system without anti-virus software
Collation of common functions in JS
Download Notepad++
MySQL performance test (slow query log)
PHP wechat red packet allocation logic
Vite Basics
MQTT 协议中文版
Student management system
PHP generate schedule
深度学习与CV教程(14) | 图像分割 (FCN,SegNet,U-Net,PSPNet,DeepLab,RefineNet)
数组,整型,字符变量在全局和局部的存在形式
Leetcdoe 2037. 使每位学生都有座位的最少移动次数(可以,一次过)
Tp6+memcached configuration
Fiddler automatically saves the result of the specified request to a file
PHP specifies the number of people to distribute the specified amount equally at random (scaling method)
Valentina Studio Pro for MAC (MAC database management software)
Chapter 3 search
JS open common application market
在一个“去QA化”的项目中,QA能做什么?
PHP curl function