PDA

View Full Version : 68HC912: load file and LCD and button driver


qdai
04-11-02, 09:40 PM
I am beginner and just got an 68HC912B32, loaded with FORTH, with 5 button, and 2 row LCD.
May I program in C? How to load file to the board? What to load?
Also, any sample program to drive the mini buttons and LCD, in C or assembly, if possible?
Thank you very much.

nmitech
04-12-02, 11:59 AM
Only have the FORTH sample program at this time.
I Hope this will give you something to start with.


( Software for mini PoBox Rob Chapman June 12, 2000 )

COLD
( ****************************************************************
( I/O PORTS
(
( I/O Ports Assign Function
( ~~~ ~~~~~ ~~~~~~~~~~~~~~~
( O PP4 LCD, DB4
( O PP5 LCD, DB5
( O PP6 LCD, DB6
( O PP7 LCD, DB7
( O PS2 LCD, RS (register select. LOW: command, HIGH: data)
( O PS3 LCD, R/W (READ: signal high, WRITE: signal low)
( O PS4 LCD, E (chip enable, active high)
( O PS5 RTC /WR (write signal for RTC, active low)
( O PS6 RTC /RD (read signal for RTC, active low)
( O PS7 RTC /CS0 ( chip select to RTC, active low)
( I/O PT0 RTC D0 ( data signals)
( I/O PT1 RTC D1
( I/O PT2 RTC D2
( I/O PT3 RTC D3
( O PT4 RTC A0 ( address signals )
( O PT5 RTC A1
( O PT6 RTC A2
( O PT7 RTC A3
( I PDLC0 SERIAL BOOT LOADER INPUT
( O PDLC1 BUZZER
( I PDLC2 BUTTON1
( I PDLC3 BUTTON2
( I PDLC4 BUTTON3
( I PDLC5 BUTTON4
( I PDLC6 BUTTON5
( ****************************************************************

HEX
( ==== Find empty space in Flash ROM ==== )
: FLBLANK ( -- a ) F800 F000 8000
DO FF I 10 OVER + SWAP DO I C@ AND LOOP FF = IF DROP I LEAVE THEN
100 +LOOP DUP F800 = IF CR ." Flash is full." CR ELSE DUP U. THEN ;

FLBLANK FDP ! FORGET FLBLANK

( ==== ROM it all with auto-rom words
: ; [COMPILE] ; FLWORD ; IMMEDIATE FLWORD
: CONSTANT CONSTANT FLWORD ;
: CREATE HERE CONSTANT ;
: VARIABLE CREATE 2 ALLOT ;

( ==== Interrupt control ==== )
CODE-SUB +INT
10EF , ( cli ; enable interrupts
3D C, ( rts
END-CODE

CODE-SUB -INT
1410 , ( sei ; disable interrupts
3D C, ( rts
END-CODE

: COLD -INT COLD ;

( ==== Real time interrupt interface tools ==== )
( ==== Registers ==== )
14 CONSTANT RTICTL ( real time interrupt control register
15 CONSTANT RTIFLG ( real time interrupt flag register
1E CONSTANT INTCR ( IRQ control register

( ==== Real time interrupt ==== )
VARIABLE ticks ( incremented by RTI interrupt )

CODE-SUB SIR-RTI ( real time interrupt: increment a ticker
4D C, RTIFLG C, 7F C, ( BCLR RTIFLG,$7F ; clear RTIF
FE C, ticks , ( LDX ticks ; get timer contents
08 C, ( INX ; increment it
7E C, ticks , ( STX ticks ; store it back
0B C, ( RTI ; return from interrupt
END-CODE

( The interrupt vector is stored in EEPROM so it can be compiled
( and changed many times. The location in Flash can only be changed
( by clearing all of the flash. So it is always the same.
FFD F7F0 FL! ( interrupt vector is in EEPROM
06 FFD EEC! ' SIR-RTI @ FFE EE! ( jmp to interrupt

DECIMAL
( ==== Time supplements ==== )
VARIABLE start-time

: MSEC ( n -- n' ) 4 + 1000 UM* 4096 UM/MOD SWAP DROP ;
: SEC ( n -- n' ) 1000 * MSEC ;
: TIME ( -- n ) ticks @ ;
: WAIT ( n -- ) TIME >R BEGIN DUP TIME R@ - U< ?TERMINAL OR UNTIL
R> 2DROP ;
: START ( -- ) TIME start-time ! ;
: FINIS ( -- ) TIME start-time @ - 0 1 SEC UM/MOD ?DUP
IF 0 U.R ." ." 4096 UM* 1000 UM/MOD
4 - 0 MAX 0 <# # # # #> TYPE ." sec"
ELSE 4096 UM* 1000 UM/MOD 4 - U. ." msec" THEN DROP ;

( START and FINIS can be used to time things. Try: START 10 SEC WAIT FINIS )

HEX
( ==== initialization ==== )
: INIT-RTI ( -- ) -INT ( disable interrupts )
INTCR C@ BF AND INTCR C! ( disable IRQ interrupts )
83 RTICTL C! ( enable real time interrupt at 4ms )
+INT ; ( enable interrupts )

( ==== Ports DLC, P, T and S interface ==== )
AE CONSTANT PORTT
AF CONSTANT DDRT ( data direction register for port T )
D6 CONSTANT PORTS
D7 CONSTANT DDRS
56 CONSTANT PORTP
57 CONSTANT DDRP
FD CONSTANT DLCSCR
FE CONSTANT PORTDLC
FF CONSTANT DDRDLC

: INIT-PORTS ( -- )
70 PORTS C! ( E, /WR and /RD set to high )
FC DDRS C! ( outputs for /CSO /RD /WR E R/W RS )
F0 DDRP C! ( outputs for DB7-4 )
F0 DDRT C! ( outputs for A3-0 )
2 DLCSCR C! ( active pullups on the inputs )
0 PORTDLC C! ( any outputs set to zero )
2 DDRDLC C! ( output to drive beeper ) ;

( ==== Tools ==== )
: <NIB ( d -- d' ) 2* 2* 2* 2* ;
: NIB> ( d -- d' ) 2/ 2/ 2/ 2/ ;

( ==== Buzzzer ==== )
: BEEPING? ( -- f ) PORTDLC C@ DDRDLC C@ AND 2 AND ;
: BEEP-ON ( -- ) 2 PORTDLC C! ;
: BEEP-OFF ( -- ) 0 PORTDLC C! ;
: WARBLE ( half-period \ cycles -- )
0 DO BEEPING? IF BEEP-OFF ELSE BEEP-ON THEN DUP WAIT LOOP DROP ;
: WEEBLE ( on-period \ off-period \ cycles -- )
0 DO BEEPING? IF BEEP-OFF DUP ELSE BEEP-ON OVER THEN WAIT LOOP
2DROP ;

: BREEP ( -- ) 1 MSEC 20 MSEC 10 WEEBLE ;
: RINGS ( n -- ) 0 DO BREEP 100 MSEC WAIT BREEP 500 MSEC WAIT LOOP ;

( ==== Buttons ==== )
VARIABLE minimum-interval 50 MSEC minimum-interval !
VARIABLE last-sample
VARIABLE last-time
4 CONSTANT BUTTON1
8 CONSTANT BUTTON2
10 CONSTANT BUTTON3
20 CONSTANT BUTTON4
40 CONSTANT BUTTON5

: PRESSED? ( -- f )
PORTDLC C@ 7D XOR 2/ 2/ ; ( bits on for buttons pressed )

: BUTTON ( -- n ) PRESSED?
6 0 DO ?DUP IF 2/ ELSE I LEAVE THEN LOOP ; ( return which one )

: VALID? ( -- f ) last-sample @ PRESSED? =
IF minimum-interval @ TIME last-time @ - U<
ELSE 0 PRESSED? last-sample ! TIME last-time ! THEN ;

( ==== LCD interface ==== )
4 CONSTANT lcddata ( data register of LCD module )
0 CONSTANT lcdcmd ( command register of LCD module )

: LCD@ ( a -- d )
E8 OR DUP PORTS C! ( rtc off; E off; R/W read; RS = a )
00 DDRP C! ( data in )
DUP 10 OR PORTS C! ( enable chip )
PORTP C@ F0 AND ( read data bus )
SWAP PORTS C! ; ( disable chip )

: LCD! ( d \ a -- )
E0 OR DUP PORTS C! ( rtc off; E off; R/W write; RS = a )
F0 DDRP C! ( data out )
SWAP F0 AND PORTP C! ( output data )
DUP 10 OR PORTS C! ( enable chip )
PORTS C! ; ( disable chip )

: CMMD> ( -- d ) lcdcmd LCD@ lcdcmd LCD@ NIB> OR ;
: DATA> ( -- d ) lcddata LCD@ lcddata LCD@ NIB> OR ;
: WNB ( -- ) BEGIN CMMD> 80 AND 0= ?TERMINAL OR UNTIL ;
: >CMMD ( d -- ) DUP lcdcmd LCD! WNB <NIB lcdcmd LCD! WNB ;
: >LCD ( d -- ) DUP lcddata LCD! WNB <NIB lcddata LCD! WNB ;

: INIT-LCD ( -- ) INIT-PORTS
33 >CMMD ( reset sequence for
32 >CMMD ( four bit interface
28 >CMMD ( 4 bit data length; 2 lines; 5x7 dots
6 >CMMD ( entry mode set: increment; no shift
F >CMMD ( display on; cursor on; blink on
1 >CMMD ( display clear
80 >CMMD ; ( cursor in top left position

: LCD-TYPE ( a \ n -- ) 0 DO COUNT >LCD LOOP DROP ;

: .LCD ( n -- ) DUP ABS 0 <# #S SIGN #> LCD-TYPE ;

: L1 ( -- ) 80 >CMMD ; ( line 1 )
: L2 ( -- ) C0 >CMMD ; ( line 1 )

( ==== Real time clock ==== )
: RTC@ ( a -- d )
E0 PORTS C! ( cso\ off; rd\ off; wr\ off )
F0 DDRT C! ( address out; data in )
<NIB PORTT C! ( write out address )
60 PORTS C! ( enable chip select: cso\ on )
20 PORTS C! ( enable chip read: rd\ on )
PORTT C@ F AND ( read data bus )
60 PORTS C! ( disable chip read: rd\ off )
E0 PORTS C! ; ( disable chip select: cso\ off )

: RTC! ( d \ a -- )
E0 PORTS C! ( cso\ off; rd\ off; wr\ off )
FF DDRT C! ( address out; data out )
<NIB OR PORTT C! ( write out address and data )
60 PORTS C! ( enable chip select: cso\ on )
40 PORTS C! ( enable chip write: wr\ on )
60 PORTS C! ( disable chip write: wr\ off )
E0 PORTS C! ; ( disable chip select: cso\ off )

: INIT-RTC ( -- ) INIT-LCD
1 F RTC! ( reset chip
5 F RTC! ( set as 24 hour time
4 F RTC! ; ( turn off reset

: RUN-CLOCK ( -- ) 0 F RTC! ;
: STOP-CLOCK ( -- ) 2 F RTC! ;

: .nn ( a -- ) DUP RTC@ .LCD 1 - RTC@ .LCD ;

: ` ( -- n ) BL WORD 1 + C@ [COMPILE] LITERAL [ IMMEDIATE ] ;

: DATE ( -- )
L1 9 .nn ` / >LCD 7 .nn ` / >LCD B .nn ( YY/MM/DD )
BL >LCD BL >LCD C RTC@ .LCD ( day of week )
L2 5 .nn ` : >LCD 3 .nn ` : >LCD 1 .nn ; ( HH:MM:SS )

: SET-DATE ( dd \ MM \ DD \ YY \ HH \ mm \ SS \ -- ) INIT-RTC STOP-CLOCK
3 0 DO A /MOD I 2* 1 + RTC! I 2* RTC! LOOP
A /MOD B RTC! A RTC!
5 3 DO A /MOD I 2* 1 + RTC! I 2* RTC! LOOP C RTC! RUN-CLOCK DATE ;

DECIMAL
( ==== Sample main control loop ==== )
( MAIN displays the time and date on the LCD and if a button is pressed,
( then 1 to 5 beeps are emitted depending on which button is pressed.

: BUTTONS ( -- ) PRESSED?
IF VALID? IF 100 MSEC DUP BUTTON 2* WEEBLE THEN THEN ;

: MAIN INIT-RTI INIT-PORTS INIT-LCD INIT-RTC
BEGIN DATE BUTTONS ?TERMINAL UNTIL KEY DROP ;