begin process at 2008 09 05 10:20:35
1 237 139 membres
101 nouveaux aujourd'hui
14 312 membres club

Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum.
Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !

BUS I2C POUR PIC16XXX


Information sur la source

Catégorie :Les PIC Niveau : Débutant Date de création : 08/06/2003 Date de mise à jour : 08/06/2003 22:14:15 Vu / téléchargé: 9 273 / 637

Note :
8 / 10 - par 1 personne
8,00 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

Commentaire sur cette source (1)
Ajouter un commentaire et/ou une note

Description

Que dire de plus, que vous trouverez ici les principales commandes I2C.
Il vous suffit ensuite en fonction du composant avec lequel vous communiquez, de les assember dans le bon ordre.

Source

  • ;
  • ; Routines I2C V1.0
  • ;
  • ; O.R 04/2003
  • ;-----------------------------------------------------------------------------------------
  • ;
  • ;
  • ; Cette routine nécessite :
  • ; - Les deux macros : SDA_IN et SDA_OUT
  • ; - Les variables : I2C_flag, I2C_DATA, I
  • ; - Le fichier : Q4mhz.asm
  • ;
  • ;
  • I2C_INIT:
  • SDA_OUT ;SDA en sortie
  • bsf SDA ;SDA a 1
  • bsf SCL ;SCL a 1
  • return
  • I2C_START: ;SDA_OUT
  • ;SDA=1 SCL=1
  • bsf SCL ;SCL a 1
  • bsf SDA ;SDA a 1
  • bcf SDA ;SDA a 0
  • return
  • I2C_STOP: ;SDA_OUT
  • ;SDA=X SCL=0
  • bcf SDA ;SDA a 0
  • bsf SCL ;SCL a 1
  • bsf SDA ;SDA a 1
  • return
  • I2C_SLAVE_ACK: ;SDA_OUT
  • ;SDA=X SCL=0
  • bcf SCL ;SCL a 0
  • SDA_IN ;SDA en entrée
  • CALL Tempo_11us
  • bsf SCL ;SCL a 1
  • CALL Tempo_11us
  • btfsc SDA ;Si 1 ecriture d'un flag d'erreur
  • call NO_ACK
  • bcf SCL ;SCL a 0
  • CALL Tempo_11us
  • SDA_OUT ;SDA en sortie
  • return
  • I2C_MASTER_ACK: ;SDA_IN
  • ;SDA=X SCL=0
  • bcf SCL ;SCL a 0
  • SDA_OUT ;SDA en sortie
  • bcf SDA ;SDA a 0
  • bsf SCL ;SCL a 1
  • CALL Tempo_11us
  • bcf SCL ;SCL a 0
  • CALL Tempo_11us
  • bsf SDA ;SDA a 1
  • return
  • I2C_MASTER_NACK: ;SDA_IN
  • ;SDA=X SCL=0
  • bcf SCL ;SCL a 0
  • SDA_OUT ;SDA en sortie
  • bsf SDA ;SDA a 1
  • bsf SCL ;SCL a 1
  • CALL Tempo_11us
  • bcf SCL ;SCL a 0
  • CALL Tempo_11us
  • bcf SDA ;SDA a 0
  • return
  • I2C_SEND_BYTE:
  • movwf I2C_DATA ;w contient les data a envoyer
  • movlw d'8'
  • movwf I
  • Next_data_s:
  • decf I,f
  • bcf SCL ;SCL a 0
  • CALL Tempo_11us
  • rlf I2C_DATA,f ;Rotation a gauche bit(I) dans la carry
  • btfsc carry ;Si I=1
  • bsf SDA ;SDA a 1
  • btfss carry ;Si I=0
  • bcf SDA ;SDA a 0
  • bsf SCL ;SCL a 1
  • CALL Tempo_11us
  • incf I,f
  • decfsz I,f ;Si I!=0
  • goto Next_data_s
  • bcf SCL ;SCL a 0
  • CALL Tempo_11us
  • call I2C_SLAVE_ACK
  • return
  • I2C_RECEIVE_BYTE:
  • movlw d'8'
  • movwf I
  • clrf I2C_DATA
  • SDA_IN ;SDA en entrée
  • Next_data_r:
  • decf I,f
  • bcf SCL ;SCL a 0
  • CALL Tempo_11us
  • bsf SCL ;SCL a 1
  • CALL Tempo_11us
  • btfsc SDA ;Si SDA=1
  • bsf carry
  • btfss SDA ;Si SDA=0
  • bcf carry
  • rlf I2C_DATA,f ;décallage à gauche et met la carry dans le LSB
  • incf I,f
  • decfsz I,f ;Si I!=0
  • goto Next_data_r
  • bcf SCL ;SCL a 0
  • CALL Tempo_11us
  • call I2C_MASTER_ACK
  • return
  • I2C_RECEIVE_BYTE_LAST:
  • movlw d'8'
  • movwf I
  • SDA_IN ;SDA en entrée
  • Next_data_rl:
  • decf I,f
  • bcf SCL ;SCL a 0
  • CALL Tempo_11us
  • bsf SCL ;SCL a 1
  • CALL Tempo_11us
  • btfsc SDA ;Si SDA=1
  • bsf carry
  • btfss SDA ;Si SDA=0
  • bcf carry
  • rlf I2C_DATA,f ;décallage à gauche et met la carry dans le LSB
  • incf I,f
  • decfsz I,f ;Si I!=0
  • goto Next_data_rl
  • bcf SCL ;SCL a 0
  • CALL Tempo_11us
  • call I2C_MASTER_NACK
  • return
  • ;-----------------------------------------------------------------------------------------
  • ; I2C_flag
  • ; 0: NO_ACK
  • ; 1:
  • ; 2:
  • ; 3:
  • ; 4:
  • ; 5:
  • ; 6:
  • ; 7:
  • ;
  • NO_ACK:
  • bsf I2C_flag,0 ;Pas d'ack de l'esclave -> problème de comm
  • return
;
;			Routines I2C                            	V1.0
;
;									O.R	04/2003
;-----------------------------------------------------------------------------------------
;
;
;	Cette routine nécessite :
;		- Les deux macros 	: SDA_IN et SDA_OUT
;		- Les variables   	: I2C_flag, I2C_DATA, I
;		- Le fichier	  	: Q4mhz.asm
;
;


I2C_INIT:
	SDA_OUT			;SDA en sortie
	bsf	SDA		;SDA a 1
	bsf	SCL		;SCL a 1

	return

I2C_START:			;SDA_OUT
				;SDA=1 SCL=1
	bsf	SCL		;SCL a 1
	bsf	SDA		;SDA a 1
	bcf	SDA		;SDA a 0		

	return

I2C_STOP:			;SDA_OUT
				;SDA=X SCL=0
	bcf	SDA		;SDA a 0
	bsf	SCL		;SCL a 1
	bsf	SDA		;SDA a 1		

	return

I2C_SLAVE_ACK:			;SDA_OUT
				;SDA=X SCL=0
	bcf	SCL		;SCL a 0
	SDA_IN			;SDA en entrée
	CALL	Tempo_11us
	bsf	SCL		;SCL a 1
	CALL	Tempo_11us
	btfsc	SDA		;Si 1 ecriture d'un flag d'erreur
	call	NO_ACK
	bcf	SCL		;SCL a 0
	CALL	Tempo_11us

	SDA_OUT			;SDA en sortie

	return

I2C_MASTER_ACK:			;SDA_IN 
				;SDA=X SCL=0
	bcf	SCL		;SCL a 0
	SDA_OUT			;SDA en sortie
	bcf	SDA		;SDA a 0 
	bsf	SCL		;SCL a 1
	CALL	Tempo_11us
	bcf	SCL		;SCL a 0
	CALL	Tempo_11us
	bsf	SDA		;SDA a 1

	return

I2C_MASTER_NACK:		;SDA_IN 
				;SDA=X SCL=0
	bcf	SCL		;SCL a 0
	SDA_OUT			;SDA en sortie
	bsf	SDA		;SDA a 1 
	bsf	SCL		;SCL a 1
	CALL	Tempo_11us
	bcf	SCL		;SCL a 0
	CALL	Tempo_11us
	bcf	SDA		;SDA a 0 
	
	return

I2C_SEND_BYTE:
	movwf	I2C_DATA	;w contient les data a envoyer
	movlw	d'8'
	movwf	I
Next_data_s:
	decf	I,f
	bcf	SCL		;SCL a 0
	CALL	Tempo_11us
	rlf	I2C_DATA,f	;Rotation a gauche bit(I) dans la carry
	btfsc	carry		;Si I=1
	bsf	SDA		;SDA a 1
	btfss	carry		;Si I=0
	bcf	SDA		;SDA a 0
	bsf	SCL		;SCL a 1 
	CALL	Tempo_11us
	incf	I,f
	decfsz	I,f		;Si I!=0
	goto	Next_data_s

	bcf	SCL	;SCL a 0
	CALL	Tempo_11us

	call	I2C_SLAVE_ACK

	return

I2C_RECEIVE_BYTE:
	movlw	d'8'
	movwf	I
	clrf	I2C_DATA
	SDA_IN			;SDA en entrée
Next_data_r:
	decf	I,f
	bcf	SCL		;SCL a 0
	CALL	Tempo_11us
	bsf	SCL		;SCL a 1
	CALL	Tempo_11us
	btfsc	SDA		;Si SDA=1
 	bsf	carry     
	btfss	SDA		;Si SDA=0
	bcf	carry      

	rlf	I2C_DATA,f	;décallage à gauche et met la carry dans le LSB 

	incf	I,f
	decfsz	I,f		;Si I!=0
	goto	Next_data_r

	bcf	SCL	;SCL a 0
	CALL	Tempo_11us

	call	I2C_MASTER_ACK

	return

I2C_RECEIVE_BYTE_LAST:
	movlw	d'8'
	movwf	I
	SDA_IN			;SDA en entrée
Next_data_rl:
	decf	I,f
	bcf	SCL		;SCL a 0
	CALL	Tempo_11us
	bsf	SCL		;SCL a 1
	CALL	Tempo_11us
	btfsc	SDA		;Si SDA=1
 	bsf	carry     
	btfss	SDA		;Si SDA=0
	bcf	carry      

	rlf	I2C_DATA,f	;décallage à gauche et met la carry dans le LSB 

	incf	I,f
	decfsz	I,f		;Si I!=0
	goto	Next_data_rl

	bcf	SCL	;SCL a 0
	CALL	Tempo_11us

	call	I2C_MASTER_NACK

	return


;-----------------------------------------------------------------------------------------
;	I2C_flag
;		0: NO_ACK
;		1:	
;		2:
;		3:
;		4:
;		5:
;		6:
;		7:
;


NO_ACK:
	bsf	I2C_flag,0	;Pas d'ack de l'esclave -> problème de comm

	return

Pour les "Membres Club", vous pouvez télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !

Télécharger le zip

  • signaler à un administrateur
    Commentaire de Bigonoff le 08/10/2003 22:07:11

    Salut
    ------

    Petite remarque : en I²C, on ne peut JAMAIS imposer un niveau haut sur SCL ou sur SDA.
    Ce programme ne respecte donc pas la norme, sauf si électronique externe (mais ce n'est pas précisé).
    Tu devrais revoir tes routines ;)

    A+
    Bigonoff

Ajouter un commentaire

Pub



Appels d'offres

Recherche developpeur ...
Budget : 700€
SITE MARCHAND LOCATION...
Budget : 3 000€
SITE MARCHAND POUR HOTEL
Budget : 4 000€

CalendriCode

Septembre 2008
LMMJVSD
1234567
891011121314
15161718192021
22232425262728
2930     

VS Express FR Gratuit !

VS Express en français et 100% gratuit !

Boutique

Boutique de goodies CodeS-SourceS