Display scan program
Configuration Bits
012
013
014
015
016
017
018 | __config h'3F10' ;OSC is Int 4MHz
;RA5,RA6,RA7,RB4 are I/O
;Power-up timer ON
;Code protection OFF
;Data code protection OFF
;Brown-out detection OFF
;Watchdog timer OFF |
The 4MHz oscillator is built in PIC16F628. By using this, an external oscillator becomes unnecessary and a circuit becomes simple. Moreover, thereby, an I/O Port can be used up to 16 ports. Since 14 LEDs were scanned in this circuit, the built-in oscillator needed to be used.
Scanning pattern
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058 | ra_adr equ t10h ;RA scan data table adr
d10h equ b'00000001' ;10th of hour
d1h equ b'00000010' ;1st of hour
d10m equ b'00000100' ;10th of minute
d1m equ b'00001000' ;1st of minute
d10s equ b'01000000' ;10th of second
d1s equ b'10000000' ;1st of second
rb_adr equ t1000y ;RB scan data table adr
d1000y equ b'11111110' ;1000th of year
d100y equ b'11111101' ;100th of year
d10y equ b'11111011' ;10th of year
d1y equ b'11110111' ;1st of year
d10mo equ b'11101111' ;10th of month
d1mo equ b'11011111' ;1st of month
d10d equ b'10111111' ;10th of day
d1d equ b'01111111' ;1st of day |
In this circuit, fourteen 7 segment LEDs are used except for the day of the week display. These 14 LEDs are scanned, being divided into two groups. Therefore, two LEDs light up simultaneously. Since the scan is high-speed, all LEDs seem to light up at the same time. In case of one group, an electricity consumption is more suppressed. However, the display becomes dark. Therefore, I did to two groups.
The 1st group is six LEDs of the hour, the minute and the second. They are controlled by the I/O Port of PORTA. Since RA5 is a port only for input, it cannot be used for this use.
The 2nd group is eight LEDs of the year, the month and the day. These are controlled by the I/O Port of PORTB.
LED connected to PORTA is turned on when an output is "1." LED connected to PORTB is turned on when an output is "0." A group was not classified according to this difference. Control is possible even when intermingled.
Initialization processing of various registers
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090 | ;******************* Initial Process ********************
init
;*** Set Port mode
bsf status,rp0 ;Change to Bank1
movlw b'00000000' ;RA7-0:OUT mode
movwf trisa ;Set TRISA reg
movlw b'00000000' ;RB7-0:OUT mode
movwf trisb ;Set TRISB reg
;*** Set Option reg
movlw b'00000001' ;PS=1:4
movwf option_reg ;Set OPTION_REG
;*** Set PIE1 reg
movlw b'00000000' ;TMR1IE=OFF
movwf pie1 ;Set PIE1 reg
bcf status,rp0 ;Change to Bank0
;*** Set CMCON reg
movlw b'00000111' ;RA port to digital
movwf cmcon ;Set CMCON reg
;*** Set TMR0 reg
movlw d'6' ;256-1000us/4us=6
movwf tmr0 ;Set TMR0 |
In initialization processing, all of PORTA and PORTB are set as output mode. Moreover, a setup which makes all ports a digital port is carried out to a CMCON register.
A timer uses only TMR0 and sets up interruption of one millisecond. The built-in 4MHz oscillator is used this time. Since the cycle is 0.25 microseconds, the clock of PIC of operation becomes 1 microsecond. The prescaler is set as 1:4 and a count is set up every 4 microseconds. 1 millisecond (1000 microseconds) is 250 counts. Count-up is performed by TMR0 register, and if a count value is set to 0, an interrupt will occur. Since maximum countable by TMR0 is 255, 256-250=6 is the preset value of TMR0. The reason for subtracting from 256 is for an interrupt to occur, when set to 0 from maximum (255).
Initialization processing of work areas
092
093
094
095
096
097
098
099
100
101
102
103
104
| ;*** Other registers and work area initialization
clrf porta ;Clear PORTA
clrf portb ;Clear PORTB
clrf ra_index ;Clear PORTA index
clrf rb_index ;Clear PORTB index
movlw d1000y ;Read 1000th of year data
movwf t1000y ;Set 1000th of year data
movlw d100y ;Read 100th of year data
movwf t100y ;Set 100th of year data
movlw d10y ;Read 10th of year data
movwf t10y ;Set 10th of year data
movlw d1y ;Read 1st of year data
movwf t1y ;Set 1st of year data
[Skip] |
Initialization of PORTA and PORTB and initialization of various work areas are performed.
A scanning pattern is set to GPR. This is for indirect address processing.
Interruption setup and waiting processing for interruption
126
127
128
129
130 | ;*** Set interrupt condition
movlw b'10100000' ;GIE,TOIE=1
movwf intcon ;Interruption enable
goto $ ;Wait interruption |
After all initialization processings finish, a setup which can be interrupted is performed to an INTCON register. Interruption to be used is only interruption of TMR0.
The rest only waits for interruption. It executes its address repeatedly. "$" points out its own address.
Interruption processing
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
| int
;***** TMR0 Interruption ( 1 millisecond interval) ******
bcf intcon,t0if ;Clear timer int flag
movlw d'6' ;Set 1 millisecond
movwf tmr0 ;Set TMR0
movlw ra_adr ;Read RA table head adr
addwf ra_index,w ;RA table head + index
movwf fsr ;Set table address
movfw indf ;Read data
movwf porta ;Set data to PORTA
incf ra_index,f ;RA index + 1
movfw ra_index ;Read RA index
sublw d'5' ;Check upper limit
btfss status,c ;Over ?
clrf ra_index ;Yes. Clear PORTA index
[Skip] |
An interrupt occurs for every 1 millisecond.
As for doing first by the interrupting processing, the interrupting flag is clear. If this is not performed, regardless of the time of a timer, an interrupt will always occur.
Next, a timer value is re-set to TMR0. The value of TMR0 is rewritten by the count.
The next is scanning processing. As for scanning data, the data for PORTA and the data for PORTB are made by initialization processing at GPR. ra_index" is the index of the data for PORTA and "rb_index" is the index of the data for PORTB. The data of the address which added the index value to the head address is data set to the port. One index is added each time, and when maximum is exceeded, it is set to 0.
It goes around the group of PORTA in 6 milliseconds, and goes around the group of PORTB in 8 milliseconds.
In the case of LED of the group of PORTA, the light is switched on for 1 millisecond and put out for 5 milliseconds. In the case of LED of the group of PORTB, the light is switched on for 1 millisecond and put out for 7 milliseconds.
Since blink is performed in a millisecond, it seems to have switched on the light continuously to the eyes of the human being. |