Getting Started

(lily) #1

Chapter 8: C Pointers and Arrays


Does anybody know what time it is? A Real Time Clock.


r
le real time clock. This was derived from the more capable

tim alibration makes the
sn’t
real
as an nput that when the count reaches 32768, we will know
o
in
s is no accident since the
ep real time (well, ‘real’ to

ical
as a
cerea thoug they n’t m hy.

loop to wait for the external crystal to

for(int i = 0; i < 10; i++)

erru.

ter 2 nable, TOIE2, bit in the
pt M , to disable the timer output

D

Let’s combine our knowledge of interrupts with what we learned in the messenge
project to make a simp
clock in the Butterfly software.


A one second interrupt


We saw how to use the 32.768kHz watch crystal to calibrate the cpu clock
oscillator in the chapter on ers and interrupts. While that c
oscillator accurate enough for communicating with the PC via the USART, it i
accurate enough to keep time like a watch. We will use Timer/counter2 with
the watch crystal i so
that one-second has passed and can throw an interrupt allowing us to d
something at exact one second intervals. Notice that 32768 is 0x8000
hexadecimal and 1000000000000000 in binary, thi
crystals were designed to allow digital systems to ek
humans anyway). The low speed (kilohertz verus mega or giga hertz) and
precision timing allowed watches more accurate than expensive mechan
chronometers to be manufactured so cheaply that its not unusual to find one
prize in a box of l, h are ilk proof and are a bit too crunc


We start the software by using a delay
stabilize.


_delay_loop_2(30000);


Disable global int pts


cli();


Clear the Timer/Coun Output Interrupt E
Timer/Counter 2 Interru ask Register, TIMSK2
interrupt enable.


cbi(TIMSK2, TOIE2);

Free download pdf