[ASM] Programando un PIC (Control motor paso a paso)

Iniciado por kal23, Enero 23, 2014, 05:52:00 PM

Tema anterior - Siguiente tema

0 Miembros y 1 Visitante están viendo este tema.

Enero 23, 2014, 05:52:00 PM Ultima modificación: Noviembre 11, 2014, 08:48:47 PM por ANTRAX
Viendo el post que hizo "mr.blood" sobre programación ASM para PIC, quise colaborar aportando algunos programas sencillos.

Este proyecto consiste en controlar 2 motores paso a paso desde un pic 16f690, El sistema de control consiste en pulsadores para mover los motores en diferentes sentidos y sensores para no sobrepasar el limite del eje. Lo hice de esta manera porque use este programa para controlar el brazo de una CNC. También tiene una modalidad de prueba automática, que se activa presionando los 4 pulsadores al mismo tiempo.

Espero que sea de su utilidad.

Código: asm


list p=16f690 ;Especifica microcontrolador
#include <P16F690.inc>

;Opciones de configuración
     __config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF)

; '__CONFIG' se utiliza para acceder a opciones de configuración desde un archivo .asm
; Las etiquetas a que se hace referencia están definidas en el archivo .inc correspondiente
; Refiérase a la hoja de datos para conocer la función de casa etiqueta

;ZONA DE DECLARACIÓN DE VARIABLES
;********************************
    cblock 0x20
Delay1                   ;Define 2 variables para retardo
Delay2                   ;en las posiciones 0x20 y 0x21
     endc

;INICIALIZACION
;********************************
     ORG 0x00 ;vector de reset

     CLRF    STATUS ;Selecciona página #1

     bsf       STATUS,RP0     ; Abre página de registro 1
     movlw     b'11011111'
     movwf     TRISA          ; A5 salida, el resto entradas

     movlw     0xF0
     movwf     TRISB          ; PORTB será sólo entradas

     clrf      TRISC          ; PORTC será sólo salidas

     bcf       STATUS,RP0     ; Abre página de registro 2
     bsf       STATUS,RP1
     movlw     0x00           ; Todos los pines de PortA serán entradas digitales
     movwf     ANSEL
     movlw     0x00           ; Todos los pines de PortB serán entradas digitales
     movwf     ANSELH
     bcf       STATUS,RP0     ; Regresa a página de registro 0
     bcf       STATUS,RP1


;INSTRUCCIONES
;------------------------------------------------
main:                         ;inicio
    clrf PORTA                ;Iniciar en 0 Puerto A
    clrf PORTB                ;Iniciar en 0 Puerto B
    clrf PORTC                ;Iniciar en 0 Puerto C
test:                   ;En esta subrutina se verificara si los 4 entradas están activas
    BTFSS       PORTA,0 ;Si Port A0 = 0 salta a la etiqueta up, si es 1 verificara el estado de A1
    goto        up
    BTFSS       PORTA,1 ;Si Port A1 = 0 salta a la etiqueta up, si es 1 verificara el estado de A2
    goto        up
    BTFSS       PORTA,2 ;Si Port A2 = 0 salta a la etiqueta up, si es 1 verificara el estado de A4
    goto        up
    BTFSS       PORTA,4 ;Si Port A4 = 0 salta a la etiqueta up, si es 1 verificara salta a la  subrutina Test_Motor
    goto        up
    goto        Test_Motor
up:
    BTFSS       PORTA,0       ;Si PORT A0 es 1 salta una linea, si es 0 lee la siguiente
    goto        down          ;Ir a etiqueta Down
    BTFSS       PORTB,4       ;Si Port B4 esta en 0 leerá linea siguiente
                              ;Sistema de seguridad asociado a Sensor 1
    goto        Motor_Up      ;Ir a subrutina Motor_Up
    goto        main          ;Volver a etiqueta main
down:
    BTFSS       PORTA,1       ;Si PORT A1 es 1 salta una linea
    goto        left
    BTFSS       PORTB,5
    goto        Motor_Down
    goto        main
left:
    BTFSS       PORTA,2       ;Si PORT A2 es 1 salta una linea
    goto        right
    BTFSS       PORTB,6
    goto        Motor_Left
    goto        main
right:
    BTFSS       PORTA,4       ;Si PORT A4 es 1 salta una linea
    goto        main
    BTFSS       PORTB,7
    goto        Motor_Right
    goto        main

;Subrutinas
;--------------------------------------------------

Motor_Up:  ;Con esa subrutina el motor 1 gira hacia la izquierda
    movlw    b'00000011'
    movwf      PORTC
    bsf        PORTA,5        ;Enciende led indicador
    call    Retardo_100ms
    movlw    b'00000110'
    movwf      PORTC
    call    Retardo_100ms
    movlw    b'00001100'
    movwf      PORTC
    call    Retardo_100ms
    movlw    b'00001001'
    movwf      PORTC
    bcf        PORTA,5        ;Apaga led indicador
    call    Retardo_100ms
    goto    main

Motor_Down: ;Con esa subrutina el motor 1 gira hacia la derecha
    movlw    b'00001100'
    movwf      PORTC
    bsf        PORTA,5        ;Enciende led indicador
    call    Retardo_100ms
    movlw    b'00000110'
    movwf      PORTC
    call    Retardo_100ms
    movlw    b'00000011'
    movwf      PORTC
    call    Retardo_100ms
    movlw    b'00001001'
    movwf      PORTC
    bcf        PORTA,5        ;Apaga led indicador
    call    Retardo_100ms
    goto    main

Motor_Left: ;Con esa subrutina el motor 2 gira hacia la izquierda
    movlw    b'00110000'
    movwf      PORTC
    bsf        PORTA,5        ;Enciende led indicador
    call    Retardo_100ms
    movlw    b'01100000'
    movwf      PORTC
    bcf        PORTA,5        ;Apaga led indicador
    call    Retardo_100ms
    movlw    b'11000000'
    movwf      PORTC
    bsf        PORTA,5        ;Enciende led indicador
    call    Retardo_100ms
    movlw    b'10010000'
    movwf      PORTC
    bcf        PORTA,5        ;Apaga led indicador
    call    Retardo_100ms
    goto    main

Motor_Right: ;Con esa subrutina el motor 1 gira hacia la derecha
    movlw    b'11000000'
    movwf      PORTC
    bsf        PORTA,5        ;Enciende led indicador
    call    Retardo_100ms
    movlw    b'01100000'
    movwf      PORTC
    bcf        PORTA,5        ;Apaga led indicador
    call    Retardo_100ms
    movlw    b'00110000'
    movwf      PORTC
    bsf        PORTA,5        ;Enciende led indicador
    call    Retardo_100ms
    movlw    b'10010000'
    movwf      PORTC
    bcf        PORTA,5        ;Apaga led indicador
    call    Retardo_100ms
    goto    main

Test_Motor:

    bsf        PORTA,5        ;Enciende led indicador
    Motor1_Up:                ;Motor 1 girara hacia la izquierda
    movlw    b'00000011'
    movwf      PORTC
    call    Retardo_100ms
    movlw    b'00000110'
    movwf      PORTC
    call    Retardo_100ms
    movlw    b'00001100'
    movwf      PORTC
    call    Retardo_100ms
    movlw    b'00001001'
    movwf      PORTC
    call    Retardo_100ms
    BTFSS       PORTB,4     ;Si Port B4 = 0, lee la linea siguiente, si es 1 Salta una linea
    goto Motor1_Up   ;Si Port B4 (Sensor1) es 0, vuelve a leer la subrutina Motor1_Up
    goto        Motor1_Down ;Si Port B4 es 1 saltara a la subrutina Motor1_Down
    Motor1_Down:            ;Motor 1 girara hacia la derecha
    movlw    b'00001100'
    movwf      PORTC
    call    Retardo_100ms
    movlw    b'00000110'
    movwf      PORTC
    call    Retardo_100ms
    movlw    b'00000011'
    movwf      PORTC
    call    Retardo_100ms
    movlw    b'00001001'
    movwf      PORTC
    call    Retardo_100ms
    BTFSS       PORTB,5     ;Si Port B5 = 0, lee la linea siguiente, si es 1 Salta una linea
    goto Motor1_Down ;Si Port B5 (Sensor2) es 0, vuelve a leer la subrutina Motor1_Down
    goto        Motor2_Left ;Si Port B5 es 1 saltara a la subrutina Motor1_Left
    Motor2_Left:            ;Motor 2 girara hacia la izquierda
    movlw    b'00110000'
    movwf      PORTC
    call    Retardo_100ms
    movlw    b'01100000'
    movwf      PORTC
    call    Retardo_100ms
    movlw    b'11000000'
    movwf      PORTC
    call    Retardo_100ms
    movlw    b'10010000'
    movwf      PORTC
    call    Retardo_100ms
    BTFSS       PORTB,6      ;Si Port B6 = 0, lee la linea siguiente, si es 1 Salta una linea
    goto Motor2_Left  ;Si Port B6 (Sensor3) es 0, vuelve a leer la subrutina Motor1_Left
    goto        Motor2_Right ;Si Port B6 es 1 saltara a la subrutina Motor1_Right
    Motor2_Right:            ;Motor 2 girara hacia la Derecha
    movlw    b'11000000'
    movwf      PORTC
    call    Retardo_100ms
    movlw    b'01100000'
    movwf      PORTC
    call    Retardo_100ms
    movlw    b'00110000'
    movwf      PORTC
    call    Retardo_100ms
    movlw    b'10010000'
    movwf      PORTC
    call    Retardo_100ms
    BTFSS       PORTB,7       ;Si Port B7 = 0, lee la linea siguiente, si es 1 Salta una linea
    goto Motor2_Right  ;Si Port B7 (Sensor4) es 0, vuelve a leer la subrutina Motor1_Right
    clrf        PORTC         ;Si Port B7 es 1 limpia Port C
    bcf         PORTA,5       ;Si Port B7 es 1 Apaga Led indicador
    goto        main          ;Si Port B7 es 1 Vuelve a la etiqueta main


    Include <RETARDOS.INC>  ;Incluimos la libreria llamada RETARDOS.INC
    END                 ;fin del programa


Adjunto el circuito esquemático para la simulación en proteus, de igual forma dejare al final del post el .DSN, las librerías que utilice y el .ASM



Link de descarga del proyecto completo: No tienes permitido ver los links. Registrarse o Entrar a mi cuenta