18.11.2014 Views

The Microcontroller Idea Book - Jan Axelson's Lakeview Research

The Microcontroller Idea Book - Jan Axelson's Lakeview Research

The Microcontroller Idea Book - Jan Axelson's Lakeview Research

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Listing 10-1. Uses BASIC-52’s real-time clock to count seconds, minutes,<br />

and hours.<br />

10 REM set XTAL to match your crystal’s frequency<br />

20 XTAL=12000000<br />

30 REM set and initialize clock<br />

40 GOSUB 200<br />

50 REM increment clock variables once per minute<br />

60 DO<br />

70 ONTIME 60,500<br />

80 WHILE 1=1<br />

90 END<br />

200 PRINT “Please enter the current time:”<br />

210 INPUT “AM (0) or PM (1)? ”,AP<br />

220 INPUT “Hour (1-12)? ”,H<br />

230 INPUT “Minutes (0-59)? ”,M<br />

240 INPUT “Seconds (0-59)? ”,S<br />

250 REM initialize clock to current seconds<br />

260 TIME=S<br />

270 REM start clock<br />

280 CLOCK 1<br />

290 RETURN<br />

500 REM increment and display time once per minute<br />

510 REM reset seconds<br />

520 TIME=0<br />

530 REM increment minutes<br />

540 M=M+1<br />

550 IF M=60 THEN GOSUB 700<br />

560 REM display current time<br />

570 PRINT “the time is :”<br />

580 PRINT H,"hours"<br />

590 PRINT M,"minutes"<br />

600 IF AP=0 THEN PRINT “ AM” ELSE PRINT “ PM”<br />

610 RETI<br />

700 REM once/hour timekeeping<br />

710 REM reset minutes<br />

720 M=0<br />

730 REM increment hours<br />

740 H=H+1<br />

750 REM at 12:00, toggle am/pm<br />

760 IF H=12 THEN AP=A<br />

770 REM at 1:00, reset hours<br />

780 IF H=13 THEN H=1<br />

790 RETURN<br />

Clocks and Calendars<br />

<strong>The</strong> <strong>Microcontroller</strong> <strong>Idea</strong> <strong>Book</strong> 173

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!