Question About 8051 Software Real Time Clock (RTC) - Page 1
Forum section: electronics / beginners

author: moimoi
author_rank: Newbie
timestamp: November 26, 2014, 03:39:54 pm
content: Hello guys,My name is Tim and I'm currently in my second year of EE. I'm trying to make a 8051 Real Time Clock based on the tutorial from 8052.comhttp://8052.com/tutrtc. I followed their code and make some changes according to the hardware that i am using and modify the code a bit to produce output to 6 7-segment displays (hook up with 7447) to display the time. I've got some questions that i don't understand, and since that 8052.com site forum is no longer active, I don't know anywhere else to ask except here. So here is the codeCode:HOURSEQU07CHMINUTESEQU07DHSECONDSEQU07EHTICKSEQU07FH;CRYSTALEQU22118400;TMRCYCLEEQU12;TMR_SECEQUCRYSTAL/TMRCYCLE;F50TH_OF_SECEQUTMR_SEC*0.02;F50TH_OF_SECEQU36864;RESET_VALUEEQU65536 - F50TH_OF_SECRESET_VALUEEQU28672ORG 0000HLJMP _MAINORG 001BHPUSH ACCPUSH PSWCLR TR1MOV TH1,#HIGH RESET_VALUEMOV TL1,#LOW RESET_VALUESETB TR1DJNZ TICKS,EXIT_RTCMOV TICKS,#50INC SECONDSMOV A,SECONDSCJNE A,#60,EXIT_RTCMOV SECONDS,#0INC MINUTESMOV A,MINUTESCJNE A,#60,EXIT_RTCMOV MINUTES,#0INC HOURSMOV A,HOURSCJNE A,#24,EXIT_RTCMOV HOURS,#0EXIT_RTC:; Convert the time to BCD and display it to the 7-segment displayMOV A,SECONDSMOV B,#10DDIV ABRL ARL ARL ARL AADD A,BMOV P2,AMOV A,MINUTESMOV B,#10DDIV ABRL ARL ARL ARL AADD A,BMOV P0,AMOV A,HOURSMOV B,#10DDIV ABRL ARL ARL ARL AADD A,BMOV P1,APOP PSWPOP ACCRETI_MAIN:MOV TH1,#HIGH RESET_VALUEMOV TL1,#LOW RESET_VALUEMOV TMOD,#10HSETB TR1MOV HOURS,#00MOV MINUTES,#55MOV SECONDS,#00; 50 Ticks is needed for the timer to count a secondMOV TICKS,#50SETB EASETB ET1ENDSo according to the tutorial, if I want to start the timer to count from 23:00:00, i need only to set the HOURS value to 23 like thisCode:MOV HOURS,#23MOV MINUTES,#00MOV SECONDS,#00MOV TICKS,#50But after i tried, it still starts at 00:00:00 after reset. I've been trying to find the problem and changing code here and there for 3 hours but i still got no luck.Strangely, i have also tried to remove the initialization processCode:MOV HOURS,#23MOV MINUTES,#00MOV SECONDS,#00MOV TICKS,#50But it doesn't produce an error and it still starts from 00:00:00. I have no idea where i was doing wrong. I'm still a beginner with the 8051 programming, so i might have overlook something stupid here. I'm using M-IDE51 and FLIP to program the board. The board is custom made by my school. Any help is appreciated, and thank you for your time guysEDIT : The solution for the problem is adding a loop likeloop: sjmp loopBefore the END statement of the programThumbs up for @PA0PBZCheers,Best Regards,Tim

author: JoeO
author_rank: Frequent Contributor
timestamp: November 26, 2014, 09:13:46 pm
content: You need to comment your code. The few comments you have are insufficient.Your instructor should be emphasizing this.Put the comments in as you write the code, not after. You may think you are saving yourself time but you are not.

author: moimoi
author_rank: Newbie
timestamp: November 27, 2014, 12:37:53 pm
content: Quote from: JoeO on November 26, 2014, 09:13:46 pmHi there, sorry for the lack of comments, i know i should add more comments, and my instructor never emphasized about writing comment (His example code also lacks comments). I will edit the code

author: PA0PBZ
author_rank: Super Contributor
timestamp: November 27, 2014, 12:54:11 pm
content: I'd put aloop: sjmp loopjust before the end. I haven's seen the output of your assembler but chances are that it just continues to run and loops around.

author: moimoi
author_rank: Newbie
timestamp: November 27, 2014, 02:07:41 pm
content: Quote from: PA0PBZ on November 27, 2014, 12:54:11 pmHi there, thanks for the reply, in fact that is the problem, iIve found out about that also. I forgot to update this thread about the answer. Thumbs up sirBtw, do you know or have any references about configuring the 8051 input ? I've found some but its not very clear to me yet. I'm trying to connect a 4x4 keypad for the user to input the starting time. Thanks in advance

author: JoeO
author_rank: Frequent Contributor
timestamp: November 28, 2014, 03:40:44 am
content: Quote from: moimoi on November 27, 2014, 12:37:53 pmThat is too bad that your instructor does not enforce the use of comments. He/she would not last long in industry.Out of curiosity, where do you go to school?

author: vsq
author_rank: Contributor
timestamp: November 28, 2014, 06:37:23 am
content: Quote from: JoeO on November 28, 2014, 03:40:44 amWell-written and -structured code does not need comments.

author: miguelvp
author_rank: Super Contributor
timestamp: November 28, 2014, 06:42:40 am
content: ^ As long as it's read by the original programmer and s/he might even have trouble remembering after a few years.Comments are good, documentation and unit tests even better.

author: JoeO
author_rank: Frequent Contributor
timestamp: November 28, 2014, 08:10:49 pm
content: Quote from: miguelvp on November 28, 2014, 06:42:40 amTotally agree.Most people who code, work for a living. Someone will most likely have to pick up their code at a later time in order to fix or add features. Comments are a necessity.The Obfuscated C contests prove that comments are needed.

author: moimoi
author_rank: Newbie
timestamp: November 29, 2014, 06:48:12 am
content: Quote from: JoeO on November 28, 2014, 03:40:44 amHi there, sorry for the late reply, have been busy all day with school and trying to add functions to the RTC project.I'm currently taking my degree in Electrical Engineering in Taiwan (National Chung Cheng University), I'm an overseas students from Indonesia.

author: moimoi
author_rank: Newbie
timestamp: November 29, 2014, 06:54:07 am
content: Quote from: miguelvp on November 28, 2014, 06:42:40 amYes indeed, if I were to write a C code, i will always put comments along when i was typing the code, but in assembly i don't know why i write comments later. I think it might have something to do with the MIDE-51 IDE which is bad for typing codes.

author: JoeO
author_rank: Frequent Contributor
timestamp: November 29, 2014, 02:02:35 pm
content: Quote from: moimoi on November 29, 2014, 06:48:12 amThank you for taking the time to answer. Good luck to you!

author: moimoi
author_rank: Newbie
timestamp: November 29, 2014, 02:36:47 pm
content: Quote from: JoeO on November 29, 2014, 02:02:35 pmThanks sir!