📄 pwm_ctrl.psm
字号:
; KCPSM3 Program - Pulse Width Modulation (PWM) Control on the Spartan-3E Starter Kit.
;
; Ken Chapman - Xilinx Ltd
;
; Version v1.00 - 22nd May 2006
;
; Provides control for 12 channels of PWM with a Pulse Repetition Frequency (PRF) of 1KHz
; and an 8-bit duty cycle resolution (256 steps). Control is provided for each channel
; via the UART interface to the PC running HyperTerminal or similar to enter simple text
; commands.
;
;**************************************************************************************
; Port definitions
;**************************************************************************************
;
;
;
CONSTANT LED_port, 80 ;8 simple LEDs
CONSTANT LED0, 01 ; LED 0 - bit0
CONSTANT LED1, 02 ; 1 - bit1
CONSTANT LED2, 04 ; 2 - bit2
CONSTANT LED3, 08 ; 3 - bit3
CONSTANT LED4, 10 ; 4 - bit4
CONSTANT LED5, 20 ; 5 - bit5
CONSTANT LED6, 40 ; 6 - bit6
CONSTANT LED7, 80 ; 7 - bit7
;
;
CONSTANT simple_port, 40 ;4 simple outputs
CONSTANT simple_IO9, 01 ; Header IO9 - bit0
CONSTANT simple_IO10, 02 ; IO10 - bit1
CONSTANT simple_IO11, 04 ; IO11 - bit2
CONSTANT simple_IO12, 08 ; IO12 - bit3
;
;
;
CONSTANT status_port, 00 ;UART status input
CONSTANT tx_half_full, 01 ; Transmitter half full - bit0
CONSTANT tx_full, 02 ; FIFO full - bit1
CONSTANT rx_data_present, 04 ; Receiver data present - bit2
CONSTANT rx_half_full, 08 ; FIFO half full - bit3
CONSTANT rx_full, 10 ; full - bit4
CONSTANT spare1, 20 ; spare '0' - bit5
CONSTANT spare2, 40 ; spare '0' - bit6
CONSTANT spare3, 80 ; spare '0' - bit7
;
CONSTANT UART_read_port, 01 ;UART Rx data input
;
CONSTANT UART_write_port, 20 ;UART Tx data output
;
;
;
;**************************************************************************************
; Special Register usage
;**************************************************************************************
;
NAMEREG sF, UART_data ;used to pass data to and from the UART
;
;
;
;**************************************************************************************
;Scratch Pad Memory Locations
;**************************************************************************************
;
CONSTANT PWM_duty_counter, 00 ;Duty Counter 0 to 255 within 1KHz period (1ms)
CONSTANT PWM_channel0, 01 ;PWM settings for each channel
CONSTANT PWM_channel1, 02 ; Channels 0 to 7 = LEDs 0 to 7
CONSTANT PWM_channel2, 03 ; Channels 8 to 11 = IO9 to IO12
CONSTANT PWM_channel3, 04
CONSTANT PWM_channel4, 05
CONSTANT PWM_channel5, 06
CONSTANT PWM_channel6, 07
CONSTANT PWM_channel7, 08
CONSTANT PWM_channel8, 09
CONSTANT PWM_channel9, 0A
CONSTANT PWM_channel10, 0B
CONSTANT PWM_channel11, 0C
CONSTANT ISR_preserve_s0, 0D ;preserve register contents during Interrupt Service Routine
CONSTANT ISR_preserve_s1, 0E
CONSTANT ISR_preserve_s2, 0F
;
;
;
;
;UART character strings will be stored in scratch pad memory ending in carriage return.
;A string can be up to 16 characters with the start location defined by this constant.
;
CONSTANT string_start, 30
;
;
;
;**************************************************************************************
;Useful data constants
;**************************************************************************************
;
;
;
;
;ASCII table
;
CONSTANT character_a, 61
CONSTANT character_b, 62
CONSTANT character_c, 63
CONSTANT character_d, 64
CONSTANT character_e, 65
CONSTANT character_f, 66
CONSTANT character_g, 67
CONSTANT character_h, 68
CONSTANT character_i, 69
CONSTANT character_j, 6A
CONSTANT character_k, 6B
CONSTANT character_l, 6C
CONSTANT character_m, 6D
CONSTANT character_n, 6E
CONSTANT character_o, 6F
CONSTANT character_p, 70
CONSTANT character_q, 71
CONSTANT character_r, 72
CONSTANT character_s, 73
CONSTANT character_t, 74
CONSTANT character_u, 75
CONSTANT character_v, 76
CONSTANT character_w, 77
CONSTANT character_x, 78
CONSTANT character_y, 79
CONSTANT character_z, 7A
CONSTANT character_A, 41
CONSTANT character_B, 42
CONSTANT character_C, 43
CONSTANT character_D, 44
CONSTANT character_E, 45
CONSTANT character_F, 46
CONSTANT character_G, 47
CONSTANT character_H, 48
CONSTANT character_I, 49
CONSTANT character_J, 4A
CONSTANT character_K, 4B
CONSTANT character_L, 4C
CONSTANT character_M, 4D
CONSTANT character_N, 4E
CONSTANT character_O, 4F
CONSTANT character_P, 50
CONSTANT character_Q, 51
CONSTANT character_R, 52
CONSTANT character_S, 53
CONSTANT character_T, 54
CONSTANT character_U, 55
CONSTANT character_V, 56
CONSTANT character_W, 57
CONSTANT character_X, 58
CONSTANT character_Y, 59
CONSTANT character_Z, 5A
CONSTANT character_0, 30
CONSTANT character_1, 31
CONSTANT character_2, 32
CONSTANT character_3, 33
CONSTANT character_4, 34
CONSTANT character_5, 35
CONSTANT character_6, 36
CONSTANT character_7, 37
CONSTANT character_8, 38
CONSTANT character_9, 39
CONSTANT character_colon, 3A
CONSTANT character_stop, 2E
CONSTANT character_semi_colon, 3B
CONSTANT character_minus, 2D
CONSTANT character_divide, 2F ;'/'
CONSTANT character_plus, 2B
CONSTANT character_comma, 2C
CONSTANT character_less_than, 3C
CONSTANT character_greater_than, 3E
CONSTANT character_equals, 3D
CONSTANT character_space, 20
CONSTANT character_CR, 0D ;carriage return
CONSTANT character_question, 3F ;'?'
CONSTANT character_dollar, 24
CONSTANT character_exclaim, 21 ;'!'
CONSTANT character_BS, 08 ;Back Space command character
;
;
;
;
;
;**************************************************************************************
;Initialise the system
;**************************************************************************************
;
; Each PWM channels will be set to a different initial value just for the purposes
; of demonstration. In practice, the initial duty values will depend on the requirements
; of a given system but completely off (zero) is normally the safe option.
;
; Note that it is difficult to distinguish difference between the intensity of LEDs driven
; with duty factors more than 40% (40% = 102/256 or 66Hex). So using relatively small values
; will better demonstrate the PWM control of intensity.
;
; Initial values for LEDs give graduated intensity. Large change required for brighter LEDs.
;
cold_start: LOAD s0, 05 ;5/256 = 2%
STORE s0, PWM_channel0
LOAD s0, 0D ;13/256 = 5%
STORE s0, PWM_channel1
LOAD s0, 14 ;26/256 = 8%
STORE s0, PWM_channel2
LOAD s0, 26 ;38/256 = 15%
STORE s0, PWM_channel3
LOAD s0, 40 ;64/256 = 25%
STORE s0, PWM_channel4
LOAD s0, 58 ;88/256 = 34%
STORE s0, PWM_channel5
LOAD s0, 80 ;128/256 = 50%
STORE s0, PWM_channel6
LOAD s0, FF ;255/256 = 99.6% Maximum possible
STORE s0, PWM_channel7
;
; Initial values for simple outputs match documentation example
;
LOAD s0, 11 ;17/256 = 7%
STORE s0, PWM_channel8
LOAD s0, BC ;188/256 = 73%
STORE s0, PWM_channel9
LOAD s0, EF ;239/256 = 93%
STORE s0, PWM_channel10
LOAD s0, 22 ;34/256 = 13%
STORE s0, PWM_channel11
;
ENABLE INTERRUPT ;interrupts used to drive servo
;
CALL send_welcome ;Write welcome message to UART
;
;
;
;**************************************************************************************
; Main program
;**************************************************************************************
;
; Provides a prompt to which an input with one of the following formats is expected...
;
; LDn hh
;
; IOk hh
; IOkk hh
;
;
; Where
; 'LD' is a command to set one of the LED channels.
; 'IO' is a command to set one of the simple I/O outputs on J4.
; 'n' is an LED number in the range 0 to 7.
; 'k' or 'kk' is a simple I/O number in the range 9 to 12.
; 'hh' is a 2 digit hex value to specify the PWM duty factor (range 00 to FF).
;
; The input allows a degree of editing to be performed and upper and lower case letters
; to be used.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -