8 Replies Latest reply on Mar 26, 2013 7:35 AM by user_276445525

    SPI-->DS1302    ???

    user_276445525

      config:

         

       

         

       

         

      my test code:

         

      SPIM_3_Start();
          while(1)
          {
              SPIM_3_TxEnable();                           //使能SPI发送
              SPIM_3_ClearRxBuffer();                      //清空Rx缓存
              a = SPIM_3_GetRxBufferSize();
             
              SPIM_3_WriteByte(READ_SECOND_REG);           //发送地址
              SPIM_3_TxDisable();
              while(!(SPIM_3_ReadStatus() & SPIM_3_STS_TX_FIFO_EMPTY));
              b = SPIM_3_GetRxBufferSize();
             
              //使能SPI接收
              c = SPIM_3_GetRxBufferSize();
             
              while(!(SPIM_3_ReadStatus() & SPIM_3_STS_RX_FIFO_NOT_EMPTY));
              e = SPIM_3_GetRxBufferSize();
              d = SPIM_3_ReadByte();                       //读出数据
             
              sprintf(msg,"TIME:  %d %d %d  %d %d",a,b,c,d,e);
              draw_debug(msg,0,0);
          }

         

       

         

      who  can tell me that the datas i get from DS1302 always are 0xff ???

         

      thank you!!!

        • 1. Re: SPI-->DS1302    ???
          user_1377889

          SPIM_GetRxBufferSize() only returns the number of characters, not the characters itself.

             

          You ought to read the RxStatus until there is a byte in the buffer and then get it.

             

          You just posted part of your code and part of the settings, so we cannot check everything. To post the complete project use the "Create workspace Bundle" of Creator and upload the .zip archive using ms internet explorer (NOT CHROME!!!)

             

           

             

          Bob

          • 2. Re: SPI-->DS1302    ???
            user_1377889

            Another issue I just see:

               

            Before switching from transmit to receive you check the TxStatus for FiFo empty, but you should wait for complete transmission since the pin will switch to input before the transfer is complete.

               

             

               

            Bob

            • 3. Re: SPI-->DS1302    ???
              user_276445525

              now:

                 

              SPIM_3_Start();
                  while(1)
                  {
                      SPIM_3_ClearRxBuffer();
                      SPIM_3_ClearTxBuffer();
                      SPIM_3_TxEnable();
                      SPIM_3_WriteByte(0x81);
                      while(!(SPIM_3_ReadStatus() & SPIM_3_STS_TX_FIFO_EMPTY));
                      SPIM_3_TxDisable();
                     
                      while(!(SPIM_3_ReadStatus() & SPIM_3_STS_RX_FIFO_NOT_EMPTY));
                     
                      a = SPIM_3_GetRxBufferSize();
                      b = SPIM_3_ReadByte();
                      sprintf(msg,"TIME:  %d  %d",a,b);
                      draw_debug(msg,0,0);
                  }

                 

              this does not work ,too.

                 

              the result is  a = 1, b = 255.

              • 4. Re: SPI-->DS1302    ???
                user_276445525

                can you give me a example of SPI in SPI Bidirectional Mode???

                • 5. Re: SPI-->DS1302    ???
                  user_1377889

                  Your code still checks the fifo and not the completeness of transfer.

                     

                  This is your code

                     

                          while(!(SPIM_3_ReadStatus() & SPIM_3_STS_TX_FIFO_EMPTY));
                  SPIM_3_TxDisable();

                     

                  Better check for

                     

                  Bob

                     

                   

                     

                  SPIM_3_STS_SPI_DONE

                  • 6. Re: SPI-->DS1302    ???
                    user_276445525

                    ok , i will check it on Monday.

                       

                    as you said, it is better .you are not sure it is right.

                       

                    i will try it.

                       

                    thank you very much.

                       

                    谢谢.

                    • 7. Re: SPI-->DS1302    ???
                      user_276445525

                      it does not work ,too.

                         

                      i went to my Master Shifu.

                         

                      he tested  the SPI bidirection(3-wire) mode.

                         

                      finally, he wrote these codes:

                         


                      SPIM_3_Start();
                          while(1)
                          {
                              SPIM_3_ClearRxBuffer();
                              SPIM_3_ClearTxBuffer();

                         


                              SPIM_3_TxEnable();

                         


                              SPIM_3_WriteByte(0x81);     //read the second reg from the DS1302

                         

                              SPIM_3_WriteByte(0x81);    //call again,just  for txtending the clk and ss singal and waiting the back datas.

                         

                              while(!(SPIM_3_ReadStatus() & SPIM_3_STS_RX_FIFO_NOT_EMPTY));
                            
                              SPIM_3_TxDisable();
                             
                              a = SPIM_3_ReadByte();
                              b = SPIM_3_ReadByte();
                              sprintf(msg,"  %d  %d",a,b);
                              draw_debug(msg,0,0);
                          }

                         

                      the result: a = 129  (0x81)                b is a right value from the DS1302

                         

                       

                         

                      now  i am Confused, i can not  write the reg of DS1302.

                         

                      even i add the code (eg.  SPIM_3_WriteByte(dat) )after SPIM_3_TxEnable()     i will not get the right b.

                         

                      i can not find  an example of SPI bidirection(3-wire) mode, can you send me one.

                         

                      thank you.

                      • 8. Re: SPI-->DS1302    ???
                        ki.leung

                        I used to use 4 wire mode, but try the following if that works

                           

                         

                           

                         

                           

                        Please noted

                           

                        The 1st byte to ds1302 is different for read and write operation

                           

                         

                           

                        SPIM_3_WriteByte(0x80);     //WRITE TO 1ST REGISTER OF THE DS1302!!!!

                           

                        And 

                           

                        SPIM_3_WriteByte(0x81);     //READ THE 1ST REGSITER FROM THE DS1302!!!!

                           

                         

                           

                        ****************

                           

                        1. To write ds1302

                           

                         

                           

                        SPIM_3_ClearRxBuffer();

                           

                        SPIM_3_ClearTxBuffer();

                           

                        SPIM_3_TxEnable();

                           

                        SPIM_3_WriteByte(0x80);     //write to 1st register of the DS1302

                           

                        while(!(SPIM_3_ReadStatus() & SPIM_3_STS_SPI_DONE));

                           

                        SPIM_3_WriteByte(SendData);    

                           

                        while(!(SPIM_3_ReadStatus() & SPIM_3_STS_SPI_DONE));

                           

                         ************************

                           

                        2. To read ds1302

                           

                         

                           

                        SPIM_3_ClearRxBuffer();

                           

                        SPIM_3_ClearTxBuffer();

                           

                        SPIM_3_TxEnable();

                           

                        SPIM_3_WriteByte(0x81);     //read the 1st regsiter from the DS1302

                           

                        while(!(SPIM_3_ReadStatus() & SPIM_3_STS_SPI_DONE));

                           

                        SPIM_3_TxDisable();

                           

                         

                           

                        SPIM_3_ReadByte(); /* a dummy one, just to have the slave shift data out */    

                           

                        returnData = SPIM_3_ReadByte(); 

                           

                         

                           

                         *****************

                           

                        See if this works

                        • 9. Re: SPI-->DS1302    ???
                          user_276445525

                          i will try another day.

                             

                          i have given up the SPI  module, now i use the PINs to write and read the DS1302.