cevrero jeremy / Mbed 2 deprecated 1_Programme_principal

Dependencies:   mbed

Committer:
jeremycevrero
Date:
Tue May 01 21:29:55 2012 +0000
Revision:
0:40a613e7ba83

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jeremycevrero 0:40a613e7ba83 1 /******************************************************************************
jeremycevrero 0:40a613e7ba83 2 *
jeremycevrero 0:40a613e7ba83 3 * FILENAME: APSDS_9300.h
jeremycevrero 0:40a613e7ba83 4 * DATE: 2012
jeremycevrero 0:40a613e7ba83 5 * AUTHOR: Jeremy CEVRERO
jeremycevrero 0:40a613e7ba83 6 *
jeremycevrero 0:40a613e7ba83 7 * DESCRIPTION: Driver for APDS_9300 Ambiant light sensor (Avago)
jeremycevrero 0:40a613e7ba83 8 *
jeremycevrero 0:40a613e7ba83 9 ******************************************************************************/
jeremycevrero 0:40a613e7ba83 10
jeremycevrero 0:40a613e7ba83 11 I2C i2c(p28, p27);// sda, scl ;
jeremycevrero 0:40a613e7ba83 12
jeremycevrero 0:40a613e7ba83 13 /** Declaration des variables *****************************************************************/
jeremycevrero 0:40a613e7ba83 14 char ch0H;//variable ADC0 octet fort
jeremycevrero 0:40a613e7ba83 15 char ch0L;//variable ACDC0 octet faible
jeremycevrero 0:40a613e7ba83 16 int ch0;//valeur de chO sur 16 bits
jeremycevrero 0:40a613e7ba83 17 char ch1H;//variable ADC1 octet fort
jeremycevrero 0:40a613e7ba83 18 char ch1L;//variable ADC1 octet faible
jeremycevrero 0:40a613e7ba83 19 int ch1;//valeur de ch1 sur 16 bits
jeremycevrero 0:40a613e7ba83 20 float ch1divch0;//ch1/ch0
jeremycevrero 0:40a613e7ba83 21 float luminosite;// luminosite en lux
jeremycevrero 0:40a613e7ba83 22 int adresse = 0x52;//adresse du APDS-9300 ( pour adr sel a la masse)
jeremycevrero 0:40a613e7ba83 23 float a;//niveau de luminosite
jeremycevrero 0:40a613e7ba83 24
jeremycevrero 0:40a613e7ba83 25
jeremycevrero 0:40a613e7ba83 26 /*******************************************************************************
jeremycevrero 0:40a613e7ba83 27 * FUNCTION: start_9300
jeremycevrero 0:40a613e7ba83 28 * PURPOSE: Démarrer le composant.
jeremycevrero 0:40a613e7ba83 29 ******************************************************************************/
jeremycevrero 0:40a613e7ba83 30 void start_9300 ()
jeremycevrero 0:40a613e7ba83 31 {
jeremycevrero 0:40a613e7ba83 32 char cmd[2];
jeremycevrero 0:40a613e7ba83 33 cmd[0] = 0x80;//registre de commande selectionne le registre de controle
jeremycevrero 0:40a613e7ba83 34 cmd[1] = 0x03;//Registre de controle met en route le composant
jeremycevrero 0:40a613e7ba83 35 i2c.write(adresse, cmd, 2);
jeremycevrero 0:40a613e7ba83 36 wait(0.1);
jeremycevrero 0:40a613e7ba83 37 }
jeremycevrero 0:40a613e7ba83 38
jeremycevrero 0:40a613e7ba83 39 /*******************************************************************************
jeremycevrero 0:40a613e7ba83 40 * FUNCTION: stop_9300
jeremycevrero 0:40a613e7ba83 41 * PURPOSE: Stopper le composant.
jeremycevrero 0:40a613e7ba83 42 ******************************************************************************/
jeremycevrero 0:40a613e7ba83 43 void stop_9300 ()
jeremycevrero 0:40a613e7ba83 44
jeremycevrero 0:40a613e7ba83 45 {
jeremycevrero 0:40a613e7ba83 46 char cmd[2];
jeremycevrero 0:40a613e7ba83 47 cmd[0] = 0x80;//registre de commande selectionne le registre de controle
jeremycevrero 0:40a613e7ba83 48 cmd[1] = 0x12;//Registre de controle stoppe le composant
jeremycevrero 0:40a613e7ba83 49 i2c.write(adresse, cmd, 2);
jeremycevrero 0:40a613e7ba83 50 wait(0.1);
jeremycevrero 0:40a613e7ba83 51 }
jeremycevrero 0:40a613e7ba83 52
jeremycevrero 0:40a613e7ba83 53 /*******************************************************************************
jeremycevrero 0:40a613e7ba83 54 * FUNCTION: read_9300
jeremycevrero 0:40a613e7ba83 55 * PURPOSE: Lire la luminosite et l'afficher sur l'hyperterminal.
jeremycevrero 0:40a613e7ba83 56 ******************************************************************************/
jeremycevrero 0:40a613e7ba83 57 void read_9300 ()
jeremycevrero 0:40a613e7ba83 58 {
jeremycevrero 0:40a613e7ba83 59
jeremycevrero 0:40a613e7ba83 60 start_9300 ();//Allume le composant
jeremycevrero 0:40a613e7ba83 61
jeremycevrero 0:40a613e7ba83 62 char cmd[2];
jeremycevrero 0:40a613e7ba83 63 cmd[0] = 0x81;//s�lectionne le timing register
jeremycevrero 0:40a613e7ba83 64 cmd[1] = 0x02;//mode low gain temps d'int�gration = 402ms
jeremycevrero 0:40a613e7ba83 65 i2c.write(adresse, cmd, 2);
jeremycevrero 0:40a613e7ba83 66
jeremycevrero 0:40a613e7ba83 67 cmd[0] = 0x8C; //lecture datalow canal 0
jeremycevrero 0:40a613e7ba83 68 i2c.write(adresse,cmd, 1);
jeremycevrero 0:40a613e7ba83 69 i2c.read(adresse|1,&ch0L,1,0);
jeremycevrero 0:40a613e7ba83 70
jeremycevrero 0:40a613e7ba83 71 cmd[0] = 0x8D; //lecture datahigh canal 0
jeremycevrero 0:40a613e7ba83 72 i2c.write(adresse,cmd, 1);
jeremycevrero 0:40a613e7ba83 73 i2c.read(adresse|1,&ch0H,1,0);
jeremycevrero 0:40a613e7ba83 74 ch0=(ch0H <<8) + ch0L;
jeremycevrero 0:40a613e7ba83 75
jeremycevrero 0:40a613e7ba83 76 cmd[0] = 0x8E; //lecture datalow canal 1
jeremycevrero 0:40a613e7ba83 77 i2c.write(adresse,cmd, 1);
jeremycevrero 0:40a613e7ba83 78 i2c.read(adresse|1,&ch1L,1,0);
jeremycevrero 0:40a613e7ba83 79
jeremycevrero 0:40a613e7ba83 80 cmd[0] = 0x8F; //lecture datahigh canal 1
jeremycevrero 0:40a613e7ba83 81 i2c.write(adresse,cmd, 1);
jeremycevrero 0:40a613e7ba83 82 i2c.read(adresse|1,&ch1H,1,0);
jeremycevrero 0:40a613e7ba83 83 ch1=(ch1H <<8) + ch1L;
jeremycevrero 0:40a613e7ba83 84
jeremycevrero 0:40a613e7ba83 85 ch1divch0 = ch1*1.0f / ch0;//ch1divch0=ch1/ch0 flottant
jeremycevrero 0:40a613e7ba83 86
jeremycevrero 0:40a613e7ba83 87 if (0 <= ch1divch0 <= 0.52)//si ch1/ch0 compris entre 0 et 0.52 alors on applique la formule suivant
jeremycevrero 0:40a613e7ba83 88 {
jeremycevrero 0:40a613e7ba83 89 luminosite = (0.0315 * ch0) -((0.0593 * ch0) * (powf((ch1/ch0),1.4)));
jeremycevrero 0:40a613e7ba83 90 }
jeremycevrero 0:40a613e7ba83 91 else if (0.52 <= ch1divch0 <= 0.65)//sinon si il est compris entre 0.52 et 0.65
jeremycevrero 0:40a613e7ba83 92 {
jeremycevrero 0:40a613e7ba83 93 luminosite = ((0.0229 * ch0) - (0.0291 * ch1));//on applique cette formule
jeremycevrero 0:40a613e7ba83 94 }
jeremycevrero 0:40a613e7ba83 95 else if (0.65 <= ch1divch0 <= 0.80)//sinon si il est compris entre 0.65 et 0.80
jeremycevrero 0:40a613e7ba83 96 {
jeremycevrero 0:40a613e7ba83 97 luminosite = (0.0157 * ch0) - (0.0180 * ch1);//on applique cette formule
jeremycevrero 0:40a613e7ba83 98 }
jeremycevrero 0:40a613e7ba83 99 else if (0.80 <= ch1divch0<= 1.30)//sinon si il est compris entre 0.8 et 1.3
jeremycevrero 0:40a613e7ba83 100 {
jeremycevrero 0:40a613e7ba83 101 luminosite = (0.00338 * ch0) - (0.00260 * ch1);//on applique cette formule
jeremycevrero 0:40a613e7ba83 102 }
jeremycevrero 0:40a613e7ba83 103 else if (ch1divch0 >= 1.30)//sinon
jeremycevrero 0:40a613e7ba83 104 {
jeremycevrero 0:40a613e7ba83 105 luminosite = 0;
jeremycevrero 0:40a613e7ba83 106 }
jeremycevrero 0:40a613e7ba83 107 luminosite=luminosite*16;
jeremycevrero 0:40a613e7ba83 108
jeremycevrero 0:40a613e7ba83 109 pc.printf("ch1divch0 = %f, ch1 = Ox%X ,ch0 = 0x%X,luminosite = %f \n\r",ch1divch0,ch1, ch0,luminosite);// affiche ch1/ch0,ch1,ch0et luminosite
jeremycevrero 0:40a613e7ba83 110 a = luminosite/100000;//Generation du pwm
jeremycevrero 0:40a613e7ba83 111 stop_9300 ();//Eteint le composant
jeremycevrero 0:40a613e7ba83 112
jeremycevrero 0:40a613e7ba83 113 }