// Dibuja la interfaz en blanco de la informacion de la bateria.
void Mot_initInterface(GLCD *glcd_p){

    //Limpiamos la pantalla de lo que sea que tenga
    LCD_clearScreen(glcd_p);

    // Imprimimos el titulo.
    LCD_print(glcd_p, 13, 0, 15, "Sistema Motores");
    delay(30);
    //Imprimimos la informacion de las fases
    LCD_print(glcd_p, 0,32,6,"F C  V");
    delay(30);
    LCD_print(glcd_p, 0,40,1,"1");
    delay(30);
    LCD_print(glcd_p, 0,48,1,"2");
    delay(30);
    LCD_print(glcd_p, 0,56,1,"3");
    delay(50);

    //Imprimimos RC y RF.
    LCD_print(glcd_p, 50, 44, 2, "Rc");
    delay(30);
    LCD_print(glcd_p, RC_pos_x + 18, RC_pos_y, 1, "v");
    delay(20);
    LCD_print(glcd_p, 50, 55, 2, "Rf");
    delay(20);
    LCD_print(glcd_p, RF_pos_x + 18, RF_pos_y, 1, "v");
    delay(20);

    //imprimimos Indc
    LCD_print(glcd_p, 95, 44, 4, "InDC");
    delay(20);
    LCD_print(glcd_p, InDC_pos_x + 24, InDC_pos_y, 1, "A");
    delay(60);

    //Imprimimos las letras gigantes de RPM
    LCD_print(glcd_p, 95, 25, 3, "RPM");

}
Exemplo n.º 2
0
void main() {
   
    __builtin_disable_interrupts();

    // set the CP0 CONFIG register to indicate that kseg0 is cacheable (0x3)
    __builtin_mtc0(_CP0_CONFIG, _CP0_CONFIG_SELECT, 0xa4210583);

    // 0 data RAM access wait states
    BMXCONbits.BMXWSDRM = 0x0;

    // enable multi vector interrupts
    INTCONbits.MVEC = 0x1;

    // disable JTAG to get pins back
    DDPCONbits.JTAGEN = 0;
    
    // do your TRIS and LAT commands here
    TRISA = 0xFFCF; 
    TRISB = 0b0001111001110011;
   i2c_master_setup();
    ANSELBbits.ANSB2 = 0; //SDA2 set to digital
    ANSELBbits.ANSB3 = 0; //SCL2 set to digital
 
    __builtin_enable_interrupts();
   // SYSTEMConfigPerformance(48000000);
    
    SPI1_init(); 
    LCD_init();
    LCD_clearScreen(0);
//    RPB13Rbits.RPB13R = 0b0011; //SDO
//    SDI1Rbits.SDI1R = 0b0000; //A1
    RPB7Rbits.RPB7R = 0b0101; //OC1
    RPB8Rbits.RPB8R = 0b0101; //OC2
    PORTAbits.RA4 = 1; //led init
    T2CONbits.TCKPS = 2; //timer 2 prescale = 1:4
    PR2 = 1999; //period = (PR2+1) * N * 12.5 ns = 100 us, 10 kHz
    TMR2 = 0;
    OC1RS = 1000;
    OC1R = 1000;
    OC2RS = 1000;
    OC2R = 1000;
    OC1CONbits.OCTSEL = 0; //select timer2
    OC2CONbits.OCTSEL = 0;
    OC1CONbits.OCM = 0b110; //set pwm mode
    OC2CONbits.OCM = 0b110;
    T2CONbits.ON = 1;
    OC1CONbits.ON = 1;
    OC2CONbits.ON = 1;
    
    unsigned char x = 0; //sine counter
    unsigned char y = 0; //triangle counter
    char pressed = 0; //for tracking button logic
    char counter = 0;
    char m = 100; //(triangle wave frequency is 1000/2m)
    unsigned char voltage = 0;
    unsigned char channel = 0;
    int bytes = 14;
    unsigned char i2cdata[bytes];
    unsigned char i2cdatatest;
    unsigned char i2cwhoami;
    short temp = 0;
    short accel_x = 0;
    short accel_y = 0;
    short accel_z = 0;
    short gyro_x = 0;
    short gyro_y = 0;
    short gyro_z = 0;
    char i2cdatacount = 0;
    char textbuffer[20];
    char length = 0;
    
    i2cwhoami = i2c_master_read(GYRO,WHOAMI,0,0);
    i2c_master_write(GYRO,CTRL1_XL,0b10000000,0);
    i2c_master_write(GYRO,CTRL2_G,0b10000000,0);
    i2c_master_write(GYRO,CTRL3_C,0b00000100);
    //i2cdatatest = i2c_master_read(GYRO,CTRL1_XL,0,0);
//    i2c_master_write(GYRO,CTRL1_XL,0b10000000,1);
//    i2c_master_send(0b10000000);
//    i2c_master_send(0b00000100);
//    i2c_master_stop();
  
    
   
    CS = 1;
    int leet = 1337;
    sprintf(textbuffer,"Hello world %d!",leet);
    char text[2] = {'H','6'};
 
    //int text[5] = {40,30,50,30,20};
     LCD_clearScreen(0);
     
    while(1) {
        length = sizeof(textbuffer);//size must be taken here otherwise pointer size is taken instead of 
       LCD_type(28,32,textbuffer,length,0b1111100000000000);
       //LCD_char(28,32,30,0b1111100000000000);
        
    
       i2c_master_multiread(GYRO,OUT_TEMP_L,bytes,i2cdata);
//       //i2cdatatest = i2c_master_read(GYRO,0x28,0,0);
       temp = i2cdata[1];
       temp = (temp<<8)|i2cdata[0];
       temp = (unsigned short)temp;
        
       gyro_x = i2cdata[3];
       gyro_x = (gyro_x<<8)|i2cdata[2];
       gyro_x = (unsigned short)gyro_x;
       
       gyro_y = i2cdata[5];
       gyro_y = (gyro_y<<8)|i2cdata[4];
       gyro_y = (unsigned short)gyro_y;
       
       gyro_z = i2cdata[7];
       gyro_z = (gyro_z<<8)|i2cdata[6];
       gyro_z = (unsigned short)gyro_z;
       
       accel_x = i2cdata[9];
       accel_x = (i2cdata[9]<<8)|i2cdata[8];
       accel_x = (unsigned short)accel_x;
       
      accel_y = i2cdata[11];
       accel_y = (i2cdata[11]<<8)|i2cdata[10];
       accel_y = (unsigned short)accel_y;
       
       accel_z = i2cdata[13];
       accel_z = (accel_z<<8)|i2cdata[12];
       accel_z = (unsigned short)accel_z;
       
        
       OC1RS = floor((accel_x/16.768));
       OC2RS = floor((accel_y/16.768));
       if(OC1RS>2999){
           OC1RS = 2000;
            }
       else if(OC1RS<1000){
           OC1RS = 0;
       }
       else{
           OC1RS = OC1RS - 1000;
       }
       
       
       if(OC2RS>2999){
           OC2RS = 2000;
            }
       else if(OC2RS<1000){
           OC2RS = 0;
       }
       else{
           OC2RS = OC2RS - 1000;
       }
//       OC1R = floor((gyro_x/3.2768 + 10000));
//       OC2R = floor((gyro_y/3.2768 + 10000));
//-------------SPI debugging for IMU------------------
//       if (!PORTBbits.RB4){
//       CS = 0;
//            channel = counter;
//            //voltage = floor(100*sin((x*2*pi)/100)+100);
//            voltage = i2cwhoami;
//            //char voltage = 0b10101001;
//            spi1_set(channel,voltage);
//            //delay(6000);
//            CS = 1;
//       }
//       else {
//            CS = 0;
//            channel = counter;
//            //voltage = floor(100*sin((x*2*pi)/100)+100);
//            voltage = i2cdata[0];
//            //char voltage = 0b10101001;
//            spi1_set(channel,voltage);
//            //delay(6000);
//            CS = 1;
//            //++i2cdatacount %14;
//       }
//-------------------------------------------------------
       delay(960000);
                 }
            }
Exemplo n.º 3
0
Arquivo: IMU.c Projeto: ayang199/ME433
void main(){
    // *************** Initialize pins and chips ****************
    //SYSTEMConfigPerformance(48000000);
    __builtin_disable_interrupts();

    // set the CP0 CONFIG register to indicate that kseg0 is cacheable (0x3)
    __builtin_mtc0(_CP0_CONFIG, _CP0_CONFIG_SELECT, 0xa4210583);

    // 0 data RAM access wait states
    BMXCONbits.BMXWSDRM = 0x0;

    // enable multi vector interrupts
    INTCONbits.MVEC = 0x1;

    // disable JTAG to get pins back
    DDPCONbits.JTAGEN = 0;
    
    // do your TRIS and LAT commands here
    TRISA = 0xFFEF; // set pin 4 as output
    TRISB = 0b0001111001110011;     // set outputs/inputs
    
    // turn off analog pins
    ANSELBbits.ANSB2 = 0;
    ANSELBbits.ANSB3 = 0;
    
    // setup i2c
    i2c_master_setup();
    
    __builtin_enable_interrupts();
    
    // setup accelerometer
    i2c_write(ADDR, 0x10, 0b10000000);
    i2c_write(ADDR, 0x11, 0b10000000);
    i2c_write(ADDR, 0x12, 0b00000100);
    
    // initialize PWM
    RPB7Rbits.RPB7R = 0b0101; //OC1 on pin 16
    RPB8Rbits.RPB8R = 0b0101; //OC2 on pin 17
    T2CONbits.TCKPS = 4;    // Timer2 prescaler N=16 (1:16)
    PR2 = 4999;             // period = (PR2+1) * N * 12.5 ns = 1 ms, 1 kHz
    TMR2 = 0;               // initial TMR2 count is 0
    OC1RS = 2500;           // duty cycle = OC1RS/(PR2+1) = 50%
    OC1R = 2500;            // initialize before turning OC1 on; afterward it is read-only
    OC2RS = 2500;
    OC2R = 2500;
    OC1CONbits.OCTSEL = 0;  // select timer2
    OC2CONbits.OCTSEL = 0;
    OC1CONbits.OCM = 0b110; // PWM mode without fault pin; other OC1CON bits are defaults
    OC2CONbits.OCM = 0b110;
    T2CONbits.ON = 1;       // turn on Timer2
    OC1CONbits.ON = 1;      // turn on OC1
    OC2CONbits.ON = 1;
    
//    RPB13Rbits.RPB13R = 0b0011; //SDO
//    CS = 1;
    
    // initialize SPI
    SPI_init();
    LCD_init();
    LCD_clearScreen(RED);
    
    //***********************************************************
    
    
    
    
    //****************** constants ***************************
    char length = 14;
    unsigned char IMUdata[length];
    
    short temp;
    unsigned short temperature;
    
    short gx;
    short gy;
    short gz;
    
    unsigned short gyrox;
    unsigned short gyroy;
    unsigned short gyroz;
    
    short ax;
    short ay;
    short az;
    
    unsigned short accelx;
    unsigned short accely;
    unsigned short accelz;
    
    char text[100];
    
    //**********************************************************
    
    
    
    while(1){        
        int var = 1337;
        sprintf(text,"Hello World %d!",var);
        LCD_print(28,32,text,WHITE);
        
        
        //************ TEST IF IMU WORKS ***********************
//        unsigned char data = 0;
//        data = i2c_read(ADDR,0x0F);
//        
//        // read output from SPI
//        CS=0;
//        setVoltage(0,data);
//        CS=1;
        
        //************* SHOULD GET 01101001 *******************

        
        //********** READ MULTI REGISTERS FROM IMU ***************
//        i2c_multiread(ADDR,0x20,IMUdata,length);
//        
//        temp = (IMUdata[1]<<8) | IMUdata[0];
//        temperature = temp + 32768;
//        
//        gx = (IMUdata[3]<<8) | IMUdata[2];
//        gyrox = gx + 32768;
//        
//        gy = (IMUdata[5]<<8) | IMUdata[4];
//        gyroy = gy + 32768;
//        
//        gz = (IMUdata[7]<<8) | IMUdata[6];
//        gyroz = gz + 32768;
//        
//        ax = (IMUdata[9]<<8) | IMUdata[8];
//        accelx = ax + 32768;
//        
//        ay = (IMUdata[11]<<8) | IMUdata[10];
//        accely = ay + 32768;
//        
//        az = (IMUdata[13]<<8) | IMUdata[12];
//        accelz = az + 32768;
//        
//        //********************************************************
//        
//        
//        
//        //*********************** PWM ****************************
//        // Calculate OC1RS and OC2RS
//        OC1RS = floor(accelx/6.5535);
//        OC2RS = floor(accely/6.5535);
//        
//        // Set OC1 limits
//        if (OC1RS > 7500){
//            OC1RS = 5000;
//        }
//        else if (OC1RS < 2500){
//            OC1RS = 0;
//        }
//        else {
//            OC1RS = OC1RS-2500;
//        }
//        
//        // Set OC2 limits
//        if (OC2RS > 7500){
//            OC2RS = 5000;
//        }
//        else if (OC2RS < 2500){
//            OC2RS = 0;
//        }
//        else {
//            OC2RS = OC2RS-2500;
//        }
        
        //******************************************************
        
        

        delay(24000); // Delay 1ms
    }
    
}