OSDN Git Service

517b2e8efbda5fcf7b499c4c7b6fa21cdfdc6f14
[fast-forth/master.git] / MSP430_FORTH / RC5toLCD.f
1 ; -----------------------------------
2 ; RC5toLCD.4th
3 ; -----------------------------------
4
5 \ TARGET SELECTION
6 \ MSP_EXP430FR5739  MSP_EXP430FR5969    MSP_EXP430FR5994    MSP_EXP430FR6989
7 \ MSP_EXP430FR4133  CHIPSTICK_FR2433
8
9 \ MY_MSP430FR5738_1 MY_MSP430FR5738     MY_MSP430FR5948     MY_MSP430FR5948_1   
10 \ JMJ_BOX
11
12     \
13 \ Copyright (C) <2016>  <J.M. THOORENS>
14 \
15 \ This program is free software: you can redistribute it and/or modify
16 \ it under the terms of the GNU General Public License as published by
17 \ the Free Software Foundation, either version 3 of the License, or
18 \ (at your option) any later version.
19 \
20 \ This program is distributed in the hope that it will be useful,
21 \ but WITHOUT ANY WARRANTY\ without even the implied warranty of
22 \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 \ GNU General Public License for more details.
24 \
25 \ You should have received a copy of the GNU General Public License
26 \ along with this program.  If not, see <http://www.gnu.org/licenses/>.
27
28
29 \ ===========================================================================
30 \ remember: for good downloading to target, all lines must be ended with CR+LF !
31 \ ===========================================================================
32
33
34 \ REGISTERS USAGE
35 \ R4 to R7 must be saved before use and restored after
36 \ scratch registers Y to S are free for use
37 \ under interrupt, IP is free for use
38 \ interrupts reset SR register !
39
40 \ PUSHM order : PSP,TOS, IP,  S,  T,  W,  X,  Y, R7, R6, R5, R4, R3, R2,RSP, PC
41 \ example : PUSHM IP,Y
42 \
43 \ POPM  order :  PC ,RSP, R2, R3, R4, R5, R6, R7,  Y,  X,  W,  T,  S, IP,TOS,PSP
44 \ example : POPM Y,IP
45
46 \ ASSEMBLER conditionnal usage after IF UNTIL WHILE : S< S>= U< U>= 0= 0<> 0>=
47 \ ASSEMBLER conditionnal usage before ?JMP ?GOTO    : S< S>= U< U>= 0= 0<> 0< 
48
49 \ FORTH conditionnal    : 0= 0< = < > U<
50
51 \ display on a LCD 2x20 CHAR the code sent by an IR remote under philips RC5 protocol
52 \ target : any TI MSP-EXP430FRxxxx launchpad (FRAM)
53 \ LPM_MODE = LPM0 because use SMCLK for LCDVo
54
55 \ DEMO : driver for IR remote compatible with the PHILIPS RC5 protocol
56 \ plus : driver for 5V LCD 2x20 characters display with 4 bits data interface
57 \        without usage of an auxiliary 5V to feed the LCD_Vo
58 \        and without potentiometer to adjust the LCD contrast :
59 \        to adjust LCD contrast, just press S1 (-) or S2 (+)
60 \        LCDVo current consumption ~ 500 uA.
61
62 \ ===================================================================================
63 \ notice : adjust TA0EX0,TB0CTL,TB0EX0 and 20_us to the target frequency if <> 8MHz !
64 \ ===================================================================================
65
66
67 \ layout : I/O are defined in the launchpad.pat file (don't work with ChipStick_FR2433)
68
69 \  GND  <-------+---0V0---------->  1 LCD_Vss
70 \  VCC  >------ | --3V6-----+---->  2 LCD_Vdd
71 \               |           |
72 \              ___    470n ---
73 \               ^          ---
74 \              / \ 1N4148   |
75 \              ---          |
76 \          100n |    2k2    |
77 \ TB0.2 >---||--+--^/\/\/v--+---->  3 LCD_Vo (= 0V6 without modulation)
78 \       ------------------------->  4 LCD_RW
79 \       ------------------------->  5 LCD_RW
80 \       ------------------------->  6 LCD_EN
81 \       <------------------------> 11 LCD_DB4
82 \       <------------------------> 12 LCD_DB5
83 \       <------------------------> 13 LCD_DB5
84 \       <------------------------> 14 LCD_DB7
85
86 \       <----- LCD contrast + <---    Sw1   <--- (finger) :-)
87 \       <----- LCD contrast - <---    Sw2   <--- (finger) :-)
88
89 \ rc5   <--- OUT IR_Receiver (1 TSOP32236)
90
91 [DEFINED] {RC5TOLCD} [IF] {RC5TOLCD} [THEN]     \ remove application
92
93 [DEFINED] ASM [IF]      \ security test
94     \
95 MARKER {RC5TOLCD}
96     \
97 [UNDEFINED] MAX [IF]    \ MAX and MIN are defined in {ANS_COMP}
98     \
99 CODE MAX    \    n1 n2 -- n3       signed maximum
100     CMP @PSP,TOS    \ n2-n1
101     S<  ?GOTO FW1   \ n2<n1
102 BW1 ADD #2,PSP
103     MOV @IP+,PC
104 ENDCODE
105     \
106
107 CODE MIN    \    n1 n2 -- n3       signed minimum
108     CMP @PSP,TOS     \ n2-n1
109     S<  ?GOTO BW1    \ n2<n1
110 FW1 MOV @PSP+,TOS
111     MOV @IP+,PC
112 ENDCODE
113
114 [THEN]
115     \
116
117 [UNDEFINED] U.R [IF]        \ defined in {UTILITY}
118 : U.R                       \ u n --           display u unsigned in n width (n >= 2)
119   >R  <# 0 # #S #>  
120   R> OVER - 0 MAX SPACES TYPE
121 ;
122 [THEN]
123     \
124
125 \ CODE 20_US                      \ n --      n * 20 us
126 \ BEGIN                           \ 3 cycles loop + 6~  
127 \ \    MOV     #5,W                \ 3 MCLK = 1 MHz
128 \ \    MOV     #23,W               \ 3 MCLK = 4 MHz
129 \ \    MOV     #51,W               \ 3 MCLK = 8 MHz
130 \     MOV     #104,W              \ 3 MCLK = 16 MHz
131 \ \    MOV     #158,W              \ 3 MCLK = 24 MHz
132 \     BEGIN                       \ 3 cycles loop ==> 3 * W / F us = 100 us - 1 @ 8 MHz
133 \         SUB #1,W                \ 1
134 \     0= UNTIL                    \ 2
135 \     SUB     #1,TOS              \ 1
136 \ 0= UNTIL                        \ 2
137 \     MOV     @PSP+,TOS           \ 2
138 \     MOV     @IP+,PC             \ 4
139 \ ENDCODE
140 \     \
141
142 CODE 20_US                  \ n --      n * 20 us
143 BEGIN                       \ here we presume that TB0IFG = 1...
144     BEGIN
145         BIT #1,&TB0CTL      \ 3
146     0<> UNTIL               \ 2         loop until TB0IFG set
147     BIC #1,&TB0CTL          \ 3         clear TB0IFG
148     SUB #1,TOS              \ 1
149 U< UNTIL                    \ 2 ...so add a dummy loop with U< instead of 0=
150 MOV @PSP+,TOS               \ 2
151 MOV @IP+,PC                 \ 4
152 ENDCODE
153     \
154
155 CODE TOP_LCD                    \ LCD Sample
156 \                               \ if write : %xxxxWWWW --
157 \                               \ if read  : -- %0000RRRR
158     BIS.B #LCD_EN,&LCD_CMD_OUT  \ lcd_en 0-->1
159     BIT.B #LCD_RW,&LCD_CMD_IN   \ lcd_rw test
160 0= IF                           \ write LCD bits pattern
161     AND.B #LCD_DB,TOS           \ 
162     MOV.B TOS,&LCD_DB_OUT       \ send LCD_Data
163     BIC.B #LCD_EN,&LCD_CMD_OUT  \ lcd_en 1-->0 ==> strobe data
164     MOV @PSP+,TOS               \
165     MOV @IP+,PC
166 THEN                            \ read LCD bits pattern
167     SUB #2,PSP
168     MOV TOS,0(PSP)
169     BIC.B #LCD_EN,&LCD_CMD_OUT  \ lcd_en 1-->0 ==> strobe data
170     MOV.B &LCD_DB_IN,TOS        \ get LCD_Data
171     AND.B #LCD_DB,TOS           \
172     MOV @IP+,PC
173 ENDCODE
174     \
175
176 CODE LCD_W                      \ byte --       write byte to LCD 
177     SUB #2,PSP                  \
178     MOV TOS,0(PSP)              \ -- %xxxxLLLL %HHHHLLLL
179     RRUM #4,TOS                 \ -- %xxxxLLLL %xxxxHHHH
180     BIC.B #LCD_RW,&LCD_CMD_OUT  \ lcd_rw=0
181     BIS.B #LCD_DB,&LCD_DB_DIR   \ LCD_Data as output
182 COLON                           \ high level word starts here 
183     TOP_LCD 2 20_US             \ write high nibble first
184     TOP_LCD 2 20_US 
185 ;
186     \
187
188 CODE LCD_WrC                    \ char --         Write Char
189     BIS.B #LCD_RS,&LCD_CMD_OUT  \ lcd_rs=1
190     JMP LCD_W 
191 ENDCODE
192     \
193
194 CODE LCD_WrF                    \ func --         Write Fonction
195     BIC.B #LCD_RS,&LCD_CMD_OUT  \ lcd_rs=0
196     JMP LCD_W 
197 ENDCODE
198     \
199
200 : LCD_Clear 
201     $01 LCD_WrF 100 20_us      \  $01 LCD_WrF 80 20_us ==> bad init !
202 ;
203     \
204
205 : LCD_Home 
206     $02 LCD_WrF 100 20_us 
207 ;
208     \
209
210 : LCD_Entry_set     $04 OR LCD_WrF ;
211     \
212 : LCD_DSP_Ctrl      $08 OR LCD_WrF ;
213     \
214 : LCD_DSP_Shift     $10 OR LCD_WrF ;
215     \
216 : LCD_Fn_Set        $20 OR LCD_WrF ;
217     \
218 : LCD_CGRAM_Set     $40 OR LCD_WrF ;
219     \
220 : LCD_Goto          $80 OR LCD_WrF ;
221     \
222 CODE LCD_R                      \ -- byte       read byte from LCD
223     BIC.B #LCD_DB,&LCD_DB_DIR   \ LCD_Data as intput
224     BIS.B #LCD_RW,&LCD_CMD_OUT  \ lcd_rw=1
225 COLON                           \ starts a FORTH word
226     TOP_LCD 2 20_us             \ -- %0000HHHH
227     TOP_LCD 2 20_us             \ -- %0000HHHH %0000LLLL
228 HI2LO                           \ switch from FORTH to assembler
229     RLAM #4,0(PSP)              \ -- %HHHH0000 %0000LLLL
230     ADD.B @PSP+,TOS             \ -- %HHHHLLLL
231     MOV @RSP+,IP                \ restore IP saved by COLON
232     MOV @IP+,PC                 \
233 ENDCODE
234     \
235
236 CODE LCD_RdS                    \ -- status       Read Status
237     BIC.B #LCD_RS,&LCD_CMD_OUT  \ lcd_rs=0
238     JMP LCD_R
239 ENDCODE
240     \
241
242 CODE LCD_RdC                    \ -- char         Read Char
243     BIS.B #LCD_RS,&LCD_CMD_OUT  \ lcd_rs=1
244     JMP LCD_R
245 ENDCODE
246     \
247
248
249 \ ******************************\
250 ASM WDT_INT                     \ Watchdog interrupt routine, warning : not FORTH executable !
251 \ ******************************\
252 \ XOR.B #LED1,&LED1_OUT           \ to visualise WDT
253 BIT.B #SW2,&SW2_IN              \ test switch S2
254 0= IF                           \ case of switch S2 pressed
255     CMP #19,&TB0CCR2             \ maxi Ton = 19/20 & VDD=3V6 ==> LCD_Vo = -1V4
256     U< IF
257         ADD #1,&TB0CCR2         \ action for switch S2 (P2.5) : 150 mV / increment
258     THEN
259 ELSE
260     BIT.B #SW1,&SW1_IN          \ test switch S1 input
261     0= IF                       \ case of Switch S1 pressed
262         CMP #3,&TB0CCR2         \ mini Ton = 3/20 & VDD=3V6 ==> LCD_Vo = 0V
263         U>= IF                  \
264             SUB #1,&TB0CCR2     \ action for switch S1 (P2.6) : -150 mV / decrement
265         THEN                    \
266     THEN                        \
267 THEN                            \
268 BW1                             \ from quit on truncated RC5 message
269 BW2                             \ from repeated RC5 command
270 BW3                             \ from end of RC5_INT
271 BIC #$78,0(RSP)                 \4  SCG0,OSCOFF,CPUOFF and GIE are OFF in retiSR to force LPM0_LOOP despite pending interrupt
272 RETI                            \5
273 ENDASM
274     \
275
276 \ ******************************\
277 ASM RC5_INT                     \   wake up on Px.RC5 change interrupt
278 \ ******************************\
279 \ IR_RC5 driver                 \ IP,S,T,W,X,Y registers are free for use
280 \ ******************************\
281 \                               \ in :  SR(9)=old Toggle bit memory (ADD on)
282 \                               \       SMclock = 8|16|24 MHz
283 \                               \ use : T,W,X,Y, TA1 timer, TA1R register
284 \                               \ out : X = 0 C6 C5 C4 C3 C2 C1 C0
285 \                               \       SR(9)=new Toggle bit memory (ADD on)
286 \ ******************************\
287 \ RC5_FirstStartBitHalfCycle:   \
288 \ ******************************\                division in TA1CTL (SMCLK/1|SMCLK/1|SMCLK/2|SMCLK/4|SMCLK/8)
289 \ MOV #0,&TA1EX0                \ predivide by 1 in TA1EX0 register ( 125kHz|  1MHz |  2MHZ |  4MHZ |  8MHZ ), reset value
290   MOV #1,&TA1EX0                \ predivide by 2 in TA1EX0 register ( 250kHZ|  2MHz |  4MHZ |  8MHZ | 16MHZ )
291 \ MOV #2,&TA1EX0                \ predivide by 3 in TA1EX0 register ( 375kHz|  3MHz |  6MHZ | 12MHZ | 24MHZ )
292 \ MOV #3,&TA1EX0                \ predivide by 4 in TA1EX0 register ( 500kHZ|  4MHz |  8MHZ | 16MHZ )
293 \ MOV #4,&TA1EX0                \ predivide by 6 in TA1EX0 register ( 625kHz|  5MHz | 10MHZ | 20MHZ )
294 \ MOV #5,&TA1EX0                \ predivide by 6 in TA1EX0 register ( 750kHz|  6MHz | 12MHZ | 24MHZ )
295 \ MOV #6,&TA1EX0                \ predivide by 7 in TA1EX0 register ( 875kHz|  7MHz | 14MHZ | 28MHZ )
296 \ MOV #7,&TA1EX0                \ predivide by 8 in TA1EX0 register (  1MHz |  8MHz | 16MHZ | 32MHZ )
297 MOV #1778,X                     \ RC5_Period * 1us
298 \ MOV #222,X                    \ RC5_Period * 8us (SMCLK/1 and first column above)
299 MOV #14,W                       \ count of loop
300 BEGIN                           \
301 \ ******************************\
302 \ RC5_HalfCycle                 \ <--- loop back ---+ with readjusted RC5_Period
303 \ ******************************\                   |
304 \   MOV #%1000100100,&TA1CTL    \ (re)start timer_A | SMCLK/1 time interval,free running,clear TA1_IFG and TA1R
305 \   MOV #%1002100100,&TA1CTL    \ (re)start timer_A | SMCLK/2 time interval,free running,clear TA1_IFG and TA1R
306 \   MOV #%1010100100,&TA1CTL    \ (re)start timer_A | SMCLK/4 time interval,free running,clear TA1_IFG and TA1R
307     MOV #%1011100100,&TA1CTL    \ (re)start timer_A | SMCLK/8 time interval,free running,clear TA1_IFG and TA1R
308 \ RC5_Compute_3/4_Period:       \                   |
309     RRUM    #1,X                \ X=1/2 cycle       |
310     MOV     X,Y                 \                   ^
311     RRUM    #1,Y                \ Y=1/4
312     ADD     X,Y                 \ Y=3/4 cycle
313     BEGIN   CMP Y,&TA1R         \3 wait 1/2 + 3/4 cycle = n+1/4 cycles 
314     U>= UNTIL                   \2
315 \ ******************************\
316 \ RC5_SampleOnFirstQuarter      \ at n+1/4 cycles, we sample RC5_input, ST2/C6 bit first
317 \ ******************************\
318     BIT.B   #RC5,&IR_IN         \ C_flag = IR bit
319     ADDC    T,T                 \ C_flag <-- T(15):T(0) <-- C_flag
320     MOV.B   &IR_IN,&IR_IES      \ preset Px_IES.y state for next IFG
321     BIC.B   #RC5,&IR_IFG        \ clear Px_IFG.y after 4/4 cycle pin change
322     SUB     #1,W                \ decrement count loop
323 \                               \  count = 13 ==> T = x  x  x  x  x  x  x  x |x  x  x  x  x  x  x /C6
324 \                               \  count = 0  ==> T = x  x /C6 Tg A4 A3 A2 A1|A0 C5 C4 C3 C2 C1 C0  1 
325 0<> WHILE                       \ ----> out of loop ----+
326     ADD X,Y                     \                       |   Y = n+3/4 cycles = time out because n+1/2 cycles edge is always present
327     BEGIN                       \                       |
328         MOV &TA1R,X             \3                      |   X grows from n+1/4 up to n+3/4 cycles
329         CMP Y,X                 \1                      |   cycle time out of bound ?
330         U>= IF                  \2                  ^   |   yes:
331             BIC #$30,&TA1CTL    \                   |   |      stop timer
332             GOTO BW1            \                   |   |      quit on truncated RC5 message
333         THEN                    \                   |   |
334         BIT.B #RC5,&IR_IFG      \3                  |   |   n+1/2 cycles edge is always present
335     0<> UNTIL                   \2                  |   |
336 REPEAT                          \ ----> loop back --+   |   with X = new RC5_period value
337 \ ******************************\                       |
338 \ RC5_SampleEndOf:              \ <---------------------+
339 \ ******************************\
340 BIC #$30,&TA1CTL                \   stop timer
341 \ ******************************\
342 \ RC5_ComputeNewRC5word         \
343 \ ******************************\
344 RLAM    #1,T                    \ T =  x /C6 Tg A4 A3 A2 A1 A0|C5 C4 C3 C2 C1 C0  1  0
345 MOV.B   T,X                     \ X = C5 C4 C3 C2 C1 C0  1  0
346 RRUM    #2,X                    \ X =  0  0 C5 C4 C3 C2 C1 C0
347 \ ******************************\
348 \ RC5_ComputeC6bit              \
349 \ ******************************\
350 BIT     #BIT14,T                \ test /C6 bit in T
351 0= IF   BIS #BIT6,X             \ set C6 bit in X
352 THEN                            \ X =  0  C6 C5 C4 C3 C2 C1 C0
353 \ ******************************\
354 \ RC5_CommandByteIsDone         \ -- BASE RC5_code
355 \ ******************************\
356 \ Only New_RC5_Command ADD_ON   \ use SR(9) bit as toggle bit
357 \ ******************************\
358 RRUM    #3,T                    \ new toggle bit = T(13) ==> T(10)
359 XOR     @RSP,T                  \ (new XOR old) Toggle bits
360 BIT     #UF10,T                 \ repeated RC5_command ?
361 0= ?GOTO BW2                    \ yes, RETI without UF10 change and without action !
362 XOR #UF10,0(RSP)                \ 5 toggle bit memory
363 \ ******************************\
364 \ Display IR_RC5 code           \ X = RC5 code
365 \ ******************************\
366 SUB #4,PSP                      \
367 MOV &BASE,2(PSP)                \ save current base
368 MOV #$10,&BASE                  \ set hex base
369 MOV TOS,0(PSP)                  \ save TOS
370 MOV X,TOS                       \
371 LO2HI                           \ switch from assembler to FORTH
372     ['] LCD_CLEAR IS CR         \ redirects CR
373     ['] LCD_WrC  IS EMIT        \ redirects EMIT
374     CR ." $" 2 U.R              \ print IR_RC5 code
375     ['] (CR) IS CR              \ restore CR
376     ['] (EMIT) IS EMIT          \ restore EMIT
377 HI2LO                           \ switch from FORTH to assembler
378 MOV TOS,&BASE                   \ restore current BASE
379 MOV @PSP+,TOS                   \
380 \ ******************************\
381 GOTO BW3
382 \ ******************************\
383 ENDASM
384     \ 
385
386 \ ------------------------------\
387 ASM BACKGROUND                  \ 
388 \ ------------------------------\
389 \ ...                           \ insert here your background task
390 \ ...                           \
391 \ ...                           \
392 MOV #(SLEEP),PC                 \ Must be the last statement of BACKGROUND
393 ENDASM                          \
394 \ ------------------------------\
395     \
396
397 CODE START                      \
398 \ ------------------------------\
399 \ TB0CTL =  %0000 0010 1001 0100\$3C0
400 \               - -             \CNTL Counter lentgh \ 00 = 16 bits
401 \                   --          \TBSSEL TimerB clock select \ 10 = SMCLK
402 \                      --       \ID input divider \ 10 = /4
403 \                        --     \MC Mode Control \ 01 = up to TB0CCR0
404 \                            -  \TBCLR TimerB Clear
405 \                             - \TBIE
406 \                              -\TBIFG
407 \ -------------------------------\
408 \ TB0CCTLx = %0000 0000 0110 0000\$3C{2,4,6,8,A,C,E}
409 \             --                 \CM Capture Mode
410 \               --               \CCIS
411 \                  -             \SCS
412 \                   --           \CLLD
413 \                     -          \CAP
414 \                       ---      \OUTMOD \ 011 = set/reset
415 \                          -     \CCIE
416 \                            -   \CCI
417 \                             -  \OUT
418 \                              - \COV
419 \                               -\CCIFG
420 \ -------------------------------\
421 \ TB0CCRx                        \
422 \ -------------------------------\
423 \ TB0EX0                         \ 
424 \ ------------------------------\
425 \ set TB0 to make 50kHz PWM     \ for LCD_Vo, works without interrupt
426 \ ------------------------------\
427 \    MOV #%1000010100,&TB0CTL   \ SMCLK/1, up mode, clear timer, no int
428 \    MOV #0,&TB0EX0             \ predivide by 1 in TB0EX0 register (1 MHZ)
429 \ ------------------------------\
430 \    MOV #%1001010100,&TB0CTL   \ SMCLK/2, up mode, clear timer, no int
431 \    MOV #1,&TB0EX0             \ predivide by 2 in TB0EX0 register (2 MHZ)
432 \ ------------------------------\
433 \    MOV #%1010010100,&TB0CTL   \ SMCLK/4, up mode, clear timer, no int
434 \    MOV #1,&TB0EX0             \ predivide by 2 in TB0EX0 register (4 MHZ)
435 \ ------------------------------\
436 \    MOV #%1011010100,&TB0CTL    \ SMCLK/8, up mode, clear timer, no int
437 \    MOV #0,&TB0EX0              \ predivide by 1 in TB0EX0 register (8 MHZ)
438 \ ------------------------------\
439     MOV #%1011010100,&TB0CTL   \ SMCLK/8, up mode, clear timer, no int
440     MOV #1,&TB0EX0             \ predivide by 2 in TB0EX0 register (16 MHZ)
441 \ ------------------------------\
442 \    MOV #%1011010100,&TB0CTL   \ SMCLK/8, up mode, clear timer, no int
443 \    MOV #2,&TB0EX0             \ predivide by 3 in TB0EX0 register (24 MHZ)
444 \ ------------------------------\
445     MOV #19,&TB0CCR0            \ 19+1=20*1us=20us
446 \ ------------------------------\
447 \ set TB0.2 to generate PWM for LCD_Vo
448 \ ------------------------------\
449     MOV #%01100000,&TB0CCTL2    \ output mode = set/reset \ clear CCIFG
450     MOV #10,&TB0CCR2            \ contrast adjust : 10/20 ==> LCD_Vo = -0V6|+3V6 (Vcc=3V6)
451 \    MOV #12,&TB0CCR2            \ contrast adjust : 12/20 ==> LCD_Vo = -1V4|+3V3 (Vcc=3V3)
452 \ ------------------------------\
453     BIS.B #LCDVo,&LCDVo_DIR     \
454     BIS.B #LCDVo,&LCDVo_SEL     \ SEL.2 TB0.2
455 \ ------------------------------\
456     BIS.B #LCD_CMD,&LCD_CMD_DIR \ lcd_cmd as outputs
457     BIC.B #LCD_CMD,&LCD_CMD_REN \ lcd_cmd pullup/down disable
458 \ ------------------------------\
459     BIS.B #LCD_DB,&LCD_DB_DIR   \ as output, wired to DB(4-7) LCD_Data
460     BIC.B #LCD_DB,&LCD_DB_REN   \ LCD_Data pullup/down disable
461 \ ******************************\
462 \ init RC5_Int                  \
463 \ ******************************\
464     BIS.B #RC5,&IR_IE           \ enable RC5_Int
465     BIC.B #RC5,&IR_IFG          \ reset RC5_Int flag
466     MOV #RC5_INT,&IR_Vec        \ init interrupt vector
467 \ ******************************\
468 \ init WatchDog TA0             \ eUSCI_A0 (FORTH terminal) has higher priority than TA0
469 \ ******************************\
470 \              %01 0001 0100    \ TAxCTL
471 \               --              \ TASSEL    CLK = ACLK = LFXT = 32768 Hz
472 \                  --           \ ID        divided by 1
473 \                    --         \ MC        MODE = up to TAxCCRn
474 \                        -      \ TACLR     clear timer count
475 \                         -     \ TAIE
476 \                          -    \ TAIFG
477 \ ------------------------------\
478     MOV #%0100010100,&TA0CTL    \ start TA0, ACLK, up mode, disable int, 
479 \ ------------------------------\
480 \                        000    \ TAxEX0
481 \                        ---    \ TAIDEX    pre divisor
482 \ ------------------------------\
483 \          %0000 0000 0000 0101 \ TAxCCR0
484     MOV ##1638,&TA0CCR0         \ init WDT for LFXT: 32768/20=1638 ==> 50ms
485 \    MOV ##400,&TA0CCR0          \ init WDT for VLO: 8000/20=400 ==> 50ms
486 \ ------------------------------\
487 \          %0000 0000 0001 0000 \ TAxCCTL0
488 \                   -           \ CAP capture/compare mode = compare
489 \                        -      \ CCIEn
490 \                             - \ CCIFGn
491     MOV #%10000,&TA0CCTL0       \ enable compare interrupt, clear CCIFG0
492 \ ------------------------------\
493     MOV #WDT_INT,&TA0_0_Vec     \ for only CCIFG0 int, this interrupt clears automatically CCIFG0
494 \ ------------------------------\
495 \ define LPM mode for ACCEPT    \
496 \ ------------------------------\
497 \    MOV #LPM4,&LPM_MODE         \ with MSP430FR59xx
498 \    MOV #LPM2,&LPM_MODE         \ with MSP430FR57xx, terminal input don't work for LPMx > 2
499 \                               \ with MSP430FR2xxx, terminal input don't work for LPMx > 0 ; LPM0 is the default value
500
501 \ ------------------------------\
502 \ redirects to background task  \
503 \ ------------------------------\
504     MOV #SLEEP,X                \
505     MOV #BACKGROUND,2(X)        \
506 \ ------------------------------\
507
508 LO2HI                           \ no need to push IP because (WARM) resets the Return Stack ! 
509
510 \ ------------------------------\
511 \ Init LCD 2x20                 \
512 \ ------------------------------\
513     $03E8 20_US                 \ 1-  wait 20 ms
514     $03 TOP_LCD                 \ 2- send DB5=DB4=1
515     $CD 20_US                   \ 3- wait 4,1 ms
516     $03 TOP_LCD                 \ 4- send again DB5=DB4=1
517     $5 20_US                    \ 5- wait 0,1 ms
518     $03 TOP_LCD                 \ 6- send again again DB5=DB4=1
519     $2 20_US                    \    wait 40 us = LCD cycle
520     $02 TOP_LCD                 \ 7- send DB5=1 DB4=0
521     $2 20_US                    \    wait 40 us = LCD cycle
522     $28 LCD_WRF                 \ 8- %001DNFxx "FonctionSet" D=8/4 DataBus width, Number of lines=2/1, Font bold/normal
523     $08 LCD_WRF                 \ 9- %1DCB   "DisplayControl" : Display off, Cursor off, Blink off. 
524     LCD_Clear                   \ 10- "LCD_Clear"
525     $06 LCD_WRF                 \ 11- %01xx   "LCD_EntrySet" : address and cursor shift after writing in RAM
526     $0C LCD_WRF                 \ 12- %1DCB "DisplayControl" : Display on, Cursor off, Blink off. 
527     LCD_Clear                   \ 10- "LCD_Clear"
528     ['] LCD_HOME IS CR          \ ' CR redirected to LCD_HOME
529     ['] LCD_WRC  IS EMIT        \ ' EMIT redirected to LCD_WrC
530     CR ." I love you"   
531     ['] (CR) IS CR              \ ' (CR) is CR
532     ['] (EMIT) IS EMIT          \ ' (EMIT) is EMIT
533 \    NOECHO                      \ uncomment to run this app without terminal connexion
534     CR
535     ."    RC5toLCD is running. Type STOP to quit"
536     LIT RECURSE IS WARM         \ insert this START routine between WARM and (WARM)...
537     (WARM)                      \ ...and continue with (WARM), must be the START last statement.
538 ;
539     \
540
541 CODE STOP                   \ stops multitasking, must to be used before downloading app
542     MOV #SLEEP,X            \
543     MOV #(SLEEP),2(X)       \ restore the default background
544 COLON
545     ['] (WARM) IS WARM      \ remove START app from FORTH init process
546     ECHO COLD               \ reset CPU, interrupt vectors, and start FORTH
547 ;
548     \
549
550 ECHO
551             ; downloading RC5toLCD.4th is done
552 RST_HERE    ; this app is protected against <reset>
553     \
554 [THEN]      \ ASM
555     \
556 START