Advertisement
donkangru

Untitled

Jan 22nd, 2020
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 10.19 KB | None | 0 0
  1. ;*******************************************************************
  2. ;Port definitions
  3. ;*******************************************************************
  4. ;
  5. CONSTANT LED_port, 80 ;8 simple LEDs
  6. CONSTANT LED0, 01 ; LED 0 - bit0
  7. CONSTANT LED1, 02 ; 1 - bit1
  8. CONSTANT LED2, 04 ; 2 - bit2
  9. CONSTANT LED3, 08 ; 3 - bit3
  10. CONSTANT LED4, 10 ; 4 - bit4
  11. CONSTANT LED5, 20 ; 5 - bit5
  12. CONSTANT LED6, 40 ; 6 - bit6
  13. CONSTANT LED7, 80 ; 7 - bit7
  14. ;
  15. CONSTANT switch_port, 00 ;Read switches and press buttons
  16. CONSTANT switch0, 01 ; Switches SW0 - bit0
  17. CONSTANT switch1, 02 ; SW1 - bit1
  18. CONSTANT switch2, 04 ; SW2 - bit2
  19. CONSTANT switch3, 08 ; SW3 - bit3
  20. CONSTANT BTN_east, 10 ; Buttons East - bit4
  21. CONSTANT BTN_south, 20 ; South - bit5
  22. CONSTANT BTN_north, 40 ; North - bit6
  23. CONSTANT BTN_west, 80 ; West - bit7
  24. ;
  25. ;
  26. ;LCD interface ports
  27. ;
  28. ;The master enable signal is not used by the LCD display itself
  29. ;but may be required to confirm that LCD communication is active.
  30. ;This is required on the Spartan-3E Starter Kit if the StrataFLASH
  31. ;is used because it shares the same data pins and conflicts must be
  32. ;avoided.
  33. ;
  34. CONSTANT LCD_output_port, 40 ;LCD character module output
  35.  ;data and control
  36. CONSTANT LCD_E, 01 ; active High Enable E - bit0
  37. CONSTANT LCD_RW, 02 ; Read=1 Write=0 RW - bit1
  38. CONSTANT LCD_RS, 04 ; Instruction=0 Data=1 RS - bit2
  39. CONSTANT LCD_drive, 08 ; Master enable (active High)
  40.  ; - bit3
  41. CONSTANT LCD_DB4, 10 ; 4-bit Data DB4 - bit4
  42. CONSTANT LCD_DB5, 20 ; interface Data DB5 - bit5
  43. CONSTANT LCD_DB6, 40 ; Data DB6 - bit6
  44. CONSTANT LCD_DB7, 80 ; Data DB7 - bit7
  45. ;
  46. ;
  47. CONSTANT LCD_input_port, 02 ;LCD character module input data
  48. CONSTANT LCD_read_spare0, 01 ; Spare bits - bit0
  49. CONSTANT LCD_read_spare1, 02 ; are zero - bit1
  50. CONSTANT LCD_read_spare2, 04 ; - bit2
  51. CONSTANT LCD_read_spare3, 08 ; - bit3
  52. CONSTANT LCD_read_DB4, 10 ; 4-bit Data DB4 - bit4
  53. CONSTANT LCD_read_DB5, 20 ; interface Data DB5 - bit5
  54. CONSTANT LCD_read_DB6, 40 ; Data DB6 - bit6
  55. CONSTANT LCD_read_DB7, 80 ; Data DB7 - bit7
  56. ;
  57. ;
  58.  
  59.  
  60.  
  61. Xilinx Spartan-3E Project Navigator Version 14.3
  62. ;Constant to define a software delay of 1us. This must be adjusted
  63. ;to reflect the clock applied to KCPSM3. Every instruction executes
  64. ;in 2 clock cycles making the calculation highly predictable. The
  65. ;'6' in the following equation even allows for 'CALL delay_1us'
  66. ;instruction in the initiating code.
  67. ;
  68. ;delay_1us_constant = (clock_rate - 6)/4 Where 'clock_rate'
  69. ;is in MHz
  70. ;
  71. ;Example: For a 50MHz clock the constant value is (10-6)/4 = 11
  72. ;(0B Hex).
  73. ;For clock rates below 10MHz the value of 1 must be used and the
  74. ;operation will become lower than intended.
  75. ;
  76. CONSTANT delay_1us_constant, 0B
  77. ;
  78.  
  79.  ;
  80.  ;*******************************************
  81.  ;Initialise the system
  82.  ;*******************************************
  83.  ;
  84.  cold_start: CALL LCD_reset ;initialise LCD display
  85.  ;
  86.  Main: INPUT s7, switch_port ;read in switches
  87.  OUTPUT s7, LED_port ;output to LEDs
  88.  JUMP Main
  89.  ;
  90.  ;
  91.  
  92.  ;
  93.  ;*******************************************
  94.  ;Software delay routines
  95.  ;*******************************************
  96.  ;
  97.  ;
  98.  ;Delay of 1us.
  99.  ;
  100.  ;Constant value defines reflects the clock applied
  101.  ;to KCPSM3. Every instruction executes in 2 clock
  102.  ;cycles making the calculation highly predictable.
  103.  ;The '6' in the following equation even allows for
  104.  ;'CALL delay_1us' instruction in the initiating code.
  105.  ;
  106.  ; delay_1us_constant = (clock_rate - 6)/4
  107.  ; Where 'clock_rate' is in MHz
  108.  ;
  109.  ;Registers used s0
  110.  ;
  111.  delay_1us: LOAD s0, delay_1us_constant
  112.  wait_1us: SUB s0, 01
  113.  JUMP NZ, wait_1us
  114.  RETURN
  115.  ;
  116.  ;Delay of 40us.
  117.  ;
  118.  ;Registers used s0, s1
  119.  ;
  120.  
  121.  
  122. Xilinx Spartan-3E Project Navigator Version 14.3
  123.  delay_40us: LOAD s1, 28 ;40 x 1us = 40us
  124.  wait_40us: CALL delay_1us
  125.  SUB s1, 01
  126.  JUMP NZ, wait_40us
  127.  RETURN
  128.  ;
  129.  ;
  130.  ;Delay of 1ms.
  131.  ;
  132.  ;Registers used s0, s1, s2
  133.  ;
  134.  delay_1ms: LOAD s2, 19 ;25 x 40us = 1ms
  135.  wait_1ms: CALL delay_40us
  136.  SUB s2, 01
  137.  JUMP NZ, wait_1ms
  138.  RETURN
  139.  ;
  140.  ;Delay of 20ms.
  141.  ;
  142.  ;Delay of 20ms used during initialisation.
  143.  ;
  144.  ;Registers used s0, s1, s2, s3
  145.  ;
  146.  delay_20ms: LOAD s3, 14 ;20 x 1ms = 20ms
  147.  wait_20ms: CALL delay_1ms
  148.  SUB s3, 01
  149.  JUMP NZ, wait_20ms
  150.  RETURN
  151.  ;
  152.  ;Delay of approximately 1 second.
  153.  ;
  154.  ;Registers used s0, s1, s2, s3, s4
  155.  ;
  156.  delay_1s: LOAD s4, 32 ;50 x 20ms = 1000ms
  157.  wait_1s: CALL delay_20ms
  158.  SUB s4, 01
  159.  JUMP NZ, wait_1s
  160.  RETURN
  161.  ;
  162.  
  163.  ;
  164.  ;
  165.  ;**************************************************
  166.  ;LCD Character Module Routines
  167.  ;**************************************************
  168.  ;
  169.  ;LCD module is a 16 character by 2 line display but
  170.  ;all displays are very similar The 4-wire data
  171.  ;interface will be used (DB4 to DB7).
  172.  ;
  173.  ;The LCD modules are relatively slow and software
  174.  ;delay loops are used to slow down KCPSM3
  175.  ;adequately for the LCD to communicate. The delay
  176.  ;routines are provided in a different section (see
  177.  ;above in this case).
  178.  ;
  179.  ;
  180.  
  181.  
  182. Xilinx Spartan-3E Project Navigator Version 14.3
  183.  ;Pulse LCD enable signal 'E' high for greater than
  184.  ;230ns (1us is used).
  185.  ;
  186.  ;Register s4 should define the current state of the
  187.  ;LCD output port.
  188.  ;
  189.  ;Registers used s0, s4
  190.  ;
  191.  LCD_pulse_E: XOR s4, LCD_E ;E=1
  192.  OUTPUT s4, LCD_output_port
  193.  CALL delay_1us
  194.  XOR s4, LCD_E ;E=0
  195.  OUTPUT s4, LCD_output_port
  196.  RETURN
  197.  ;
  198.  ;Write 4-bit instruction to LCD display.
  199.  ;
  200.  ;The 4-bit instruction should be provided in the
  201.  ;upper 4-bits of register s4.
  202.  ;Note that this routine does not release the master
  203.  ;enable but as it is only
  204.  ;used during initialisation and as part of the
  205.  ;8-bit instruction write it should be acceptable.
  206.  ;
  207.  ;Registers used s4
  208.  ;
  209. LCD_write_inst4: AND s4, F8 ;Enable=1 RS=0
  210.  ;Instruction, RW=0
  211.  ;Write, E=0
  212.  ;
  213.  OUTPUT s4, LCD_output_port ;set up RS and RW>40ns
  214.  ;before enable pulse
  215.  ;
  216.  CALL LCD_pulse_E
  217.  RETURN
  218.  ;
  219.  ;
  220.  ;Write 8-bit instruction to LCD display.
  221.  ;
  222.  ;The 8-bit instruction should be provided in
  223.  ; register s5.
  224.  ;Instructions are written using the following
  225.  ;sequence
  226.  ; Upper nibble
  227.  ; wait >1us
  228.  ; Lower nibble
  229.  ; wait >40us
  230.  ;
  231.  ;Registers used s0, s1, s4, s5
  232.  ;
  233. LCD_write_inst8: LOAD s4, s5
  234.  AND s4, F0 ;Enable=0 RS=0
  235.  ;Instruction, RW=0
  236.  ;Write, E=0
  237.  ;
  238.  
  239.  
  240. Xilinx Spartan-3E Project Navigator Version 14.3
  241.  OR s4, LCD_drive ;Enable=1
  242.  CALL LCD_write_inst4 ;write upper nibble
  243.  CALL delay_1us ;wait >1us
  244.  LOAD s4, s5 ;select lower nibble
  245.  ;with
  246.  SL1 s4 ;Enable=1
  247.  SL0 s4 ;RS=0 Instruction
  248.  SL0 s4 ;RW=0 Write
  249.  SL0 s4 ;E=0
  250.  CALL LCD_write_inst4 ;write lower nibble
  251.  CALL delay_40us ;wait >40us
  252.  LOAD s4, F0 ;Enable=0 RS=0
  253.  ;Instruction, RW=0
  254.  ;Write, E=0
  255.  ;
  256.  OUTPUT s4, LCD_output_port ;Release master enable
  257.  ;
  258.  RETURN
  259.  ;
  260.  ;Write 8-bit data to LCD display.
  261.  ;
  262.  ;The 8-bit data should be provided in register s5.
  263.  ;Data bytes are written using the following sequence
  264.  ; Upper nibble
  265.  ; wait >1us
  266.  ; Lower nibble
  267.  ; wait >40us
  268.  ;
  269.  ;Registers used s0, s1, s4, s5
  270.  ;
  271.  LCD_write_data: LOAD s4, s5
  272.  AND s4, F0 ;Enable=0 RS=0
  273.  ;Instruction, RW=0
  274.  ;Write, E=0
  275.  ;
  276.  OR s4, 0C ;Enable=1 RS=1 Data,
  277.  ;RW=0 Write, E=0
  278.  ;
  279.  OUTPUT s4, LCD_output_port ;set up RS and RW>40ns
  280.  ;before enable pulse
  281.  ;
  282.  CALL LCD_pulse_E ;write upper nibble
  283.  CALL delay_1us ;wait >1us
  284.  LOAD s4, s5 ;select lower nibble
  285.  ;with
  286.  SL1 s4 ;Enable=1
  287.  SL1 s4 ;RS=1 Data
  288.  SL0 s4 ;RW=0 Write
  289.  SL0 s4 ;E=0
  290.  OUTPUT s4, LCD_output_port ;set up RS and RW>40ns
  291.  ;before enable pulse
  292.  ;
  293.  CALL LCD_pulse_E ;write lower nibble
  294.  CALL delay_40us ;wait >40us
  295.  LOAD s4, F0 ;Enable=0 RS=0
  296.  ;Instruction, RW=0
  297.  ;Write, E=0
  298.  
  299.  
  300. Xilinx Spartan-3E Project Navigator Version 14.3
  301.  ;
  302.  OUTPUT s4, LCD_output_port ;Release master enable
  303.  ;
  304.  RETURN
  305.  ;
  306.  ;Read 8-bit data from LCD display.
  307.  ;
  308.  ;The 8-bit data will be read from the current LCD
  309.  ;memory address and will be returned in register s5.
  310.  ;It is advisable to set the LCD address (cursor
  311.  ;position) before using the data read for the first
  312.  ;time otherwise the display may generate invalid data
  313.  ;on the first read.
  314.  ;
  315.  ;Data bytes are read using the following sequence
  316.  ; Upper nibble
  317.  ; wait >1us
  318.  ; Lower nibble
  319.  ; wait >40us
  320.  ;
  321.  ;Registers used s0, s1, s4, s5
  322.  ;
  323.  LCD_read_data8: LOAD s4, 0E ;Enable=1 RS=1 Data,
  324.  ;RW=1 Read, E=0
  325.  ;
  326.  OUTPUT s4, LCD_output_port ;set up RS and RW>40ns
  327.  ;before enable pulse
  328.  ;
  329.  XOR s4, LCD_E ;E=1
  330.  OUTPUT s4, LCD_output_port
  331.  CALL delay_1us ;wait >260ns to
  332.  ;access data
  333.  ;
  334.  INPUT s5, LCD_input_port ;read upper nibble
  335.  XOR s4, LCD_E ;E=0
  336.  OUTPUT s4, LCD_output_port
  337.  CALL delay_1us ;wait >1us
  338.  XOR s4, LCD_E ;E=1
  339.  OUTPUT s4, LCD_output_port
  340.  CALL delay_1us ;wait >260ns to
  341.  ;access data
  342.  ;
  343.  INPUT s0, LCD_input_port ;read lower nibble
  344.  XOR s4, LCD_E ;E=0
  345.  OUTPUT s4, LCD_output_port
  346.  AND s5, F0 ;merge upper and
  347.  ;lower nibbles
  348.  SR0 s0
  349.  SR0 s0
  350.  SR0 s0
  351.  SR0 s0
  352.  OR s5, s0
  353.  LOAD s4, 04 ;Enable=0 RS=1 Data,
  354.  ;RW=0 Write, E=0
  355.  ;
  356.  
  357.  
  358. Xilinx Spartan-3E Project Navigator Version 14.3
  359.  OUTPUT s4, LCD_output_port ;Stop reading 5V
  360.  ;device and release
  361.  ;master enable
  362.  ;
  363.  CALL delay_40us ;wait >40us
  364.  RETURN
  365.  ;
  366.  ;Reset and initialise display to communicate using
  367.  ;4-bit data mode
  368.  ;Includes routine to clear the display.
  369.  ;
  370.  ;Requires the 4-bit instructions 3,3,3,2 to be sent
  371.  ;with suitable delays following by the 8-bit
  372.  ;instructions to set up the display.
  373.  ;
  374.  ; 28 = '001' Function set, '0' 4-bit mode, '1' 2-line,
  375.  ; '0' 5x7 dot matrix, 'xx'
  376.  ; 06 = '000001' Entry mode, '1' increment, '0'
  377.  ; no display shift
  378.  ; 0C = '00001' Display control, '1' display on,
  379.  ; '0' cursor off, '0' cursor blink off
  380.  ; 01 = '00000001' Display clear
  381.  ;
  382.  ;Registers used s0, s1, s2, s3, s4
  383.  ;
  384.  LCD_reset: CALL delay_20ms ;wait more that
  385.  ;15ms for display
  386.  ;to be ready
  387.  ;
  388.  LOAD s4, 30
  389.  CALL LCD_write_inst4 ;send '3'
  390.  CALL delay_20ms ;wait >4.1ms
  391.  CALL LCD_write_inst4 ;send '3'
  392.  CALL delay_1ms ;wait >100us
  393.  CALL LCD_write_inst4 ;send '3'
  394.  CALL delay_40us ;wait >40us
  395.  LOAD s4, 20
  396.  CALL LCD_write_inst4 ;send '2'
  397.  CALL delay_40us ;wait >40us
  398.  LOAD s5, 28 ;Function set
  399.  CALL LCD_write_inst8
  400.  LOAD s5, 06 ;Entry mode
  401.  CALL LCD_write_inst8
  402.  LOAD s5, 0C ;Display control
  403.  CALL LCD_write_inst8
  404.  LCD_clear: LOAD s5, 01 ;Display clear
  405.  CALL LCD_write_inst8
  406.  CALL delay_1ms ;wait >1.64ms for
  407.  ;display to clear
  408.  CALL delay_1ms
  409.  RETUR
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement