Advertisement
Guest User

Untitled

a guest
Oct 18th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;*****************************************************************
  2. ;* This stationery serves as the framework for a                 *
  3. ;* user application (single file, absolute assembly application) *
  4. ;* For a more comprehensive program that                         *
  5. ;* demonstrates the more advanced functionality of this          *
  6. ;* processor, please see the demonstration applications          *
  7. ;* located in the examples subdirectory of the                   *
  8. ;* Freescale CodeWarrior for the HC12 Program directory          *
  9. ;*****************************************************************
  10.  
  11. ; export symbols
  12.             XDEF Entry, _Startup            ; export 'Entry' symbol
  13.             ABSENTRY Entry        ; for absolute assembly: mark this as application entry point
  14.  
  15.  
  16.  
  17. ; Include derivative-specific definitions
  18.         INCLUDE 'derivative.inc'
  19.  
  20. ROMStart    EQU  $4000  ; absolute address to place my code/constant data
  21.  
  22. ; variable/data section
  23.  
  24.             ORG RAMStart
  25.  ; Insert here your data definition.
  26.  
  27. ;*****************************************************************
  28. ;* Displaying battery voltage and bumper states (s19c32)         *
  29. ;*****************************************************************
  30. ; Definitions
  31. LCD_DAT   EQU PORTB   ;LCD data port, bits - PB7,...,PB0
  32. LCD_CNTR  EQU PTJ     ;LCD control port, bits - PE7(RS),PE4(E)
  33. LCD_E     EQU $80     ;LCD E-signal pin
  34. LCD_RS    EQU $40     ;LCD RS-signal pin
  35.  
  36. ; Variable/data section
  37.  
  38.           ORG $3850
  39.          
  40. BCD_BUFFER  EQU *   ;The following registers are the BCD buffer area          
  41. TEN_THOUS   RMB 1   ;10,000 digit
  42. THOUSANDS   RMB 1   ;1,000 digit
  43. HUNDREDS    RMB 1   ;100 digit
  44. TENS        RMB 1   ;10 digit
  45. UNITS       RMB 1   ;1 digit
  46. BCD_SPARE  0 RMB 10  ;Extra space for decimal point and string terminator
  47. NO_BLANK    RMB 1   ;Used in ’leading zero’ blanking by BCD2ASC
  48.  
  49. ; Code section
  50.           ORG $4000
  51. Entry:
  52. _Startup:
  53.           LDS #$4000    ;initialize the stack pointer
  54.           JSR initAD    ;initialize ATD converter
  55.           JSR initLCD   ;initialize LCD
  56.           JSR clrLCD    ;clear LCD & home cursor
  57.           LDX #msg1     ;display msg1
  58.           JSR putsLCD   ;"
  59.           LDAA #$C0     ;move LCD cursor to the 2nd row
  60.           JSR cmd2LCD
  61.           LDX #msg2     ;display msg2
  62.           JSR putsLCD   ;"
  63.          
  64. lbl       MOVB #$90,ATDCTL5     ;r.just., unsign., sing.conv., mult., ch0, start conv.
  65.           BRCLR ATDSTAT0,$80,*  ;wait until the conversion sequence is complete
  66.          
  67.           LDAA ATDDR4          ;load the ch4 result into AccA                                 CHANGE THIS
  68.           LDAB #$27             ;AccB = 39
  69.           MUL                   ;AccD = 1st result x 39
  70.           ADDD #$258            ;AccD = 1st result x 39 + 600
  71.          
  72.           JSR int2BCD
  73.           JSR BCD2ASC
  74.          
  75.           LDAA $8D             ;move LCD cursor to the 1st row, end of msg1   ADDED
  76.           JSR cmd2LCD           ;"
  77.          
  78.           LDAA TEN_THOUS        ;output the TEN_THOUS ASCII character
  79.           JSR putcLCD           ;"
  80.          
  81.           LDAA THOUSANDS        ;output the THOUSDANDS ASCII character       ADDED
  82.           JSR putcLCD
  83.          
  84.           LDAA #$2C              ;output the COMMA (",") ASCII character      ADDED
  85.           JSR putcLCD           ;"           ;"
  86.          
  87.           LDAA HUNDREDS         ;output the hundreds ASCII character        ADDED
  88.           JSR putcLCD           ;"
  89.          
  90.           LDAA TENS             ;output the TEns ASCII character             ADDED
  91.           JSR putcLCD           ;"
  92.          
  93.           LDAA UNITS            ;output the Ones ASCII character             ADDED
  94.           JSR putcLCD           ;"
  95.          
  96.  
  97.           LDAA #$CA               ;move LCD cursor to the 2nd row, end of msg2 ADDED
  98.           JSR cmd2LCD             ;"
  99.          
  100.           BRCLR PORTAD0,#$80,bowON         ;ORIGINALLY   BRCLR PORTAD0,...,bowON                    CHANGE THIS
  101.           LDAA #$31                      ;output ’1’ if bow sw OFF
  102.           BRA bowOFF
  103.          
  104. bowON     LDAA #$30             ;output ’0’ if bow sw ON
  105. bowOFF    JSR putcLCD
  106.  
  107.           LDAA #$20             ;           ADDED
  108.           JSR putcLCD           ;" output a space character in ASCII         ADDED
  109.  
  110.           BRCLR PORTAD0,#$80,sternON       ; ORIGINALLY   BRCLR PORTAD0,...,sternON                    CHANGE THIS
  111.           LDAA #$31                       ;output ’1’ if stern sw OFF
  112.           BRA sternOFF
  113.          
  114. sternON   LDAA #$30             ;output ’0’ if stern sw ON
  115. sternOFF  JSR putcLCD
  116.  
  117.           JMP lbl
  118.          
  119. msg1      dc.b "Battery volt ",0
  120. msg2      dc.b "Sw status ",0
  121.  
  122. ; Subroutine section
  123.  
  124. ;*******************************************************************
  125. ;* Initialization of the LCD: 4-bit data width, 2-line display,    *
  126. ;* turn on display, cursor and blinking off. Shift cursor right.   *
  127. ;*******************************************************************
  128. initLCD   BSET DDRS,%11110000 ; configure pins PS7,PS6,PS5,PS4 for output
  129.           BSET DDRE,%10010000 ; configure pins PE7,PE4 for output
  130.           LDY #2000 ; wait for LCD to be ready
  131.           JSR del_50us ; -"-
  132.           LDAA #$28 ; set 4-bit data, 2-line display
  133.           JSR cmd2LCD ; -"-
  134.           LDAA #$0C; display on, cursor off, blinking off
  135.           JSR cmd2LCD ; -"-
  136.           LDAA #$06 ; move cursor right after entering a character
  137.           JSR cmd2LCD ; -"-
  138.  
  139.           RTS
  140.          
  141. ;*******************************************************************
  142. ;* Clear display and home cursor                                   *
  143. ;*******************************************************************
  144. clrLCD    LDAA #$01 ; clear cursor and return to home position
  145.           JSR cmd2LCD ; -"-
  146.           LDY #40 ; wait until "clear cursor" command is complete
  147.           JSR del_50us ; -"-
  148.           RTS
  149.          
  150. ;*******************************************************************
  151. ;* ([Y] x 50us)-delay subroutine. E-clk=41,67ns.                   *
  152. ;*******************************************************************
  153. del_50us:   PSHX ;2 E-clk
  154. eloop:      LDX #36 ;2 E-clk -
  155. iloop:      PSHA ;2 E-clk |
  156.             PULA ;3 E-clk |
  157.            
  158.             PSHA ;2 E-clk |
  159.             PULA ;3 E-clk |
  160.             PSHA ;2 E-clk |
  161.             PULA ;3 E-clk |
  162.             PSHA ;2 E-clk |
  163.             PULA ;3 E-clk |
  164.             PSHA ;2 E-clk |
  165.             PULA ;3 E-clk |
  166.                                
  167.             PSHA ;2 E-clk | 50us
  168.             PULA ;3 E-clk |
  169.             NOP  ;1 E-clk |
  170.             NOP  ;1 E-clk |
  171.             DBNE X,iloop ;3 E-clk -                
  172.             DBNE Y,eloop ;3 E-clk
  173.             PULX ;3 E-clk
  174.             RTS ;5 E-clk
  175.                        
  176. ;*******************************************************************
  177. ;* This function sends a command in accumulator A to the LCD       *
  178. ;*******************************************************************
  179. cmd2LCD:  BCLR LCD_CNTR,LCD_RS ; select the LCD Instruction Register (IR)
  180.           JSR dataMov ; send data to IR
  181.           RTS
  182.  
  183. ;*******************************************************************
  184. ;* This function outputs a NULL-terminated string pointed to by X  *
  185. ;*******************************************************************
  186. putsLCD   LDAA 1,X+ ; get one character from the string
  187.           BEQ donePS ; reach NULL character?
  188.          
  189.          
  190.           JSR putcLCD
  191.           BRA putsLCD
  192.          
  193. donePS    RTS
  194.          
  195. ;*******************************************************************
  196. ;* This function outputs the character in accumulator in A to LCD  *
  197. ;*******************************************************************
  198. putcLCD   BSET LCD_CNTR,LCD_RS ; select the LCD Data register (DR)
  199.           JSR dataMov ; send data to DR
  200.           RTS
  201.          
  202. ;*******************************************************************
  203. ;* This function sends data to the LCD IR or DR depening on RS     *
  204. ;*******************************************************************
  205. dataMov   BSET LCD_CNTR,LCD_E ; pull the LCD E-sigal high
  206.           STAA LCD_DAT ; send the upper 4 bits of data to LCD
  207.           BCLR LCD_CNTR,LCD_E ; pull the LCD E-signal low to complete the write oper.
  208.          
  209.           LSLA ; match the lower 4 bits with the LCD data pins
  210.           LSLA ; -"-
  211.           LSLA ; -"-
  212.           LSLA ; -"-
  213.          
  214.           BSET LCD_CNTR,LCD_E ; pull the LCD E signal high
  215.           STAA LCD_DAT ; send the lower 4 bits of data to LCD
  216.           BCLR LCD_CNTR,LCD_E ; pull the LCD E-signal low to complete the write oper.
  217.          
  218.           LDY #1 ; adding this delay will complete the internal
  219.           JSR del_50us ; operation for most instructions
  220.           RTS
  221.          
  222. ;******************************************************************
  223. ;* Integer to BCD Conversion Routine                              *
  224. ;* This routine converts a 16 bit binary number in .D into        *
  225. ;* BCD digits in BCD_BUFFER.                                      *
  226. ;* Peter Hiscocks                                                 *
  227. ;* Algorithm:                                                     *
  228. ;* Because the IDIV (Integer Division) instruction is available on*
  229. ;* the HCS12, we can determine the decimal digits by repeatedly   *
  230. ;* dividing the binary number by ten: the remainder each time is  *
  231. ;* a decimal digit. Conceptually, what we are doing is shifting   *
  232. ;* the decimal number one place to the right past the decimal     *
  233. ;* point with each divide operation. The remainder must be        *
  234. ;* a decimal digit between 0 and 9, because we divided by 10.     *
  235. ;* The algorithm terminates when the quotient has become zero.    *
  236. ;* Bug note: XGDX does not set any condition codes, so test for   *
  237. ;* quotient zero must be done explicitly with CPX.                *
  238. ;******************************************************************
  239. int2BCD       XGDX              ;Save the binary number into .X
  240.               LDAA #0           ;Clear the BCD_BUFFER
  241.               STAA TEN_THOUS
  242.               STAA THOUSANDS
  243.               STAA HUNDREDS
  244.               STAA TENS
  245.               STAA UNITS
  246.               STAA BCD_SPARE
  247.               STAA BCD_SPARE+1
  248. *
  249.               CPX #0            ;Check for a zero input
  250.               BEQ CON_EXIT      ;and if so, exit
  251. *
  252.               XGDX              ;Not zero, get the binary number back to .D as dividend
  253.               LDX #10           ;Setup 10 (Decimal!) as the divisor
  254.               IDIV              ;Divide: Quotient is now in .X, remainder in .D
  255.               STAB UNITS        ;Store remainder
  256.               CPX #0            ;If quotient is zero,
  257.               BEQ CON_EXIT      ;then exit
  258. *
  259.               XGDX              ;else swap first quotient back into .D
  260.               LDX #10           ;and setup for another divide by 10
  261.               IDIV
  262.               STAB TENS
  263.               CPX #0
  264.               BEQ CON_EXIT
  265. *
  266.               XGDX              ;Swap quotient back into .D
  267.               LDX #10           ;and setup for another divide by 10
  268.               IDIV
  269.               STAB HUNDREDS
  270.               CPX #0
  271.               BEQ CON_EXIT
  272. *
  273.               XGDX              ;Swap quotient back into .D
  274.               LDX #10           ;and setup for another divide by 10
  275.               IDIV
  276.               STAB THOUSANDS
  277.               CPX #0
  278.               BEQ CON_EXIT
  279. *
  280.               XGDX              ;Swap quotient back into .D
  281.               LDX #10           ;and setup for another divide by 10
  282.               IDIV
  283.               STAB TEN_THOUS
  284. *
  285. CON_EXIT      RTS      ;We’re done the conversion
  286. ;****************************************************************
  287. ;* BCD to ASCII Conversion Routine                              *
  288. ;* This routine converts the BCD number in the BCD_BUFFER       *
  289. ;* into ascii format, with leading zero suppression.            *
  290. ;* Leading zeros are converted into space characters.           *
  291. ;* The flag ’NO_BLANK’ starts cleared and is set once a non-zero*
  292. ;* digit has been detected.                                     *
  293. ;* The ’units’ digit is never blanked, even if it and all the   *
  294. ;* preceding digits are zero.                                   *
  295. ;* Peter Hiscocks                                               *
  296. ;****************************************************************
  297. BCD2ASC     LDAA #0           ;Initialize the blanking flag
  298.             STAA NO_BLANK     ;
  299. *
  300. C_TTHOU     LDAA TEN_THOUS    ;Check the ’ten_thousands’ digit
  301.             ORAA NO_BLANK
  302.             BNE NOT_BLANK1
  303. *
  304. ISBLANK1      LDAA #' '       ;It’s blank
  305.               STAA TEN_THOUS  ;so store a space
  306.               BRA C_THOU      ;and check the ’thousands’ digit
  307. *
  308. NOT_BLANK1    LDAA TEN_THOUS  ;Get the ’ten_thousands’ digit
  309.               ORAA #$30       ;Convert to ascii
  310.               STAA TEN_THOUS
  311.               LDAA #$1        ;Signal that we have seen a ’non-blank’ digit
  312.               STAA NO_BLANK
  313. *
  314. C_THOU        LDAA THOUSANDS  ;Check the thousands digit for blankness
  315.               ORAA NO_BLANK   ;If it’s blank and ’no-blank’ is still zero
  316.               BNE NOT_BLANK2
  317. *
  318. ISBLANK2      LDAA #' '       ;Thousands digit is blank
  319.               STAA THOUSANDS  ;so store a space
  320.               BRA C_HUNS      ;and check the hundreds digit
  321. *
  322. NOT_BLANK2    LDAA THOUSANDS  ;(similar to ’ten_thousands’ case)
  323.               ORAA #$30
  324.               STAA THOUSANDS
  325.               LDAA #$1
  326.               STAA NO_BLANK
  327. *
  328. C_HUNS        LDAA HUNDREDS   ;Check the hundreds digit for blankness
  329.               ORAA NO_BLANK   ;If it’s blank and ’no-blank’ is still zero
  330.               BNE NOT_BLANK3
  331. *
  332. ISBLANK3      LDAA #' '       ;Hundreds digit is blank
  333.               STAA HUNDREDS   ;so store a space
  334.               BRA C_TENS      ;and check the tens digit
  335. *
  336. NOT_BLANK3    LDAA HUNDREDS   ;(similar to ’ten_thousands’ case)
  337.               ORAA #$30
  338.               STAA HUNDREDS
  339.               LDAA #$1
  340.               STAA NO_BLANK
  341. *
  342. C_TENS        LDAA TENS       ;Check the tens digit for blankness
  343.               ORAA NO_BLANK   ;If it’s blank and ’no-blank’ is still zero
  344.               BNE NOT_BLANK4
  345. *
  346. ISBLANK4      LDAA #' '       ;Tens digit is blank
  347.               STAA TENS       ;so store a space
  348.               BRA C_UNITS     ;and check the units digit
  349. *
  350. NOT_BLANK4    LDAA TENS       ;(similar to ’ten_thousands’ case)
  351.               ORAA #$30
  352.               STAA TENS
  353. *
  354. C_UNITS       LDAA UNITS      ;No blank check necessary, convert to ascii.
  355.               ORAA #$30
  356.               STAA UNITS
  357. *
  358.               RTS             ;We’re done
  359.  
  360.  
  361. initAD    MOVB  #$C0,ATDCTL2     ;power up AD, select fast flag clear
  362.           JSR   del_50us         ;wait for 50 us
  363.           MOVB  #$00,ATDCTL3     ;8 conversions in a sequence
  364.           MOVB #$85,ATDCTL4      ;res=8, conv-clks=2, prescal=12
  365.           BSET ATDDIEN,$0C       ;configure pins AN03,AN02 as digital inputs
  366.           RTS
  367.            
  368.  
  369. ;**************************************************************
  370. ;*                 Interrupt Vectors                          *
  371. ;**************************************************************
  372.             ORG   $FFFE
  373.             DC.W  Entry           ; Reset Vector
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement