Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Please review my code/circuit for an 8051 based project

Status
Not open for further replies.

amsung2

New Member
I thought I was the only one trying to make a digital clock that works like a an ordinary ticking clock but without arms. the display will be taken care of by a bunch of LEDs.
until I came across this:
https://www.electro-tech-online.com...t-for-a-digital-clock-a-level-project.104263/
well, I intend to do the exact same thing, but unlike this guy, I'm using an 89s52 microcontroller.
I've built up a code, and come up with a circuit. I'm unable to prototype it because IC 4017 that I intend to use in it, is acting a lot weird.

A brief explanation:
the microcontroller provides all the required signals. it is connected to 3 4017s that drive the LEDs that indicate hours, minutes, and seconds.
the minutes' 4017 and the seconds' 4017 are both connected to 60 LEDS arranged in banks on 10, that are controlled by the microcontroller via a transistor. therefore, 60 LEDs are controlled by the microcontroller, driven by a single 4017.
similarly, the hours' 4017 is connected to 12 LEDs.
the clock pulse that is supplied to the seconds' 4017 is 1Hz in frequency, and that for the minutes' 4017 is .0167Hz and so on.
each 4017 is connected to multiple number if LEDs, but we know that a 4017 can drive only 10 at a time. so I use transistors to switch a bank of 10 LEDs on or off. these transistors are controlled buy the microcontroller.

I'd be thankful if someone went through the code to point out any banana peels.

**broken link removed**

Code:
HOURS           EQU 07Ch             ;Our HOURS variable
MINUTES         EQU 07Dh             ;Our MINUTES variable
SECONDS         EQU 07Eh             ;Our SECONDS variable
TICKS           EQU 07Fh             ;Our 20th of a second countdown timer
CRYSTAL         EQU 11059200         ;The crystal speed
TMRCYCLE        EQU 12               ;The number of crystal cycles per timer increment
TMR_SEC         EQU 921600           ;The # of timer increments per second
F20TH_OF_SECOND EQU 46080            ;46080
RESET_VALUE     EQU 65536-F20TH_OF_SECOND

ORG 0000h                 ;Start assembly at 0000h
LJMP MAIN                 ;Jump to the main routine

ORG 0003H                 ;This is where the ISR for INT0 starts, ie, minutes have to be incremented.
PUSH ACC
PUSH PSW
inc MINUTES
MOV A,MINUTES
cjne A,#61,stable         ;if by mistake, minutes exceed 60
MOV MINUTES,#00
stable:
POP ACC
POP PSW
RETI

ORG 0013H                 ;This is where the ISR for INT1 starts, ie, hours have to be incremented.
PUSH ACC
PUSH PSW
inc HOURS
MOV A,HOURS
cjne A,#13,stble         ;if by mistake, hours ecxeed 12
MOV HOURS,#00
stble:
POP ACC
POP PSW
RETI

ORG 001Bh                 ;This is where Timer 1 Interrupt Routine starts
PUSH ACC                  ;We'll use the accumulator, so we need to protect it
PUSH PSW                  ;Protect PSW flags
CLR TR1                   ;Turn off timer 1 as we reset the value
MOV TH1,#HIGH (RESET_VALUE-5);Set the high byte of the reset value
MOV TL1,#LOW (RESET_VALUE-5);Set the low byte of the reset value
SETB TR1                  ;Restart timer 1 now that it has been initialized
MOV A,TICKS
CJNE A,#10,cont
setb P0.0                 ;inntitialise the seconds clock (at 1Hz, ie, a square wave of half a second duration)
cont:
DJNZ TICKS,EXIT_RTC       ;Decrement TICKS, if not yet zero we exit immediately
MOV TICKS,#20             ;Reset the ticks variable
INC SECONDS               ;Increment the second varaiable
clr P0.0              
MOV A,SECONDS             ;Move the seconds variable into the accumulator
CJNE A,#60,EXIT_RTC       ;If we haven't counted 60 seconds, we're done.
MOV SECONDS,#0            ;Reset the seconds varaible
INC MINUTES               ;Increment the number of minutes
MOV A,MINUTES             ;Move the minutes variable into the accumulator
CJNE A,#60,EXIT_RTC       ;If we haven't counted 60 minutes, we're done
MOV MINUTES,#0            ;Reset the minutes variable
INC HOURS                 ;Increment the hour variable
MOV A,HOURS               ;Move the minutes variable into the accumulator
CJNE A,#12,EXIT_RTC       ;If we haven't counted 12 hours, we're done
MOV HOURS,#0              ;Reset the hours variable
EXIT_RTC:
POP PSW                   ;Restore the PSW register
POP ACC                   ;Restore the accumulator
RETI                      ;Exit the interrupt routine

MAIN:
MOV TH1,#HIGH RESET_VALUE ;Initialize timer high-byte
MOV TL1,#LOW RESET_VALUE  ;Initialize timer low-byte
MOV TMOD,#10h             ;Set timer 1 to 16-bit mode
SETB TR1                  ;Start timer 1 running
MOV IP,#8                 ;Timer 1 Priority=1, all others = 0
MOV HOURS,#00             ;Initialize to 0 hours
MOV MINUTES,#00           ;Initialize to 0 minutes
MOV SECONDS,#00           ;Initialize to 0 seconds
MOV TICKS,#20             ;Initialize countdown tick counter to 20
SETB EA                   ;Initialize interrupts
SETB ET1                  ;Initialize Timer 1 interrupt
setb p1.0                 ;enable the first bank of 10 LEDs for first 10 seconds
setb p2.0                 ;enable the first bank of 10 LEDs for first 10 minutes
setb p3.0                 ;enable the first bank of 6 LEDs for first 6 hours
setb p0.0                 ;set the first clock pusle to the seconds 4017
setb p0.1                 ;set the first clock pulse to the minutes 4017
setb p0.2                 ;set the first clock pulse to the hours 4017
setb EX0                  ;enable the external interrupt to set the minutes
setb EX1                  ;enable the external interrupt to set the hours
check:
MOV A,SECONDS
cjne A,#10,sfw1       ;check if 10 seconds have passed.
setb P1.1                 ;enable the next bank of 10 leds for seconds
clr P1.0
sfw1:

cjne A,#20,sfw2       ;check if 20 seconds have passed.
setb p1.2                 ;enable the next bank of 10 leds for seconds
clr P1.1
sfw2:

cjne A,#30,sfw3       ;check if 30 seconds have passed.
setb p1.3                 ;enable the next bank of 10 leds for seconds
clr P1.2
sfw3:

cjne A,#40,sfw4       ;check if 40 seconds have passed.
setb p1.4                 ;enable the next bank of 10 leds for seconds
clr P1.3
sfw4:

cjne A,#50,sfw5       ;check if 50 seconds have passed.
setb p1.5                 ;enable the next bank of 10 leds for seconds
clr P1.4

sfw5:
setb P1.0                 ;therefore less than 10 seconds have passed, enable the first bank
cjne A,#60,nomin       ;check if 60 seconds have passed
setb p0.1                 ;send a clock to the minutes 4017 if a minute has passed

nomin:
MOV A,MINUTES
cjne A,#60,nohr       ;check if 60 minutes have passed
setb p0.2                 ;send a clock to the hours 4017 if an hour has passed
nohr:
cjne A,#10,mfw1       ;check if 10 minutes have passed.
setb p2.1                 ;enable the next bank of 10 leds for minutes
clr P2.0
mfw1:

cjne A,#20,mfw2       ;check if 20 minutes have passed.
setb p2.2                 ;enable the next bank of 10 leds for minutes
clr P2.1
mfw2:

cjne A,#30,mfw3       ;check if 30 minutes have passed.
setb p2.3                 ;enable the next bank of 10 leds for minutes
clr P2.2
mfw3:

cjne A,#40,mfw4       ;check if 40 minutes have passed.
setb p2.3                 ;enable the next bank of 10 leds for minutes
clr P1.0
mfw4:

cjne A,#50,mfw5       ;check if 50 minutes have passed.
setb p2.5                 ;enable the next bank of 10 leds for minutes
clr P2.4
mfw5:
setb P2.0                 ;therefore less than 10 minutes have passed, enable the first bank

MOV A,HOURS
cjne A,#6,hfw1         ;check if 6 hours have passed.
setb p3.1                 ;enable the next bank of 6 leds for hours
clr P3.0
hfw1:
setb P3.0                 ;therefore less than 6 hours have passed, enable the first bank
clr p0.1                  ;clear the clock pusle to the 4017, to make way for the next clock.
clr p0.2                  ;clear the clock pusle to the 4017, to make way for the next clock.
sjmp check                ;loop unendingly
END

Sorry for a lengthy post.
 
MY problem is, when there is no clock applied to the 4017, the LEDs just keep blinking continuously (in the correct sequence,ie, 0-9) at a very fast speed. they blink from 0 to 9 about 2-3 times a second!
and when the clock signal is applied, they do the same when the clock is low, but when the signal is high, the blinking stops, and when the signal goes low, the whole blinking thing starts again!
the thing is when the clock is high an LED holds ON, but this happens to a random LED.
what I expected was, when there was no clock applied, only the first LED should stay on, and when the clock is applied, the LEDs should start blinking one after the other in sync with the clock.
I've gone to great lengths to fine tune my code, but this is an unexpected problem.
by the way I'm using HEF4017BP manufactured by NXP.
 
Wait guys, I found the solution to the erratic behavior of the 4017. You just need to connect an 8-10 K Ohm pulldown resistor to the clock pin along with the clock signal and viola!
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top