jack96013

ex0105

Jan 4th, 2021 (edited)
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MPASM 6.38 KB | None | 0 0
  1. list p=16F877A
  2. #include <p16f877A.inc> ; Include file locate at defult directory
  3. ; CONFIG
  4. ; __config 0xFF7A
  5.  __CONFIG _FOSC_XT & _WDTE_OFF & _PWRTE_OFF & _BOREN_ON & _LVP_OFF & _CPD_OFF & _WRT_OFF & _CP_OFF
  6.  
  7. ;
  8. VAL_US equ .40 ; 1ms delay valum
  9. VAL_MS equ .100
  10. count equ 0x20 ; Defined temp reg. for 1ms delay
  11. count_ms equ 0x21            ; Defined delay reg.
  12.  
  13. VAL_500u equ .80 ; 1ms delay valum
  14. count_500u equ 0x37 ; Defined temp reg. for 1ms delay
  15.  
  16. ADDR_POLL equ 0x25 ;掃描顯示
  17.  
  18. ADDR_DISP equ 0x26 ;轉 7 Segment
  19.  
  20. ADDR_SOU equ 0x27 ;轉BCD (個位)
  21. ADDR_TEN equ 0x28 ;轉BCD 十位
  22.  
  23. ADDR_CLOCK_SEC equ 0x22  ;秒
  24. ;ADDR_CLOCK_MIN equ 0x23  ;分
  25. ;ADDR_CLOCK_HUR equ 0x24  ;小時
  26.  
  27. VAL_SEG_LEN equ .1 ;共顯示n位
  28. ADDR_SEG_DIG0 equ 0x31 ;第一位顯示
  29. ADDR_SEG_DIG1 equ 0x32 ;第二位顯示
  30. ADDR_SEG_DIG2 equ 0x33 ;第三位顯示
  31. ADDR_SEG_DIG3 equ 0x34 ;第四位顯示
  32. ADDR_SEG_DIG4 equ 0x35 ;第五位顯示
  33. ADDR_SEG_DIG5 equ 0x36 ;第六位顯示
  34. ADDR_SEG_DIG6 equ 0x37 ;第七位顯示
  35. ADDR_SEG_DIG7 equ 0x38 ;第八位顯示
  36.  
  37.  
  38. ;##### ISR #####
  39. ;### Temp For Interrupt
  40. ADDR_TEMP_STATUS equ 0x2D
  41. ADDR_TEMP_W equ 0x2E
  42. ;##### Timer 2 #####
  43. ;(1/4M)*4*16*16*(X+1)*Y = 10s
  44. VAL_TMR2_INIT equ .194 ;X
  45. VAL_TMR2_DIV equ .200 ;中斷Y次
  46. ADDR_TMR2_DIV equ 0x2F
  47.  
  48.  
  49.  ;
  50. ;***************************************
  51. ;           Program start              *
  52. ;***************************************
  53. org 0x00 ; reset vector
  54.     nop ; Reserve for MPLAB-ICD
  55.     goto Initial
  56. org 0x04
  57.     goto INTSUB
  58.    
  59. Initial:
  60.    
  61.     clrw ; W =0
  62.     clrf PCLATH
  63.  
  64.     banksel TRISA ; Select to bank1
  65.     movlw 0x06
  66.     movwf ADCON1
  67.     clrf TRISA
  68.     clrf TRISD
  69.     clrf TRISE;TEST
  70.     movlw b'11111111'
  71.     movwf TRISB
  72.     ;####################
  73.     ;# Interupt Configure
  74.     ;####################
  75.     ;Timer 2 Configure
  76.     bsf PIE1,TMR2IE
  77.     movlw VAL_TMR2_INIT
  78.     movwf PR2
  79.  
  80.  
  81.     banksel PORTA ; Select to bank0
  82.     bsf PORTE,0
  83.     bsf PORTE,1
  84.     movlw .0
  85.     movwf ADDR_POLL
  86.  
  87.     ;Global & Timer 0 Interupt Configure
  88.     movlw b'11000000'
  89.     movwf INTCON
  90.     ; <5> TMR0 Overflow Interrupt Enable bit
  91.  
  92.     ;##### Timer 2 Configuration
  93.     movlw b'01111111'
  94.     movwf T2CON
  95.     clrf TMR2 ; Timer 2 的X
  96.    
  97.     bcf PIR1,TMR2IF
  98.  
  99.     ;##### Other
  100.     clrf ADDR_CLOCK_SEC
  101.    
  102.     movlw b'11000000'
  103.     ;movwf ADDR_DISP
  104.     ;call bcd_to_segment
  105.     ;movf ADDR_DISP,0
  106.     movwf ADDR_SEG_DIG0
  107.     movwf ADDR_SEG_DIG1
  108.     movwf ADDR_SEG_DIG2
  109.     movwf ADDR_SEG_DIG3
  110.     movwf ADDR_SEG_DIG4
  111.     movwf ADDR_SEG_DIG5
  112.     ;movlw b'10110011'
  113.     movwf ADDR_SEG_DIG6
  114.     movwf ADDR_SEG_DIG7
  115.    
  116.     movlw VAL_TMR2_DIV
  117.     movwf ADDR_TMR2_DIV
  118. ;
  119. ;************ Main *********************
  120. ;
  121. start:
  122.     ;call test
  123.     ;call task_button ;按鈕工作
  124.     call task_display_time ;轉換工作 (BCD to Seg Code)
  125.    ; call task_running_indcator
  126.     call digi_polling ;掃描顯示工作 (Invoked to TMR0)
  127.  
  128.     call delay_1ms ; Call delay routine
  129.     goto start
  130.  
  131.    
  132. task_display_time: ;顯示時間至7段
  133.     ;更新秒
  134.     movf ADDR_CLOCK_SEC,0
  135.  
  136.     call val_to_segment
  137.     movf ADDR_SOU,0
  138.     movwf ADDR_SEG_DIG0
  139.     movf ADDR_TEN,0
  140.     movwf ADDR_SEG_DIG1
  141.     return
  142.    
  143.  
  144. val_to_segment: ;原值轉 7 段顯示碼
  145.     movwf ADDR_SOU
  146.     call BCD
  147.    
  148.     movf ADDR_SOU,0 ;個位轉SEG
  149.     movwf ADDR_DISP
  150.     call bcd_to_segment
  151.     movwf ADDR_SOU
  152.    
  153.     movf ADDR_TEN,0 ;十位轉SEG
  154.     movwf ADDR_DISP
  155.     call bcd_to_segment
  156.     movwf ADDR_TEN
  157.     return
  158.  
  159. digi_polling: ;掃描顯示
  160.     ;Polling the digits pin
  161.     incf ADDR_POLL
  162.     movlw VAL_SEG_LEN
  163.     subwf ADDR_POLL,W
  164.     btfss STATUS,C
  165.         goto digi_polling_fin
  166.     clrf ADDR_POLL
  167.  
  168.     digi_polling_fin:
  169.     movlw b'00111111'
  170.     movwf PORTA
  171.     movlw b'00000011'
  172.     movwf PORTE
  173.  
  174.     movf ADDR_POLL,0
  175.     addwf ADDR_POLL,0
  176.     addwf ADDR_POLL,0
  177.     addwf PCL
  178.  
  179.     movf ADDR_SEG_DIG0,0
  180.     bcf PORTE,1
  181.     goto seg_out
  182.  
  183.     movf ADDR_SEG_DIG1,0
  184.     bcf PORTE,0
  185.     goto seg_out
  186.  
  187.     movf ADDR_SEG_DIG2,0
  188.     bcf PORTA,5
  189.     goto seg_out
  190.  
  191.     movf ADDR_SEG_DIG3,0
  192.     bcf PORTA,4
  193.     goto seg_out
  194.  
  195.     movf ADDR_SEG_DIG4,0
  196.     bcf PORTA,3
  197.     goto seg_out
  198.  
  199.     movf ADDR_SEG_DIG5,0
  200.     bcf PORTA,2
  201.     goto seg_out
  202.  
  203.     movf ADDR_SEG_DIG6,0
  204.     bcf PORTA,1
  205.     goto seg_out
  206.  
  207.     movf ADDR_SEG_DIG7,0
  208.     bcf PORTA,0
  209.     goto seg_out
  210.  
  211.     seg_out:
  212.     movwf PORTD
  213.  
  214.     return
  215.    
  216. bcd_to_segment: ;BCD 轉 7 段碼
  217.     ;Edge Check
  218.     movlw ADDR_DISP
  219.     sublw .9
  220.     btfsc STATUS,0
  221.     return
  222.     movf ADDR_DISP,0
  223.     addwf PCL
  224.     retlw b'11000000' ;0
  225.     retlw b'11111001' ;1
  226.     retlw b'10100100' ;2
  227.     retlw b'10110000' ;3
  228.     retlw b'10011001' ;4
  229.     retlw b'10010010' ;5
  230.     retlw b'10000010' ;6
  231.     retlw b'11111000' ;7
  232.     retlw b'10000000' ;8
  233.     retlw b'10010000' ;9
  234.    
  235. BCD: ;轉 BCD 碼
  236.     clrf ADDR_TEN
  237.     BCD_TEN:
  238.     movlw .10
  239.     subwf ADDR_SOU,0
  240.     btfss STATUS,0
  241.     return
  242.     movwf ADDR_SOU
  243.     incf ADDR_TEN,1
  244.     goto BCD_TEN
  245.  
  246. INTSUB:
  247.     ;Backup
  248.     movwf ADDR_TEMP_W
  249.     swapf STATUS,W
  250.     movwf ADDR_TEMP_STATUS
  251.    
  252.     btfss PIR1,TMR2IF
  253.         goto INTSUB_RESTORE
  254.     T0A:
  255.     ;軟除頻
  256.         decfsz ADDR_TMR2_DIV
  257.         goto T0_RESTORE
  258.     movlw VAL_TMR2_DIV
  259.     movwf ADDR_TMR2_DIV
  260.    
  261.     T0_INCREASE: ;開始遞增
  262.     incf ADDR_CLOCK_SEC
  263.     movlw .6
  264.     subwf ADDR_CLOCK_SEC,w
  265.     btfsc STATUS,Z
  266.         clrf ADDR_CLOCK_SEC
  267.        
  268.     T0_RESTORE:
  269.         bcf PIR1,TMR2IF
  270.         clrf TMR2
  271.         goto INTSUB_RESTORE
  272.  
  273.     INTSUB_RESTORE:
  274.         swapf ADDR_TEMP_STATUS,W
  275.         movwf STATUS
  276.         swapf ADDR_TEMP_W,F
  277.         swapf ADDR_TEMP_W,W
  278.         RETFIE
  279.  
  280. ;-------- 100 ms delay routine ----------
  281. delay_100ms:
  282. movlw VAL_MS
  283. movwf count_ms
  284.     loop_ms
  285.     call delay_1ms
  286.     decfsz count_ms,f
  287.     goto loop_ms
  288.     return
  289.    
  290. ;-------- 1 ms delay routine -----------
  291. delay_500us:
  292.     movlw VAL_500u
  293.     movwf count_500u
  294.     dec_loop_500u
  295.     call D_short
  296.     decfsz count_500u,f
  297.     goto dec_loop_500u
  298.     return
  299.    
  300. ;-------- 1 ms delay routine -----------
  301. delay_1ms:
  302.     movlw VAL_US
  303.     movwf count
  304.     dec_loop
  305.     call D_short
  306.     decfsz count,f
  307.     goto dec_loop
  308.     return
  309. ;
  310. ;--------     Delay 5uS    ------------- 25ms
  311. D_short
  312.     call D_ret ; 1uS * 4 + 1uS = 10uS
  313.     call D_ret
  314.     call D_ret
  315.     call D_ret
  316.     nop
  317.     nop
  318.     D_ret return
  319. ;
  320. end
Add Comment
Please, Sign In to add comment