test

Dependencies:   mbed MMA8452Q

Fork of HelloWorld by Simon Ford

main.cpp

Committer:
kkalsi
Date:
2017-09-08
Revision:
27:0f602aca65f1
Parent:
26:523ac79471d8
Child:
28:f56de0b0764e

File content as of revision 27:0f602aca65f1:

/**** Vincent Labbé - labv2507  *****/
/**** Karan Kalsi -   *****/
#include "mbed.h"    
#include "MMA8452Q.h"

Serial pc(USBTX, USBRX); // tx, rx
//SPI spi(p11,p12,p13);
//DigitalOut cs(p14);
// Communication I2C
//I2C i2c(p9,p10); // sda, scl

//int main() {
  //  }
    


// on utilise l'afficheur 7 SEG en mode SPI pour la mise au point
SPI spi(p5, p6, p7); // mosi, miso, sclk
DigitalOut cs(p8);

AnalogOut signal(p18);
AnalogIn ain(p20);
BusOut myleds(LED1, LED2, LED3, LED4);  // for display 4 leds in hex format
unsigned short value;
Ticker display;

short table[16] = { 0x00,   // 0 0 - 1/16
                    0x00,   // 1 1/16 - 1/8
                    0x01,   // 2 1/8 - 3/16
                    0x01,   // 3 3/16 - 1/4
                    0x01,   // 4 1/4 - 5/16
                    0x01,   // 5 5/16 - 3/8
                    0x03,   // 6 3/8 - 7/16
                    0x03,   // 7 7/16 - 1/2
                    0x03,   // 8 1/2 - 9/16
                    0x03,   // 9 9/16 - 5/8
                    0x07,   // a 5/8 - 11/16
                    0x07,   // b 11/16 - 3/4
                    0x07,   // c 3/4 - 13/16
                    0x07,   // d 13/16 - 7/8
                    0x0f,   // e 7/8 - 15/16
                    0x0f};  // f 15/16 - 1

void disp() {
    short i;
    value = ain.read_u16();  // read input
    i = (value >> 12) & 0x000f;  // transform to hex
    
        cs = 0;         // affichage 7 SEG via SPI
        wait_us(5);
        spi.write(i); 
        wait_us(5);
        cs = 1;
        
    myleds = table[i];  // affichage sur les LEDs
}

int main() {
   spi.format(8,0);     // initialisation du port SPI pour l'afficheur
   spi.frequency(100000);
   
         cs = 0;
         wait_us(5);
         spi.write(0x76);   //clear
         wait_us(5);
         cs = 1;
         
    display.attach(&disp, 0.1); // connexion de la fonction périodique aux 1/10 de seconde // roule constamment
    while (1){     
    
    }
    
}





// SPI COMMUNICATION
/*
int main() {

     float x, y, z ;
     MMA8452Q acc(p9,p10,0x1d);     // acceleration object
     
         cs = 0;    //Clear display SPI
         wait(0.1);
         spi.write(0x76); // Clear display
         cs = 1;
         wait(0.1);
         cs = 1;
         
     while (true) {       
         x = acc.getAccX() ; 
         y = acc.getAccY() ;
         z = acc.getAccZ() ;
         printf("X[%.3f] Y[%.3f] Z[%.3f]\n",x, y, z) ;
         wait(0.1);

        int X = x * 1000; //Pour enlever floating point et garder une précision
        int Y = y * 1000;
        int Z = z * 1000;

        int angle = (1000-(((2*X*X+2*Y*Y)*1000/(X*X+Y*Y+Z*Z)))); //Calcul vectoriel pour calculer l'angle à l'horizontal
        
        pc.printf("avant acos = %d", angle);
        
        float ratioZaccel = angle/1000.0; //Remettre en floating point pour acos
        int resultatRAD = 500*acos((ratioZaccel)); //Reponse en RAD
        
        pc.printf("valeur rad new= %d", resultatRAD);
        int degree = resultatRAD * 18000/31400; //Tranfo degree
        pc.printf("valeur deg new = %d", degree);
        pc.printf("valeur deg new = %d", degree);
        
        // extracting digits
        int digit;
        int digits[4] = {0,0,0,0};
        int i = 0;
        while(degree > 0) {
        digit = degree % 10; //to get the right most digit
        digits[i]=digit;
        pc.printf("digit %d = %d, degree int: %d", i, digits[i], degree);
        degree /= 10;              //reduce the number by one digit
        ++i;
        }
         
         spi.write(0x77); // Decimal control command    // Pour la communication SPI
         cs = 1;
         wait(0.01);
         cs = 0;
         spi.write(0x04);// Turn on decimal
         cs = 1;
         wait(0.01);
         cs = 0;
         spi.write(digits[3]);
         cs = 1;
         wait(0.01);
         cs = 0;
         spi.write(digits[2]);
         cs = 1;
         wait(0.01);
         cs = 0;
         spi.write(digits[1]);
         cs = 1;
         wait(0.01);
         cs = 0;
         spi.write(digits[0]);
         cs = 1; 
         */
         /*     Test
         cs = 0;
         wait(0.1);
         spi.write(0x5);
         cs = 1;
         */
    //    }
//} 



// UART COMMUNICATION
/*
void UARTInit()
{
 uint16_t usFdiv;   
    
 LPC_SC->PCONP       |=  (1 << 25);  // Power up the UART3 it's disabled on powerup.
  
 LPC_PINCON->PINSEL1 |= (3 << 18);   // Pin P0.25 used as TXD0 (Com0)     // Enable the pins on the device to use UART3 
 LPC_PINCON->PINSEL1 |= (3 << 20);   // Pin P0.26 used as RXD0 (Com0) 
    
 LPC_SC->PCLKSEL1    &= ~(3 << 18); // Clean all to 0    // Setup the PCLK for UART3
 LPC_SC->PCLKSEL1    |=  (1 << 18); // PCLK = CCLK

 LPC_UART3->LCR  =   0x83;  // 0000 0000 1000 0011       // Word select 8-bit character length and set DLAB

 usFdiv = (SystemCoreClock / (16*9600));    //Baud rate calculation
 LPC_UART3->DLM  =  usFdiv / 256;
 LPC_UART3->DLL  =   usFdiv % 256;

 LPC_UART3->FCR       = 0x7;     // Enable and reset UART3 FIFOs.

 LPC_UART3->LCR &= ~(1 << 7);    // Clear DLAB
}

char UART3Transmit(int out)
{
    LPC_UART3 -> THR = out;
    while(! (LPC_UART3->LSR & (0x01 << 0x06)));

    return 1;
}

int main() {
    
 UARTInit();

     float x, y, z ;
    
     MMA8452Q acc(p9,p10,0x1d);     // acceleration object
         
         UART3Transmit(0x76); // Clear display UART
     while (true) {  
          
         x = acc.getAccX() ; 
         y = acc.getAccY() ;
         z = acc.getAccZ() ;
         printf("X[%.3f] Y[%.3f] Z[%.3f]\n",x, y, z) ;
         wait(0.1);

        int X = x * 1000; //Pour enlever floating point et garder une précision
        int Y = y * 1000;
        int Z = z * 1000;

        int angle = (1000-(((2*X*X+2*Y*Y)*1000/(X*X+Y*Y+Z*Z)))); //Calcul vectoriel pour calculer l'angle à l'horizontal
        
        pc.printf("avant acos = %d", angle);
        
        float ratioZaccel = angle/1000.0; //Remettre en floating point pour acos
        int resultatRAD = 500*acos((ratioZaccel)); //Reponse en RAD
        
        pc.printf("valeur rad new= %d", resultatRAD);
        int degree = resultatRAD * 18000/31400; //Tranfo degree
        pc.printf("valeur deg new = %d", degree);
        
        pc.printf("valeur deg new = %d", degree);
        
        
        int digit;      // extracting digits
        int digits[4] = {0,0,0,0};
        int i = 0;
        while(degree > 0) {
        digit = degree % 10; //to get the right most digit
        digits[i]=digit;
        pc.printf("digit %d = %d, degree int: %d", i, digits[i], degree);
        degree /= 10;              //reduce the number by one digit
        ++i;
        }
         
         UART3Transmit(0x77); // Decimal control command    // Pour la communication UART
         UART3Transmit(0x04);// Turn on decimal
         
         UART3Transmit(digits[3]);
         UART3Transmit(digits[2]);
         UART3Transmit(digits[1]);
         UART3Transmit(digits[0]);      //UART3Transmit(0xA5);
        }
} 
*/        


/* // communication I2C 
Serial pc(USBTX, USBRX); // tx, rx
I2C i2c(p9,p10);   

int main() {
    i2c.frequency(100000);
    char cmd[2] = {0,0};
    int addr = 0x5A;
    int data = 0xA4;
    cmd[0] = addr;
    cmd[1] = data;
    
while(1){
    i2c.write(0x3A,cmd,2);
    wait_ms(5);
    }
}
 */
// I2C write
//i2c.frequency(100000);
//int cmd[0] = 0x0D;
//12c.write(addr,cmd,1,true);
//i2c.read(addr,cmd,1);

 /* // communication UART
Serial pc(USBTX, USBRX); // tx, rx
Serial mc(p9,p10);
int main() {
    
    int nombre;
    pc.printf("Entrez un nombre de 4 chiffres : ");
    pc.scanf("%d", &nombre);
    pc.printf("Votre numero entrez est le : %d", nombre);
    //mc.printf(nombre);
    mc.putc(nombre);
}
 */

/*
DigitalOut myled(LED3);

int main() {
    while(1) {
        myled = 1;
        wait(0.2);
        myled = 0;
        wait(0.2);
    }
}
*/