Proteus simulation DS1302 + 8-bit digital tube display test

Publisher:shtlswLatest update time:2017-10-02 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Proteus simulation of DS1302 + 8-bit digital tube display test.

The simulation effect is as follows:

 

The source program is as follows:

/*
51 single-chip microcomputer: DS1302 + digital tube Proteus simulation program.
Function: digital tube clock display.

Simulation results:
(1) The 8-bit digital tube displays the set time and date.
(2) The time and date display can be switched by pressing the button.
*/

#include

sbit LE_DUAN = P2^0; //Define 573 latch enable port digital tube segment latch
sbit LE_WEI = P2^1; //Define 573 latch enable port digital tube bit latch
#define SEGPORT P0 //Define digital tube connection port

sbit SCK = P3^6; // DS1302 clock line 
sbit SDA = P3^4; // DS1302 data line 
sbit RST = P3^5; // DS1302 reset line

//DS1302 reset redefine
#define RST_CLR RST=0 //Level set to low
#define RST_SET RST=1 //Level set to high

//DS1302 data
#define SDA_CLR SDA=0 //Level low
#define SDA_SET SDA=1 //Level high
#define SDA_RD SDA //Level read

//DS1302 clock
#define SCK_CLR SCK=0 //Clock signal
#define SCK_SET SCK=1 //Level set high

#define DS1302_SEC 0x80 //Seconds data address
#define DS1302_MIN 0x82 //Minutes data address
#define DS1302_HOUR 0x84 //Hours data address
#define DS1302_DATE 0x86 //Day data address
#define DS1302_MON 0x88 //Month data address
#define DS1302_DAY 0x8a //Week data
address#define DS1302_YEAR 0x8c //Year data address
#define DS1302_CTRL 0x8e //Control data address
#define DS1302_CHARGE 0x90 //Trickle charge  

bit ReadRTC_Flag; //Read DS1302 flag. 1 means read, 0 means not read.
unsigned char TimeMode; //Date and time switching flag.

unsigned char time_buf1[8] = {40,14,2,14,10,59,50,7}; // -year month day hour minute second week 2014-02-14 10:59:50 7 week
unsigned char time_buf[8] ; // -year month day hour minute second week
unsigned char TempData[8] ;

unsigned char code Seg_Wei[] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; //Bit code of digital tube, low level is effective. unsigned char code Seg_Duan[] =
{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40}; //Common anode digital tube display segment code value 0~9, -

void DS1302_Init(void);
void DS1302_Write_Byte(unsigned char addr, unsigned char d);
unsigned char DS1302_Read_Byte(unsigned char addr) ;
void DS1302_Read_Time(void);
void DS1302_Write_Time(void);

void Seg_Disp(unsigned char FirstBit,unsigned char Num);
void InitTIMER0(void);//inital timer0
void Delay_1ms(unsigned int i);
unsigned char GetKey(void);

void main(void)    
{
 unsigned char Key;
    InitTIMER0();
 DS1302_Init();
  DS1302_Write_Time();
 P2=0xff; //51 defaults to input
 while(1)
 {
  Key = GetKey();
  if(Key&0x01)
  {
   TimeMode = ~TimeMode; //After a key is pressed, the mode is reversed and the current display mode is changed
  }
  if(ReadRTC_Flag==1)
  {
   ReadRTC_Flag=0;
   DS1302_Read_Time();
   if(TimeMode)
   {
     TempData[0]=Seg_Duan[time_buf1[1]/10]; //Conversion of year data,
     TempData[1]=Seg_Duan[time_buf1[1]%10]; //Because we use the digital tube display 0~9, separate the data
     TempData[2]=0x40; //Add "-"
     TempData[3]=Seg_Duan[time_buf1[2]/10]; //Month
     TempData[4]=Seg_Duan[time_buf1[2]%10];
     TempData[5]=0x40;
     TempData[6]=Seg_Duan[time_buf1[3]/10]; //Daily
     TempData[7]=Seg_Duan[time_buf1[3]%10]; 
   }
   else
   {
     TempData[0]=Seg_Duan[time_buf1[4]/10]; //Time data conversion
     TempData[1]=Seg_Duan[time_buf1[4]%10]; //Since we use the digital tube 0~9 to display, separate the data
     TempData[2]=0x40; //Add "-"
     TempData[3]=Seg_Duan[time_buf1[5]/10]; //Minutes
     TempData[4]=Seg_Duan[time_buf1[5]%10];
     TempData[5]=0x40;
     TempData[6]=Seg_Duan[time_buf1[6]/10]; //Seconds
     TempData[7]=Seg_Duan[time_buf1[6]%10]; 
   }
  }
 } 
}

/*------------------------------------------------
           DS1302 initialization
------------------------------------------------*/
void DS1302_Init(void)
{
 RST_CLR; //RST pin set low
 SCK_CLR; //SCK pin set low
 DS1302_Write_Byte(DS1302_SEC,0x00); 
}

/*------------------------------------------------
           Write one byte of data to DS1302
------------------------------------------------*/
void DS1302_Write_Byte(unsigned char addr, unsigned char dat)
{
 unsigned char i;
 RST_SET; 
 addr = addr & 0xFE; //The lowest bit of the write address is W write, low level
 for (i = 0; i < 8; i++) 
 { 
  if (addr & 0x01) 
  {
   SDA_SET;
  }
  else 
  {
   SDA_CLR;
  }
  SCK_SET;
  SCK_CLR;
  addr = addr >> 1;
 }
 
 //Write data: dat
 for (i = 0; i < 8; i ++) 
    {
  if (dat & 0x01) 
      {
   SDA_SET;
   }
  else 
      {
   SDA_CLR;
   }
  SCK_SET;
  SCK_CLR;
  dat = dat >> 1;
  }
 RST_CLR; //Stop DS1302 bus
}


/*------------------------------------------------
           Read one byte of data from DS1302
------------------------------------------------*/

unsigned char DS1302_Read_Byte(unsigned char addr) 
{

 unsigned char i;
 unsigned char temp;
 RST_SET;
  
 addr = addr | 0x01; //Lowest RD, valid as high level
 for (i = 0; i < 8; i ++) 
 {
  if (addr & 0x01) 
  {
   SDA_SET;
  }
  else 
  {
   SDA_CLR;
  }
  SCK_SET;
  SCK_CLR;
  addr = addr >> 1;
 }
 
 //Output data: temp
 for (i = 0; i < 8; i ++) 
 {
  temp = temp >> 1;
  if (SDA_RD) 
  {
   temp |= 0x80;
  }
  else 
  {
   temp &= 0x7F;
  }
  SCK_SET;
  SCK_CLR;
 }
 
 RST_CLR; //Stop DS1302 bus
 return temp;
}


/*------------------------------------------------
           Read clock data from DS1302
------------------------------------------------*/
void DS1302_Read_Time(void)  

        unsigned char i,tmp;
 time_buf[1]=DS1302_Read_Byte(DS1302_YEAR); //yeartime_buf 
 [2]=DS1302_Read_Byte(DS1302_MON); //monthtime_buf 
 [3]=DS1302_Read_Byte(DS1302_DATE); //daytime_buf 
 [4]=DS1302_Read_Byte(DS1302_HOUR); // 
 hourtime_buf[5]=DS1302_Read_Byte(DS1302_MIN); //Minutes 
 time_buf[6]=(DS1302_Read_Byte(DS1302_SEC))&0x7F; //Seconds 
 time_buf[7]=DS1302_Read_Byte(DS1302_DAY); //Week

 for(i=0;i<8;i++)
 {              //BCD处理
  tmp=time_buf[i]/16;
  time_buf1[i]=time_buf[i]%16;
  time_buf1[i]=time_buf1[i]+tmp*10;
 }
}


/*------------------------------------------------
           Write clock data to DS1302
------------------------------------------------*/
void DS1302_Write_Time(void) 
{
     
    unsigned char i,tmp;
 for(i=0;i<8;i++)
 { //BCD processing
  tmp=time_buf1[i]/10;
  time_buf[i]=time_buf1[i]%10;
  time_buf[i]=time_buf[i]+tmp*16;
 }
 DS1302_Write_Byte(DS1302_CTRL,0x00); //Turn off write protection 
 DS1302_Write_Byte(DS1302_SEC,0x80); //Pause 
 //DS1302_Write_Byte(DS1302_CHARGE,0xa9); //Trickle charge 
 DS1302_Write_Byte(DS1302_YEAR,time_buf[1]); //Year 
 DS1302_Write_Byte(DS1302_MON,time_buf[2]); //Month 
 DS1302_Write_Byte(DS1302_DATE,time_buf[3]); //Day 
 DS1302_Write_Byte(DS1302_HOUR,time_buf[4]); //Hour 
 DS1302_Write_Byte(DS1302_MIN,time_buf[5]); //Minute
 DS1302_Write_Byte(DS1302_SEC,time_buf[6]); //Second
 DS1302_Write_Byte(DS1302_DAY,time_buf[7]); //Week 
 DS1302_Write_Byte(DS1302_CTRL,0x80); //Open write protection 
}

/*------------------------------------------------
           Digital tube display, pos is the starting position
------------------------------------------------*/
void Seg_Disp(unsigned char pos,unsigned char Num)
{
      static unsigned char i=0;

    SEGPORT=0; //Clear data to prevent alternating ghosting
       LE_DUAN=1; //Segment latch
       LE_DUAN=0;

       SEGPORT=Seg_Wei[i+pos]; //Get bit code 
       LE_WEI=1; //Latch bit
       LE_WEI=0;

       SEGPORT=TempData[i]; //Get display data, segment code
       LE_DUAN=1; //Segment latch
       LE_DUAN=0;
       
    i++;
       if(i==Num)
       i=0;
}


/*------------------------------------------------
           Timer0 initialization, interruption enabled, 2ms timing
------------------------------------------------*/
void InitTIMER0(void)
{
 TMOD|=0x01; //Timer setting 16 bits
 TH0=(65536-2000)/256; //Timing time 2ms
 TL0=(65536-2000)%256;
 EA=1;
 ET0=1;
 TR0=1;
}


/*------------------------------------------------
           Simple delay program: about 1ms
------------------------------------------------*/
void delay_1ms(unsigned int i)

 unsigned char j;
 while(i--)
 for(j=0;j<125; j++);
}


/*------------------------------------------------
          Key scanning, the key here is connected to P2.2
------------------------------------------------*/
unsigned char GetKey(void)
{
 unsigned char KeyTemp,CheckValue,Key=0x00;
 CheckValue = P2&0x04;
 if(CheckValue==0x04)
  return 0x00;
 delay_1ms(10);
 KeyTemp = P2&0x04;
 if(KeyTemp == CheckValue)
  return 0x00;
 if(!(CheckValue&0x04))
  Key = 0x01;
 return Key;
}

 

 

 


void tim0_isr(void) interrupt 1 //interrupt, used for digital tube scanning
{
    static unsigned char num;
  TH0=(65536-2000)/256; //Reassign 2ms
  TL0=(65536-2000)%256;

 Seg_Disp(0.8);
 num++;

  if(num==50) //roughly 100ms
   {
    num=0;
    ReadRTC_Flag=1; //read flag position 1
 }
 }

 

The above program was passed in Keil + proteus7.8.


http://download.csdn.net/detail/tcjy1000/9833785

Project package download


Reference address:Proteus simulation DS1302 + 8-bit digital tube display test

Previous article:1~99 seconds countdown digital tube display C program + Proteus simulation
Next article:Proteus simulation of 8-bit digital tube dynamic scanning display test

Recommended ReadingLatest update time:2024-11-16 13:59

A complete application example of DS1302 clock on STM32
A complete DS1302 clock application on STM32      /*DS1302 clock chip*/   uint8_t read = {0x81,0x83,0x85,0x87,0x89,0x8b,0x8d}; //Read the register addresses of seconds, minutes, hours, days, months, weeks and years    uint8_t write = {0x80,0x82,0x84,0x86,0x88,0x8a,0x8c}; //Write the register addresses of seconds, minu
[Microcontroller]
DS1302 read and write integrated C51 source program
DS1302 read and write integrated C51 source program (hotpower)   /*---------------------------------------------- DS1302 read and write integrated C51 source program  HotPower@126.com ------------------------------------------------*/ //Open DS1302 void TimeSpiOpen(void) {   TIMECLK = 0;   TIMERST = 0; //Di
[Microcontroller]
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号