/* int MUXTest() { UINT8 address; //UINT8 check = 0; //UINT8 trByte; UINT8 recByte; recByte = 0; //Write control byte to mux StartI2C1(); IdleI2C1(); //Wait to complete address = (PCAMUX_ADD<<1) & 0xFE; //write MasterWriteI2C1(address); IdleI2C1(); if(I2C1STATbits.ACKSTAT) { StopI2C1(); IdleI2C1(); return -1; } MasterWriteI2C1(0x0A); IdleI2C1(); if(I2C1STATbits.ACKSTAT) { StopI2C1(); IdleI2C1(); return -1; } delay_t1(100); //Read byte from mux StartI2C1(); IdleI2C1(); //Wait to complete address = (PCAMUX_ADD<<1) | 0x01; //read MasterWriteI2C1(address); IdleI2C1(); if(I2C1STATbits.ACKSTAT) { StopI2C1(); IdleI2C1(); return -1; } recByte = MasterReadI2C1(); NotAckI2C1(); IdleI2C1(); StopI2C1(); IdleI2C1(); return recByte; } */ int MuxRead(void){ UINT8 address = 0; UINT8 recByte = 0; //Read byte from mux I2C1Start(); I2C1Idle(); address = (PCAMUX_ADD<<1) | 0x01; //read I2C1MasterWrite(address); I2C1Idle(); if(I2C1STATbits.ACKSTAT) { I2C1Stop(); I2C1Idle(); return -1; } recByte = I2C1MasterRead(); I2C1NotAck(); I2C1Idle(); I2C1Stop(); I2C1Idle(); return recByte; }
/* * Function select I2C "PCA9547" multiplexor channel * inputs: enable - mux enable * channel - mux channel - [0, 7] * returns: -1 - error * [0 7] - No of selected channel */ int I2C1ChSelect(UINT8 enable, UINT8 channel) { UINT8 address, data, recByte; digitalHigh(MUXRST); if(channel > 7) //Check channel range return -1; data = (enable << 3) | channel; //Write byte to mux I2C1Start(); I2C1Idle(); address = (PCAMUX_ADD<<1) & 0xFE; //write I2C1MasterWrite(address); I2C1Idle(); if(I2C1STATbits.ACKSTAT) { I2C1Stop(); I2C1Idle(); return -2; } I2C1MasterWrite(data); I2C1Idle(); if(I2C1STATbits.ACKSTAT) { I2C1Stop(); I2C1Idle(); return -3; } int i = 0; while((data != recByte)&&(i<3)){ recByte = MuxRead(); data &= 0x0F; recByte &= 0x0F; i++; } if(data != recByte){ //Channel selection error return -1; } recByte &= 0x07; return (char)recByte; }
int I2C1ReadByte(UINT8 slaveAdd, UINT8 registerAdd) { unsigned char recByte = 0; UINT8 address; I2C1Start(); I2C1Idle(); address = (slaveAdd<<1) & 0xFE; //write I2C1MasterWrite(address); I2C1Idle(); if(I2C1STATbits.ACKSTAT) { I2C1Stop(); I2C1Idle(); return -1; } //Send register register location I2C1MasterWrite(registerAdd); I2C1Idle(); if(I2C1STATbits.ACKSTAT) { I2C1Stop(); I2C1Idle(); return -1; } I2C1Restart(); I2C1Idle(); address = (slaveAdd<<1) | 0x01; //Read I2C1MasterWrite(address); I2C1Idle(); if(I2C1STATbits.ACKSTAT) { I2C1Stop(); I2C1Idle(); return -1; } recByte = I2C1MasterRead(); //I2C1NotAck(); I2C1Idle(); I2C1Stop(); I2C1Idle(); return recByte; }
/* * function reads byte from I2C data * inputs: slaveAdd * data * returns: 0 -> error is occurred * 1 -> return read data */ int I2C1WriteByte(UINT8 slaveAdd, UINT8 registerAdd, UINT8 data) { UINT8 address; I2C1Start(); I2C1Idle(); address = (slaveAdd<<1) & 0xFE; //write I2C1MasterWrite(address); I2C1Idle(); if(I2C1STATbits.ACKSTAT) { I2C1Stop(); I2C1Idle(); return -1; } //Send register register location I2C1MasterWrite(registerAdd); I2C1Idle(); if(I2C1STATbits.ACKSTAT) { I2C1Stop(); I2C1Idle(); return -1; } //Send register register location I2C1MasterWrite(data); I2C1Idle(); if(I2C1STATbits.ACKSTAT) { I2C1Stop(); I2C1Idle(); return -1; } I2C1Stop(); return 1; //Byte is successful sent }
// old version of single channel PWM in void __attribute__ ((interrupt("IRQ"))) pwmin_SC_ISR(void) { if (PWMChannelR2 == 0x4) { // read the HMC compass as DRDY went high IO2_INT_CLR |= 0x4; I2C1Start(); } else { if (timer_counter1 < timerTemp) { IO0_INT_EN_R |= 0x00078070; IO2_INT_EN_R |= 0x000000A4; timer_counter1 = 0; timerTemp = 0; IO0_INT_CLR = 0xffffffff; IO2_INT_CLR = 0xffffffff; } else { timerTemp = timer_counter1; PWMChannelR1 = IO0_INT_STAT_R; PWMChannelR2 = IO2_INT_STAT_R; PWMChannelF = IO0_INT_STAT_F; PWMChannelF2 = IO2_INT_STAT_F; IO_PINS = FIO0PIN; IO_PINS2 = FIO2PIN; if (PWMChannelR1 != 0) { switch (PWMChannelR1) { case PWMIN_PIN_0: PWM_SC_Temp[0] = timerTemp; break; case PWMIN_PIN_1: PWM_SC_Temp[1] = timerTemp; break; case PWMIN_PIN_2: PWM_SC_Temp[2] = timerTemp; break; case PWMIN_PIN_3: PWM_SC_Temp[3] = timerTemp; break; case PWMIN_PIN_4: PWM_SC_Temp[4] = timerTemp; break; case PWMIN_PIN_5: PWM_SC_Temp[5] = timerTemp; break; case PWMIN_PIN_6: PWM_SC_Temp[6] = timerTemp; break; } IO0_INT_EN_R ^= PWMChannelR1; IO0_INT_EN_F |= PWMChannelR1; IO0_INT_CLR |= PWMChannelR1; } if (PWMChannelR2 != 0) { switch (PWMChannelR2) { case PWMIN_PIN_7: PWM_SC_Temp[7] = timerTemp; break; case PWMIN_PIN_8: //PWM_SC_Temp[8] = timer_counter1; //disabled as PWM in for now to use it as the DRDY for the Compass //MM3_runtime.STATE = MM3_DRDY; break; } IO2_INT_EN_R ^= PWMChannelR2; IO2_INT_EN_F |= PWMChannelR2; IO2_INT_CLR |= PWMChannelR2; } if (PWMChannelF != 0) { switch (PWMChannelF) { case PWMIN_PIN_0: PWM_channel[0] = (signed int)(timerTemp - PWM_SC_Temp[0] - sysSetup.PWM.mpOffset[0]); if (PWM_channel[0] > 120) PWM_channel[0] = 120; if (PWM_channel[0] < -120) PWM_channel[0] = -120; break; case PWMIN_PIN_1: PWM_channel[1] = (signed int)(timerTemp - PWM_SC_Temp[1] - sysSetup.PWM.mpOffset[1]); if (PWM_channel[1] > 120) PWM_channel[1] = 120; if (PWM_channel[1] < -120) PWM_channel[1] = -120; break; case PWMIN_PIN_2: PWM_channel[2] = (signed int)(timerTemp - PWM_SC_Temp[2] - sysSetup.PWM.mpOffset[2]); if (PWM_channel[2] > 120) PWM_channel[2] = 120; if (PWM_channel[2] < -120) PWM_channel[2] = -120; break; case PWMIN_PIN_3: PWM_channel[3] = (signed int)(timerTemp - PWM_SC_Temp[3] - sysSetup.PWM.mpOffset[3]); if (PWM_channel[3] > 120) PWM_channel[3] = 120; if (PWM_channel[3] < -120) PWM_channel[3] = -120; break; case PWMIN_PIN_4: PWM_channel[4] = (signed int)(timerTemp - PWM_SC_Temp[4] - sysSetup.PWM.mpOffset[4]); if (PWM_channel[4] > 120) PWM_channel[4] = 120; if (PWM_channel[4] < -120) PWM_channel[4] = -120; break; case PWMIN_PIN_5: PWM_channel[5] = (signed int)(timerTemp - PWM_SC_Temp[5] - sysSetup.PWM.mpOffset[5]); if (PWM_channel[5] > 120) PWM_channel[5] = 120; if (PWM_channel[5] < -120) PWM_channel[5] = -120; break; case PWMIN_PIN_6: PWM_channel[6] = (signed int)(timerTemp - PWM_SC_Temp[6] - sysSetup.PWM.mpOffset[6]); if (PWM_channel[6] > 120) PWM_channel[6] = 120; if (PWM_channel[6] < -120) PWM_channel[6] = -120; break; } IO0_INT_EN_F ^= PWMChannelF; IO0_INT_EN_R |= PWMChannelF; IO0_INT_CLR |= PWMChannelF; } if (PWMChannelF2 != 0) { switch (PWMChannelF2) { case PWMIN_PIN_7: PWM_channel[7] = (signed int)(timerTemp - PWM_SC_Temp[7] - sysSetup.PWM.mpOffset[7]); if (PWM_channel[7] > 120) PWM_channel[7] = 120; if (PWM_channel[7] < -120) PWM_channel[7] = -120; break; case PWMIN_PIN_8: PWM_channel[8] =(signed int)(timerTemp - PWM_SC_Temp[8] - sysSetup.PWM.mpOffset[8]); if (PWM_channel[8] > 120) PWM_channel[8] = 120; if (PWM_channel[8] < -120) PWM_channel[8] = -120; break; } IO2_INT_EN_F ^= PWMChannelF2; IO2_INT_EN_R |= PWMChannelF2; IO2_INT_CLR |= PWMChannelF2; } } } PWM_valid = 10000; VICVectAddr = 0; }