找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 2720|回復(fù): 9
打印 上一主題 下一主題
收起左側(cè)

單片機(jī)+DS1302時(shí)鐘編程報(bào)錯(cuò)

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:717594 發(fā)表于 2020-4-26 23:52 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
參考普中單片機(jī)編寫(xiě)的一個(gè)時(shí)鐘程序,
編譯的時(shí)候顯示ds1302文件報(bào)錯(cuò)如下,搞了半天沒(méi)鬧明白,請(qǐng)問(wèn)各位大神哪里出了問(wèn)題。。。


ds1302.h文件
#ifndef _DS1302_H_
#define _DS1302_H_
#include "reg52.h"
#include "intrins.h"
#ifndef uint
#define uint unsigned int
#endif
#ifndef uchar
#define uchar unsighed uchar
#endif
sbit DSIO=P3^4;
sbit RST=P3^5;
sbit SCLK=P3^6;
void Ds1302Write(uchar addr,uchar dat);
uchar Ds1302Read(uchar addr);
void Ds1302Init();
void Ds1302ReadTime();
extern uchar TIME[7];   //??????
#endif

ds1302.c文件
#include "ds1302.h"
uchar TIME[7]=(0,0,0X12,0X07,0X05,0X06,0X16);  //????????16??????????5??7??
uchar code READ_RTC_ADDR[7]=(0X81;0X83,0X85,0X87,0X89,0X8b,0X8d);  //???????д????????1λ???1
uchar code WRITE_RTC_ADDR[7]=(0x90,0x82,0x84,0x86,0x88,0x8a,0x8c);
void Ds1302Write(uchar addr,uchar dat)
{
  uchar n;
  RST=0;
  _nop_();
  SCLK=0;
  _nop_();
  RST=1;
  _nop_();
  for(n=0;n<8;n++) //??????
  {
    DSIO=addr&0x01;
addr>>=1;
SCLK=1;   //????????????
_nop_();
SCLK=0;
_nop_();
}
  for(n=0;n<8;n++)    //????????
  {
    DSIO=dat&0x01;
dat>>=1;
SCLK=1;
_nop_();
SCLK=0;
_nop_();
   }
   RST=0;   //???????????
   _nop_();
  }
uchar Ds1302Read(uchar addr)
{
  uchar n, dat, dat1;
  RST=0;
  _nop_();
  SCLK=0;
  _nop_();
  RST=1;    //CE????
  _nop_();
  for(n=0;n<8;n++)
  {
   DSIO=addr&0x01;  //??????λ??????
   addr>>=1;
   SCLK=1;      // ????????????
   _nop_();
   SCLK=0;  //????????????
   _nop_();
  }
  _nop_();
  for(n=0;n<8;n++)
  {
    dat1=DSIO;
dat=(dat>>1)|(dat1<<7);  //dat?????λ??dat1?????λ
SCLK=1;      // ????????????
    _nop_();
    SCLK=0;  //????????????
    _nop_();
   }
   RST=0;
   _nop_();  //δDS1302??λ????????????????
   SCLK=1;
   _nop_();
   DSIO=0;
   _nop_();
   DSIO=1;
   _nop_();
   return dat;
}
void Ds1302Init()
{
  uchar n;
  Ds1302Write(0x8E,0x00);    //???д?????????д????????
  for(n=0;n<7;n++)
  {
    Ds1302Write(WRITE_RTC_ADDR[n],TIME[n]); //д??7????????????
   }
   Ds1302Write(0x8E,0x80);  //??д????????

}
void Ds1302ReadTime()
{
  uchar n;
  for(n=0;n<7;n++)
  {
  TIME[n]=Ds1302Read(READ_RTC_ADDR[n]);
  }
}


主文件
#include "reg52.h"
#include "ds1302.h"
typedef unsigned int u16;
typedef unsigned char u8;

sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
u8 code smgduan[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
u8 DisplayData[8];
void delay(u16 i)
{
   while(i--);
}
  
void datapros()
{
   Ds1302ReadTime();
   DisplayData[0]=smgduan[TIME[2]/16];   //??? ??????????????????
   DisplayData[1]=smgduan[TIME[2]&0X0f];
   DisplayData[2]=0x40;       //?????
   DisplayData[3]=smgduan[TIME[1]/16];   //??
   DisplayData[4]=smgduan[TIME[1]&0X0f];
   DisplayData[5]=0x40;
   DisplayData[6]=smgduan[TIME[0]/16];   //??
   DisplayData[7]=smgduan[TIME[0]&0x0f];
}
void DigDisplay()
{
  u8 i;
  for(i=0;i<8;i++)
  { switch(i)
  { case(0):
    LSA=0;LSB=0;LSC=0;break; //?????0λ,??????
case(1):
LSA=1;LSB=0;LSC=0;break;
case(2):
LSA=0;LSB=1;LSC=0;break;
case(3):
LSA=1;LSB=1;LSC=0;break;
case(4):
LSA=0;LSB=0;LSC=1;break;
case(5):
LSA=1;LSB=0;LSC=1;break;
    case(6):
LSA=1;LSB=1;LSC=0;break;
case(7):
LSA=1;LSB=1;LSC=1;break;
    }
P0=DisplayData[7-i];
delay(100);
P0=0x00;
  }
}
void main()
{
   Ds1302Init();
   while(1)
   {
    datapros();
DigDisplay();
}
}


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:658665 發(fā)表于 2020-4-27 00:31 | 只看該作者
今天太晚了,我沒(méi)有時(shí)間仔細(xì)看你的程序,如果你急用的話(huà),先參考我的吧 DS1302模塊.rar (30.72 KB, 下載次數(shù): 10)
回復(fù)

使用道具 舉報(bào)

板凳
ID:170318 發(fā)表于 2020-4-27 09:12 | 只看該作者
uchar code READ_RTC_ADDR[7]=(0X81;0X83,0X85,0X87,0X89,0X8b,0X8d);  括號(hào)中的0x81后面改成逗號(hào)
回復(fù)

使用道具 舉報(bào)

地板
ID:452950 發(fā)表于 2020-4-27 09:27 | 只看該作者
1.#define uchar unsighed char
2.#ifndef uint去掉
3#ifndef uchar去掉
4.#endif只留最后一個(gè)
5.引用函數(shù)最好加上extern
回復(fù)

使用道具 舉報(bào)

5#
ID:584814 發(fā)表于 2020-4-27 09:34 | 只看該作者
如果玩不好頭文件,直接在程序里定義uchar或?qū)懭?/td>
回復(fù)

使用道具 舉報(bào)

6#
ID:70069 發(fā)表于 2020-4-27 10:37 | 只看該作者
#ifndef uchar
#define uchar unsigned char
#endif
回復(fù)

使用道具 舉報(bào)

7#
ID:717594 發(fā)表于 2020-4-28 09:26 | 只看該作者
benclee 發(fā)表于 2020-4-27 10:37
#ifndef uchar
#define uchar unsigned char
#endif

發(fā)現(xiàn)了問(wèn)題了,define里面把char 寫(xiě)成了uchar, unsigned 寫(xiě)成了unsighed
回復(fù)

使用道具 舉報(bào)

8#
ID:717594 發(fā)表于 2020-4-28 09:26 | 只看該作者
pengxxx 發(fā)表于 2020-4-27 00:31
今天太晚了,我沒(méi)有時(shí)間仔細(xì)看你的程序,如果你急用的話(huà),先參考我的吧

非常感謝
回復(fù)

使用道具 舉報(bào)

9#
ID:717594 發(fā)表于 2020-4-28 09:27 | 只看該作者
jhczy 發(fā)表于 2020-4-27 09:12
uchar code READ_RTC_ADDR[7]=(0X81;0X83,0X85,0X87,0X89,0X8b,0X8d);  括號(hào)中的0x81后面改成逗號(hào)

這里確實(shí)有問(wèn)題,謝謝
回復(fù)

使用道具 舉報(bào)

10#
ID:717594 發(fā)表于 2020-4-28 09:27 | 只看該作者
xxpp2011 發(fā)表于 2020-4-27 09:27
1.#define uchar unsighed char
2.#ifndef uint去掉
3#ifndef uchar去掉

非常感謝
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表