当前位置:网站首页>M-arch (fanwai 10) gd32l233 evaluation -spi drive DS1302
M-arch (fanwai 10) gd32l233 evaluation -spi drive DS1302
2022-06-09 22:34:00 【Lord rolling God】
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 :
routine SPI Signal description
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 Pin configuration
DS1302 The temporal :
DS1302 sequential
You can see : except CE The high and low levels are different , This timing is similar to half duplex SPI It's the same .
Half duplex SPI
DS1302 Register configuration :
DS1302 register
DS1302 The clock frequency of :
DS1302 clock frequency
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 :
DS1302 week ( register 0x8B)
Serial data :
边栏推荐
- 2022年最系统的自动化测试,测试开发面试题,10k以下不建议看
- Light Detection and Ranging (LiDAR)光探测和测距 (LiDAR)
- GameFi新的启程,AQUANEE将于6.9日登陆Gate以及BitMart
- C语言试题164之求定积分
- Chez scheme environment setup
- FPN-Feature Pyramid Network
- sparksql源码系列 | 一文搞懂Show create table 执行原理
- 常见的嵌入式端流媒体服务器开源项目!
- M-Arch(雅特力M4)【AT-START-F425测评】No.05 FLASH
- What is the "big safety" industry? Digital empowerment and great safety industry development
猜你喜欢

Début de la production de sécurité et prévention et contrôle des épidémies

Light Detection and Ranging (LiDAR)光探测和测距 (LiDAR)

继承的所有特征

汛期建筑施工现场安全生产风险智能防控

数字化工程施工企业这样开展“安全生产月”活动
![[image reconstruction] regularization based image super-resolution reconstruction with matlab code](/img/30/9ed42f3099ddb741352e26272e9c3d.png)
[image reconstruction] regularization based image super-resolution reconstruction with matlab code

2022安全生產月活動啟動安全生產與疫情防控兩手抓

什么是“大安全”产业?数字化赋能大安全产业发展

15省份发布2021年平均工资,这些行业有“钱途”

还在怀疑数字藏品么?国家队都开始入局了
随机推荐
Digital engineering construction enterprises carry out "safety production month" activities in this way
2022安全生产月活动启动安全生产与疫情防控两手抓
C语言试题164之求定积分
The survey shows that MacOS application developers generally say that their biggest challenge is how the product is discovered by users
化工企业双重预防体系数字化综合管理系统
Aquanee will land in gate and bitmart in the near future, providing a good opportunity for low-level layout
【图像分割】基于各向异性热扩散方程的图像分割附matlab代码
SDN specific network security issues
Collection operation of MySQL
Maximum value and subscript of acquisition matrix of C language test question 168
AQUANEE将在近期登陆Gate以及BitMart,低位布局的良机
Cookies and session workflows
【蓝桥杯集训100题】scratch眩晕苹果 蓝桥杯scratch比赛专项预测编程题 集训模拟练习题第12题
Information leakage and computational complexity of EMD like methods in time series prediction
经典面试题:如何快速求解根号2?
Learn how to parse SQL from kernel code
Find My产品|新款TWS耳机支持Find My功能,苹果Find My在第三方厂家应用更广泛
C语言试题163之计算某一天是对应年的第几天,这一年一共多少天;计算两个日期之间相隔的天数。两个日期由键盘输入。
Digital supervision of construction sites and the wisdom of scientific warfare
AVL树的旋转