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.

Digital clock with PIC16F84A

Status
Not open for further replies.

manba

New Member
i have designed a Digital clock using pic16f84a. I wrote the program in assembly, but i faced some problems one of them nothing is displying in the seven segments. can anyone help me.(see the attachments)


Code:
;**********************************************************************
;                                                                     *
;    Filename:	    Digital clock.asm                                 *
;    Date:          04-02-2009                                        *
;    File Version:  1.1                                               *
;                                                                     *
;    Author:        Muhammad Mused Al-Qabali                          *
;    Company:                                                         *
;                                                                     * 
;                                                                     *
;**********************************************************************
;                                                                     *
;    Files required:                                                  *
;                                                                     *
;                                                                     *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Notes:                                                           *
;                                                                     *
;                                                                     *
;                                                                     *
;                                                                     *
;**********************************************************************

LIST      p=16F84A         ; list directive to define processor
#INCLUDE <p16F84A.inc>     ; processor specific variable definitions
__CONFIG   _CP_OFF & _WDT_OFF & _PWRTE_ON & _XT_OSC
;**********
CBLOCK 0X0D
VCC_POW                   ; control the segements lighting
COUNT1
COUNT2
COUNT3
COUNT4
COUNT5
COUNT_SEC1
COUNT_SEC2
DISP_SEG1
DISP_SEG2
DISP_SEG3
DISP_SEG4
MAK_SEC
MAK_MINA            ; to generates miniuts of segment1
MAK_MINB            ; to generates miniuts of segment2
MAK_HOUA            ; to generates hours for segment3
MAK_HOUB            ; to generates hours for segment4
ENDC
;**********
BEGIN CODE 0X00
GOTO START
INTERRUPT CODE 0X04
GOTO INT
BEGIN2 CODE 0X05
GOTO START
;**********
START
GOTO INIT
;**********
INIT
BSF STATUS,RP0            ; Bank 1
CLRF TRISB
CLRF TRISA
MOVLW H'07'
MOVWF OPTION_REG          ; TMR0 prescaler= 1:256
BCF STATUS,RP0            ; Bank 0
BSF INTCON,5              ; Enable TMR0 overflue interrupts
MOVLW H'FF'
MOVWF COUNT_SEC1           ; to generate 1 second
MOVLW H'0F'
MOVWF COUNT_SEC2
MOVLW H'40'
MOVWF PORTB
MOVWF DISP_SEG1
MOVWF DISP_SEG2
MOVWF DISP_SEG3
MOVWF DISP_SEG4
CLRF PORTA
CLRF MAK_SEC
CLRF MAK_MINA
CLRF MAK_MINB
CLRF MAK_HOUA
CLRF MAK_HOUB
CLRF VCC_POW
BSF VCC_POW,1
BSF INTCON,7
CLRF TMR0
GOTO MAIN
;**********
MAIN
CALL DEL
CALL CHOSE_SEG
GOTO MAIN
;**********  interrupt *******
;*****************************
INT
BCF STATUS,RP0            ; Bank 0
DECFSZ COUNT_SEC2
GOTO END_INT
GOTO SECOND
;********** generate 60 second
SECOND
MOVLW H'0F'
MOVWF COUNT_SEC2
INCF MAK_SEC
MOVF MAK_SEC,W
BCF STATUS,Z
SUBLW D'60'
BTFSS STATUS,Z
GOTO END_INT
INCF MAK_MINA
;*****  end of interrupt 
END_INT
BCF INTCON,2
RETFIE
;**********
CHOSE_SEG  ; disply one segement with it's different number
BTFSC VCC_POW,1
CALL SEG_1
BTFSC VCC_POW,2
CALL SEG_2
BTFSC VCC_POW,3
CALL SEG_3
BTFSC VCC_POW,4
CALL SEG_4
RLF  VCC_POW
RETURN
;********** control the disply mode
SEG_1       ;control segment1 disply
BSF PORTA,0
CALL CALC_MIN1
MOVF DISP_SEG1,W
MOVWF PORTB
BCF PORTA,0
RETURN
;**********
SEG_2    ;control segment2 disply
BSF PORTA,1
CALL CALC_MIN2
MOVF DISP_SEG2,W
MOVWF PORTB
BCF PORTA,1
RETURN
;**********
SEG_3    ;control segment3 disply
BSF PORTA,2
CALL CALC_HOU1
MOVF DISP_SEG3,W
MOVWF PORTB
BCF PORTA,2
RETURN
;**********
SEG_4    ;control segment4 disply 
BSF PORTA,3
CALL CALC_HOU2
MOVF DISP_SEG4,W
MOVWF PORTB
BCF PORTA,3
RETURN
;********** MAKE SEGMENT 1 MINIUTS *******
CALC_MIN1  
BCF STATUS,Z
MOVLW H'01'
SUBWF MAK_MINA,W
BTFSC STATUS,Z
GOTO NUM1_SEG1
;*****
BCF STATUS,Z
MOVLW H'02'
SUBWF MAK_MINA,W
BTFSC STATUS,Z
GOTO NUM2_SEG1
;*****
BCF STATUS,Z
MOVLW H'03'
SUBWF MAK_MINA,W
BTFSC STATUS,Z
GOTO NUM3_SEG1
;*****
BCF STATUS,Z
MOVLW H'04'
SUBWF MAK_MINA,W
BTFSC STATUS,Z
GOTO NUM4_SEG1
;*****
BCF STATUS,Z
MOVLW H'05'
SUBWF MAK_MINA,W
BTFSC STATUS,Z
GOTO NUM5_SEG1
;*****
BCF STATUS,Z
MOVLW H'06'
SUBWF MAK_MINA,W
BTFSC STATUS,Z
GOTO NUM6_SEG1
;*****
BCF STATUS,Z
MOVLW H'07'
SUBWF MAK_MINA,W
BTFSC STATUS,Z
GOTO NUM7_SEG1
;*****
BCF STATUS,Z
MOVLW H'08'
SUBWF MAK_MINA,W
BTFSC STATUS,Z
GOTO NUM8_SEG1
;*****
BCF STATUS,Z
MOVLW H'09'
SUBWF MAK_MINA,W
BTFSC STATUS,Z
GOTO NUM9_SEG1
;*****
BCF STATUS,Z
MOVLW H'0A'
SUBWF MAK_MINA,W
BTFSC STATUS,Z
GOTO $+2           ; to increase segement2
RETURN
INCF MAK_MINB
CLRF MAK_MINA
CALL NUM0_SEG1
RETURN
;********** MAKE SEGMENT 2 MINIUTS *******
CALC_MIN2
BCF STATUS,Z
MOVLW H'01'
SUBWF MAK_MINB,W
BTFSC STATUS,Z
GOTO NUM1_SEG2
;*****
BCF STATUS,Z
MOVLW H'02'
SUBWF MAK_MINB,W
BTFSC STATUS,Z
GOTO NUM2_SEG2
;*****
BCF STATUS,Z
MOVLW H'03'
SUBWF MAK_MINB,W
BTFSC STATUS,Z
GOTO NUM3_SEG2
;*****
BCF STATUS,Z
MOVLW H'04'
SUBWF MAK_MINB,W
BTFSC STATUS,Z
GOTO NUM4_SEG2
;*****
BCF STATUS,Z
MOVLW H'05'
SUBWF MAK_MINB,W
BTFSC STATUS,Z
GOTO NUM5_SEG2
;*****
BCF STATUS,Z
MOVLW H'06'
SUBWF MAK_MINB,W
BTFSC STATUS,Z
GOTO $+2
RETURN
INCF MAK_HOUA
CLRF MAK_MINA
CLRF MAK_MINB
CALL NUM0_SEG1
CALL NUM0_SEG2
RETURN
;********** MAKE SEGMENT 3 HOURS *******
CALC_HOU1 
BCF STATUS,Z
MOVLW H'01'
SUBWF MAK_HOUA,W
BTFSC STATUS,Z
GOTO NUM1_SEG3
;*****
BCF STATUS,Z
MOVLW H'02'
SUBWF MAK_HOUA,W
BTFSC STATUS,Z
GOTO NUM2_SEG3
;*****
BCF STATUS,Z
MOVLW H'03'
SUBWF MAK_HOUA,W
BTFSC STATUS,Z
GOTO NUM3_SEG3
;*****
BCF STATUS,Z
MOVLW H'04'
SUBWF MAK_HOUA,W
BTFSC STATUS,Z
GOTO NUM4_SEG3
;*****
BCF STATUS,Z
MOVLW H'05'
SUBWF MAK_HOUA,W
BTFSC STATUS,Z
GOTO NUM5_SEG3
;*****
BCF STATUS,Z
MOVLW H'06'
SUBWF MAK_HOUA,W
BTFSC STATUS,Z
GOTO NUM6_SEG3
;*****
BCF STATUS,Z
MOVLW H'07'
SUBWF MAK_HOUA,W
BTFSC STATUS,Z
GOTO NUM7_SEG3
;*****
BCF STATUS,Z
MOVLW H'08'
SUBWF MAK_HOUA,W
BTFSC STATUS,Z
GOTO NUM8_SEG3
;*****
BCF STATUS,Z
MOVLW H'09'
SUBWF MAK_HOUA,W
BTFSC STATUS,Z
GOTO NUM9_SEG3
;*****
BCF STATUS,Z
MOVLW H'0A'
SUBWF MAK_HOUA,W
BTFSC STATUS,Z
GOTO $+2           ; to increase segement2
RETURN
INCF MAK_HOUA
CLRF MAK_MINA
CLRF MAK_MINB
CALL NUM0_SEG1
CALL NUM0_SEG2
MOVLW H'40'
MOVWF DISP_SEG1
MOVWF DISP_SEG2
RETURN
;********** MAKE SEGMENT 4 HOURS *******
CALC_HOU2 
BCF STATUS,Z
MOVLW H'01'
SUBWF MAK_HOUB,W
BTFSC STATUS,Z
GOTO NUM1_SEG4
;*****
BCF STATUS,Z
MOVLW H'02'
SUBWF MAK_HOUB,W
BTFSC STATUS,Z
GOTO NUM2_SEG4
;*****
BCF STATUS,Z
MOVLW H'03'
SUBWF MAK_MINB,W
BTFSC STATUS,Z
GOTO $+2           ; to increase segement2
RETURN
CLRF MAK_MINA
CLRF MAK_MINB
CLRF MAK_HOUA
CLRF MAK_HOUB
CALL NUM0_SEG1
CALL NUM0_SEG2
CALL NUM0_SEG3
CALL NUM0_SEG4
MOVLW H'40'
MOVWF DISP_SEG1
MOVWF DISP_SEG2
MOVWF DISP_SEG3
MOVWF DISP_SEG4
RETURN
;********** NUMBERS SEGEMENT 1 ******
NUM0_SEG1
MOVLW H'40'
MOVWF DISP_SEG1
RETURN
NUM1_SEG1
MOVLW H'F9'
MOVWF DISP_SEG1
RETURN
NUM2_SEG1
MOVLW H'24'
MOVWF DISP_SEG1
RETURN
NUM3_SEG1
MOVLW H'30'
MOVWF DISP_SEG1
RETURN
NUM4_SEG1
MOVLW H'19'
MOVWF DISP_SEG1
RETURN
NUM5_SEG1
MOVLW H'12'
MOVWF DISP_SEG1
RETURN
NUM6_SEG1
MOVLW H'02'
MOVWF DISP_SEG1
RETURN
NUM7_SEG1
MOVLW H'F8'
MOVWF DISP_SEG1
RETURN
NUM8_SEG1
MOVLW H'00'
MOVWF DISP_SEG1
RETURN
NUM9_SEG1
MOVLW H'10'
MOVWF DISP_SEG1
RETURN
;********** NUMBERS SEGEMENT 2 ******
NUM0_SEG2
MOVLW H'40'
MOVWF DISP_SEG2
RETURN
NUM1_SEG2
MOVLW H'F9'
MOVWF DISP_SEG2
RETURN
NUM2_SEG2
MOVLW H'24'
MOVWF DISP_SEG2
RETURN
NUM3_SEG2
MOVLW H'30'
MOVWF DISP_SEG2
RETURN
NUM4_SEG2
MOVLW H'19'
MOVWF DISP_SEG2
RETURN
NUM5_SEG2
MOVLW H'12'
MOVWF DISP_SEG2
RETURN
;********** NUMBERS SEGEMENT 3 ******
NUM0_SEG3
MOVLW H'40'
MOVWF DISP_SEG3
RETURN
NUM1_SEG3
MOVLW H'F9'
MOVWF DISP_SEG3
RETURN
NUM2_SEG3
MOVLW H'24'
MOVWF DISP_SEG3
RETURN
NUM3_SEG3
MOVLW H'30'
MOVWF DISP_SEG3
RETURN
NUM4_SEG3
MOVLW H'19'
MOVWF DISP_SEG3
RETURN
NUM5_SEG3
MOVLW H'12'
MOVWF DISP_SEG3
RETURN
NUM6_SEG3
MOVLW H'02'
MOVWF DISP_SEG3
RETURN
NUM7_SEG3
MOVLW H'F8'
MOVWF DISP_SEG3
RETURN
NUM8_SEG3
MOVLW H'00'
MOVWF DISP_SEG3
RETURN
NUM9_SEG3
MOVLW H'10'
MOVWF DISP_SEG3
RETURN
;********** NUMBERS SEGEMENT 4 ******
NUM0_SEG4
MOVLW H'40'
MOVWF DISP_SEG4
RETURN
NUM1_SEG4
MOVLW H'F9'
MOVWF DISP_SEG4
RETURN
NUM2_SEG4
MOVLW H'24'
MOVWF DISP_SEG4
RETURN
;**********
DEL
	CLRF COUNT1
	MOVLW H'02'
	MOVWF COUNT2
D0	DECFSZ COUNT1
	GOTO D0
D1	DECFSZ COUNT2
	GOTO D0
	RETURN
;**********
END   ; directive 'end of program'
 

Attachments

  • digital colck.JPG
    digital colck.JPG
    41.5 KB · Views: 562
Last edited:
hi,
Running the program in a simulator shows that the digits [ hh:mm] are being selected on PORTA,
but the segments [a to g] are not being output on PORTB

I would point out your code refers to LED_SEG when it should really be called LED_DIGIT.
This is possibly the cause of a programming error.
 
Last edited:
hi,
thanks a lot eng.ericgibbs for your interest and your answer.
i have solved the error, it was the delay of the LED_DIGIT.it wasn't enough to allow the number of the segment to appear. :D

eng.ericgibbs i need your advice, how to make the clock accurate??:confused:
 

Attachments

  • DIGITAL CLOCK.JPG
    DIGITAL CLOCK.JPG
    65.7 KB · Views: 473
hi,
thanks a lot eng.ericgibbs for your interest and your answer.
i have solved the error, it was the delay of the LED_DIGIT.it wasn't enough to allow the number of the segment to appear. :D

eng.ericgibbs i need your advice, how to make the clock accurate??:confused:

hi,
I would suggest a 3.876MHz crystal [ in place of the 4MHz] it would make the program a little easier.
Also it is possible to replace one of the two crystal capacitors with a trimming capacitor,
so that the crystal frequency can be trimmed for best accuracy.

You should also consider how you are going to set the clock to the correct real time.:)

EDIT:
the 3.876MHz is a typo error it should read 3.2768MHz
 
Last edited:
hi,

first:
eng.eric i will follow your advice, but could explain why using 3.876MHz instead of 4MHz.
because i couldn't find 3.876MHz crystal in my country.

second:
i depended on TMR0 to generate the seconds as follow, the prescelear is 1:256 then i'll have 256*256 us= 65.536 ms then 65.536 *5 = 327.680 ms then 327.680 * 3 = 983.040 ms appro 1 s. see the interrupt code.

Is my calculation true or false. ( i'm waiting your answer ) :confused:

Code:
INT
MOVWF  W_TEMP            ; save off current W register contents
MOVF   STATUS,W          ; move status register into W register
MOVWF  STATUS_TEMP       ; save off contents of STATUS register
;**********
BCF STATUS,RP0            ; Bank 0
DECFSZ COUNT_SEC1         ; COUNT_SEC1 = H'05'
GOTO END_INT
GOTO SECOND
;********** generate 60 second
SECOND
MOVLW H'05'
MOVWF  COUNT_SEC1 
INCF MAK_SEC
MOVF MAK_SEC,W
BCF STATUS,Z
SUBLW H'3C'               ; H'3C' = D'60'
BTFSS STATUS,Z
GOTO END_INT
INCF MAK_MINA
CLRF MAK_SEC
;*****  end of interrupt 
END_INT
BCF INTCON,2
;**********
MOVF    STATUS_TEMP,W     ; retrieve copy of STATUS register
MOVWF	STATUS            ; restore pre-isr STATUS register contents
SWAPF   W_TEMP,F
SWAPF   W_TEMP,W          ; restore pre-isr W register contents
RETFIE
 
Last edited:
hi,

first:
eng.eric i will follow your advice, but could explain why using 3.876MHz instead of 4MHz.
because i couldn't find 3.876MHz crystal in my country.

second:
i depended on TMR0 to generate the seconds as follow, the prescelear is 1:256 then i'll have 256*256 us= 65.536 ms then 65.536 *5 = 327.680 ms then 327.680 * 3 = 983.040 ms appro 1 s. see the interrupt code.

Is my calculation true or false. ( i'm waiting your answer ) :confused:

hi,
I prefer the 3.278MHz crystal because dividing by a multiple of 2 can produce a freq/period in integers of milliSecs.
The PIC divides the crystal freq by 4 , so a 3.278MHz xtal would give an internal clock of 819,200Hz. Divide this by 32768, gives a freq of 25Hz [40mSec], counting 25 of these in a program loop gives a precise one second.

The crystal can have a trim cap, so that this 1sec can be set to give a reasonable accuracy.

Your program clock at 4MHz, of 0.983sec will not be accurate.
I know that the program can be written to correct this to 1second, but IMO its far easier to use a suitable crystal.
 
Last edited:
Ahh a typo. I couldn't figure out 3.8768 either. I prefer 4.9152MHz as it's also perfect baud clock too.
Also look for Roman Blacks zero error 1 second clock.
 
Ahh a typo. I couldn't figure out 3.8768 either. I prefer 4.9152MHz as it's also perfect baud clock too.
Also look for Roman Blacks zero error 1 second clock.

Woops, thanks Bill.:)

To the OP its a 3.2768 MHz crystal.

**broken link removed**
 
Last edited:
hi, to all

thank you again eng.Eric and eng.Bill for your help.

i found 3.64MHz and 5MHz crystal in the second hand shops which one do you prefer to use.
 
I prefer the 3.278MHz crystal because dividing by a multiple of 2 can produce a freq/period in integers of milliSecs.
The PIC divides the crystal freq by 4 , so a 3.278MHz xtal would give an internal clock of 819,200Hz. Divide this by 32768, gives a freq of 25Hz [40mSec], counting 25 of these in a program loop gives a precise one second.

I see you prefer the 'John Becker' way of doing things, like I do too :D
 
hi, to all

thank you again eng.Eric and eng.Bill for your help.

i found 3.64MHz and 5MHz crystal in the second hand shops which one do you prefer to use.

hi,
Sorry, not suitable for the method I was proposing.:)
See Bill's ref, Roman Black,

Whats your location.?
 
hi, to all

thank you for helping me in this project. Now i can compelet myself and i will show you the result in the near future.

bay the way eng.Eric, i'm from YEMEN.
 
Status
Not open for further replies.

Latest threads

Back
Top