Esempio n. 1
0
/*****************************************************************************
* Function Name : gyroSetupPeripheral
* Description   : Setup I2C for gyroscope
* Parameters    : None
* Return Value  : None
*****************************************************************************/
static inline void gyroSetupPeripheral(void) {
    unsigned int I2C2CONvalue, I2C2BRGvalue;
    I2C2CONvalue = I2C2_ON & I2C2_IDLE_CON & I2C2_CLK_HLD &
                   I2C2_IPMI_DIS & I2C2_7BIT_ADD & I2C2_SLW_DIS &
                   I2C2_SM_DIS & I2C2_GCALL_DIS & I2C2_STR_DIS &
                   I2C2_NACK & I2C2_ACK_DIS & I2C2_RCV_DIS &
                   I2C2_STOP_DIS & I2C2_RESTART_DIS & I2C2_START_DIS;

    // BRG = Fcy(1/Fscl - 1/10000000)-1, Fscl = 400KHz
    I2C2BRGvalue = 95;
    OpenI2C2(I2C2CONvalue, I2C2BRGvalue);
    IdleI2C2();
}
Esempio n. 2
0
/*
 * configures and starts incoming data communication
 * The incoming I2C uses MSSP2
 */
int setupIncoming() {

    int SLAVE_ADDRESS = 0x28; // the slave address
    // set pins RC14, RC15 as inputs
    TRISCbits.TRISC3 = 1; // SCL1
    ANSELCbits.ANSC3 = 0;

    TRISCbits.TRISC4 = 1; // SDA1
    ANSELCbits.ANSC4 = 0;

    // configure MSSP2 for i2c slave operation.
    CloseI2C2();
    OpenI2C2(SLAVE_7, SLEW_OFF);
    SSPADD = SLAVE_ADDRESS;
    return (1);
}
Esempio n. 3
0
void I2C2_INIT(S32 v_I2Cfreq_S32, U8 v_IntConfig_U8)
 {
    /*** Local variable ***/  
    S32 v_Fosc_S32 = 4E6;
    U8  v_I2CBRG_U8 = 3;

    // Get the Oscillator Frequency
    v_Fosc_S32 = GET_FreqOsc();
    // Calculate BaudRate
    v_I2CBRG_U8 =  ((F32)v_Fosc_S32/v_I2Cfreq_S32) - ((F32)v_Fosc_S32/10000000) - 1;

    // Configure Interrupts
    ConfigIntI2C2(v_IntConfig_U8);
    // Open I2C with specified BaudRate
    OpenI2C2(I2C_ON, v_I2CBRG_U8);

 }
/******************************************************************************
 * Function:        void Init_IOExpander(void)
 *
 * PreCondition:    None
 *
 * Input:           None
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:        MACInit enables the Ethernet module, waits for the
 *                  to become ready, and programs all registers for future
 *                  TX/RX operations.
 *
 * Note:            This function blocks for at least 1ms, waiting for the
 *                  hardware to stabilize.
 *****************************************************************************/
void Init_IOExpander(void)
{
	while (Return_Val == Busy)
	{
		switch ( CMI )
		{
			case	INIT	:	// Clear MCLR pins of the IOexpanders
								IO_Expander_Enable = True;
								
								// Give the IO expander some time after de-reset
								Delay1KTCYx(10);
								
								// Use MSSP2 the pins of this module are connected to RD5 and RD6, 
								// all calls to I2C lib must be nummerated with 2 : OpenI2C2																													   _																																	
								//---INITIALISE THE I2C MODULE FOR MASTER MODE WITH 100KHz ---							
								OpenI2C2(MASTER,SLEW_ON);
								
								//400kHz Baud clock @41.667MHz = 0x19 // 100kHz Baud clock @41.667MHz = 0x67 // 1MHz Baud clock @41.667MHz = 0x09 // 1.7MHzBaud clock @41.667MHz = 0x05
								SSP2ADD=0x09;
								
								//read any previous stored content in buffer to clear buffer full status   EXPNDNQ
								data = SSP2BUF;	
								
								//init Active_IOEXP with te number of total IOexpanders
								Active_IOEXP = 0;
								
								CMI = START;
								break;
								
			case	START	:	switch ( CMI2 )
								{
									case	INIT	:	if ( (Active_IOEXP >= EXPNDNQ))
														{
															Return_Val = Finished;
															CMI = INIT;
															CMI2 = INIT;
															Active_IOEXP = 0;
															break;
														}
														Active_REG 		= 0;
														Active_DATA 	= 0;
														CMI2				= IDLE;
														break;
														
									case	IDLE	:	if ( Active_REG >= WRITE_REG )
														{
															CMI2 = INIT;
															Active_IOEXP++;
															break;
														}
														CMI = IDLE;
														CMI2 = STARTI2C;
														break;
														
									case	STARTI2C:	StartI2C2();
														CMI2 = WRITE1;
														break;
														
									case	WRITE1	:	CMI = ADDR;
														CMI2 = WRITE2;
														break;
														
									case	WRITE2	:	CMI = REG;
														CMI2 = WRITE3;
														break;
														
									case	WRITE3	:	CMI = DATAOUT;
														CMI2 = STOPI2C;
														break;
														
									case	STOPI2C	:	Active_REG++;
														Active_DATA++;
														StopI2C2();
														CMI2 = RSTRTI2C;
														break;
														
									case	RSTRTI2C:	RestartI2C2();
														CMI2 = IDLE;
														break;
														
									default			:	break;
								}
								break;
								
			case	IDLE	:	switch ( Return_Val_Routine = IdleI2C2() )								//check for bus idle condition in multi master communication
								{
									case	Finished	:	CMI = START;
															break;
															
									case	Busy		:	CMI = IDLE;
															break;
																
									default				:	CMI = IDLE;
															break;
								}
								break;
								
			case	ADDR	:	switch ( Return_Val_Routine = WriteI2C2 (IOEXP[Active_IOEXP].GADDR) )
								{
									case	Finished	:	CMI = START;
															break;
									
									case	Busy		:	CMI = ADDR;
															break;
																															
									default				:	CMI = ADDR;
															break;
								}
								break;
								
			case	REG		:	switch ( Return_Val_Routine = WriteI2C2 (GREG[Active_REG]) )
								{
									case	Finished	:	CMI = START;
															break;
									
									case	Busy		:	CMI = REG;
															break;
																															
									default				:	CMI = REG;
															break;
								}
								break;
								
			case	DATAOUT	:	switch ( Return_Val_Routine = WriteI2C2 (IOEXP[Active_IOEXP].GDATA[Active_DATA]) )
								{
									case	Finished	:	CMI = START;
															break;
									
									case	Busy		:	CMI = DATAOUT;
															break;
																															
									default				:	CMI = DATAOUT;
															break;
								}
								break;
								
			default			:	break;
		}
	}
}
Esempio n. 5
0
void main(void) {
        // === enable system wide interrupts  ========
    INTEnableSystemMultiVectoredInt();

//    // init the threads 
    PT_INIT(&pt_uart);

    PT_setup();
    ANSELA = 0; //make sure analog is cleared
    ANSELB = 0;
   
    OpenI2C2( I2C_EN, BRG_VAL ); 
    I2C2CON |= I2C_7BIT_ADD + I2C_SM_EN;
    I2C2CON |= I2C_RESTART_EN; //restart needed to read from ir temp
    unsigned char cmd = 0; //command line 
    unsigned char data = 0; //output data, digital value to be converted
    //unsigned char addr = 0x1C; 
    //Thermometer sensor: 5A
    
    //===========Configuring the IMU========================//
    unsigned int OPR_CODE = 0x3D;
    SendIMUData(OPR_CODE, 0x0C);        //sets IMU mode
            
    //========================================================//
            
    while(1){
//        temp = RcvIRTemp();
//        cels = temp / 50 - 273; //convert read value to celsius
//        delay_ms(10);
        PT_SCHEDULE(protothread_uart(&pt_uart));
        temp = RcvIMUData(0x20);
        delay_ms(10);

    }


//    // PuTTY
    clrscr();  //clear PuTTY screen
    home();
//    // By default, MPLAB XC32's libraries use UART2 for STDOUT.
//    // This means that formatted output functions such as printf()
//    // will send their output to UART2

//-----------------------------------------------//    
   
   
   
   


    // === configure threads ==========
    // turns OFF UART support and debugger pin, unless defines are set

    // === enable system wide interrupts  ========
    INTEnableSystemMultiVectoredInt();

//    // init the threads 
    PT_INIT(&pt_uart);


//    // === set up input capture 
//  // based on timer3 (need to configure timer 3 seperately)
//  OpenCapture1(IC_EVERY_RISE_EDGE | IC_INT_1CAPTURE | IC_CAP_32BIT | IC_TIMER2_SRC | IC_ON);
//  // turn on the interrupt so that every capture can be recorded
//  ConfigIntCapture1(IC_INT_ON | IC_INT_PRIOR_3 | IC_INT_SUB_PRIOR_3);
//  INTClearFlag(INT_IC1);
//  // connect PIN 24 to IC1 capture unit
//  PPSInput(3, IC1, RPB13);
//  mPORTBSetPinsDigitalIn(BIT_13); //Set port as input
//
//    // round-robin scheduler for threads
    while (1) {
        PT_SCHEDULE(protothread_uart(&pt_uart));
    }
} // main
Esempio n. 6
0
File: main.c Progetto: reebot/wins
int main (void)
{
        //CLKDIVbits.RCDIV = 0b000; FRC clock divider
        unsigned char accel_xh = 0x00;
        unsigned char accel_xl = 0x00;
        unsigned char accel_yh = 0x00;
        unsigned char accel_yl = 0x00;
        unsigned char accel_zh = 0x00;
        unsigned char accel_zl = 0x00;
        unsigned int _60s = 0;
        int g_x=0;
        int g_y=0;
        int g_z=0;
	// Disable Watch Dog Timer
	RCONbits.SWDTEN = 0;
	// for LED
	ODCAbits.ODA6 = 0;
	TRISAbits.TRISA6 = 0;
        TRISAbits.TRISA0 = 0;
        TRISAbits.TRISA1 = 0;
        TRISAbits.TRISA5 = 0;
        //TRISAbits.TRISA2 = 0; scl2
        TRISAbits.TRISA4 = 0;
        //TRISAbits.TRISA3 = 0; sda2

        // push button
        TRISAbits.TRISA7 = 1;
        TRISDbits.TRISD6 = 1;
        TRISDbits.TRISD7 = 1;
        TRISDbits.TRISD13 = 1;


        //Enable channel
        SPI1Init();
        LCDInit();
	OpenI2C1( I2C_ON, I2C_BRG );
	OpenI2C2( I2C_ON, I2C_BRG );
        unsigned char lcd_data1[16];
        unsigned char lcd_data2[16];
        Setup_MPU6050();
        _05ms = false;
        LDByteWriteI2C(MPU6050_ADDRESS,MPU6050_RA_PWR_MGMT_1 , 0x00);   //turn on IMU
        TimerInit();

        lcd_data1[12] = ' ';
        lcd_data1[13] = ' ';
        lcd_data1[14] = ' ';
        lcd_data1[15] = ' ';

        lcd_data2[6]='y';
        lcd_data2[7]=' ';
        lcd_data2[8]=' ';
        lcd_data2[9]='z';
        bool accel_queue = true;


        while (1) {
            //LATAbits.LATA0 = 0;
            //LATAbits.LATA6 = 0;
            //LATAbits.LATA5 = 0;
            //LATAbits.LATA1 = 0;
            if (_60s==20000){
                _60s = 0;
                LDWordReadI2C(FUELGAUGE_ADDRESS, FUELGAUGE_CONFIG, &bat1, &bat2);
                LDWordReadI2C(FUELGAUGE_ADDRESS, FUELGAUGE_SOC, &bat1, &bat2);
            }
            if(_05ms==true){
                //do something
                _60s+=1;
                if(accel_queue){
                    LDByteReadI2C(MPU6050_ADDRESS,MPU6050_RA_ACCEL_XOUT_H , &rpiData.imu[accel_p], 6);

                    if(rpiData.imu[accel_p]==0xff && rpiData.imu[accel_p+1]==0xff)
                        LDByteWriteI2C(MPU6050_ADDRESS,MPU6050_RA_PWR_MGMT_1 , 0x00);   //turn on IMU

                accel_xh=rpiData.imu[accel_p];
                accel_xl=rpiData.imu[accel_p+1];
                accel_yh=rpiData.imu[accel_p+2];
                accel_yl=rpiData.imu[accel_p+3];
                accel_zh=rpiData.imu[accel_p+4];
                accel_zl=rpiData.imu[accel_p+5];
                    accel_p+=12;
                    if (accel_p>=1200)
                        accel_p=0;
                    lcd_data1[7] = 'A';
                    lcd_data1[8] = 'C';
                    lcd_data1[9] = 'C';
                    lcd_data1[10] = 'E';
                    lcd_data1[11] = 'L';
                    accel_queue=false;
                }
                else{
                    LDByteReadI2C(MPU6050_ADDRESS,MPU6050_RA_GYRO_XOUT_H , &rpiData.imu[gyro_p], 6);

                accel_xh=rpiData.imu[gyro_p];
                accel_xl=rpiData.imu[gyro_p+1];
                accel_yh=rpiData.imu[gyro_p+2];
                accel_yl=rpiData.imu[gyro_p+3];
                accel_zh=rpiData.imu[gyro_p+4];
                accel_zl=rpiData.imu[gyro_p+5];
                    gyro_p+=12;
                    if (gyro_p>=1206)
                        gyro_p=6;

                    lcd_data1[7] = 'G';
                    lcd_data1[8] = 'Y';
                    lcd_data1[9] = 'R';
                    lcd_data1[10] = 'O';
                    lcd_data1[11] = ' ';
                    accel_queue=true;
                }

                LCDwriteLine(LCD_LINE1, lcd_data1);
                LCDwriteLine(LCD_LINE2, lcd_data2);
                _05ms=false;
                g_x=accel_xl|accel_xh<<8;
                lcd_data1[0]=g_x < 0? '-' : ' ';
                g_x=g_x > 0 ? g_x : -g_x;

                g_y=accel_yl|accel_yh<<8;
                lcd_data2[0]=g_y < 0? '-' : ' ';
                g_y=g_y > 0 ? g_y : -g_y;

                g_z=accel_zl|accel_zh<<8;
                lcd_data2[10]=g_z < 0? '-' : ' ';
                g_z=g_z > 0 ? g_z : -g_z;
                lcd_data1[5]=(g_x%10)+'0';
                lcd_data1[4]=(g_x/10)%10+'0';
                lcd_data1[3]=(g_x/100)%10+'0';
                lcd_data1[2]=(g_x/1000)%10+'0';
                lcd_data1[1]=(g_x/10000)%10+'0';

                lcd_data2[5]=(g_y%10)+'0';
                lcd_data2[4]=((g_y/10)%10)+'0';
                lcd_data2[3]=((g_y/100)%10)+'0';
                lcd_data2[2]=(g_y/1000)%10+'0';
                lcd_data2[1]=(g_y/10000)%10+'0';

                lcd_data2[15]=(g_z%10)+'0';
                lcd_data2[14]=(g_z/10)%10+'0';
                lcd_data2[13]=(g_z/100)%10+'0';
                lcd_data2[12]=(g_z/1000)%10+'0';
                lcd_data2[11]=(g_z/10000)%10+'0';

            }
      }
	return 0;
}
void main(void) {
    
    PT_setup();
    ANSELA = 0; //make sure analog is cleared
    ANSELB = 0;
   
    OpenI2C2( I2C_EN, BRG_VAL ); 
    I2C2CON |= I2C_7BIT_ADD + I2C_SM_EN;
    I2C2CON |= I2C_RESTART_EN; //restart needed to read from ir temp
    unsigned char cmd = 0; //command line 
    unsigned char data = 0; //output data, digital value to be converted
    //unsigned char addr = 0x1C; 
    //Thermometer sensor: 5A
    
//    SendData(0x75, addr);
//    temp = RcvData2(addr);
   
    while(1){
        //MasterWriteI2C2(0xb4);
    temp = RcvIRTemp(addr);
    delay_ms(10);
    }
    //temp = RcvIRTemp(addr);
//    while(1);
//    delay_ms(5000);
//        while(1) // run the code over and over again
//    { 
//
//        // start the I2C communication
//        StartI2C1(); // Send the Start Bit (begin of data send) 
//        IdleI2C1(); // Wait to complete 
//
//        // write the address of the chip, defined by pins AD0 and AD1 on the MAX518
//        MasterWriteI2C1 (addr); // address 
//        IdleI2C1(); 
//        
//        
//        while( !I2C1STATbits.ACKSTAT==0 ){}
//        
//        // write the command to tell the MAX518 to change its output on output 0
//        MasterWriteI2C1 (cmd); // command line 
//        IdleI2C1(); 
//        while( !I2C1STATbits.ACKSTAT==0 ){}
//
//        // wite the value to put on the output
//        MasterWriteI2C1(data); // output 
//        IdleI2C1(); 
//        while( !I2C1STATbits.ACKSTAT==0 ){}
//
//        // end the I2C communication
//        StopI2C1(); // end of data send 
//        IdleI2C1(); // Wait to complete 
//
//        // the total write time is ~285us with an I2C clock of 100 kHz
//
//        data++; // increase the data, make a sawtooth wave as an example
//
//        //if(data>255) { // don't have to worry about this, data is 8 bit so it will roll over automatically
//        //    data=0;
//        //}
//
//    } // end while(1)
//    
    
    
    
//------- uncomment to init the uart2 -----------//
//    UARTConfigure(UART2, UART_ENABLE_PINS_TX_RX_ONLY);
//    UARTSetLineControl(UART2, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1);
//    UARTSetDataRate(UART2, PB_FREQ, BAUDRATE);
//    UARTEnable(UART2, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX));
//    ConfigIntUART2(UART_RX_INT_EN | UART_TX_INT_EN | UART_ERR_INT_EN | UART_INT_PR0 | UART_INT_SUB_PR0);

     //rxchar = 0 ; // a received character
     //count = 0 ; // count the number of characters

//    // PuTTY
    clrscr();  //clear PuTTY screen
    home();
//    // By default, MPLAB XC32's libraries use UART2 for STDOUT.
//    // This means that formatted output functions such as printf()
//    // will send their output to UART2

//-----------------------------------------------//    
   
   
   
   


    // === configure threads ==========
    // turns OFF UART support and debugger pin, unless defines are set

    // === enable system wide interrupts  ========
    INTEnableSystemMultiVectoredInt();

//    // init the threads 
    PT_INIT(&pt_uart);
//    PT_INIT(&pt_anim);
   
// ---------- uncomment to init the tft display -----------//
//    tft_init_hw();
//    tft_begin();
//    tft_fillScreen(ILI9340_BLACK); //240x320 vertical display
//    tft_setRotation(0); // Use tft_setRotation(1) for 320x240
//---------------------------------------------------------//
    

//    // === set up input capture 
//	// based on timer3 (need to configure timer 3 seperately)
//	OpenCapture1(IC_EVERY_RISE_EDGE | IC_INT_1CAPTURE | IC_CAP_32BIT | IC_TIMER2_SRC | IC_ON);
//	// turn on the interrupt so that every capture can be recorded
//	ConfigIntCapture1(IC_INT_ON | IC_INT_PRIOR_3 | IC_INT_SUB_PRIOR_3);
//	INTClearFlag(INT_IC1);
//	// connect PIN 24 to IC1 capture unit
//	PPSInput(3, IC1, RPB13);
//	mPORTBSetPinsDigitalIn(BIT_13); //Set port as input
//
//    // round-robin scheduler for threads
    while (1) {
        PT_SCHEDULE(protothread_uart(&pt_uart));
        //PT_SCHEDULE(protothread_anim(&pt_anim));
    }
} // main