Advertisement
Guest User

pic sort

a guest
Feb 15th, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pic 16 3.17 KB | None | 0 0
  1. #include "p16f873a.inc"
  2.  
  3. ; CONFIG
  4. ; __config 0xFFFB
  5.  __CONFIG _FOSC_EXTRC & _WDTE_OFF & _PWRTE_OFF & _BOREN_ON & _LVP_ON & _CPD_OFF & _WRT_OFF & _CP_OFF
  6.    
  7. RES_VECT  CODE    0x0000            ; processor reset vector
  8.  
  9. STATUS  equ 03h
  10. porta   equ 05h
  11. trisa   equ 85h
  12. portb   equ 06h
  13. trisb   equ 86h
  14. FSR equ 04h
  15. INDF    equ 00h
  16. full    equ 7Fh
  17. aux equ 7Eh
  18. aux1    equ 7Dh
  19. i   equ 7Ch
  20. j   equ 7Bh
  21. min equ 7Ah
  22. tmp equ 79h
  23. num1    equ 78h
  24. num2    equ 77h
  25.    
  26.     GOTO    START                   ; go to beginning of program
  27.  
  28. ; TODO ADD INTERRUPTS HERE IF USED
  29.  
  30.  
  31.    
  32.    
  33. MAIN_PROG CODE                      ; let linker place main program
  34.  
  35. START
  36. ;SETUP
  37.     bsf STATUS, 5           ;Entrando no banco 1
  38.     movlw   b'01111111'
  39.     movwf   trisb           ;Seta os pinos RB1 a RB7 como input
  40.     movlw   b'11111111'        
  41.     movwf   trisa           ;Seta todos os pinos do banco a como input
  42.                     ;Medida de seguranca, recomendando pela
  43.                     ;microchip
  44.     bcf STATUS, 5           ;Voltando ao banco 0
  45.    
  46. ;Codigo
  47.     MOVLW 20h ;Endereço inicial para array
  48.     MOVWF FSR
  49.     MOVWF full ;Para ele iterar 32 vezes
  50.    
  51.     MOVF portb,w ;Lê porta b
  52.     MOVWF aux  ;e joga o valor em aux
  53.    
  54.     Filll:
  55.     MOVWF INDF ;Coloca valor na lido na posição de memória sendo indicada pelo FSR
  56.     INCF FSR   ;Passa para o endereço da frente
  57.    
  58.     DECFSZ full ;Decrementa full e verifica se ja foi os 32 elementos
  59.     GOTO LerP ;Vai ler o próximo elemento
  60.     GOTO Sort ;Passa para o método sort se sim
  61.    
  62.     LerP:
  63.     MOVF portb,w
  64.     MOVWF aux1 ;Guarda novo valor em aux1 para não perder
  65.     ANDWF aux ;Compara valor que foi lido com o valor anterior, se igual: w = 11111111
  66.     MOVWF aux
  67.     INCF aux,0 ;Incrementa aux e joga em w, se igual, w valendo 0 agora
  68.     BTFSC STATUS, 2 ;Verifica bit zero para verificar resultado anterior e pula caso for 0
  69.     GOTO LerP ;Valor lido igual o anterior, tenta ler de novo
  70.     MOVF aux1,w ;Joga o novo valor guardado anteriormente lido de volta a W
  71.     GOTO Filll ;Passa para o próximo elemento
  72.    
  73.     Sort:
  74.     MOVLW 20h
  75.     MOVWF i
  76.    
  77.     fori:
  78.     MOVF i,w
  79.     MOVWF min
  80.    
  81.     MOVWF j
  82.     INCF j
  83.    
  84.     forj:
  85.     ;num[j]
  86.     MOVF j,w
  87.     MOVWF FSR
  88.     MOVF INDF,w
  89.     MOVWF num1
  90.    
  91.     ;num[min]
  92.     MOVF min,w
  93.     MOVWF FSR
  94.     MOVF INDF,w
  95.     ;MOVWF num2
  96.    
  97.     ;if num1[j] < num[min]
  98.     SUBWF num1,w
  99.     BTFSS STATUS,0
  100.     GOTO Eforj
  101.     MOVF j,w
  102.     MOVWF min
  103.     Eforj:
  104.     INCF j
  105.     MOVLW 40h ;30 (62)
  106.     SUBWF j,w
  107.     BTFSS STATUS,0
  108.     GOTO forj
  109.    
  110.     ;if (i != min)
  111.     MOVF i,w
  112.     ANDWF min,w
  113.     MOVWF aux
  114.     INCF aux,w
  115.     BTFSC STATUS, 2
  116.     GOTO Efori
  117.     ;tmp = num[i]
  118.     MOVF i,w
  119.     MOVWF FSR
  120.     MOVF INDF,w
  121.     MOVWF tmp
  122.    
  123.     ;num1 = num[min]
  124.     MOVF min,w
  125.     MOVWF FSR
  126.     MOVF INDF,w
  127.     MOVWF num1
  128.    
  129.     ;num[i] = num[min]
  130.     MOVF i,w
  131.     MOVWF FSR
  132.     MOVF num1,w
  133.     MOVWF INDF
  134.    
  135.     ;num[min] = aux
  136.     MOVF min,w
  137.     MOVWF FSR
  138.     MOVF tmp,w
  139.     MOVWF INDF
  140.    
  141.     Efori:
  142.     INCF i
  143.     MOVLW 3Fh ;29 (61)
  144.     SUBWF i,w
  145.     BTFSS STATUS,0
  146.     GOTO fori
  147.    
  148.     MOVF i
  149.    END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement