当前位置:网站首页>M-arch (fanwai 10) gd32l233 evaluation -spi drive DS1302
M-arch (fanwai 10) gd32l233 evaluation -spi drive DS1302
2022-06-12 11:30:00 【Interesting Python】
Preface
Good architecture , High yield like sows .
Interface modification , The effect comes out .
On the second day of commencement , Do something about it SPI drive DS1302.
keyword :GD32,SPI,DS1302, Three line SPI, Half duplex SPI
SPI
Serial peripheral interface (Serial Peripheral Interface, Abbreviation for SPI) Provides the basis for SPI Data sending and receiving function of the protocol , It can work in master or slave mode .SPI The interface supports hardware CRC Full duplex and simplex modes for calculation and verification . There are some SPI The mouth also supports SPI Four wire host mode .
The conventional SPI The signal description is shown in the figure below :

DS1302
DS1302 It's the United States DALLAS The company has launched a small current charging capacity of low-power real-time clock chip . It can be used for years 、 month 、 Japan 、 Zhou 、 when 、 branch 、 Seconds to time , And it has many functions such as leap year compensation .( Baidu Encyclopedia )
DS1302 Pin configuration :

DS1302 The temporal :

You can see : except CE The high and low levels are different , This timing is similar to half duplex SPI It's the same .

DS1302 Register configuration :

DS1302 The clock frequency of :

Three line SPI drive DS1302
Half duplex SPI Basic initialization of :
Single line transmission mode
data 8 position
Host mode
Clock idle low level , The first edge reads data
NSS Soft
frequency division : according to DS1302 Configuration to , commonly 1M More suitable .
LSB
Sending process :
Set to send mode
CE Can make
Sending address
send data
CE Disability
Receiving process :
Set to send mode
CE Can make
Sending address
Set to receive mode
Wait for data reception to complete
CE Disability
Switch to send mode , Cut off SPI Clock signal
Reading data , wait for SPI Call it a day
simulation IO drive DS1302
A little ( I wrote about , Don't stick )
SPI drive DS1302
Pit father's episode :
SPI It's usually AF5, For the results SPI1 Pin is PB13,PB14,PB15, They are AF6, It's a hole 2 Hours , That's bullshit .
I hate debugging hardware , I really hope chip manufacturers can design more hardware by software engineers idea.
SPI initialization :
#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 initialization :
#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 The hour system is changed to 24 hourly
hour_24 = (hour > 0xA0)?(hour&0x1F)|DEC2BCD(12):(hour&0x1F);
write_ds1302(0x84, hour_24);
}
}Read write interface :
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 the time stamp that has been set */
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;
}test result
DS1302 week ( register 0x8B) Of SPI wave form :

Serial data :




边栏推荐
猜你喜欢

Simple solution of regular expression

M-arch (fanwai 12) gd32l233 evaluation -cau encryption and decryption (tease Xiaobian)

字节序 - 如何判断大端小端

Les humains veulent de l'argent, du pouvoir, de la beauté, de l'immortalité, du bonheur... Mais les tortues ne veulent être qu'une tortue.

Basic principle of Doppler effect

Don't swallow rice with vinegar! Teach you 2 moves to make the fish bones "run out" safely

套接字实现 TCP 通信流程

模块8作业

Signal relay rxsf1-rk271018dc110v

【clickhouse专栏】基础数据类型说明
随机推荐
B+ 树的简单认识
MySQL锁查漏补缺
arm各种交叉编译工具的区别
tensorflow 2.x 多分类混淆矩阵及评价指标计算方法(精确率、召回率、f1分数)
Mysql45 lecture 01 | infrastructure: how is an SQL query executed?
systemctl里万恶的203
SOT23(Small Outline Transistor)
AcWing 135. Maximum subsequence sum (prefix sum + monotone queue to find the minimum value of fixed length interval)
C# 36. DataGridView行号
Leetcode 162 Looking for peak value (June 11, 2022)
MCUXpresso开发NXP RT1060(3)——移植LVGL到NXP RT1060
Socket programming UDP
(37) How bee uses different data source instances at the same time
Php中redis的keys问题
Unity 连接 Microsoft SQLSERVER 数据库
Les humains veulent de l'argent, du pouvoir, de la beauté, de l'immortalité, du bonheur... Mais les tortues ne veulent être qu'une tortue.
redis 總結
Arm cross compilation chain download address
Redis keys in PHP
DS18B20数字温度计 (一) 电气特性, 供电和接线方式