Файл: Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 8672
Скачиваний: 0
558 |
Chapter 16 |
The delay routine required in this case is coded as follows:
;=======================
;delay procedure ;=======================
;For a 10Mhz clock the Fosc32 option produces a conversion
;speed of 1/(10/32) = 3.2 microseconds. At 3.2 ms per bit
;13 bits require approximately 41 ms. The instruction time
;at 10Mhz is 10 ms. 4/10 = 0.4 ms per instruction. To delay
;41 ms a 10Mhz PIC must execute 11 instructions. Add one
;more for safety.
delayAD: |
||
movlw |
.12 |
; Repeat 12 machine cycles |
movwf |
count1 |
; Store value in counter |
repeat11: |
||
decfsz |
count1,f |
; Decrement counter |
goto |
repeat11 ; Continue if not 0 |
|
return |
||
16.3 Realtime Clocks
In the context of microcontrollers and embedded systems, realtime clocks (also called RTCs) are integrated circuits designed to keep track of time in conventional hours, that is, in years, days, hours, minutes, and seconds. Many realtime clock ICs are available with various characteristics, data formats, modes of operation, and interfaces. Most of the ones used in PIC circuits have a serial interface in order to save access ports. Most RTC chips provide a battery connection so that time can be kept when the system is turned off.
In the sections that follow, we discuss one popular RTC chip: the NJU6355, but this is by no means the only option for embedded systems.
16.3.1 The NJU6355 Realtime Clock
The NJU6355 series is a serial I/O realtime clock used in microcontroller-based embedded systems. The IC includes its own quartz crystal oscillator, counter, shift register, voltage regulator, and interface controller. The PIC interface requires four lines. Operating voltage is TTL level so it can be wired directly on the typical PIC circuit. The output data includes year, month, day-of-week, hour, minutes, and seconds. Figure 16-9 is the pin diagram for the NJU6355.
NJU6355 output is in packed BCD format, that is, each decimal digit is represented by a 4-bit binary number. The chip’s logic correctly calculates the number of days in each month as well as the leap years. All unused bits are reported as binary 0. Figure 16-10 is a bitmap of the formatted timer data.
560 |
Chapter 16 |
The NJU6355 does not report valid time data until after it has been initialized, even if there are power and clock signals into the chip. Initialization requires writing data into the 6355 registers. In order to write to the IC, code must set the I/O and the
CE lines high. At this moment all clock updates stop and the RTC goes into the write mode. Input data is latched in LSB first, starting with the year and concluding with the minutes. There is no provision for writing seconds into the RTC, so the total number of bits written is 44.
The 6355 contains a mechanism for detecting conditions that could compromise the clock’s operation, such as low power. In this case, the special value 0xee is written into each digit of the internal registers to inform processing routines that the timer has been compromised.
The NJU6355 requires the installation of an external crystal oscillator. The crystal must have a frequency of 32.768 kHz. The time-keeping accuracy of the RTC is determined by the quartz oscillator. The capacity of the oscillator must match that of the RTC and of the circuit. A standard crystal with a capacitance of 12.5pF works well for applications that do not demand high clock accuracy. For more exacting applications the 6355 can be programmed to check the clock frequency and determine its error. The chip’s frequency-checking mode is described in an NJU6355 Application Note available from New Japan Radio Co., Ltd.
16.3.2 RTC Demonstration Circuit and Program
The circuit shown in Figure 16-11 is a simple application of the 6355 RTC. The circuit uses a NJU6355 in conjunction with a 16F86 PIC and an LCD. The demonstration program, named RTC2LCD, sets up RTC and reads clock data in an endless loop. The hours, minutes, and seconds are displayed at the top line of the LCD as follows:
H:xx M:xx S:xx
where xx represents the two BCD digits read from the clock and converted to ASCII decimal for display. The program initializes the 6355 to some arbitrary values contained in the corresponding #define statements. These values are copied into program variables by a local procedure and then used to initialize the RTC registers. Two procedures relate to RTC operation: one to initialize the clock hardware and the other one to read the current time. In addition, two auxiliary procedures are implemented: one to read clock data and one to write clock data. Since clock data can be in 8- or 4-bit formats each procedure contains a separate entry point to handle the 4-bit option. The procedure to initialize and the one to write clock data are coded as follows:
;============================
;init RTC ;============================
;Procedure to initialize the real time clock chip. If chip
;is not initialized it will not operate and the values
;read will be invalid.
;Since the 6355 operates in BCD format the stored values must
;be converted to packed BCD.
;According to wiring diagram
562 |
Chapter 16 |
||
bsf |
PORTB,IO ; IO high |
||
call |
delay_5 |
||
bsf |
PORTB,CE ; CE high |
||
; Data is stored in RTC as follows: |
|||
; |
year |
8 |
bits (0 to 99) |
; |
month |
8 |
bits (1 to 12) |
; |
day |
8 |
bits (1 to 31) |
; |
dayOfWeek |
4 |
bits (1 to 7) |
; |
hour |
8 |
bits (0 to 23) |
; |
minutes |
8 |
bits (0 to 59) |
; |
====== |
||
; |
Total |
44 bits |
|
;Seconds cannot be written to RTC. RTC seconds register
;is automatically initialized to zero
movf |
year,w |
; Get item from storage |
|
call |
bin2bcd |
; Convert to BCD |
|
movwf |
temp1 |
||
call |
writeRTC |
||
movf |
month,w |
||
call |
bin2bcd |
||
movwf |
temp1 |
||
call |
writeRTC |
||
movf |
day,w |
||
call |
bin2bcd |
||
movwf |
temp1 |
||
call |
writeRTC |
||
movf |
dayOfWeek,w |
; dayOfWeek of week is 4-bits |
|
call |
bin2bcd |
||
movwf |
temp1 |
||
call |
write4RTC |
||
movf |
hour,w |
||
call |
bin2bcd |
||
movwf |
temp1 |
||
call |
writeRTC |
||
movf |
minutes,w |
||
call |
bin2bcd |
||
movwf |
temp1 |
||
call |
writeRTC |
||
; Done |
|||
bcf |
PORTB,CLK |
; Hold CLK line low |
|
call |
delay_5 |
||
bcf |
PORTB,CE ; and the CE line |
||
; to the RTC |
|||
Analog to Digital and Realtime Clocks |
563 |
|
call |
delay_5 |
|
bcf |
PORTB,IO |
; RTC in output mode |
return |
||
;============================
;write 4/8 bits to RTC ;============================
;Procedure to write 4 or 8 bits to the RTC registers
;ON ENTRY:
;temp1 register holds value to be written
;ON EXIT:
;nothing
write4RTC |
||
movlw |
.4 |
; Init for 4 bits |
goto |
allBits |
|
writeRTC: |
||
movlw |
.8 |
; Init for 8 bits |
allBits: |
||
movwf |
counter |
; Store in bit counter |
writeBits: |
||
bcf |
PORTB,CLK |
; Clear the CLK line |
call |
delay_5 |
; Wait |
bsf |
PORTB,DAT |
; Set the data line to RTC |
btfss |
temp1,0 |
; Send LSB |
bcf |
PORTB,DAT |
; Clear data line |
call |
delay_5 |
; Wait for operation to |
complete |
||
bsf |
PORTB,CLK |
; Bring CLK line high to |
validate |
||
rrf |
temp1,f |
; Rotate bits in storage |
decfsz |
counter,1 |
; Decrement bit counter |
goto |
writeBits |
; Continue if not last bit |
return |
The following procedures are used by the RTC2LCD program to read the data in the RTC registers:
;============================
;read RTC data ;============================
;Procedure to read the current time from the RTC and store
;data (in packed BCD format) in local time registers.
;According to wiring diagram
;NJU6355 Interface for read operations:
; DAT |
PORTB,0 |
Input |
; CLK |
PORTB,1 |
Output |
; CE |
PORTB,2 |
Output |
; IO |
PORTB,3 |
Output |
Get_Time |
||
; Clear Port-B |