Correction programmation SPI/inclinometre
Répondre à la discussion
Affichage des résultats 1 à 1 sur 1

Correction programmation SPI/inclinometre



  1. #1
    invite8a011b82

    Correction programmation SPI/inclinometre


    ------

    Bonjour,

    J'ai réalisé un programme permettant de discuter avec un inclinometre et d'envoyer les mesures sur un pic via le port SPI.

    J'aimerai avoir des corrections et savoir comment on obtient l'angle final en partant du calcul sur l'axe x et l'axe y.

    Le programme n'est pas encore fini. Le .h du pic utilisé est en PJ.

    Merci

    Voici le code de inclinometre.h
    Code:
    #ifndef __INCLINOMETRE_H
    #define __INCLINOMETRE_H
    
    /*************************************************************************/
    /**********************List of commands for SCA100T***********************/
    /*************************************************************************/
    
    #define MEAS 	0b00000000	//Measure mode
    #define RWTR 	0b00001000	//Read and write temperature data register
    #define RDSR	0b00001010	//Read status register
    #define RLOAD 	0b00001011	//Reload NV data to memory output register
    #define STX		0b00001110	//Activate Self test for X-channel
    #define STY		0b00001111	//Activate Self test for Y-channel
    #define RDAX	0b00010000	//Read X-channel acceleration through SPI
    #define RDAY	0b00010001	//Read Y-channel acceleration through SPI
    
    /*************************************************************************/
    /******************************Buffer SCA100T*****************************/
    /*************************************************************************/
    
    typedef union{
    	unsigned char u8loctet;
    	 struct{
    	   unsigned bit0	:1;
    	   unsigned bit1	:1;
    	   unsigned bit2	:1;
    	   unsigned bit3	:1;
    	   unsigned bit4	:1;
    	   unsigned bit5	:1;
    	   unsigned bit6	:1;
    	   unsigned bit7	:1;
    	}BUFF_BITS_8;
    }BUFF_INCLI_BITS_8;
    
    
    typedef union{
    	unsigned char u11octet;
    	 struct{
    	   unsigned bit0	:1;
    	   unsigned bit1	:1;
    	   unsigned bit2	:1;
    	   unsigned bit3	:1;
    	   unsigned bit4	:1;
    	   unsigned bit5	:1;
    	   unsigned bit6	:1;
    	   unsigned bit7	:1;
    	   unsigned bit8 	:1;
    	   unsigned bit9 	:1;
    	   unsigned bit10	:1;
    	   unsigned bit11	:1;
    	}BUFF_BITS_11;
    }BUFF_INCLI_BITS_11;
    	
    
    /*************************************************************************/
    /**************************Structure of SCA100T***************************/
    /*************************************************************************/
    //Structure données calculées
    typedef double type_data;
    typedef struct{
    	type_data incli_measure_x;
    	type_data incli_measure_y;
    	type_data temp_measure;
    }STRUCT_INCLI;
    
    //Struct données brutes
    typedef struct{
    	BUFF_INCLI_BITS_11 incli_measure_x;
    	BUFF_INCLI_BITS_11 incli_measure_y;
    	BUFF_INCLI_BITS_8 temp_measure;
    }BUFF_STRUCT_INCLI;
    
    /*************************************************************************/
    /***********************List of fonctions of SCA100T**********************/
    /*************************************************************************/
    
    void init_inclinometre(void);
    void self_test_x(void);
    void self_test_y(void);
    void read_temp(BUFF_STRUCT_INCLI *s);
    void read_x(BUFF_STRUCT_INCLI *s);
    void read_y(BUFF_STRUCT_INCLI *s);
    STRUCT_INCLI calcul_struct(BUFF_STRUCT_INCLI *s);
    void envoi_donnees(STRUCT_INCLI *s);
    
    #endif

    Voici le code de inclinometre.c
    Code:
    #include <p24fj256gb110.h>
    #include <math.h>
    #include<spi.h>
    #include "inclinometre.h"
    #include "myspi.h"
    
    void init_SPI (void)
    {
    //OpenSPI3(unsigned int config1,unsigned int config2, unsigned int config3 );
    	//SPI3STAT = 0b00111101xxx00101;
    	SPI3_Clear_Recv_OV;	//Clear overflow
    	if(SPI3_Tx_Buf_Full==0&&SPI3_Rx_Buf_Full==0)//Buffer sont vides
    		{
    			//INTCON1 = 0b0000000000000001;
    			INTCON1bits.NSTDIS=1; // si on ne vx pas d'interruptions imbriquées
    			
    			//IPC22 = 0b0000000010101100;
    			SetPriorityIntSPI3(5); //niveau de proirité de l'interruption (IPC22bits.SPI3IP = priority)
    			
    			//IFS5 = 0b0000000000000000;
    			SPI3_Clear_Intr_Status_Bit; //Mise à 0 du flag (IFS5bits.SPI3IF = 0)
    			
    			//IEC5 = 0b0000000000110000;
    			EnableIntSPI3;	//Activation du bit d'interruption (IEC5bits.SPI3IE = 1)
    		}
    }
    
    void init_inclinometre(void)
    {
    	RPINR28bits.SCK3R=53; 	//SPI3 Clock input RP15_SCK
    	RPINR28bits.SDI3R=52; 	//SPI3 data input RP30_SDI
    	RPOR8bits.RP16R=32;		//SPI3 data output RP16_SDO
    							//CS chip select RP30_CS
    }
    
    void init_LCD(void)
    {
    //init LCD
    }
    
    void self_test_x(void)		
    {
    	INSTRUCT_SPI STX_spi;
    	STX_spi.u8loctet=STX;	//self test axe x actif
    }
    
    void self_test_y(void)	
    {
    	INSTRUCT_SPI STY_spi;
    	STY_spi.u8loctet=STY;		//self test axe y actif
    }
    
    void read_temp(BUFF_STRUCT_INCLI *s) 
    {
    								//CS chip select RP30_CS
    
    	if(IFS5bits.SPI3IF ==0)// si SPI3 est disponible
    	{
    		s-> temp_measure.u8loctet = RWTR; //Mode lecture température actif
    		if(SPI3_Tx_Buf_Full==0)	//si le buff de transmission es vide
    			WriteSPI3( (unsigned int) s-> temp_measure.u8loctet );//ecrit valeur temp dans mémoire [à définir]
    	}
    	SPI3_Clear_Intr_Status_Bit;
    	EnableIntSPI3;
    }
    
    void read_x(BUFF_STRUCT_INCLI *s)
    {
    	DONNEES_SPI_BITS_8 measure_x; 
    
    	measure_x.u8loctet = MEAS;	//Mode mesure actif
    	if(IFS5bits.SPI3IF ==0)// si SPI3 est disponible
    	{
    		s->incli_measure_x.u11octet = RDAX;	//mode lecture acceleration axe x
    		if(SPI3_Tx_Buf_Full==0)	//si le buff de transmission es vide
    			WriteSPI3( (unsigned int) s->incli_measure_x.u11octet );//ecrit valeur x dans mémoire [à définir]
    	}
    	SPI3_Clear_Intr_Status_Bit;
    	EnableIntSPI3;
    }
    
    void read_y(BUFF_STRUCT_INCLI *s)	
    {
    	DONNEES_SPI_BITS_8 measure_y; 
    
    	if(IFS5bits.SPI3IF ==0)// si SPI3 est disponible
    	{
    		measure_y.u8loctet = MEAS;	//Mode mesure actif
    		s->incli_measure_y.u11octet = RDAY;	//mode lecture acceleration axe y
    		if(SPI3_Tx_Buf_Full==0)	//si le buff de transmission es vide
    			WriteSPI3( (unsigned int) s->incli_measure_y.u11octet );//ecrit valeur y dans mémoire [à définir]
    	}
    	SPI3_Clear_Intr_Status_Bit;
    	EnableIntSPI3;
    }
    
    STRUCT_INCLI calcul_struct(BUFF_STRUCT_INCLI *s)	//Structure de données contenant: Température, valeur axe x et axe y
    {
    	STRUCT_INCLI *c;
    	c->temp_measure= ((double)(s->temp_measure.u8loctet) - 197)/(-1.083);		//T(°C) = (temp_lue(8_bits) -197)/(-1.083)
    	c->incli_measure_x= asin(((double)(s->incli_measure_x.u11octet) - 1024)/(819));	//angle = arcsin[(RDAx - offset)/sensitivity]
    	c->incli_measure_x= asin(((double)(s->incli_measure_y.u11octet) - 1024)/(819));//offset = 1024   sensitivity = D01:1638 D02:819
    	return *c;
    }
    
    
    void envoi_donnees(STRUCT_INCLI *s)
    {
    	if(IFS5bits.SPI3IF ==0)// si SPI3 est disponible
    	{
    	if(SPI3_Rx_Buf_Full==0)	//si le buff de transmission es vide
    		while(SPI3STATbits.SPIROV!=1)	//Transmission en cours
    			ReadSPI3();//lit valeurs temp, x et y dans mémoire [à définir]
    	}
    	SPI3_Clear_Intr_Status_Bit;
    	EnableIntSPI3;
    }
    
    void affiche(void)
    {
    //Afficher a l'ecran la temp, x et y
    }
    
    int main (void)
    {
    	BUFF_STRUCT_INCLI *a;
    	STRUCT_INCLI *b;
    	
    	init_SPI();
    	init_inclinometre();
    	init_LCD();
    	self_test_x();
    	self_test_y();
    	read_temp(a);
    	read_x(a);
    	read_y(a);
    	calcul_struct(a);
    	envoi_donnees(b);
    	affiche();
    	
    	return 0;
    }
    Voici le code de myspi.h
    Code:
    #ifndef __MYSPI_H
    #define __MYSPI_H
    
    /*************************************************************************/
    /********************SPI instruction/command register*********************/
    /*************************************************************************/
    
    typedef union{
    	unsigned char u8loctet;
    	struct{
    	   unsigned bit0	:1;
    	   unsigned bit1	:1;
    	   unsigned bit2	:1;
    	   unsigned bit3	:1;
    	   unsigned bit4	:1;
    	   unsigned bit5	:1;
    	   unsigned bit6	:1;
    	   unsigned bit7	:1;
    	}STRUCT_BITS;
    }INSTRUCT_SPI;
    
    /*************************************************************************/
    /***************************SPI donnees register**************************/
    /*************************************************************************/
    
    typedef union{
    	unsigned char u8loctet;
    	 struct{
    	   unsigned bit0	:1;
    	   unsigned bit1	:1;
    	   unsigned bit2	:1;
    	   unsigned bit3	:1;
    	   unsigned bit4	:1;
    	   unsigned bit5	:1;
    	   unsigned bit6	:1;
    	   unsigned bit7	:1;
    	}STRUCT_BITS_8;
    }DONNEES_SPI_BITS_8;
    
    
    typedef union{
    	unsigned char u11octet;
    	 struct{
    	   unsigned bit0	:1;
    	   unsigned bit1	:1;
    	   unsigned bit2	:1;
    	   unsigned bit3	:1;
    	   unsigned bit4	:1;
    	   unsigned bit5	:1;
    	   unsigned bit6	:1;
    	   unsigned bit7	:1;
    	   unsigned bit8 	:1;
    	   unsigned bit9 	:1;
    	   unsigned bit10	:1;
    	   unsigned bit11	:1;
    	}STRUCT_BITS_11;
    }DONNEES_SPI_BITS_11;
    
    /*************************************************************************/
    /*************************List of fonction of SPI************************/
    /*************************************************************************/
    
    void init_SPI (void);
    
    #endif

    -----
    Images attachées Images attachées

Discussions similaires

  1. programmation SPI envoie des donnes vers un DAc AD5322 avec 2 sortie
    Par invite934cb362 dans le forum Électronique
    Réponses: 2
    Dernier message: 12/06/2011, 13h31
  2. [débutant] Programmation CC1100 SPI
    Par invite304155d6 dans le forum Électronique
    Réponses: 10
    Dernier message: 14/04/2011, 15h10
  3. Utilisation du bus SPI au niveau de la programmation
    Par Poseidon88 dans le forum Électronique
    Réponses: 4
    Dernier message: 08/09/2010, 13h11
  4. Programmation SPI pour PIC18F97J60
    Par invite304155d6 dans le forum Électronique
    Réponses: 0
    Dernier message: 20/07/2009, 10h57
  5. Information programmation I2C SPI
    Par invite865d2ebc dans le forum Électronique
    Réponses: 5
    Dernier message: 08/07/2009, 21h35