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
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
- Popular Resources
- Popular amplifiers
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
- Principles and Applications of Single Chip Microcomputers and C51 Programming (3rd Edition) (Xie Weicheng, Yang Jiaguo)
- Teach you to learn 51 single chip microcomputer-C language version (Second Edition) (Song Xuefeng)
- ATmega16 MCU C language programming classic example (Chen Zhongping)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- 5G base stations consume too much electricity. How can we overcome this energy consumption issue?
- Microwave equipment high voltage transformer with schematic diagram
- Who knows RALINK RTP 3352 QA TOOL
- Architecture and Implementation of ADSP-2106X SHARC DSPs Software Simulator
- EEWORLD University----[High Precision Laboratory] Interface
- Live streaming portal now open | TI IoT display solution using DLP micro-projection technology
- A netizen asked me if there is a chip that can turn the machine on and off by long pressing.
- Programming: MSP430 temperature, humidity and light measurement and display
- Engineers must know: Use and selection tips of power supply filters
- Nordic nRF52832 program download problem analysis