⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pwm_ctrl.psm

📁 EDA原理及VHDL实现(何宾教授)
💻 PSM
📖 第 1 页 / 共 3 页
字号:
                     ;
         warm_start: CALL send_prompt                       ;Prompt 'KCPSM3>'
                     CALL receive_string                    ;obtain input string of up to 16 characters
                     CALL upper_case_string                 ;convert string to upper case
                     ;
                     LOAD sE, string_start                  ;sE is memory pointer
                     FETCH s0, (sE)                         ;test for carriage return
                     COMPARE s0, character_CR
                     JUMP Z, warm_start
                     COMPARE s0, character_L                ;test for 'L' of 'LD' command
                     JUMP Z, LD_command
                     COMPARE s0, character_I                ;test for 'I' of 'IO' command
                     JUMP Z, IO_command
        bad_command: CALL send_CR                           ;no valid command entered
                     CALL send_Error
                     JUMP warm_start
                     ;
                     ;Processing potential 'LD' command
                     ;
         LD_command: CALL read_next_char
                     COMPARE s0, character_D                ;test for 'D' of 'LD' command
                     JUMP NZ, bad_command
                     CALL read_next_char                    ;test for LED number
                     CALL 1char_to_value
                     JUMP C, bad_command
                     COMPARE s0, 08                         ;test for number in range 0 to 7
                     JUMP NC, bad_command
                     LOAD sD, s0                            ;convert number into memory pointer in sD
                     ADD sD, PWM_channel0
    read_duty_value: CALL read_next_char                    ;test for a space
                     COMPARE s0, character_space
                     JUMP NZ, bad_command
                     CALL read_next_char                    ;read two character hex value
                     LOAD s3, s0
                     CALL read_next_char
                     LOAD s2, s0
                     CALL ASCII_byte_to_hex                 ;convert to value in s0
                     JUMP C, bad_command
                     LOAD sC, s0                            ;remember value
                     CALL read_next_char                    ;test for carriage return to end command
                     COMPARE s0, character_CR
                     JUMP NZ, bad_command
                     STORE sC, (sD)                         ;store new PWM duty factor for an LED
                     CALL send_OK
                     JUMP warm_start
                     ;
                     ;Processing potential 'LD' command
                     ;
         IO_command: CALL read_next_char
                     COMPARE s0, character_O                ;test for '0' of 'IO' command
                     JUMP NZ, bad_command
                     CALL read_next_char                    ;test for IO number
                     COMPARE s0, character_1                ;first number must either be '1' or '9'
                     JUMP Z, next_IO_number
                     COMPARE s0, character_9
                     JUMP NZ, bad_command
                     LOAD sD, PWM_channel8                  ;IO9 is controlled by PWM channel8
                     JUMP read_duty_value
     next_IO_number: CALL read_next_char                    ;read next number for IO10 to IO12
                     CALL 1char_to_value
                     JUMP C, bad_command
                     COMPARE s0, 03                         ;test for number in range 0 to 2
                     JUMP NC, bad_command
                     LOAD sD, s0                            ;convert number into memory pointer in sD
                     ADD sD, PWM_channel9
                     JUMP read_duty_value
                     ;
                     ;Read next character from scratch pad memory
                     ;
     read_next_char: ADD sE, 01
                     FETCH s0, (sE)                         ;test for space
                     RETURN
                     ;
                     ;
                     ;
                     ;**************************************************************************************
                     ; UART communication routines
                     ;**************************************************************************************
                     ;
                     ; Read one character from the UART
                     ;
                     ; Character read will be returned in a register called 'UART_data'.
                     ;
                     ; The routine first tests the receiver FIFO buffer to see if data is present.
                     ; If the FIFO is empty, the routine waits until there is a character to read.
                     ; As this could take any amount of time the wait loop could include a call to a
                     ; subroutine which performs a useful function.
                     ;
                     ;
                     ; Registers used s0 and UART_data
                     ;
     read_from_UART: INPUT s0, status_port                  ;test Rx_FIFO buffer
                     TEST s0, rx_data_present               ;wait if empty
                     JUMP NZ, read_character
                     JUMP read_from_UART
     read_character: INPUT UART_data, UART_read_port        ;read from FIFO
                     RETURN
                     ;
                     ;
                     ;
                     ; Transmit one character to the UART
                     ;
                     ; Character supplied in register called 'UART_data'.
                     ;
                     ; The routine first tests the transmit FIFO buffer to see if it is full.
                     ; If the FIFO is full, then the routine waits until it there is space.
                     ;
                     ; Registers used s0
                     ;
       send_to_UART: INPUT s0, status_port                  ;test Tx_FIFO buffer
                     TEST s0, tx_full                       ;wait if full
                     JUMP Z, UART_write
                     JUMP send_to_UART
         UART_write: OUTPUT UART_data, UART_write_port
                     RETURN
                     ;
                     ;
                     ;
                     ;
                     ;Receive ASCII string from UART
                     ;
                     ;An ASCII string will be read from the UART and stored in scratch pad memory
                     ;commencing at the location specified by a constant named 'string_start'.
                     ;The string will have a maximum length of 16 characters including a
                     ;carriage return (0D) denoting the end of the string.
                     ;
                     ;As each character is read, it is echoed to the UART transmitter.
                     ;Some minor editing is supported using backspace (BS=08) which is used
                     ;to adjust what is stored in scratch pad memory and adjust the display
                     ;on the terminal screen using characters sent to the UART transmitter.
                     ;
                     ;A test is made for the receiver FIFO becoming full. A full status is treated as
                     ;a potential error situation and will result in a 'Overflow Error' message being
                     ;transmitted to the UART, the receiver FIFO being purged of all data and an
                     ;empty string being stored (carriage return at first location).
                     ;
                     ;Registers used s0, s1, s2 and 'UART_data'.
                     ;
     receive_string: LOAD s1, string_start                  ;locate start of string
                     LOAD s2, s1                            ;compute 16 character address
                     ADD s2, 10
  receive_full_test: INPUT s0, status_port                  ;test Rx_FIFO buffer for full
                     TEST s0, rx_full
                     JUMP NZ, read_error
                     CALL read_from_UART                    ;obtain and echo character
                     CALL send_to_UART
                     STORE UART_data, (s1)                  ;write to memory
                     COMPARE UART_data, character_CR        ;test for end of string
                     RETURN Z
                     COMPARE UART_data, character_BS        ;test for back space
                     JUMP Z, BS_edit
                     ADD s1, 01                             ;increment memory pointer
                     COMPARE s1, s2                         ;test for pointer exceeding 16 characters
                     JUMP NZ, receive_full_test             ;next character
                     CALL send_backspace                    ;hold end of string position on terminal display
            BS_edit: SUB s1, 01                             ;memory pointer back one
                     COMPARE s1, string_start               ;test for under flow
                     JUMP C, string_start_again
                     CALL send_space                        ;clear character at current position
                     CALL send_backspace                    ;position cursor
                     JUMP receive_full_test                 ;next character
 string_start_again: CALL send_greater_than                 ;restore '>' at prompt
                     JUMP receive_string                    ;begin again
                     ;Receiver buffer overflow condition
         read_error: CALL send_CR                           ;Transmit error message
                     STORE UART_data, string_start          ;empty string in memory (start with CR)
                     CALL send_Overflow_Error
                     CALL send_CR
 clear_UART_Rx_loop: INPUT s0, status_port                  ;test Rx_FIFO buffer for data
                     TEST s0, rx_data_present
                     RETURN Z                               ;finish when buffer is empty
                     INPUT UART_data, UART_read_port        ;read from FIFO and ignore
                     JUMP clear_UART_Rx_loop
                     ;
                     ;
                     ;**************************************************************************************
                     ; Useful ASCII conversion and handling routines
                     ;**************************************************************************************
                     ;
                     ;
                     ;
                     ; Convert character to upper case
                     ;
                     ; The character supplied in register s0.
                     ; If the character is in the range 'a' to 'z', it is converted
                     ; to the equivalent upper case character in the range 'A' to 'Z'.
                     ; All other characters remain unchanged.
                     ;
                     ; Registers used s0.
                     ;
         upper_case: COMPARE s0, 61                         ;eliminate character codes below 'a' (61 hex)
                     RETURN C
                     COMPARE s0, 7B                         ;eliminate character codes above 'z' (7A hex)
                     RETURN NC
                     AND s0, DF                             ;mask bit5 to convert to upper case
                     RETURN
                     ;
                     ;
                     ;
                     ; Convert string held in scratch pad memory to upper case.
                     ;
                     ; Registers used s0, s1
                     ;
  upper_case_string: LOAD s1, string_start
           ucs_loop: FETCH s0, (s1)
                     COMPARE s0, character_CR
                     RETURN Z
                     CALL upper_case
                     STORE s0, (s1)
                     ADD s1, 01
                     JUMP ucs_loop
                     ;
                     ;
                     ; Convert character '0' to '9' to numerical value in range 0 to 9
                     ;
                     ; The character supplied in register s0. If the character is in the
                     ; range '0' to '9', it is converted to the equivalent decimal value.
                     ; Characters not in the range '0' to '9' are signified by the return
                     ; with the CARRY flag set.
                     ;
                     ; Registers used s0.
                     ;
     1char_to_value: ADD s0, C6                             ;reject character codes above '9' (39 hex)
                     RETURN C                               ;carry flag is set
                     SUB s0, F6                             ;reject character codes below '0' (30 hex)
                     RETURN                                 ;carry is set if value not in range
                     ;
                     ;
                     ;
                     ; Convert the HEX ASCII characters contained in 's3' and 's2' into
                     ; an equivalent hexadecimal value in register 's0'.
                     ;     The upper nibble is represented by an ASCII character in register s3.
                     ;     The lower nibble is represented by an ASCII character in register s2.
                     ;
                     ; Input characters must be in the range 00 to FF hexadecimal or the CARRY flag
                     ; will be set on return.
                     ;
                     ; Registers used s0, s2 and s3.
                     ;
  ASCII_byte_to_hex: LOAD s0, s3                            ;Take upper nibble
                     CALL ASCII_to_hex                      ;convert to value
                     RETURN C                               ;reject if out of range
                     LOAD s3, s0                            ;remember value
                     SL0 s3                                 ;multiply value by 16 to put in upper nibble
                     SL0 s3
                     SL0 s3
                     SL0 s3
                     LOAD s0, s2                            ;Take lower nibble
                     CALL ASCII_to_hex                      ;convert to value
                     RETURN C                               ;reject if out of range
                     OR s0, s3                              ;merge in the upper nibble with CARRY reset
                     RETURN

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -