Bienvenue aux nouveaux arrivants sur FantasPic !
- Pensez à lire les règles durant votre visite, il n'y en a pas beaucoup, mais encore faut-il les respecter .
- N’hésitez pas à faire des remarques et/ou suggestions sur le Forum, dans le but de l'améliorer et de rendre vos prochaines visites plus agréables.
- Vous pouvez regarder votre "panneau de l'utilisateur" afin de configurer vos préférences.
- Un passage par "l'utilisation du forum" est recommandé pour connaître les fonctionnalités du forum.
--- L’équipe FantasPic ---
- Pensez à lire les règles durant votre visite, il n'y en a pas beaucoup, mais encore faut-il les respecter .
- N’hésitez pas à faire des remarques et/ou suggestions sur le Forum, dans le but de l'améliorer et de rendre vos prochaines visites plus agréables.
- Vous pouvez regarder votre "panneau de l'utilisateur" afin de configurer vos préférences.
- Un passage par "l'utilisation du forum" est recommandé pour connaître les fonctionnalités du forum.
--- L’équipe FantasPic ---
Modérateur : mazertoc
PIC18F56Q24 - MODULE SPI
-
Francois27100
Membre- Messages : 5
- Enregistré en : mars 2025
Bonjour à tous,
Je suis bloqué pour la programmation d'un PIC18F56Q24 pour programmer le module SPI.
J'utilise la carte PIC18F56Q24 CURIOSITY NANO
Et l'application MPLAB X IDE v6.20
Ci-après le code complet, Rien n'est connecté aux sorties, et j'ai un oscilloscope sur les sorties RB3 pour SD01 et RA5 pour le CS.
Il y a uniquement un signal sur RA5, démontrant que la boucle principale fonctionne et que le sous-programme d'envoi ne bloque pas.
Y a t'il quelqu'un qui peut tester le code sur son MCU pour me dire s'il a un signal ou pas ?
et/ou
un expert pour me dire s'il y a une erreur de paramétrage ou programmation.
Cela fait 3/4 semaines que je recherche avec les docs Microchip, les IA : Chatgpt, Claude, Perplexity, Gimini, (en version libre), CoPilot et rien ne va.
Par avance, merci de votre aide.
;*****************************************************************************
; Programme SPI utilisant le module SPI matériel du PIC18F56Q24
; - Pins utilisés:
; - SDO1 : RB3 (sortie données)
; - SCK1 : RC1 (horloge)
; - CS : RA5 (sélection manuelle du périphérique)
;*****************************************************************************
PROCESSOR 18F56Q24
; ************************** Paramétrage des registres de configuration *******
CONFIG FEXTOSC = ECH ; CONFIG1 Oscillateur 64 MHz (above 8MHz)
CONFIG RSTOSC = HFINTOSC_1MHZ; CONFIG1 Fréquence sélectionnée 4 MHz et CDIV = 4:1
CONFIG CSWEN = ON ; Commutation d'horloge permise
CONFIG FCMEN = ON ; Moniteur d'horloge activé
CONFIG WDTE = OFF ; CONFIG5 WatchDog OFF
CONFIG LVP = ON ; Programmation basse tension autorisée (ON/OFF)
CONFIG BOREN = OFF ; CONFIG3 Brown-OUT RESET DISABLED
CONFIG BORV = 0 ; Brown-out Reset Voltage (Vbor) sélection du niveau le plus bas
; ************************** Paramétrage des registre *************************
TRISF EQU 0x014D ; Bank1 - (0=Sortie - 1=Entrée)
TRISA EQU 0x0148
TRISB EQU 0x0149
TRISC EQU 0x014A
LATF EQU 0x0145
LATA EQU 0x0140
LATB EQU 0x0141
LATC EQU 0x0142
ANSELF EQU 0x0428 ; Bank4 - ANAL/NUM (0=Num - 1=Analog.)
ANSELA EQU 0x0400
ANSELB EQU 0x0408
ANSELC EQU 0x0410
WPUA EQU 0x0401
WPUB EQU 0x0409
WPUC EQU 0x0411
WPUF EQU 0x0429
WREG EQU 0x04E8
SPI1CON0 EQU 0x084 ; Bank0 - SPI Control Register 0
SPI1CON1 EQU 0x085 ; Bank0 - SPI Control Register 1
SPI1CON2 EQU 0x086 ; Bank0 - SPI Control Register 2
SPI1BAUD EQU 0x089 ; Bank0 - SPI Baud Rate Register (Pre-scaler)
SPI1CLK EQU 0x08C ; Bank0 - SPI Clock Selection Register
SPI1TWIDTH EQU 0x088 ; Bank0 - SPI Transfer Width Register [0-2]
SPI1TCNT EQU 0x082 ; Bank0 - SPI Transfer Counter Register [0-10]
SPI1TXB EQU 0x081 ; Bank0 - SPI Transmit Buffer
SPI1STATUS EQU 0x087 ; Bank0 - SPI Status Register
SPI1INTF EQU 0x8A ; Bank0 - SPI Interrupt Flag Register
SPI1INTE EQU 0x08B ; Bank0 - SPI Interrupt Enable Register
OSCFRQ EQU 0x0B1 ; Bank0 - HFINTOSC Frequency Selection Register
;SPI1 - pin allocation ???
RA5PPS EQU 0x0206 ; CS on pin RA5
RB2PPS EQU 0x20B ; SDO on pin RB2
RB3PPS EQU 0x020C ; SDO on pin RB3
RC1PPS EQU 0x0212 ; CLK on pin RC1
; Variables
PSECT udata
delay_cnt: DS 1 ; Compteur pour délai
; *****************************************************************************
PSECT code
ORG 0x0000 ; Adresse de départ
GOTO Main
; *****************************************************************************
NOP
SPI_Init:
BANKSEL TRISC
bcf TRISC, 1 ; RC1 en sortie (sck)
BANKSEL TRISB
bcf TRISB, 3 ; RB3 en sortie (sdo)
BANKSEL TRISB
bcf TRISB, 2 ; RB2 en sortie
BANKSEL TRISA
bcf TRISA, 5 ; RA5 en sortie (SS)
BANKSEL TRISF
bcf TRISF, 2 ; RF2 en sortie (LED)
; *****************************************************************************
BANKSEL ANSELC
bcf ANSELC, 1 ; RC1 en numérique (sck)
BANKSEL ANSELB
bcf ANSELB, 3 ; RB3 en numérique (sdo)
BANKSEL ANSELB
bcf ANSELB, 2 ; RB2 en numérique
BANKSEL ANSELA
bcf ANSELA, 5 ; RA5 en numérique (SS)
BANKSEL ANSELF
bcf ANSELF, 2 ; RF2 en numérique (LED)
; *****************************************************************************
BANKSEL WPUB
bcf WPUB, 3 ; Active le pull-up sur RB3 (SDO1)
BANKSEL WPUC
bcf WPUC, 1 ; Active le pull-up sur RC1 (SCK1)
; *****************************************************************************
BANKSEL LATA
bsf LATA, 5 ; CS désactivé (high)
BANKSEL LATF
bsf LATF, 2 ; LED off
; *****************************************************************************
BANKSEL RB2PPS
movlw 0x1E ; 0x1E = SDO1 (voir datasheet)
movwf RB2PPS ; Assigne SDO1 à RB2
BANKSEL RC1PPS
movlw 0x1D ; 0x1D = SCK1 (voir datasheet)
movwf RC1PPS ; Assigne SCK1 à RC1
; *****************************************************************************
BANKSEL SPI1CON0
movlw 0b10000011 ; 0BMODE=1, 1MST=1, 2LSBF=0, 7EN=1
movwf SPI1CON0
BANKSEL SPI1CON1
movlw 0b10000000 ; 7SMP=1
movwf SPI1CON1
BANKSEL SPI1CON2
bcf SPI1CON2, 2 ; 2TXR=0
BANKSEL SPI1CLK
movlw 0x00 ; FOSC/4 pour le générateur d'horloge
movwf SPI1CLK
BANKSEL OSCFRQ
movlw 0x00 ; 4MHz
movwf OSCFRQ
BANKSEL SPI1BAUD
movlw 0x0F ; -
movwf SPI1BAUD
BANKSEL SPI1TCNT
movlw 0x08 ; 8 bits
movwf SPI1TCNT
BANKSEL SPI1TWIDTH
movlw 0x08 ; -
movwf SPI1TWIDTH
BANKSEL SPI1INTE
movlw 0x00 ; Aucune interruption activée
movwf SPI1INTE
return
;*****************************************************************************
; Fonction de délai microseconde
;*****************************************************************************
Delay_1us:
nop
nop
nop
nop
return
;*****************************************************************************
; Fonction pour envoyer un octet via SPI
;*****************************************************************************
SPI_SendByte:
; Allumer la LED pendant la transmission
BANKSEL LATF
bcf LATF, 2
BANKSEL LATB
bcf LATB, 2
;
; Activez CS avant la transmission
BANKSEL LATA
bcf LATA, 5 ; Active CS (mise à 0)
; Placez les données dans le buffer
BANKSEL SPI1TXB
movwf SPI1TXB ; Place la donnée dans le buffer TX
;
; Attendez que la transmission soit complète
SPI_Wait:
BANKSEL SPI1STATUS
btfsc SPI1STATUS, 0 ; Vérifier si TXBE est à 1 (buffer de transmission vide)
goto SPI_Wait
;
; Désactivez CS après la transmission
BANKSEL LATA
bsf LATA, 5 ; Désactive CS (mise à 1)
;
; Etteindre la LED à la fin de la transmission
BANKSEL LATB
bsf LATB, 2
BANKSEL LATF
bsf LATF, 2
return
;*****************************************************************************
; Programme principal
;*****************************************************************************
Main:
; Configure l'oscillateur à 4MHz
call SPI_Init ; Initialise SPI
MainLoop:
nop
movlw 0x55
call SPI_SendByte
BANKSEL LATA
bcf LATA, 5 ; Active CS (mise à 0)
call Delay_1us
call Delay_1us
call Delay_1us
call Delay_1us
call Delay_1us
BANKSEL LATA
bsf LATA, 5 ; Active CS (mise à 0)
goto MainLoop
END
Je suis bloqué pour la programmation d'un PIC18F56Q24 pour programmer le module SPI.
J'utilise la carte PIC18F56Q24 CURIOSITY NANO
Et l'application MPLAB X IDE v6.20
Ci-après le code complet, Rien n'est connecté aux sorties, et j'ai un oscilloscope sur les sorties RB3 pour SD01 et RA5 pour le CS.
Il y a uniquement un signal sur RA5, démontrant que la boucle principale fonctionne et que le sous-programme d'envoi ne bloque pas.
Y a t'il quelqu'un qui peut tester le code sur son MCU pour me dire s'il a un signal ou pas ?
et/ou
un expert pour me dire s'il y a une erreur de paramétrage ou programmation.
Cela fait 3/4 semaines que je recherche avec les docs Microchip, les IA : Chatgpt, Claude, Perplexity, Gimini, (en version libre), CoPilot et rien ne va.
Par avance, merci de votre aide.
;*****************************************************************************
; Programme SPI utilisant le module SPI matériel du PIC18F56Q24
; - Pins utilisés:
; - SDO1 : RB3 (sortie données)
; - SCK1 : RC1 (horloge)
; - CS : RA5 (sélection manuelle du périphérique)
;*****************************************************************************
PROCESSOR 18F56Q24
; ************************** Paramétrage des registres de configuration *******
CONFIG FEXTOSC = ECH ; CONFIG1 Oscillateur 64 MHz (above 8MHz)
CONFIG RSTOSC = HFINTOSC_1MHZ; CONFIG1 Fréquence sélectionnée 4 MHz et CDIV = 4:1
CONFIG CSWEN = ON ; Commutation d'horloge permise
CONFIG FCMEN = ON ; Moniteur d'horloge activé
CONFIG WDTE = OFF ; CONFIG5 WatchDog OFF
CONFIG LVP = ON ; Programmation basse tension autorisée (ON/OFF)
CONFIG BOREN = OFF ; CONFIG3 Brown-OUT RESET DISABLED
CONFIG BORV = 0 ; Brown-out Reset Voltage (Vbor) sélection du niveau le plus bas
; ************************** Paramétrage des registre *************************
TRISF EQU 0x014D ; Bank1 - (0=Sortie - 1=Entrée)
TRISA EQU 0x0148
TRISB EQU 0x0149
TRISC EQU 0x014A
LATF EQU 0x0145
LATA EQU 0x0140
LATB EQU 0x0141
LATC EQU 0x0142
ANSELF EQU 0x0428 ; Bank4 - ANAL/NUM (0=Num - 1=Analog.)
ANSELA EQU 0x0400
ANSELB EQU 0x0408
ANSELC EQU 0x0410
WPUA EQU 0x0401
WPUB EQU 0x0409
WPUC EQU 0x0411
WPUF EQU 0x0429
WREG EQU 0x04E8
SPI1CON0 EQU 0x084 ; Bank0 - SPI Control Register 0
SPI1CON1 EQU 0x085 ; Bank0 - SPI Control Register 1
SPI1CON2 EQU 0x086 ; Bank0 - SPI Control Register 2
SPI1BAUD EQU 0x089 ; Bank0 - SPI Baud Rate Register (Pre-scaler)
SPI1CLK EQU 0x08C ; Bank0 - SPI Clock Selection Register
SPI1TWIDTH EQU 0x088 ; Bank0 - SPI Transfer Width Register [0-2]
SPI1TCNT EQU 0x082 ; Bank0 - SPI Transfer Counter Register [0-10]
SPI1TXB EQU 0x081 ; Bank0 - SPI Transmit Buffer
SPI1STATUS EQU 0x087 ; Bank0 - SPI Status Register
SPI1INTF EQU 0x8A ; Bank0 - SPI Interrupt Flag Register
SPI1INTE EQU 0x08B ; Bank0 - SPI Interrupt Enable Register
OSCFRQ EQU 0x0B1 ; Bank0 - HFINTOSC Frequency Selection Register
;SPI1 - pin allocation ???
RA5PPS EQU 0x0206 ; CS on pin RA5
RB2PPS EQU 0x20B ; SDO on pin RB2
RB3PPS EQU 0x020C ; SDO on pin RB3
RC1PPS EQU 0x0212 ; CLK on pin RC1
; Variables
PSECT udata
delay_cnt: DS 1 ; Compteur pour délai
; *****************************************************************************
PSECT code
ORG 0x0000 ; Adresse de départ
GOTO Main
; *****************************************************************************
NOP
SPI_Init:
BANKSEL TRISC
bcf TRISC, 1 ; RC1 en sortie (sck)
BANKSEL TRISB
bcf TRISB, 3 ; RB3 en sortie (sdo)
BANKSEL TRISB
bcf TRISB, 2 ; RB2 en sortie
BANKSEL TRISA
bcf TRISA, 5 ; RA5 en sortie (SS)
BANKSEL TRISF
bcf TRISF, 2 ; RF2 en sortie (LED)
; *****************************************************************************
BANKSEL ANSELC
bcf ANSELC, 1 ; RC1 en numérique (sck)
BANKSEL ANSELB
bcf ANSELB, 3 ; RB3 en numérique (sdo)
BANKSEL ANSELB
bcf ANSELB, 2 ; RB2 en numérique
BANKSEL ANSELA
bcf ANSELA, 5 ; RA5 en numérique (SS)
BANKSEL ANSELF
bcf ANSELF, 2 ; RF2 en numérique (LED)
; *****************************************************************************
BANKSEL WPUB
bcf WPUB, 3 ; Active le pull-up sur RB3 (SDO1)
BANKSEL WPUC
bcf WPUC, 1 ; Active le pull-up sur RC1 (SCK1)
; *****************************************************************************
BANKSEL LATA
bsf LATA, 5 ; CS désactivé (high)
BANKSEL LATF
bsf LATF, 2 ; LED off
; *****************************************************************************
BANKSEL RB2PPS
movlw 0x1E ; 0x1E = SDO1 (voir datasheet)
movwf RB2PPS ; Assigne SDO1 à RB2
BANKSEL RC1PPS
movlw 0x1D ; 0x1D = SCK1 (voir datasheet)
movwf RC1PPS ; Assigne SCK1 à RC1
; *****************************************************************************
BANKSEL SPI1CON0
movlw 0b10000011 ; 0BMODE=1, 1MST=1, 2LSBF=0, 7EN=1
movwf SPI1CON0
BANKSEL SPI1CON1
movlw 0b10000000 ; 7SMP=1
movwf SPI1CON1
BANKSEL SPI1CON2
bcf SPI1CON2, 2 ; 2TXR=0
BANKSEL SPI1CLK
movlw 0x00 ; FOSC/4 pour le générateur d'horloge
movwf SPI1CLK
BANKSEL OSCFRQ
movlw 0x00 ; 4MHz
movwf OSCFRQ
BANKSEL SPI1BAUD
movlw 0x0F ; -
movwf SPI1BAUD
BANKSEL SPI1TCNT
movlw 0x08 ; 8 bits
movwf SPI1TCNT
BANKSEL SPI1TWIDTH
movlw 0x08 ; -
movwf SPI1TWIDTH
BANKSEL SPI1INTE
movlw 0x00 ; Aucune interruption activée
movwf SPI1INTE
return
;*****************************************************************************
; Fonction de délai microseconde
;*****************************************************************************
Delay_1us:
nop
nop
nop
nop
return
;*****************************************************************************
; Fonction pour envoyer un octet via SPI
;*****************************************************************************
SPI_SendByte:
; Allumer la LED pendant la transmission
BANKSEL LATF
bcf LATF, 2
BANKSEL LATB
bcf LATB, 2
;
; Activez CS avant la transmission
BANKSEL LATA
bcf LATA, 5 ; Active CS (mise à 0)
; Placez les données dans le buffer
BANKSEL SPI1TXB
movwf SPI1TXB ; Place la donnée dans le buffer TX
;
; Attendez que la transmission soit complète
SPI_Wait:
BANKSEL SPI1STATUS
btfsc SPI1STATUS, 0 ; Vérifier si TXBE est à 1 (buffer de transmission vide)
goto SPI_Wait
;
; Désactivez CS après la transmission
BANKSEL LATA
bsf LATA, 5 ; Désactive CS (mise à 1)
;
; Etteindre la LED à la fin de la transmission
BANKSEL LATB
bsf LATB, 2
BANKSEL LATF
bsf LATF, 2
return
;*****************************************************************************
; Programme principal
;*****************************************************************************
Main:
; Configure l'oscillateur à 4MHz
call SPI_Init ; Initialise SPI
MainLoop:
nop
movlw 0x55
call SPI_SendByte
BANKSEL LATA
bcf LATA, 5 ; Active CS (mise à 0)
call Delay_1us
call Delay_1us
call Delay_1us
call Delay_1us
call Delay_1us
BANKSEL LATA
bsf LATA, 5 ; Active CS (mise à 0)
goto MainLoop
END
Vous n’avez pas les permissions nécessaires pour voir les fichiers joints à ce message.
PIC18F56Q24 - MODULE SPI
Bonjour,
Est ce que l'oscillo affiche le SCK sortant ?
On avait parlé avec Paul de ce module SPI ici viewtopic.php?t=1292&start=20
Avec BMODE=1, il faut mettre 0 dans SPI1TWIDTH pour indiquer qu'on envoie des mots de 8 bits et SPI1TCNT n'intervient pas, on peut quand même l'utiliser pour décompter les octets échangés.
Voir le réglage de TXR dans SPI1CON2, il semble que l'on doit le mettre à 1.
Est ce que l'oscillo affiche le SCK sortant ?
Là il y a un problème, SDO est sur RB2 ou RB3 ?Francois27100 a écrit :Source du message movwf RB2PPS ; Assigne SDO1 à RB2
On avait parlé avec Paul de ce module SPI ici viewtopic.php?t=1292&start=20
Avec BMODE=1, il faut mettre 0 dans SPI1TWIDTH pour indiquer qu'on envoie des mots de 8 bits et SPI1TCNT n'intervient pas, on peut quand même l'utiliser pour décompter les octets échangés.
Voir le réglage de TXR dans SPI1CON2, il semble que l'on doit le mettre à 1.
PIC18F56Q24 - MODULE SPI
-
Francois27100
Membre- Messages : 5
- Enregistré en : mars 2025
Bonjour,
Merci d'avoir pris le temps de regarder mon code.
Pour 'SCK' c'est pareil, aucun signal.
J'ai corrigé movwf RB2PPS avec RB3 (c'était un test de changer la sortie) : toujours pareil.
J'ai mis BMODE à 0 : toujours pareil.
J'ai mis SPI1TWIDTH à 0 ; toujours pareil.
TXR doit bien être à 0, mais j'ai testé à 1 et c'est toujours pareil.
Je vais étudier le topic que vous avez joint.
Avez-vous un topic sur comment visualiser avec "MPLAB Data Visualizer" ?
Cdt
François
Merci d'avoir pris le temps de regarder mon code.
Pour 'SCK' c'est pareil, aucun signal.
J'ai corrigé movwf RB2PPS avec RB3 (c'était un test de changer la sortie) : toujours pareil.
J'ai mis BMODE à 0 : toujours pareil.
J'ai mis SPI1TWIDTH à 0 ; toujours pareil.
TXR doit bien être à 0, mais j'ai testé à 1 et c'est toujours pareil.
Je vais étudier le topic que vous avez joint.
Avez-vous un topic sur comment visualiser avec "MPLAB Data Visualizer" ?
Cdt
François
PIC18F56Q24 - MODULE SPI
Bonjour,
Attention, TXR c'est le bit 1 de SPI1CON2, pas le 2. La non utilisation de movlw a fait très mal
Dans le datasheet, il est bien indiqué que si TXR et RXR sont à 0, il n'y a aucun transfert (Page 625). Le mode transmit only nécessite TXR=1 et RXR=0.
Ici on parle un peu du MPLAB Data Visualizer -> viewtopic.php?p=14193
J'ai tapé dans google -> "Fantaspic MPLAB Data Visualizer"
Attention, TXR c'est le bit 1 de SPI1CON2, pas le 2. La non utilisation de movlw a fait très mal

Dans le datasheet, il est bien indiqué que si TXR et RXR sont à 0, il n'y a aucun transfert (Page 625). Le mode transmit only nécessite TXR=1 et RXR=0.
Ici on parle un peu du MPLAB Data Visualizer -> viewtopic.php?p=14193
J'ai tapé dans google -> "Fantaspic MPLAB Data Visualizer"
PIC18F56Q24 - MODULE SPI
Bonjour Francois27100, satinas, et tout le forum,
la datasheet de ce pic serait la bienvenue, Peux tu fournir le fichier p18F56Q24.inc, car je ne le possède pas, pour tester ton programme,
C'est pour communiquer avec quelle module ?
Sinon il existe le SPI Big bang ......
Je sais quand peut le faire, mais pourquoi as tu déclaré toutes tes registres, alors qu'il suffit de mettre le fichier p18F56Q24.inc
A+

C'est pour communiquer avec quelle module ?
Sinon il existe le SPI Big bang ......
Je sais quand peut le faire, mais pourquoi as tu déclaré toutes tes registres, alors qu'il suffit de mettre le fichier p18F56Q24.inc

PIC18F56Q24 - MODULE SPI
-
Francois27100
Membre- Messages : 5
- Enregistré en : mars 2025
PIC18F56Q24 - MODULE SPI
-
Francois27100
Membre- Messages : 5
- Enregistré en : mars 2025
Bonjour,
MERCI, MERCI, MERCI, MERCI, MERCI, @satinas et à vous tous, j'ai des signaux en sortie,
Au scope : 55 suivi de 82
ci-dessous le code corrigé et fonctionnel
;*****************************************************************************
; Programme SPI utilisant le module SPI matériel du PIC18F56Q24
; - Pins utilisés:
; - SDO1 : RB3 (sortie données)
; - SCK1 : RC1 (horloge)
; - CS : RA5 (sélection manuelle du périphérique)
;*****************************************************************************
PROCESSOR 18F56Q24
; ************************** Paramétrage des registres de configuration *******
CONFIG FEXTOSC = ECH ; CONFIG1 Oscillateur 64 MHz (above 8MHz)
CONFIG RSTOSC = HFINTOSC_1MHZ ; CONFIG1 Fréquence sélectionnée 4 MHz et CDIV = 4:1
CONFIG CSWEN = ON ; Commutation d'horloge permise
CONFIG FCMEN = ON ; Moniteur d'horloge activé
CONFIG WDTE = OFF ; CONFIG5 WatchDog OFF
CONFIG LVP = ON ; Programmation basse tension autorisée (ON/OFF)
CONFIG BOREN = OFF ; CONFIG3 Brown-OUT RESET DISABLED
CONFIG BORV = 0 ; Brown-out Reset Voltage (Vbor) sélection du niveau le plus bas
; ************************** Paramétrage des registre *************************
TRISF EQU 0x014D ; Bank1 - (0=Sortie - 1=Entrée)
TRISA EQU 0x0148
TRISB EQU 0x0149
TRISC EQU 0x014A
LATF EQU 0x0145
LATA EQU 0x0140
LATB EQU 0x0141
LATC EQU 0x0142
ANSELF EQU 0x0428 ; Bank4 - ANAL/NUM (0=Num - 1=Analog.)
ANSELA EQU 0x0400
ANSELB EQU 0x0408
ANSELC EQU 0x0410
WPUA EQU 0x0401
WPUB EQU 0x0409
WPUC EQU 0x0411
WPUF EQU 0x0429
WREG EQU 0x04E8
SPI1CON0 EQU 0x084 ; Bank0 - SPI Control Register 0
SPI1CON1 EQU 0x085 ; Bank0 - SPI Control Register 1
SPI1CON2 EQU 0x086 ; Bank0 - SPI Control Register 2
SPI1BAUD EQU 0x089 ; Bank0 - SPI Baud Rate Register (Pre-scaler)
SPI1CLK EQU 0x08C ; Bank0 - SPI Clock Selection Register
SPI1TWIDTH EQU 0x088 ; Bank0 - SPI Transfer Width Register [0-2]
SPI1TCNT EQU 0x082 ; Bank0 - SPI Transfer Counter Register [0-10]
SPI1TXB EQU 0x081 ; Bank0 - SPI Transmit Buffer
SPI1STATUS EQU 0x087 ; Bank0 - SPI Status Register
SPI1INTF EQU 0x8A ; Bank0 - SPI Interrupt Flag Register
SPI1INTE EQU 0x08B ; Bank0 - SPI Interrupt Enable Register
OSCFRQ EQU 0x0B1 ; Bank0 - HFINTOSC Frequency Selection Register
PORTWCLK EQU 0x010A ; Bank1 - Signal Routing Port Clock Input Selection
;SPI1 - pin allocation ???
RA5PPS EQU 0x0206 ; CS on pin RA5
RB2PPS EQU 0x20B ; SDO on pin RB2
RB3PPS EQU 0x020C ; SDO on pin RB3
RC1PPS EQU 0x0212 ; CLK on pin RC1
RC3PPS EQU 0x0214 ; Test
; *****************************************************************************
PSECT code
ORG 0x0000 ; Adresse de départ
; *****************************************************************************
; SPI_Init:
; *****************************************************************************
BANKSEL TRISC
bcf TRISC, 1 ; RC1 en sortie (sck)
BANKSEL TRISC
bcf TRISC, 3 ; RC3 en sortie
BANKSEL TRISB
bcf TRISB, 3 ; RB3 en sortie (sdo)
BANKSEL TRISB
bcf TRISB, 2 ; RB2 en sortie
BANKSEL TRISA
bcf TRISA, 5 ; RA5 en sortie (SS)
BANKSEL TRISF
bcf TRISF, 2 ; RF2 en sortie (LED)
; *****************************************************************************
BANKSEL ANSELC
bcf ANSELC, 1 ; RC1 en numérique (sck)
BANKSEL ANSELC
bcf ANSELC, 1 ; RC3 en numérique
BANKSEL ANSELB
bcf ANSELB, 3 ; RB3 en numérique (sdo)
BANKSEL ANSELB
bcf ANSELB, 2 ; RB2 en numérique
BANKSEL ANSELA
bcf ANSELA, 5 ; RA5 en numérique (SS)
BANKSEL ANSELF
bcf ANSELF, 2 ; RF2 en numérique (LED)
; *****************************************************************************
BANKSEL WPUB
bcf WPUB, 3 ; DesActive le pull-up sur RB3 (SDO1)
BANKSEL WPUC
bcf WPUC, 1 ; DesActive le pull-up sur RC1 (SCK1)
BANKSEL WPUC
bcf WPUC, 1 ; DesActive le pull-up sur RC3
; *****************************************************************************
BANKSEL RB3PPS
movlw 0x1E ; 0x1E = SDO1 (voir datasheet)
movwf RB3PPS ; Assigne SDO1 à RB3
BANKSEL PORTWCLK
movlw 0b00001 ; HFINTOSC
movwf PORTWCLK ;
BANKSEL RC1PPS
movlw 0x1D ; 0x1D = SCK1 (voir datasheet)
movwf RC1PPS ; Assigne SCK1 à RC1
; *****************************************************************************
BANKSEL SPI1CON0
movlw 0b10000011 ; 0BMODE=1, 1MST=1, 2LSBF=0, 7EN=1
movwf SPI1CON0
BANKSEL SPI1CON1
movlw 0b10000000 ; 7SMP=1
movwf SPI1CON1
BANKSEL SPI1CON2
bsf SPI1CON2, 1 ; 2TXR=1
BANKSEL SPI1CLK
movlw 0x01 ; FOSC/4 pour le générateur d'horloge
movwf SPI1CLK
BANKSEL OSCFRQ
movlw 0x00 ; 4MHz
movwf OSCFRQ
BANKSEL SPI1BAUD
movlw 0x0F ; -
movwf SPI1BAUD
BANKSEL SPI1TCNT
movlw 0x08 ; 8 bits
movwf SPI1TCNT
BANKSEL SPI1TWIDTH
movlw 0x00 ; -
movwf SPI1TWIDTH
BANKSEL SPI1INTE
movlw 0x00 ; Aucune interruption activée
movwf SPI1INTE
Goto MainLoop
;*****************************************************************************
; Fonction pour envoyer un octet via SPI
;*****************************************************************************
SPI_SendByte:
; Allumer la LED pendant la transmission
BANKSEL LATF
bcf LATF, 2
; Placez les données dans le buffer
BANKSEL SPI1TXB
movwf SPI1TXB ; Place la donnée dans le buffer TX
; Attendez que la transmission soit complète
SPI_Wait:
BANKSEL SPI1STATUS
btfss SPI1STATUS, 5 ; Vérifier si TXBE est à 1 (buffer de transmission vide)
goto SPI_Wait
;
;
; Etteindre la LED à la fin de la transmission
BANKSEL LATF
bsf LATF, 2
return
;*****************************************************************************
; Programme principal
;*****************************************************************************
MainLoop:
nop
BANKSEL LATF
bcf LATF, 2 ; Active CS (mise à 0)
movlw 0x55
call SPI_SendByte
movlw 0x82
call SPI_SendByte
call Delay_1us
call Delay_1us
call Delay_1us
call Delay_1us
call Delay_1us
BANKSEL LATF
bsf LATF, 2 ; Active CS (mise à 0)
goto MainLoop
;*****************************************************************************
; Fonction de délai microseconde
;*****************************************************************************
Delay_1us:
nop
nop
nop
; nop
return
MERCI, MERCI, MERCI, MERCI, MERCI, @satinas et à vous tous, j'ai des signaux en sortie,
Au scope : 55 suivi de 82
ci-dessous le code corrigé et fonctionnel
;*****************************************************************************
; Programme SPI utilisant le module SPI matériel du PIC18F56Q24
; - Pins utilisés:
; - SDO1 : RB3 (sortie données)
; - SCK1 : RC1 (horloge)
; - CS : RA5 (sélection manuelle du périphérique)
;*****************************************************************************
PROCESSOR 18F56Q24
; ************************** Paramétrage des registres de configuration *******
CONFIG FEXTOSC = ECH ; CONFIG1 Oscillateur 64 MHz (above 8MHz)
CONFIG RSTOSC = HFINTOSC_1MHZ ; CONFIG1 Fréquence sélectionnée 4 MHz et CDIV = 4:1
CONFIG CSWEN = ON ; Commutation d'horloge permise
CONFIG FCMEN = ON ; Moniteur d'horloge activé
CONFIG WDTE = OFF ; CONFIG5 WatchDog OFF
CONFIG LVP = ON ; Programmation basse tension autorisée (ON/OFF)
CONFIG BOREN = OFF ; CONFIG3 Brown-OUT RESET DISABLED
CONFIG BORV = 0 ; Brown-out Reset Voltage (Vbor) sélection du niveau le plus bas
; ************************** Paramétrage des registre *************************
TRISF EQU 0x014D ; Bank1 - (0=Sortie - 1=Entrée)
TRISA EQU 0x0148
TRISB EQU 0x0149
TRISC EQU 0x014A
LATF EQU 0x0145
LATA EQU 0x0140
LATB EQU 0x0141
LATC EQU 0x0142
ANSELF EQU 0x0428 ; Bank4 - ANAL/NUM (0=Num - 1=Analog.)
ANSELA EQU 0x0400
ANSELB EQU 0x0408
ANSELC EQU 0x0410
WPUA EQU 0x0401
WPUB EQU 0x0409
WPUC EQU 0x0411
WPUF EQU 0x0429
WREG EQU 0x04E8
SPI1CON0 EQU 0x084 ; Bank0 - SPI Control Register 0
SPI1CON1 EQU 0x085 ; Bank0 - SPI Control Register 1
SPI1CON2 EQU 0x086 ; Bank0 - SPI Control Register 2
SPI1BAUD EQU 0x089 ; Bank0 - SPI Baud Rate Register (Pre-scaler)
SPI1CLK EQU 0x08C ; Bank0 - SPI Clock Selection Register
SPI1TWIDTH EQU 0x088 ; Bank0 - SPI Transfer Width Register [0-2]
SPI1TCNT EQU 0x082 ; Bank0 - SPI Transfer Counter Register [0-10]
SPI1TXB EQU 0x081 ; Bank0 - SPI Transmit Buffer
SPI1STATUS EQU 0x087 ; Bank0 - SPI Status Register
SPI1INTF EQU 0x8A ; Bank0 - SPI Interrupt Flag Register
SPI1INTE EQU 0x08B ; Bank0 - SPI Interrupt Enable Register
OSCFRQ EQU 0x0B1 ; Bank0 - HFINTOSC Frequency Selection Register
PORTWCLK EQU 0x010A ; Bank1 - Signal Routing Port Clock Input Selection
;SPI1 - pin allocation ???
RA5PPS EQU 0x0206 ; CS on pin RA5
RB2PPS EQU 0x20B ; SDO on pin RB2
RB3PPS EQU 0x020C ; SDO on pin RB3
RC1PPS EQU 0x0212 ; CLK on pin RC1
RC3PPS EQU 0x0214 ; Test
; *****************************************************************************
PSECT code
ORG 0x0000 ; Adresse de départ
; *****************************************************************************
; SPI_Init:
; *****************************************************************************
BANKSEL TRISC
bcf TRISC, 1 ; RC1 en sortie (sck)
BANKSEL TRISC
bcf TRISC, 3 ; RC3 en sortie
BANKSEL TRISB
bcf TRISB, 3 ; RB3 en sortie (sdo)
BANKSEL TRISB
bcf TRISB, 2 ; RB2 en sortie
BANKSEL TRISA
bcf TRISA, 5 ; RA5 en sortie (SS)
BANKSEL TRISF
bcf TRISF, 2 ; RF2 en sortie (LED)
; *****************************************************************************
BANKSEL ANSELC
bcf ANSELC, 1 ; RC1 en numérique (sck)
BANKSEL ANSELC
bcf ANSELC, 1 ; RC3 en numérique
BANKSEL ANSELB
bcf ANSELB, 3 ; RB3 en numérique (sdo)
BANKSEL ANSELB
bcf ANSELB, 2 ; RB2 en numérique
BANKSEL ANSELA
bcf ANSELA, 5 ; RA5 en numérique (SS)
BANKSEL ANSELF
bcf ANSELF, 2 ; RF2 en numérique (LED)
; *****************************************************************************
BANKSEL WPUB
bcf WPUB, 3 ; DesActive le pull-up sur RB3 (SDO1)
BANKSEL WPUC
bcf WPUC, 1 ; DesActive le pull-up sur RC1 (SCK1)
BANKSEL WPUC
bcf WPUC, 1 ; DesActive le pull-up sur RC3
; *****************************************************************************
BANKSEL RB3PPS
movlw 0x1E ; 0x1E = SDO1 (voir datasheet)
movwf RB3PPS ; Assigne SDO1 à RB3
BANKSEL PORTWCLK
movlw 0b00001 ; HFINTOSC
movwf PORTWCLK ;
BANKSEL RC1PPS
movlw 0x1D ; 0x1D = SCK1 (voir datasheet)
movwf RC1PPS ; Assigne SCK1 à RC1
; *****************************************************************************
BANKSEL SPI1CON0
movlw 0b10000011 ; 0BMODE=1, 1MST=1, 2LSBF=0, 7EN=1
movwf SPI1CON0
BANKSEL SPI1CON1
movlw 0b10000000 ; 7SMP=1
movwf SPI1CON1
BANKSEL SPI1CON2
bsf SPI1CON2, 1 ; 2TXR=1
BANKSEL SPI1CLK
movlw 0x01 ; FOSC/4 pour le générateur d'horloge
movwf SPI1CLK
BANKSEL OSCFRQ
movlw 0x00 ; 4MHz
movwf OSCFRQ
BANKSEL SPI1BAUD
movlw 0x0F ; -
movwf SPI1BAUD
BANKSEL SPI1TCNT
movlw 0x08 ; 8 bits
movwf SPI1TCNT
BANKSEL SPI1TWIDTH
movlw 0x00 ; -
movwf SPI1TWIDTH
BANKSEL SPI1INTE
movlw 0x00 ; Aucune interruption activée
movwf SPI1INTE
Goto MainLoop
;*****************************************************************************
; Fonction pour envoyer un octet via SPI
;*****************************************************************************
SPI_SendByte:
; Allumer la LED pendant la transmission
BANKSEL LATF
bcf LATF, 2
; Placez les données dans le buffer
BANKSEL SPI1TXB
movwf SPI1TXB ; Place la donnée dans le buffer TX
; Attendez que la transmission soit complète
SPI_Wait:
BANKSEL SPI1STATUS
btfss SPI1STATUS, 5 ; Vérifier si TXBE est à 1 (buffer de transmission vide)
goto SPI_Wait
;
;
; Etteindre la LED à la fin de la transmission
BANKSEL LATF
bsf LATF, 2
return
;*****************************************************************************
; Programme principal
;*****************************************************************************
MainLoop:
nop
BANKSEL LATF
bcf LATF, 2 ; Active CS (mise à 0)
movlw 0x55
call SPI_SendByte
movlw 0x82
call SPI_SendByte
call Delay_1us
call Delay_1us
call Delay_1us
call Delay_1us
call Delay_1us
BANKSEL LATF
bsf LATF, 2 ; Active CS (mise à 0)
goto MainLoop
;*****************************************************************************
; Fonction de délai microseconde
;*****************************************************************************
Delay_1us:
nop
nop
nop
; nop
return
Vous n’avez pas les permissions nécessaires pour voir les fichiers joints à ce message.
PIC18F56Q24 - MODULE SPI
Bonjour
,
Les instructions bcf/bsf sont ok pour modifier les registres TRIS,LAT,... car en cas d'erreur on comprend vite pourquoi cela coince. Par contre pour modifier des bits de configuration de périphérique, il vaut mieux utiliser movlw/mowf qui prennent en compte tous les bits, le risque d'erreur est plus faible.
D'ailleurs l'assembleur permet de faire des macros pour raccourcir et clarifier le programme source (l'exécutable reste le même), les macros sont à définir en début de programme.
On préfère mettre les noms de macro en majuscules pour les distinguer des instrucions machine. Pour le réglage SPI :
Parfois on inverse par mégarde les 2 paramètres de MOVLF, cela passe à l'assemblage, et là aussi on peut y perdre 2 semaines

Les instructions bcf/bsf sont ok pour modifier les registres TRIS,LAT,... car en cas d'erreur on comprend vite pourquoi cela coince. Par contre pour modifier des bits de configuration de périphérique, il vaut mieux utiliser movlw/mowf qui prennent en compte tous les bits, le risque d'erreur est plus faible.
D'ailleurs l'assembleur permet de faire des macros pour raccourcir et clarifier le programme source (l'exécutable reste le même), les macros sont à définir en début de programme.
Code : Tout sélectionner
MOVLF macro val,reg
BANKSEL reg
movlw val
movwf reg
endm
On préfère mettre les noms de macro en majuscules pour les distinguer des instrucions machine. Pour le réglage SPI :
Code : Tout sélectionner
MOVLF 0x1E,RB3PPS ; SDO1 vers RB3
MOVLF 0x1D,RC1PPS ; SCK1 vers RC1
MOVLF 0b00000001,PORTWCLK ; HFINTOSC
MOVLF 0b10000011,SPI1CON0 ; BMODE=1, MST=1, LSBF=0, EN=1
MOVLF 0b10000000,SPI1CON1 ; SMP=1
MOVLF 0b00000010,SPI1CON2 ; TXR=1
MOVLF 0x01,SPI1CLK ; FOSC/4 pour le générateur d'horloge
MOVLF 0x00,OSCFRQ ; 4MHz
MOVLF 0x0F,SPI1BAUD ; -
clrf SPI1TWIDTH ; 8 bits
Parfois on inverse par mégarde les 2 paramètres de MOVLF, cela passe à l'assemblage, et là aussi on peut y perdre 2 semaines

Retourner vers « Langage ASM »
Qui est en ligne
Utilisateurs parcourant ce forum : Aucun utilisateur enregistré et 4 invités