OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 3743|回复: 9

实在搞不定了!求助原子哥发STM32 IO口驱动DS1302源码!

[复制链接]

4

主题

26

帖子

0

精华

初级会员

Rank: 2

积分
157
金钱
157
注册时间
2015-7-10
在线时间
25 小时
发表于 2015-10-24 11:05:17 | 显示全部楼层 |阅读模式
5金钱
小弟调1302两个星期了还没调出来!这个数据端口也设置成开漏输出了!上拉电阻也加了不知为何就是读不出数据来!全是0XFF

最佳答案

查看完整内容[请看2#楼]

http://www.openedv.com/posts/list/32492.htm
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

120

主题

7878

帖子

13

精华

资深版主

Rank: 8Rank: 8

积分
12012
金钱
12012
注册时间
2013-9-10
在线时间
427 小时
发表于 2015-10-24 11:05:18 | 显示全部楼层
http://www.openedv.com/posts/list/32492.htm
现在,程序把烂铜烂铁变得智能化了,人呢,一旦离开了这烂铜烂铁就不知道干啥了
回复

使用道具 举报

4

主题

26

帖子

0

精华

初级会员

Rank: 2

积分
157
金钱
157
注册时间
2015-7-10
在线时间
25 小时
 楼主| 发表于 2015-10-24 12:15:46 | 显示全部楼层
回复【2楼】八度空间:
---------------------------------
老师!这个IO口到底设置成开漏输出还是推挽输出?为何网上会出现两种答案?按您给你加上拉电阻的图,应该是开漏输出!可是程序里面是推挽输出?
回复

使用道具 举报

120

主题

7878

帖子

13

精华

资深版主

Rank: 8Rank: 8

积分
12012
金钱
12012
注册时间
2013-9-10
在线时间
427 小时
发表于 2015-10-24 13:14:42 | 显示全部楼层
回复【3楼】aust19:
---------------------------------
推挽输出即可,输入就配置成上拉输入,外部加上拉电阻的话,可以配置成开漏的
现在,程序把烂铜烂铁变得智能化了,人呢,一旦离开了这烂铜烂铁就不知道干啥了
回复

使用道具 举报

4

主题

26

帖子

0

精华

初级会员

Rank: 2

积分
157
金钱
157
注册时间
2015-7-10
在线时间
25 小时
 楼主| 发表于 2015-10-24 14:36:49 | 显示全部楼层
回复【4楼】八度空间:
---------------------------------
还是0!算了我直接上程序:
#ifndef DS1302_H
#define DS1302_H
#include"sys.h"
/******************************************************************************
                                  ?¨??????
                         ?ù????IO?????ù??????????
           ?è??IO??·??ò????????????????????????×????òIO??MCU
******************************************************************************/

#define DS1302_SDA_IN() {GPIOA->CRH &= 0xfff0ffff;GPIOA->CRL |= 0x00080000;}
#define DS1302_SDA_OUT() {GPIOA->CRH &= 0xfff0ffff;GPIOA->CRL |= 0x00030000;}

#define DS1302_RST PAout(13) //RST
#define DS1302_SCL PAout(14) //SCL
#define DS1302_SDA PAout(12) //SDA
#define DS1302_IN_SDA PAin(12) //????SDA??????????

/******************************************************************************
                                 ?¨???±???á??
******************************************************************************/

typedef struct
{
u8 year; //?ê
u8 month; //??
u8 date; //??
u8 week; //????
u8 hour; //???±
u8 minute; //·???
u8 second; //???? 
}Time_Typedef;

extern Time_Typedef TimeValue; //?¨???±??????????

#define test_data 0x55 //???????????ì??DS1302??·???????×???????????????????????

/******************************************************************************
                                ?¨???à???ü???ê
******************************************************************************/

#define Clock_Address_Bass 0x80 //?ù???·

#define Address_year (Clock_Address_Bass | 0x0c) //?ê
#define Address_month (Clock_Address_Bass | 0x08) //??
#define Address_date (Clock_Address_Bass | 0x06) //??
#define Address_week (Clock_Address_Bass | 0x0a) //????
#define Address_hour (Clock_Address_Bass | 0x04) //???±
#define Address_minute (Clock_Address_Bass | 0x02) //·???
#define Address_second (Clock_Address_Bass | 0x00) //??

#define Clock_Burst (Clock_Address_Bass | 0x3e) //?±???í????

//??±??¤?????÷
#define Address_WP (Clock_Address_Bass | 0x0e) //??±??¤

#define WP_Enable 0x80 //?ò????±??¤
#define WP_Disable 0x00 //??±???±??¤

/******************************************************************************
???????í?????÷???è??
    bit7  bit6  bit5  bit4  bit3  bit2  bit1  bit0
TCS3  TCS2  TCS1  TCS0   DS1   DS0   RS1   RS0

TCSx?????????÷?ó????????????0xa0?±?ò???????????????±??±???DS1302????????±???
DSx???è??VCC1??VCC2??????????????????????0x01?±??????????????????0x10?±??????????????????0x00?ò0x11?±??±?????????
RSx???è?????÷??×è?ó????0x00?????·????±???????????0x01??2K??0x10??4K??0x11??8K
******************************************************************************/
//???????í?????÷?á??
typedef struct
{
u8 TCSx; //????????
u8 DSx; //??????????
u8 RSx; //???÷??×è 
}Charge_Typedef;

extern Charge_Typedef ChargeValue; //?¨???????????÷

//???????í
#define Trickle_Charger_Address (Clock_Address_Bass | 0x10) //???í???????÷?????????±?????è????±???????????

#define Open_Trickle_Charge 0x0a
#define Close_Trickle_Charge 0x00 //?è????????????????

#define One_Diode 0x01
#define Two_Diode 0x10
#define None_Diode 0x00 //or 0x11??????????????±?????????

#define Resistor_For2K 0x01
#define Resistor_For4K 0x10
#define Resistor_For8K 0x11
#define None_Resistor 0x00 //???????????÷??×è????±?????????


//RAM???·
#define RAM_Address_Bass 0xc0 //?ù???·

#define RAM_Address0 (RAM_Address_Bass | 0x00) //RAM???· = ?ù???· + ???·??????
#define RAM_Address1 (RAM_Address_Bass | 0x02)
#define RAM_Address2 (RAM_Address_Bass | 0x04)
#define RAM_Address3 (RAM_Address_Bass | 0x06)
#define RAM_Address4 (RAM_Address_Bass | 0x08)
#define RAM_Address5 (RAM_Address_Bass | 0x0a)
#define RAM_Address6 (RAM_Address_Bass | 0x0c)
#define RAM_Address7 (RAM_Address_Bass | 0x0e)

#define RAM_Address8 (RAM_Address_Bass | 0x10)
#define RAM_Address9 (RAM_Address_Bass | 0x12)
#define RAM_Address10 (RAM_Address_Bass | 0x14)
#define RAM_Address11 (RAM_Address_Bass | 0x16)
#define RAM_Address12 (RAM_Address_Bass | 0x18)
#define RAM_Address13 (RAM_Address_Bass | 0x1a)
#define RAM_Address14 (RAM_Address_Bass | 0x1c)
#define RAM_Address15 (RAM_Address_Bass | 0x1e)

#define RAM_Address16 (RAM_Address_Bass | 0x20)
#define RAM_Address17 (RAM_Address_Bass | 0x22)
#define RAM_Address18 (RAM_Address_Bass | 0x24)
#define RAM_Address19 (RAM_Address_Bass | 0x26)
#define RAM_Address20 (RAM_Address_Bass | 0x28)
#define RAM_Address21 (RAM_Address_Bass | 0x2a)
#define RAM_Address22 (RAM_Address_Bass | 0x2c)
#define RAM_Address23 (RAM_Address_Bass | 0x2e)

#define RAM_Address24 (RAM_Address_Bass | 0x32)
#define RAM_Address25 (RAM_Address_Bass | 0x34)
#define RAM_Address26 (RAM_Address_Bass | 0x36)
#define RAM_Address27 (RAM_Address_Bass | 0x38)
#define RAM_Address28 (RAM_Address_Bass | 0x3a)
#define RAM_Address29 (RAM_Address_Bass | 0x3c)

#define RAM_BURST (RAM_Address_Bass | 0x3e)


//??????00???????±??
#define DS1302_Start_Second() (DS1302_Write_Data(Address_second,0x00))

//??±??¤?ü??
#define DS1302_WP_Enable() (DS1302_Write_Data(Address_WP,WP_Enable)) //?ò????±??¤
#define DS1302_WP_Disable() (DS1302_Write_Data(Address_WP,WP_Disable)) //??±???±??¤

//??±????????????????±???÷??
#define DS1302_Disable_Charge() (DS1302_Write_Data(Trickle_Charger_Address,0x00))

/******************************************************************************
                                 ????????????
******************************************************************************/

void DS1302_GPIOInit(void); //GPIO??????

void DS1302_Init(Time_Typedef *Time); //DS1302??????

extern unsigned DS1302_Read_Data(u8 add); //??DS1302?????·????????

extern void DS1302_Write_Data(u8 add,u8 dat); //?òDS1302?????·????????

extern u8 DS1302_Check(void); //?????è±?????

extern void DS1302_Charge_Manage(Charge_Typedef *CHG_dat); //?÷??????±????????????è??

extern void DS1302_RAM_WriteRead_Data(u8* pBuff,u8 WRadd,u8 num,u8 RW); //DS1302??????RAM??????×÷












#endif

#include"DS1302.h"
#include"delay.h"
#include "usart.h"

typedef struct
{
uint8_t second;
uint8_t minute;
uint8_t hour;
uint8_t day;
uint8_t month;
uint8_t week;
uint8_t year;

}Datatime;

Datatime RTC_DS1302;


/******************************************************************************
                                  ?¨??±???
******************************************************************************/

Time_Typedef TimeValue; //?¨???±??????????

Charge_Typedef ChargeValue; //?¨???????????÷

/******************************************************************************
* Function Name --> GPIO??????
* Description   --> none
* Input         --> none
* Output        --> none
* Reaturn       --> none 
******************************************************************************/
void DS1302_GPIOInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //????GPIOC???è?±??

/* ??????GPIOC */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12| GPIO_Pin_13 | GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  //???ì???? 
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/******************************************************************************
* Function Name --> DS1302????????×???????
* Description   --> none
* Input         --> dat??????????????
* Output        --> none
* Reaturn       --> none 
******************************************************************************/
void DS1302_Write_Byte(u8 dat)
{
u8 i;
DS1302_SDA_OUT(); //?è??????????
for(i=0;i<8;i++)
{
DS1302_SCL = 0; //?±????????
if(dat & 0x01) DS1302_SDA = 1; //??????·???????????·?????
else DS1302_SDA = 0;
dat >>= 1; //????????????
DS1302_SCL = 1; //·??????????????????§
}
}
/******************************************************************************
* Function Name --> DS1302????????×???????
* Description   --> none
* Input         --> none
* Output        --> none
* Reaturn       --> ?????????? 
******************************************************************************/
unsigned DS1302_Read_Byte()
{
u8 ReData=0x00;
u8 i;
DS1302_SDA_IN(); //?è??????????
for(i=0;i<8;i++)
{
if(DS1302_IN_SDA == 1)
{ ReData |= 0x80; } //???????????????°1?±
DS1302_SCL = 0; //?±????????
ReData >>= 1; //?????????????????????????????????°0?±
delay_us(3);
DS1302_SCL = 1; //??????????????
}
return(ReData); //·???????????????
}
/******************************************************************************
* Function Name --> ?òDS1302?????·????????
* Description   --> none
* Input         --> add??????×÷?????·
*                   dat??????????????
* Output        --> none
* Reaturn       --> none
******************************************************************************/
void DS1302_Write_Data(u8 add,u8 dat)
{
DS1302_RST = 0; //??????????
DS1302_SCL = 0; //?±????????
DS1302_RST = 1; //??????????
DS1302_Write_Byte(add); //????????×÷???·
DS1302_Write_Byte(dat); //????????
DS1302_RST = 0;
DS1302_SCL = 0;
}
/******************************************************************************
* Function Name --> ??DS1302?????·????????
* Description   --> none
* Input         --> add??????×÷?????·
* Output        --> none
* Reaturn       --> ?????????????÷??????
******************************************************************************/
unsigned DS1302_Read_Data(u8 add)
{
u8 Temp;
DS1302_RST = 1;
DS1302_Write_Byte(add); //????????×÷???·
Temp = DS1302_Read_Byte(); //????????????
DS1302_RST = 0;
return(Temp); //·???????????????
}
/******************************************************************************
* Function Name --> ?÷??????±????????????è??
* Description   --> ????±????????????????????????????ò???????????????????±?ò??
*                   ?????ò??DS1302?????????·???????÷?????????????????????????????????é·?
* Input         --> *CHG_dat???????÷????????
* Output        --> none
* Reaturn       --> none
******************************************************************************/
void DS1302_Charge_Manage(Charge_Typedef* CHG_dat)
{
u8 CHG_Value;

    CHG_Value = (CHG_dat->TCSx << 4) | (CHG_dat->DSx << 2) | CHG_dat->RSx;  

DS1302_WP_Disable(); //??????±??¤
DS1302_Write_Data(Trickle_Charger_Address,CHG_Value);
DS1302_WP_Enable(); //?ò????±??¤
}
/******************************************************************************
* Function Name --> DS1302??????RAM??????×÷
* Description   --> none
* Input         --> *pBuff????????????·???
*                   WRadd?????????????·??·??§??RAM_Address0 ~ RAM_Address28??????×??ó???????·??????????
*                   num??????×???????????????·??§??1 ~ 28????
*                   RW??????????????0x00??????×÷??0x01??????×÷
* Output        --> none
* Reaturn       --> none
******************************************************************************/
void DS1302_RAM_WriteRead_Data(u8* pBuff,u8 WRadd,u8 num,u8 RW)
{  
u8 i;

if(WRadd == RAM_Address29) return; //????????????RAM???·??×??ó???????±??????
//?ò??×??ó????×??????????ì??DS1302??
if(RW == 0x00) //????????×÷
{
for(i = 0;i < num;i++)
{
DS1302_WP_Disable(); //??????±??¤
DS1302_Write_Data(WRadd+(i<<1),pBuff);
DS1302_WP_Enable(); //?ò????±??¤
}
}
else
{
for(i = 0;i < num;i++)
{
DS1302_WP_Disable(); //??????±??¤
pBuff = DS1302_Read_Data(WRadd+1+(i<<1));
DS1302_WP_Enable(); //?ò????±??¤
}
}
}
/******************************************************************************
* Function Name --> ?????è±?????
* Description   --> ??DS1302??????RAM??×??ó???????·????????????????????????
*                   ???????????????à????????????????????·??ò?ò???????±??
* Input         --> none
* Output        --> none
* Reaturn       --> 0???è±?????????????????????
*                   1???è±??í?ó?ò????????
******************************************************************************/
u8 DS1302_Check(void)
{
u8 test_value;

DS1302_WP_Disable(); //??????±??¤
test_value = DS1302_Read_Data(RAM_Address29 + 1); //????×??ó????RAM???·????????
DS1302_WP_Enable(); //?ò????±??¤
if(test_value == test_data) //??????????·???
{
return 0; //?è±?????????????????????
}
else
{
return 1; //???????????????ò???è±??ì??

// DS1302_WP_Disable(); //??????±??¤
// DS1302_Write_Data(RAM_Address29,0x55); //?òRAM×??ó???????·??????????
// DS1302_WP_Enable(); //?ò????±??¤
//
// _nop_();_nop_();
//
// DS1302_WP_Disable(); //??????±??¤
// test_value = DS1302_Read_Data(RAM_Address29 + 1); //????×??ó????RAM???·????????
// DS1302_WP_Enable(); //?ò????±??¤
//
// if(test_value == test_data) return 0; //?è±?????????????????????
}
// return 1; //?è±????????ò????????
}
/******************************************************************************
* Function Name --> DS1302??????
* Description   --> none
* Input         --> *Time: ?±???á????????
* Output        --> none
* Reaturn       --> none
******************************************************************************/
void DS1302_Init(Time_Typedef* Time)
{
DS1302_RST = 0;
DS1302_SCL = 0;
DS1302_WP_Disable(); //??????±??¤
DS1302_Write_Data(Address_year,Time->year); //???ê????
DS1302_Write_Data(Address_week,Time->week); //??????????
DS1302_Write_Data(Address_month,Time->month); //????????
DS1302_Write_Data(Address_date,Time->date); //????????
DS1302_Write_Data(Address_hour,Time->hour); //?????±????
DS1302_Write_Data(Address_minute,Time->minute); //??·???????
DS1302_Write_Data(Address_second,Time->second); //?????????????±??????
DS1302_WP_Enable(); //?ò????±??¤


DS1302_WP_Disable(); //??????±??¤
DS1302_Write_Data(RAM_Address29,test_data); //?òRAM×??ó???????·??????????
DS1302_WP_Enable(); //?ò????±??¤
}    

不知道哪里配错了!以我的直觉要么就引脚模式配错了,要么就时序不对!
回复

使用道具 举报

4

主题

26

帖子

0

精华

初级会员

Rank: 2

积分
157
金钱
157
注册时间
2015-7-10
在线时间
25 小时
 楼主| 发表于 2015-10-24 14:44:13 | 显示全部楼层
回复【4楼】八度空间:
---------------------------------
老师!初始化和1302初始化都在main函数进来执行一次!然后在定时器里面1s直接读1302寄存器里面的数据还是0,这十有八九还是引脚配的不对啊
回复

使用道具 举报

4

主题

26

帖子

0

精华

初级会员

Rank: 2

积分
157
金钱
157
注册时间
2015-7-10
在线时间
25 小时
 楼主| 发表于 2015-10-24 16:33:11 | 显示全部楼层
难道论坛里就没人会么?全国的大神们呢?这个坑爹的1302
回复

使用道具 举报

120

主题

7878

帖子

13

精华

资深版主

Rank: 8Rank: 8

积分
12012
金钱
12012
注册时间
2013-9-10
在线时间
427 小时
发表于 2015-10-24 22:07:01 | 显示全部楼层
回复【6楼】aust19:
---------------------------------
抓管脚波形看下
现在,程序把烂铜烂铁变得智能化了,人呢,一旦离开了这烂铜烂铁就不知道干啥了
回复

使用道具 举报

13

主题

41

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
270
金钱
270
注册时间
2014-8-4
在线时间
56 小时
发表于 2016-1-18 15:28:40 | 显示全部楼层
楼主,你的DS1302程序调出来没?能不能发一份程序,让我看看。我现在遇到的问题,和你的一样。怎么读都是0!晶振还不输出波形,万用表测量晶振电压一个为0,一个为1.4左右!
在吃苦的年纪,何必那么安逸?
回复

使用道具 举报

13

主题

41

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
270
金钱
270
注册时间
2014-8-4
在线时间
56 小时
发表于 2016-1-18 15:31:48 | 显示全部楼层
楼主,你的DS1302程序调出来没?能不能发一份程序,让我看看。我现在遇到的问题,和你的一样。怎么读都是0!晶振还不输出波形,万用表测量晶振电压一个为0,一个为1.4左右!请发到我的邮箱:852500593@qq.com
在吃苦的年纪,何必那么安逸?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-6-21 15:00

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表