I have used 51 to drive DS1302 before, and it output the correct time in a short time. At that time, I thought this chip was simple and simple. But now I use STM32 to do a project, using the same chip, and I think it is not difficult. I just need to copy and paste the program and change the IO settings. But things are far from being as simple as I thought.
After 3 days of struggle, I now realize how naive I was at that time.
For the basic operation of DS1302, please see here: http://www.cnblogs.com/qsyll0916/p/7712695.html
Okay, enough nonsense, let’s get to the point.
First of all, the DS1302 read and write mode belongs to 3-wire SPI. CE, SCK, IO. Among them, the IO port is a bidirectional IO port, and we have to go through this IO port for reading and writing. When using 51 for development, because it is a quasi-bidirectional IO, we don't need to pay extra attention to its input and output settings. When output is needed, just write P0^1 = 1;
When you need to detect external input, just write if(P0^1 == 1) ,
All are very convenient, but the convenience also brings limitations on read and write speed. In STM32, each IO port has 8 output modes. Complexity also means that each mode is specially customized, which brings speed advantages. So when porting this program, you need to pay attention to the setting of this bidirectional IO. At first, I didn't understand it very well. I searched for information on Baidu and asked people. Finally, I learned that there are two ways to achieve bidirectional IO read and write settings.
First:
#define DS1302_DATIN PBin(6)
#define DS1302_DATOUT PBout(6)
#define DS1302_DAT_INPUT() {GPIOB->CRL&= 0XF0FFFFFF;GPIOB->CRL|= 8<<24;} //Set to pull-up or pull-down input mode, external pull-up resistor is required
#define DS1302_DAT_OUTPUT() {GPIOB->CRL&= 0XF0FFFFFF;GPIOB->CRL|= 3<<24;} //Set to a maximum of 50M general push-pull output
Through simple register operation, fast switching of input and output can be achieved. Pull-up resistors need to be connected at the port.
second:
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStruct.GPIO_Pin = DS1302_IO_PIN; //It is best to set it to open drain here. Of course, you can also use normal push-pull, but you need to keep switching the input and output modes later.
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_OD; //Open drain output, needs to be pulled up, no need to switch input and output.
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; //Output speed 50MHz
Configure the IO to open-drain output mode. You must connect an external pull-up resistor, otherwise all the reads will be low level. In this way, you don't have to keep switching the input and output modes, and you can use it directly like 51.
The configuration of bidirectional IO is basically the two cases mentioned above, but there may be other ways. However, I only know this way at present, and I will supplement it after learning it later.
After configuring the IO, we start to operate the IO to read and write DS1302 and get the real-time time. Pay special attention when reading and writing. Many online tutorials do not mention this point, probably because it is too basic. But this small problem has troubled me for a long time. When operating the write command and data of DS1302, we need to send the data in order from low to high. When sending data:
In 51, we often write like this: { SCIO = byte_1 & 0x01; byte_1 >> = 1; }
So can we write this in 32? : { PBout(6) = byte_1 & 0x01; byte_1 >> = 1; }
Absolutely not. Even though it looks like there is nothing wrong with it and there are no errors, it just doesn't communicate correctly. Very annoying.
The reason is that operations like PBout(6) belong to the bit-band operation of STM32. However, bit-band operation is not allowed to assign values other than 0 and 1 in CM3.
That is to say, the above operation assigns 2, 3, 4, etc. to PBout(6), but stm32 cannot understand what this means because it only recognizes 0 and 1!!!
So we can simply handle it like this:
if((byte_1 & t) != 0) //The previous problem lies here. The 32-bit bit-band operation cannot assign values other than 0 and 1.
{
DS1302_DATOUT = 1;
}
else
{
DS1302_DATOUT = 0;
}
In this way, communication can be normal. However, if it cannot be handled as above, the phenomenon is that the number 85 is always read. Regarding reading the number 85, in addition to the special case I mentioned above, there are still many people on the Internet who have experience in this regard. I have summarized them, which are roughly the following situations:
1. After reading the time, I did not pull the DATA_IO pin low. This caused question marks and 85 and other messy things to be displayed. But I added it.
2. The voltage is not enough, less than 4.6V. But this is controversial online. I connected 5V and measured 4.8V, which should be fine.
3. No pull-up resistors were connected. I only added pull-up resistors where bidirectional IO was needed. I used the reserved IIC SDA on the board, which has a 4.7K pull-up. I connected the IO here. It should be fine.
4. The simulation timing is wrong. But I have used this timing to implement the same function on 51 before, so there should be no problem porting it to 32. The delay time is also simulated, strictly in accordance with the 1us delay simulation.
However, I still discovered many other phenomena during my experiment, and recorded them again to prevent others from encountering the same problem and wasting so much time.
1. You must pay attention to the voltage of DS1302. It is best not to use the 3.3V on the STM32 development board. I have not made it. If you use an external power supply to power DS1302, you need to share the ground with the development board, otherwise the output will be 85.
2. If the DS1302 needs to modify the time, you need to remove the power-on protection in the initialization function, download the reset time again, and then add the power-on protection section to prevent the time from being reset after reset.
That's all the questions I have now. After solving the above problems one by one, I can read the time accurately. The error in 24 hours a day is no more than 2s, which is still very accurate.
OK, here is the source code.
First is the header file of DS1302, which mainly contains some bit-band operations and predefined
#ifndef __DS1302_H
#define __DS1302_H
#include
#include "hardware.h"
//Corresponding IO port configuration
#define DS1302_PORT GPIOB
#define DS1302_SCK_PIN GPIO_Pin_7 //Clock
#define DS1302_IO_PIN GPIO_Pin_6 //bidirectional IO port,
#define DS1302_CE_PIN GPIO_Pin_5 //Chip select enable, set high when reading and writing are required
#define DS1302_SCK PBout(7) //Bit-band operation, you can directly give high and low levels, but remember not to give numbers other than 0.1. Remember
#define DS1302_CE PBout(5)
#define DS1302_DATIN PBin(6)
#define DS1302_DATOUT PBout(6)
//Storage time
typedef struct _time{
u8 second;
u8 minute;
u8 hour;
u8 date;
u8 month;
u8 week;
u8 year;
}my_time;
void DS1302_Init(void);
void ds1302_readtime(void);
void display_real_time(void); //Display real time
#endif
DS1302 operation source file:
#include "ds1302.h"
#include "spi.h"
//READ_RTC_ADDR[7] = {0x81, 0x83, 0x85, 0x87, 0x89, 0x8b, 0x8d}; //Read the command address of the time. These addresses have been directly implemented through read and write operations
//WRITE_RTC_ADDR[7] = {0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c}; //Write time command address
my_time TIME = {0}; //Structure showing time
u8 init_time[] = {0x00,0x28,0x21,0x27,0x12,0x06,0x17}; //Initialization time: seconds, minutes, hours, days, months, and years
static void ds1302_gpio_init(void);
static void ds1302_writebyte(u8 byte_1); //Write a byte; byte is a reserved word and cannot be used as a variable
static void ds1302_writedata(u8 addr,u8 data_);//Write data to a certain address, data is a keyword inside c51, which means that the variable is defined in the data storage area, so data_ is used here;
static u8 ds1302_readbyte(void); //read a byte
static u8 ds1302_readdata(u8 addr);//Read a register data;
static void DS1302_delay_us(u16 time); //Simple delay 1us
//Basic IO settings
static void ds1302_gpio_init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
//Turn on the GPIOD clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
//Set the basic parameters of GPIO
GPIO_InitStruct.GPIO_Pin = DS1302_SCK_PIN | DS1302_CE_PIN ;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; //These two common ports are set to push-pull output
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; //Output speed 50MHz
GPIO_Init(DS1302_PORT, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = DS1302_IO_PIN; //It is best to set it to open drain here. Of course, you can also use normal push-pull, but you need to keep switching the input and output modes later.
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_OD; //Open drain output, needs to be pulled up, no need to switch input and output.
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; //Output speed 50MHz
GPIO_Init(DS1302_PORT, &GPIO_InitStruct);
}
//Write a byte
//Data and address are transmitted starting from the lowest bit
static void ds1302_writebyte(u8 byte_1)
{
u8 i = 0;
u8 t = 0x01;
for(i = 0;i<8;i++)
{
if((byte_1 & t) != 0) //The previous problem lies here. The 32-bit bit-band operation cannot assign values other than 0 and 1.
{
DS1302_DATOUT = 1;
}
else
{
DS1302_DATOUT = 0;
}
DS1302_delay_us(2);
DS1302_SCK = 1; //Rising edge write
DS1302_delay_us(2);
DS1302_SCK = 0;
DS1302_delay_us(2);
t<<= 1;
}
DS1302_DATOUT = 1; //Release IO, the subsequent reading will be much more accurate
DS1302_delay_us(2); //Because if IO is set to low level after writing, it will be affected when reading in open-drain output mode. It is best to pull it high first and then read
}
//Address write data
static void ds1302_writedata(u8 addr,u8 data_)
{
DS1302_CE = 0; DS1302_delay_us(2);
DS1302_SCK = 0; DS1302_delay_us(2);
DS1302_CE = 1; DS1302_delay_us(2); // Enable chip select signal
ds1302_writebyte((addr<<1)|0x80); //Convenient for later writing, the value of the address register after conversion,
ds1302_writebyte(data_);
DS1302_CE = 0; DS1302_delay_us(2); //Data transmission is finished, chip select is disabled
DS1302_SCK = 0; DS1302_delay_us(2); //Pull low to prepare for the next data write
}
//Read a byte, read on the rising edge
static u8 ds1302_readbyte(void)
{
u8 i = 0;
u8 data_ = 0;
//Because the port has been set to open drain above, and a pull-down resistor is connected to the outside of the circuit, you can use it directly without switching the input and output modes.
// DS1302_DAT_INPUT();
DS1302_SCK = 0;
DS1302_delay_us(3);
for(i=0;i<7;i++) //It is found here that if it is set to 8, the output data is incorrect and very messy
{
if((DS1302_DATIN) == 1)
{
data_ = data_ | 0x80; // low bit first, read bit by bit, it is wrong at the beginning, probably this is the problem
}
data_>>= 1;
DS1302_delay_us(3);
DS1302_SCK = 1;
DS1302_delay_us(3);
DS1302_SCK = 0;
DS1302_delay_us(3);
}
return (data_);
}
//Read the register value
static u8 ds1302_readdata(u8 addr)
{
u8 data_ = 0;
DS1302_CE = 0; DS1302_delay_us(2);
DS1302_SCK = 0; DS1302_delay_us(2);
DS1302_CE = 1; DS1302_delay_us(2); //CE must be high for read and write operations, and changes when SCK is low
ds1302_writebyte((addr<<1)|0x81); //Write the command to read the time
data_ = ds1302_readbyte();
DS1302_SCK = 1; DS1302_delay_us(2);
DS1302_CE = 0; DS1302_delay_us(2);
DS1302_DATOUT = 0; DS1302_delay_us(3); // Many people here say that it needs to be pulled low, but I found that it can also be displayed without this, but for safety reasons, I still add it.
DS1302_DATOUT = 1; DS1302_delay_us(2);
return data_;
}
void DS1302_Init(void)
{
u8 i = 0;
ds1302_gpio_init(); //Port initialization
DS1302_CE = 0; DS1302_delay_us(2);
DS1302_SCK = 0; DS1302_delay_us(2);
i = ds1302_readdata(0x00); //Read the second register,
if((i & 0x80) != 0) //Determine whether the time is initialized when the power is turned on next time by judging whether there is data in the second register, which is power-off protection
{
ds1302_writedata(7,0x00); //Remove write protection, allow writing data, 0x8e, 0x00
for(i = 0;i<7;i++)
{
ds1302_writedata(i,init_time[i]);
}
}
ds1302_writedata(7,0x80); //Turn on the write protection function to prevent data writing caused by interference.
}
//************
void ds1302_readtime(void) //Read time
{
u8 i;
for(i = 0;i<7;i++)
{
init_time[i] = ds1302_readdata(i);
}
}
static void DS1302_delay_us(u16 time)
{
u16 i = 0;
while(time--)
{
i = 5; //self-defined
while(i--);
}
}
//Display real time
void display_real_time(void)
{
ds1302_readtime(); //Get the time into the buffer first
//BCD code conversion ASCII code
TIME.year = ((init_time[6]&0x70)>>4)*10 + (init_time[6]&0x0f); //high three bits plus low four bits
TIME.month = ((init_time[4]&0x70)>>4)*10 + (init_time[4]&0x0f);
TIME.date = ((init_time[3]&0x70)>>4)*10 + (init_time[3]&0x0f);
TIME.week = ((init_time[5]&0x70)>>4)*10 + (init_time[5]&0x0f);
TIME.hour = ((init_time[2]&0x70)>>4)*10 + (init_time[2]&0x0f);
TIME.minute = ((init_time[1]&0x70)>>4)*10 + (init_time[1]&0x0f);
TIME.second = ((init_time[0]&0x70)>>4)*10 + (init_time[0]&0x0f);
OLED_ShowNum(48,0,TIME.hour,2,16);
OLED_ShowChar(64,0,':');
OLED_ShowNum(72,0,TIME.minute,2,16);
OLED_ShowChar(88,0,':');
OLED_ShowNum(96,0,TIME.second,2,16);
}
When driving DS1302, I basically encountered the above problems. If you have other problems, you can discuss them together.
Previous article:stm32F103 state machine matrix keyboard
Next article:DS1302 clock chip driver based on STM32
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Altium.Designer schematic and PCB design
- Choosing the Right Processor for Your Data Concentrator
- DSP TMS320C5509A Timer TIMER
- Purgatory Legends-Character State Machine Battle
- Application of FPGA in image processing.pdf
- F28335 ADC single channel single sampling code + comments
- Download the latest official schematics, PCB and drivers for each package of CH340
- 【Qinheng Trial】 Use touch buttons and GPIO to complete dialing and hanging up functions#3
- 【BLE 5.3 wireless MCU CH582】4. Serial port variable length data reception
- MSP430FR2311 Microcontroller IR Reflection Sensing Reference Design