Файл: 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.


Analog to Digital and Realtime Clocks

559

Vcc

I/O

1

8

XT

2

7

DATA

_XT

NJU6355

CLK

3

6

GND

4

5

CE

NJU6355 PINOUT

I/O -

Input/Output select

XT -

Quartz crystal input (f=32.768kHz)

_XT -

Quartz crystal output

GND -

Ground

CE -

Input enable

CLK -

Clock input

DATA -

Serial timer input/output

Vcc -

+5V power

Figure 16-9 NJU6355 Pin Diagram

Timer data is read when the I/O line is low and the CE line is high. Output from the 6355 is LSB first. A total of 52 significant bits are read in bottom-up order for data as shown in Figure 16-10. That is, the first bit received is the least-significant bit of the year, then the month, then the day, and so forth. All date items are eight bits, except the day of week which is four bits. Non-significant bits in each field are reported as zero; this means that the value for the 10th month (October) is encoded as binary digits 00001010. Reporting unused digits as zero simplifies the conversion into BCD and ASCII.

MSB

LSB

RANGE:

seconds

S6

S5

S4

S3

S2

S1

S0

0

to 59

0

to 59

minutes

M6

M5

M4

M3

M2

M1

M0

hours

0

to 23

H5

H4

H3

H2

H1

H0

1

to 7

day of week

W2

W1

W0

day

1

to 31

D5

D4

D3

D2

D1

D0

month

1

to 12

M4

M3

M2

M1

M0

year

0

to 99

Y7

Y6

Y5

Y4

Y3

Y2

Y1

Y0

Figure 16-10 NJU6355 Timer Data Format


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

Analog to Digital and Realtime Clocks

561

Osc 4Mhz

+5V

R/W

RESET

R=10K

32.768 kHz Crystal

RS

1

18

E

LCD

RA2

RA1

2

17

2 rows x 16

RA3

RA0

3

16

RA4/TOCKI

OSC1

4

15

MCLR

OSC2

14

16F84

5

14

+5V

Vss

Vdd

6

13

RB0/INT

RB7

7

12

RB1

RB6

8

11

RB2

RB5

9

10

RB3

RB4

RS

+5v

E

1

8

IO NJU6355ED+5v

2

7

X1

DATA

R/W

3

6

X2

CLK

4

5

+5V

GND

CE

1

HD44780

Figure 16-11 Real-time Clock Demonstration Circuit

; NJU6355 Interface for setting time:

; DAT

PORTB,0

Output

; CLK

PORTB,1

Output

; CE

PORTB,2

Output

; IO

PORTB,3

Output

setRTC:

Bank1

movlw

b’00000000’

; All lines are output

movlw

TRISB

Bank0

;Writing to the 6355 requires that the CLK bit be held

;low while the IO and CE lines are high

bcf

PORTB,CLK

; CLK low

call delay_5


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