giovedì 26 gennaio 2012

Template di un progetto Mplab per la scrittura di firmware con il PIC16F1827

Datasheet del PIC16F1827
Dispense del prof. Giulio Vitale sul PIC16F887 (gennaio 2010)


- Scaricare ed installare Mplab

Creazione di un nuovo progetto con Mplab
- project wizard
- scegliere come micro il PIC16F1827
- creare una nuova cartella e dare un nuovo nome al progetto (fare attenzione)
- finito il wizard, creare un nuovo file e salvarlo immediatamente assegnandogli un nome ed estensione .asm (facenbdo attenzione alla cartella di salvataggio, il file creato va aggiunto al progetto) oppure aggiungere un file template esistente.

 ATTENZIONE! tutti i file relativi al progetto devono trovarsi all'interno della cartella creata

- Personalizzare il workspace

Scrittura del firmware:
- Scrivere un firmware per testare i 4 led e i due pulsanti
usare come traccia il seguente template

Firmware per testare il circuito v1.0.1



;*****************************************************************************************
;
;   Project:    PIC16F1827_ASM_Template1
;   Filename:   main.asm
;   Date:       19.01.12
;   Version:    1.0.2
;
;   Author:     Filippo Bilardo
;
;*****************************************************************************************
;   <inserire una breve descrizione del progetto>
;
;*****************************************************************************************
;   <Revision History>
;   <Descrivere per ogni revisione o cambio di versione le modifiche fatte>
;
;*****************************************************************************************

    list        p=16f1827      ; list directive to define processor
    #include    <p16f1827.inc> ; processor specific variable definitions

;-----------------------------------------------------------------------------------------
;
; CONFIGURATION WORD SETUP
;
; The 'CONFIG' directive is used to embed the configuration word within the
; .asm file. The lables following the directive are located in the respective
; .inc file.  See the data sheet for additional information on configuration
; word settings.
;
;-----------------------------------------------------------------------------------------

    __CONFIG _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_ON & _CP_OFF & _CPD_OFF & _BOREN_OFF & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF
    __CONFIG _CONFIG2, _WRT_OFF & _PLLEN_OFF & _STVREN_OFF & _BORV_19 & _LVP_OFF

;-----------------------------------------------------------------------------------------
; VARIABLE DEFINITIONS
;-----------------------------------------------------------------------------------------
tmp1                EQU    0x21    ; variabile temporanea1
tmp2                EQU    0x22    ; variabile temporanea2
tmp3                EQU    0x23    ; variabile temporanea3

;-----------------------------------------------------------------------------------------
; CONSTANT DEFINITIONS
;-----------------------------------------------------------------------------------------
; Costanti relative all'oscillatore interno
;                         /------------    SPLLEN: 1 = 4x PLL Is enabled
;                         |////--------    IRCF3:IRCF0: Internal Oscillator Frequency Select bits
;                         |||||             1101 = 4MHz HF
;                         |||||             1110 = 8 MHz or 32 MHz HF
;                         |||||             1111 = 16 MHz HF
;                         ||||| //----- SCS<1:0>: System Clock Select bits
;                         ||||| ||          1x = Internal oscillator block
OSCCON_CFG          EQU B'01101010'
OSCTUNE_CFG         EQU B'00000000'

;-----------------------------------------------------------------------------------------
; RESET VECTOR
;-----------------------------------------------------------------------------------------
        ORG     0x000       ; processor reset vector
        goto    main        ; go to beginning of program
        ORG     0x004       ; interrupt vector location
        goto    isr

;-----------------------------------------------------------------------------------------
; ROUTINE
;-----------------------------------------------------------------------------------------
;-----------------------------------------------------------------------------------------
; VERSIONE:         1.0.0
; ULTIMA MODIFICA:  28.03.12
; INPUT:            NONE
; OUTPUT:           NONE
; inizializzazione dell'oscillatore interno
;-----------------------------------------------------------------------------------------
osc_init
        movlb   1
        movlw   OSCCON_CFG
        movwf   OSCCON
        ;movlb  1
        movlw   OSCTUNE_CFG
        movwf   OSCTUNE
        return
        ;GLOBAL osc_init
;-----------------------------------------------------------------------------------------
; VERSIONE:         1.0.1
; ULTIMA MODIFICA:  18.01.12
; INPUT:            NONE
; OUTPUT:           NONE
; ritardo 1ms (quando il clock è a 4MHz)
;-----------------------------------------------------------------------------------------
ritardo_1ms
        return

;-----------------------------------------------------------------------------------------
; VERSIONE:         1.0.1
; ULTIMA MODIFICA:  18.01.12
; INPUT:            NONE
; OUTPUT:           NONE
; ritardo 100ms (quando il clock è a 4MHz)
; ritardo = a+y+b = 8+tmp1*(3+1+3*tmp2)-1 = 7+tmp1*(4+3*tmp2) =
; ritardo = a+y+b = 8+1887d = 100485us
;-----------------------------------------------------------------------------------------
ritardo_100ms
        return              ; b = 2us

;-----------------------------------------------------------------------------------------
; VERSIONE:         1.0.2
; ULTIMA MODIFICA:  18.01.12
; INPUT:            NONE
; OUTPUT:           NONE
; Inizializzazione dei pin RB0 e RB5 della portaB
; ai quali sono collegati i pulsanti P1 e P2
; e impostazione come digitale il pin RB5
;-----------------------------------------------------------------------------------------
puls_init
        return

;-----------------------------------------------------------------------------------------
; VERSIONE:         1.0.2
; ULTIMA MODIFICA:  18.01.12
; INPUT:            NONE
; OUTPUT:           NONE
; Test dei due pulsanti. Viene letto la stato dei led per il successivo ripristino al
; termine della routine. Alla pressione di P1 vengono accesi led3 e led2 alla pressione
; di P2 vengono accesi led1 e led0.
;-----------------------------------------------------------------------------------------
puls_test
        return

;-----------------------------------------------------------------------------------------
; VERSIONE:         1.0.2
; ULTIMA MODIFICA:  18.01.12
; INPUT:            NONE
; OUTPUT:           NONE
; Inizializzazione dei pin ai quali sono collegati i 4 led
; i pin sono impostati come output
;-----------------------------------------------------------------------------------------
led_init
        return

;-----------------------------------------------------------------------------------------
; VERSIONE:         1.0.2
; ULTIMA MODIFICA:  18.01.12
; INPUT:            NONE
; OUTPUT:           NONE
; Accensione del led0
;-----------------------------------------------------------------------------------------
led0_on
        return
;-----------------------------------------------------------------------------------------
; VERSIONE:         1.0.2
; ULTIMA MODIFICA:  18.01.12
; INPUT:            NONE
; OUTPUT:           NONE
; Accensione del led1
;-----------------------------------------------------------------------------------------
led1_on
        return
;-----------------------------------------------------------------------------------------
; VERSIONE:         1.0.2
; ULTIMA MODIFICA:  18.01.12
; INPUT:            NONE
; OUTPUT:           NONE
; Accensione del led2
;-----------------------------------------------------------------------------------------
led2_on
        return
;-----------------------------------------------------------------------------------------
; VERSIONE:         1.0.2
; ULTIMA MODIFICA:  18.01.12
; INPUT:            NONE
; OUTPUT:           NONE
; Accensione del led3
;-----------------------------------------------------------------------------------------
led3_on
        return
;-----------------------------------------------------------------------------------------
; VERSIONE:         1.0.2
; ULTIMA MODIFICA:  18.01.12
; INPUT:            NONE
; OUTPUT:           NONE
; Spegnimento del led0
;-----------------------------------------------------------------------------------------
led0_off
        return
;-----------------------------------------------------------------------------------------
; VERSIONE:         1.0.2
; ULTIMA MODIFICA:  18.01.12
; INPUT:            NONE
; OUTPUT:           NONE
; Spegnimento del led1
;-----------------------------------------------------------------------------------------
led1_off
        return
;-----------------------------------------------------------------------------------------
; VERSIONE:         1.0.2
; ULTIMA MODIFICA:  18.01.12
; INPUT:            NONE
; OUTPUT:           NONE
; Spegnimento del led2
;-----------------------------------------------------------------------------------------
led2_off
        return
;-----------------------------------------------------------------------------------------
; VERSIONE:         1.0.2
; ULTIMA MODIFICA:  18.01.12
; INPUT:            NONE
; OUTPUT:           NONE
; Spegnimento del led3
;-----------------------------------------------------------------------------------------
led3_off
        return
;*****************************************************************************************
; VERSIONE:         1.0.2
; ULTIMA MODIFICA:  18.01.12
; INPUT:            NONE
; OUTPUT:           NONE
; Accensione di tutti i led
;*****************************************************************************************
led_on
        return
;-----------------------------------------------------------------------------------------
; VERSIONE:         1.0.2
; ULTIMA MODIFICA:  18.01.12
; INPUT:            NONE
; OUTPUT:           NONE
; Spegnimento di tutti i led
;-----------------------------------------------------------------------------------------
led_off
        return
;-----------------------------------------------------------------------------------------
; VERSIONE:         1.0.2
; ULTIMA MODIFICA:  18.01.12
; INPUT:            NONE
; OUTPUT:           W = stato attuale dei led
; lettura dello stato attuale dei led
;-----------------------------------------------------------------------------------------
led_read
        return
;-----------------------------------------------------------------------------------------
; VERSIONE:         1.0.2
; ULTIMA MODIFICA:  18.01.12
; INPUT:            W = led da accendere o spegnere
; OUTPUT:           NONE
; Accensione dei led in base al valore passato in W. Es. W=1100 -> vengono accesi led3
; e led2 e spenti led1 e led0; w=0000 -> tutti i led vengono spenti
;-----------------------------------------------------------------------------------------
led_write
        return
;-----------------------------------------------------------------------------------------
; VERSIONE:         1.0.1
; ULTIMA MODIFICA:  11.01.12
; INPUT:            NONE
; OUTPUT:           NONE
; 4 lampeggi di tutti i led ad intervalli di 100ms
;-----------------------------------------------------------------------------------------
led_test
        return
;-----------------------------------------------------------------------------------------
; VERSIONE:         1.0.1
; ULTIMA MODIFICA:  11.01.12
; INPUT:
;   par1 = led da accendere e spegnere
;   par2 = numero di ripetizioni
; OUTPUT:           NONE
; Lampeggi dei led selezionati.
; TODO: parametrizzare anche il ritardo
;-----------------------------------------------------------------------------------------
led_blink
        return
;-----------------------------------------------------------------------------------------
; VERSIONE:         1.0.0
; ULTIMA MODIFICA:  26.10.11
; INPUT:            NONE
; OUTPUT:           NONE
; Routine per la gestione degli interrupt
;-----------------------------------------------------------------------------------------
isr
        ; isr code can go here or be located as a call subroutine elsewhere
        retfie      ; return from interrupt

;-----------------------------------------------------------------------------------------
; MAIN ROUTINE
;-----------------------------------------------------------------------------------------
main
        ; Inizializzazioni
        call    osc_init
        call    led_init
        call    puls_init

        ; Autodiagnostica
        call    led_test

mainloop:

        call    puls_test

        goto    mainloop

;-----------------------------------------------------------------------------------------
        END

Nessun commento:

Posta un commento