Some thoughts on STM32 driving DS1302

Publisher:salahc1983Latest update time:2018-06-25 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.


Keywords:STM32 Reference address:Some thoughts on STM32 driving DS1302

Previous article:stm32F103 state machine matrix keyboard
Next article:DS1302 clock chip driver based on STM32

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号