void MCP3422_read3(uint8_t* byte1, uint8_t* byte2, uint8_t* config_byte) { StartI2C(); WriteI2C(0xD1); // I2C address (for reading) *byte1 = ReadI2C(); *byte2 = ReadI2C(); *config_byte = ReadLastI2C(); StopI2C(); }
/* * MAIN */ int main(int argc, char** argv) { // RA1 as DO LATA = 0; ADCON1 = 0x06; TRISA1 = 0; LATA1 = 1; // I2C configuration i2c_setup(); unsigned char w; for(w=0;w<20;w++) I2C_Recv[w]=0; unsigned int temps = 25; unsigned char temp, length=0; while(1){ LATA1 = ~LATA1; Delay10KTCYx(temps); check_interruptions(); if (DataRdyI2C()==1) { temps += 25; temp = ReadI2C(); //********************* Data reception from master by slave ********************* do { while(DataRdyI2C()==0); // WAIT UNTILL THE DATA IS TRANSMITTED FROM master I2C_Recv[length++]=getcI2C(); // save byte received } while(length!=20); } } //******************** write sequence from slave ******************************* while(SSPSTATbits.S!=1); //wait untill STOP CONDITION //********************* Read the address sent by master from buffer ************** while(DataRdyI2C()==0); //WAIT UNTILL THE DATA IS TRANSMITTED FROM master temp = ReadI2C(); //********************* Slave transmission ************************************ if(SSPSTAT & 0x04) //check if master is ready for reception while(putsI2C(I2C_Send)); // send the data to master //-------------TERMINATE COMMUNICATION FROM MASTER SIDE--------------- CloseI2C(); //close I2C module while(1) { LATA1 = ~LATA1; Delay10KTCYx(100); }; //End of program return (EXIT_SUCCESS); }
void main(void) { /* Configure the oscillator for the device */ ConfigureOscillator(); /* Initialize I/O and Peripherals for application */ InitApp(); // i2c conf // /* // i2c slave TRISBbits.TRISB3 = 1; TRISBbits.TRISB4 = 1; //Statement to configure the DATA pin as an input. OpenI2C(SLAVE_7, SLEW_OFF); SSPADD = 0xB0; // */ /* //i2c master TRISBbits.TRISB3 = 1; TRISBbits.TRISB4 = 1; //Statement to configure the DATA pin as an input. OpenI2C( MASTER, SLEW_OFF); SSPADD = 0x27; //SSPADD Baud Register used to calculate I2C clock speed in MASTER mode (in this case 100Khz) */ unsigned char addr; unsigned char data; int delayVal = 5; LEDPin = 1;//Set LED Pin while(1) { // /* //i2c slave if(DataRdyI2C() == 1) { addr = ReadI2C(); while(DataRdyI2C() == 0); data = ReadI2C(); } //*/ /* //i2c master StartI2C(); IdleI2C(); putcI2C( 0xB0 ); //send address IdleI2C(); putcI2C( 0x11 ); //send data IdleI2C(); StopI2C(); */ LEDPin = ~LEDPin;//Toggle LED Pin Delay10KTCYx(delayVal);//Delay 250K cycles (1 second at 1MHz since each instruction takes 4 cycles) } }
/** Read multiple words from a 16-bit device register. * @param devAddr I2C slave device address * @param regAddr First register regAddr to read from * @param length Number of words to read * @param data Buffer to store read data in * @return Number of words read (-1 indicates failure) */ int8 I2Cdev_readWords(uint8 devAddr, uint8 regAddr, uint8 length, uint16 *data) { #ifdef I2CDEV_SERIAL_DEBUG i2cdev_debug_depth++; int z; for(z=0;z<i2cdev_debug_depth;z++) DBG_vPrintf(TRACE_APP, "\t"); DBG_vPrintf(TRACE_APP, "readWords\r\n"); #endif uint8 count = 0; // Start I2C StartI2C(); // Device write address WriteAddressI2C(devAddr, COMMAND_WRITE); // Register address WriteRegisterI2C(regAddr); // Device read address WriteAddressI2C(devAddr, COMMAND_READ); for (count = 0; count < length; count++) { // The PIC 18 doesn't send an ACK between bytes of words. Is it a bug? // I think so (https://github.com/jrowberg/i2cdevlib/issues/172), therefore // I stick to my implementation data[count] = ReadI2C(SEND_ACK, SEND_CONTINUE) << 8; if (count == length - 1) { // NACK for final LSB, to end transmission data[count] |= ReadI2C(SEND_NACK, SEND_STOP); } else { // ACK otherwise, to continue reading data[count] |= ReadI2C(SEND_ACK, SEND_CONTINUE); count++; } } // Stop I2C StopI2C(); #ifdef I2CDEV_SERIAL_DEBUG i2cdev_debug_depth--; #endif return count; }
/*! \brief LCD IO Ports lesen \return LCD IO Port */ unsigned char GetIOLCD(void) { unsigned char data = 0x00; StartI2C(LCD_DEV+1); data = ReadI2C(0); StopI2C(); return data; }
void main(void) { unsigned char outputData[2]; unsigned int result = 0; unsigned char data = 0; Delay100TCYx(10); //let the device startup usart_init(); i2c_init(); printf("Starting\r\n"); /* ******************************* Compass code and ADSL Code ********************** */ //Tests compass by getting slave address //it should be 0x21 Delay10TCYx(0); printf("Slave Address: 0x%02X \r\n", HMC6352_getSlaveAddress()); HMC6352_setOpMode(HMC6352_CONTINUOUS , 1, 20); printf("Op Mode: 0x%02X \r\n", HMC6352_getOpMode()); printf("Output Mode: 0x%02X \r\n", HMC6352_getOutputMode()); while(1) { StartI2C(); WriteI2C((HMC6352_I2C_ADDRESS << 1) | 1); outputData[0] = ReadI2C(); NotAckI2C(); outputData[1] = ReadI2C(); NotAckI2C(); StopI2C(); result = outputData[0]; result = result << 8; result = result | outputData[1]; /* if(data2 <275 || data2 > 3599) printf("North \r\n"); */ printf("Heading: %i \r\n", result-255); } }
static ssize_t amp_i2cData_show(struct device *dev, struct device_attribute *attr, char *buf) { char ret; int err; err = ReadI2C(amp_i2c_reg,&ret); return sprintf(buf, "%d (0x%X)\n", ret,ret); }
//------------------------------- // Read 1 byte form I2C //------------------------------- unsigned char ReadBYTE(unsigned char Addr) { unsigned char Data; Start(); WriteI2C(0xD0); WriteI2C(Addr); Start(); WriteI2C(0xD1); Data = ReadI2C(NO_ACK); Stop(); return(Data); }
static ssize_t tpa2028d_Noise_Gate_Threshold_show(struct device *dev, struct device_attribute *attr, char *buf) { struct audio_amp_platform_data *pdata = amp_data->pdata; char val=0; ReadI2C(AGC1_CONTROL, &val); D("[tpa2028d_Noise_Gate_Threshold_show] val : %x \n",val); return sprintf(buf, "Noise_Gate_Threshold : %x, pdata->Noise_Gate_Threshold : %d\n", val, pdata->Noise_Gate_Threshold); }
static ssize_t tpa2028d2_out_lim_show(struct device *dev, struct device_attribute *attr, char *buf) { struct audio_amp_platform_data *pdata = amp_data[1]->pdata; char val=0; ReadI2C(1, AGC1_CONTROL, &val); D("[tpa2028d2_out_lim_show] val : %x \n",val); return sprintf(buf, "AGC1_CONTROL : %x, pdata->agc_output_limiter_disable : %d\n", val, pdata->agc_output_limiter_disable); }
static ssize_t tpa2028d2_fixed_gain_show(struct device *dev, struct device_attribute *attr, char *buf) { struct audio_amp_platform_data *pdata = amp_data[1]->pdata; char val=0; ReadI2C(1, AGC_FIXED_GAIN_CONTROL, &val); D("[tpa2028d2_fixed_gain_show] val : %x \n",val); return sprintf(buf, "fixed_gain : %x, pdata->agc_fixed_gain : %d\n", val, pdata->agc_fixed_gain); }
static ssize_t tpa2028d_AGC_Max_Gain_show(struct device *dev, struct device_attribute *attr, char *buf) { struct audio_amp_platform_data *pdata = amp_data->pdata; char val=0; ReadI2C(AGC2_CONTROL, &val); D("[tpa2028d_AGC_Max_Gain_show] val : %x \n",val); return sprintf(buf, "AGC_Max_Gain : %x, pdata->AGC_Max_Gain : %d\n", val, pdata->AGC_Max_Gain); }
static ssize_t tpa2028d2_comp_rate_show(struct device *dev, struct device_attribute *attr, char *buf) { struct audio_amp_platform_data *pdata = amp_data[1]->pdata; char val = 0; ReadI2C(1, AGC2_CONTROL, &val); D("[tpa2028d2_comp_rate_show] val : %x\n",val); return sprintf(buf, "AGC2_CONTROL : %x, pdata->agc_compression_rate : %d\n", val, pdata->agc_compression_rate); }
unsigned char rtc_get_time (unsigned char address) { unsigned char time; StartI2C(); WriteI2C(0xD0); WriteI2C(address); RestartI2C(); WriteI2C(0xD1); time = ReadI2C(); return (((time & 0xF0) >> 4) * 10) + (time & 0x0F); // BCD to decimal }
static ssize_t tpa2028d_Output_limit_level_show(struct device *dev, struct device_attribute *attr, char *buf) { struct audio_amp_platform_data *pdata = amp_data->pdata; char val=0; ReadI2C(AGC1_CONTROL, &val); D("[tpa2028d_Output_limit_level_show] val : %x \n",val); return sprintf(buf, "Output_limit_level : %x, pdata->Output_limit_level : %d\n", val, pdata->Output_limit_level); }
static ssize_t tpa2028d_Hold_time_show(struct device *dev, struct device_attribute *attr, char *buf) { struct audio_amp_platform_data *pdata = amp_data->pdata; char val=0; ReadI2C(AGC_HOLD_TIME_CONTROL, &val); D("[tpa2028d_Hold_time_show] val : %x \n",val); return sprintf(buf, "Hold_time : %x, pdata->Hold_time : %d\n", val, pdata->Hold_time); }
//--------------------------------------- // Main program //--------------------------------------- void main(void) { InitSerial(); // Initialize serial port putchar(0x0C); // clear hyper terminal DelayMs(5); WriteBYTE(0x0000); WriteI2C('A'); //Write Data's Here WriteI2C('B'); WriteI2C('C'); WriteI2C('D'); WriteI2C('E'); WriteI2C('F'); Stop(); DelayMs(10); ReadBYTE(0x0000); EData[0] = ReadI2C(NO_ACK); EData[1] = ReadI2C(NO_ACK); EData[2] = ReadI2C(NO_ACK); EData[3] = ReadI2C(NO_ACK); EData[4] = ReadI2C(NO_ACK); EData[5] = ReadI2C(NO_ACK); for(i=0;i<6;i++) { printf("value = %c\n",EData[i]); // display data */ DelayMs(100); } while(1); }
/** Read multiple bytes from an 8-bit device register. * @param devAddr I2C slave device address * @param regAddr First register regAddr to read from * @param length Number of bytes to read * @param data Buffer to store read data in * @return Number of bytes read (-1 indicates failure) */ int8 I2Cdev_readBytes(uint8 devAddr, uint8 regAddr, uint8 length, uint8 *data) { #ifdef I2CDEV_SERIAL_DEBUG i2cdev_debug_depth++; int z; for(z=0;z<i2cdev_debug_depth;z++) DBG_vPrintf(TRACE_APP, "\t"); DBG_vPrintf(TRACE_APP, "readBytes\r\n"); #endif uint8 count = 0; // Start I2C StartI2C(); // Device write address WriteAddressI2C(devAddr, COMMAND_WRITE); // Register address WriteRegisterI2C(regAddr); // Device read address WriteAddressI2C(devAddr, COMMAND_READ); for (count = 0; count < length; count++) { if (count == length - 1) { // NACK data[count] = ReadI2C(SEND_NACK, SEND_STOP); } else { // ACK data[count] = ReadI2C(SEND_ACK, SEND_CONTINUE); } } // Stop I2C StopI2C(); #ifdef I2CDEV_SERIAL_DEBUG i2cdev_debug_depth--; #endif return count; }
int16_t ReadsMasterI2C (uint8_t adresse, uint8_t nb_octet, uint8_t offset) { uint8_t j; int16_t retval = 0; if (StartI2C () != 1) { return ERREUR_I2C_START; } if (WriteI2C ((adresse<<1)+1) != 1) { return ERREUR_I2C_WRITE; } for (j=0; j<nb_octet; j++) { retval = ReadI2C (); //FIXME : WTF?!!! Probleme depuis ajout aversive, strat autom // Carte PLVL 1 ok, test avec backup i2c ok, donc le problème se situe bien au niveau // de cette carte... Delay_Xx10us (70); if (retval < 0) { i2c.buffer_rx[offset+j] = 0; return ERREUR_I2C_READ; } else { i2c.buffer_rx[offset+j] = (unsigned char)retval; } if ((j+1) >= nb_octet) NackI2C (); else AckI2C (); } if (StopI2C () != 1) { return ERREUR_I2C_STOP; } return 1; }
unsigned char ITG3200_read(unsigned char reg) { unsigned char buf; StartI2C(); WriteI2C(ITG3200_ADDR_WRITE); WriteI2C(reg); RestartI2C(); WriteI2C(ITG3200_ADDR_READ); buf = ReadI2C(); NotAckI2C(); StopI2C(); return buf; }
char readEEPROMexterna (char endereco) { //while(!DataRdyI2C()); IdleI2C(); StartI2C(); IdleI2C(); WriteI2C(0xA0); IdleI2C(); WriteI2C(0x00); IdleI2C(); //HB WriteI2C(endereco); IdleI2C(); RestartI2C(); IdleI2C(); WriteI2C(0xA1); IdleI2C(); value = ReadI2C(); IdleI2C(); //Dados NotAckI2C(); IdleI2C(); StopI2C();IdleI2C(); return value; }
/* *------------------------------------------------------------------------------ * void ReadRtcTimeAndDate(UINT8 * buff) * * Summary : Read time and date related registers from RTC * * Input : UINT8 *buff - buffer pointer to receive the data * * Output : None * *------------------------------------------------------------------------------ */ void ReadRtcTimeAndDate(UINT8 * buff) { StartI2C(); WriteI2C(DEV_ADDR_RTC); WriteI2C(RTC_REG_SEC); StartI2C(); WriteI2C(DEV_ADDR_RTC + 1); *(buff+0)=ReadI2C(ACK); // Second *(buff+1)=ReadI2C(ACK); // Minute *(buff+2)=ReadI2C(ACK); // hour *(buff+3)=ReadI2C(ACK); // Day *(buff+4)=ReadI2C(ACK); // date *(buff+5)=ReadI2C(ACK); // month *(buff+6)=ReadI2C(NO_ACK); // year StopI2C(); }
static void EEPROM_read_seq(BYTE devaddr, BYTE *data, BYTE size) { BYTE i = 0; StartI2C(); IdleI2C(); WriteI2C( (devaddr << 1) | 0x01 ); IdleI2C(); for(; i < size; ++i) { data[i] = ReadI2C(); if(i < size-1) { AckI2C(); } else { NotAckI2C(); } } StopI2C(); }
//------------------------------- // Read RTC (all real time) //------------------------------- void ReadRTC(unsigned char * buff) { Start(); WriteI2C(0xD0); WriteI2C(0x00); Start(); WriteI2C(0xD1); *(buff+0)=ReadI2C(ACK); // Second *(buff+1)=ReadI2C(ACK); // Minute *(buff+2)=ReadI2C(ACK); // hour *(buff+3)=ReadI2C(ACK); // Day *(buff+4)=ReadI2C(ACK); // date *(buff+5)=ReadI2C(ACK); // month *(buff+6)=ReadI2C(NO_ACK); // year Stop(); }
void ADXL345_multiByteRead(unsigned char startAddress, char* buffer, unsigned char size) { #if I2C unsigned char i; StartI2C(); WriteI2C(ADXL343_ADDR_WRITE); WriteI2C(startAddress); RestartI2C(); WriteI2C(ADXL343_ADDR_READ); for (i = 0; i < size; i++) { buffer[i] = ReadI2C(); //keep the clock pulsing // if not last byte, send ack // if last byte, send nack if(i < size-1) { AckI2C(); } else { NotAckI2C(); } } StopI2C(); #elif SPI unsigned char tx = (ADXL345_SPI_READ | ADXL345_MULTI_BYTE | (startAddress & 0x3F)); // the &0x3F restricts reading from only the XYZ data registers unsigned char i; SPI_CS_PIN = 0; //CS pin low, ie enable chip Delay1TCY(); // delay at least 5 ns WriteSPI(tx); //Send address to start reading from. for (i = 0; i < size; i++) { buffer[i] = ReadSPI(); //keep the clock pulsing } SPI_CS_PIN = 1; //CS pin high, ie disable chip #endif }
unsigned char ADXL345_oneByteRead(unsigned char address) { unsigned char data = 0; #if I2C StartI2C(); WriteI2C(ADXL343_ADDR_WRITE); WriteI2C(address); RestartI2C(); WriteI2C(ADXL343_ADDR_READ); data = ReadI2C(); NotAckI2C(); StopI2C(); return data; #elif SPI SPI_CS_PIN = 0; //CS pin low, ie enable chip Delay1TCY(); // delay at least 5 ns address = address | ADXL345_SPI_READ; WriteSPI(address); // read bit, multibyte bit, A5-A0 address data = ReadSPI(); SPI_CS_PIN = 1; //CS pin high, ie disable chip Delay1TCY(); // delay at least 5 ns return data; #endif }
void getTemperaturaHumidade (void) { unsigned char TEMPL=0, TEMPH=0, HUMIDL=0, HUMIDH=0; unsigned char DUMMY=0, OP=0, BT=0; float humidade, temperatura; char msg[55]; LED_AMAR=1; //#define StartI2C() SSPCON2bits.SEN=1;while(SSPCON2bits.SEN) StartI2C(); // ACORDAR DEVICE __delay_us(16); WriteI2C(0xB8); // endereco Slave do AM2315 __delay_us(135); StopI2C(); //#define StopI2C() SSPCON2bits.PEN=1;while(SSPCON2bits.PEN) // com clock de 4 mhz: // 10K (100) = 1000 ms // 1K (100) = 100 ms // 1K (10) = 10 ms // 1K (2) = 2 ms // Delay100TCYx(); __delay_us(25); RestartI2C(); // REQUISITAR PEDIDO DE BYTES __delay_us(16); WriteI2C(0xB8); // endereco Slave do AM2315 __delay_us(60); // manual do AM2315 recomenda minimo de 30us WriteI2C(0x03); // byte que simboliza a temperatura __delay_us(60); WriteI2C(0x00); // start byte para leitura __delay_us(60); WriteI2C(0x04); // quantidades de bytes a serem lidos; //AckI2C(); __delay_us(16); StopI2C(); //#define StopI2C() SSPCON2bits.PEN=1;while(SSPCON2bits.PEN) __delay_ms(10); // manual do AM2315 recomenda esperar no minimo 10ms RestartI2C(); WriteI2C(0xB9); // endereco Slave do AM2315 //AckI2C(); // retirado por nao necessitar (?) __delay_us(60); // manual do AM2315 recomenda minimo de 30us IdleI2C(); OP = ReadI2C(); // 1o byte AckI2C(); IdleI2C(); BT = ReadI2C(); // 2o byte AckI2C(); IdleI2C(); HUMIDL = ReadI2C(); // 3o byte AckI2C(); IdleI2C(); HUMIDH = ReadI2C(); // 4o byte AckI2C(); IdleI2C(); TEMPL = ReadI2C(); // 5o byte AckI2C(); IdleI2C(); TEMPH = ReadI2C(); // 6o byte AckI2C(); IdleI2C(); DUMMY = ReadI2C(); // 7o byte AckI2C(); IdleI2C(); DUMMY = ReadI2C(); // 8 byte //__delay_us(16); StopI2C(); //#define StopI2C() SSPCON2bits.PEN=1;while(SSPCON2bits.PEN) LED_VERM=0; LED_AMAR=0; LED_VERD=1; // Calculos obtidos do exemplo do Arduino humidade = HUMIDL; humidade *= 256; humidade += HUMIDH; humidade /= 10; temperatura = TEMPL; temperatura *= 256; temperatura += TEMPH; temperatura /= 10; /* ou ainda RH = RHH << 8; RH |= RHL; TEMP = TEMPH << 8; TEMP |= TEMPL; */ sprintf (msg, "Temp= %0.2f, Humid= %0.2f .", temperatura, humidade); while(BusyUSART()); putsUSART(msg); while(BusyUSART()); putrsUSART("\n\r"); LED_VERD=0; }
void getDS1307(void) { int hora=0, minuto=0, segundo=0, diasemana=0, dia=0, mes=0, ano=0, dummy=0; char msg[40]; //#define StartI2C() SSPCON2bits.SEN=1;while(SSPCON2bits.SEN) LED_AMAR=1; IdleI2C(); StartI2C(); //IdleI2C(); __delay_us(16); WriteI2C( 0xD0 ); //IdleI2C(); __delay_us(60); WriteI2C( 0x00 ); IdleI2C(); __delay_us(16); //AckI2C();AckI2C();AckI2C();AckI2C();AckI2C();AckI2C();AckI2C();AckI2C(); StopI2C(); //#define StopI2C() SSPCON2bits.PEN=1;while(SSPCON2bits.PEN) //IdleI2C(); __delay_us(26); RestartI2C(); __delay_us(16); WriteI2C( 0xD1 ); __delay_us(1); IdleI2C(); segundo =ReadI2C(); AckI2C(); IdleI2C(); minuto =ReadI2C(); AckI2C(); IdleI2C(); hora =ReadI2C(); AckI2C(); IdleI2C(); diasemana=ReadI2C(); AckI2C(); IdleI2C(); dia =ReadI2C(); AckI2C(); IdleI2C(); mes =ReadI2C(); AckI2C(); IdleI2C(); ano =ReadI2C(); AckI2C(); IdleI2C(); dummy =ReadI2C(); //AckI2C(); //__delay_us(16); //IdleI2C(); //NotAckI2C(); //IdleI2C(); StopI2C(); //#define StopI2C() SSPCON2bits.PEN=1;while(SSPCON2bits.PEN) LED_VERM = 0; LED_AMAR=0; LED_VERD=1; sprintf(msg,"%xh:%xm:%xs _ dia %x/%x/%x _ ", hora,minuto,segundo,dia,mes,ano); while(BusyUSART()); putsUSART( msg ); LED_VERD=0; }
void main(void) { M8C_EnableGInt ; // Uncomment this line to enable Global Interrupts M8C_EnableIntMask(INT_MSK1, INT_MSK1_DBB01); // Enable DBB01 Interrupt for TempCounter M8C_EnableIntMask(INT_MSK1, INT_MSK1_DBB11); // Enable DBB01 Interrupt for MotorDriver M8C_EnableIntMask(INT_MSK0, INT_MSK0_GPIO); // Enable GPIO interrupt for Tout // Start the UART(with no parity), LCD, TempCounter and MotorDriver UART_Start(UART_PARITY_NONE); LCD_Start(); TempCounter_EnableInt(); // Enable interrupts for counter TempCounter_Start(); MotorDriver_EnableInt(); // Enable interrupts for counter // Start I2CHW I2CHW_Start(); I2CHW_EnableMstr(); I2CHW_EnableInt(); WriteI2C(slaveAddress, 0xAC, 1, 0x02); // Write to access config, sets mode to cooling(POL = 1), also turns 1-SHOT off, continuous conversions WriteI2C(slaveAddress, 0xA1, 2, (setTemp + tolerance), 0x00); // Sets initial high temp to be setTemp + tolerance WriteI2C(slaveAddress, 0xA2, 2, (setTemp - tolerance), 0x00); // Sets initial low temp to be setTemp - tolerance WriteI2C(slaveAddress, 0xEE, 0); // This tells the temperature IC to start converting the temperatures // Writes initial string to LCD. When LCD is updated, only the numbers will be changed LCD_Position(0,0); LCD_PrCString("CUR: 00 OFF "); LCD_Position(1,0); LCD_PrCString("SET: 00 FAN OFF "); // This is the command usage string UART_CPutString("#################### Heating/Cooling Stepper Motors ##################\r\n\ # S ##\r\n\ # S - Set the desired Temperature\r\n\ # ## - Desired temperature in celsius\r\n\ #\r\n\ # T ##\r\n\ # T - Set the desired tolerance\r\n\ # ## - Desired tolerance in celsius\r\n\ #\r\n\ # M X\r\n\ # M - Change the mode of the thermostat\r\n\ # X - C is for cool, H is for heat, F is for off\r\n\ #\r\n\ # F X S\r\n\ # F - Change the mode of the fan\r\n\ # X - A is for automatic fan control, M is for always on\r\n\ # S - Speed of the fan, H = high, M = medium, L = low\r\n\ #####################################################################\r\n"); while (1) { char *cmd; char *params; if (GetLine(buf, &strPos, 79)) // Only process the data if GetLine returns true { cmd = Lowercase(cstrtok(buf, " ")); // Lowercase the first word from the inputted string if (strlen(cmd) == 1 && cmd[0] == 's') // If the person entered s { int temp; params = cstrtok(0x00, " "); // Read next word // If next word isnt number or isnt 1 or 2 characters long, then return error if (!IsNumber(params) || strlen(params) < 1 || strlen(params) > 2 || csscanf(params, "%d", &temp) != 1) goto error; // If there is additional data at end of string or if number is not within 0-99, return error if (cstrtok(0x00, " ") != 0x00) goto error; if ( temp > 99 || temp < 0) goto error; setTemp = temp; WriteI2C(slaveAddress, 0xA1, 2, (setTemp + tolerance), 0x00); // Sets high temp to be setTemp + tolerance WriteI2C(slaveAddress, 0xA2, 2, (setTemp - tolerance), 0x00); // Sets low temp to be setTemp - tolerance updateLCD = TRUE; // Update the LCD } else if (strlen(cmd) == 1 && cmd[0] == 't') // If the person entered t { int tol; params = cstrtok(0x00, " "); // Read next word // If next word isnt number or isnt 1 or 2 characters long, then return error if (!IsNumber(params) || strlen(params) < 1 || strlen(params) > 2 || csscanf(params, "%d", &tol) != 1) goto error; // If there is additional data at end of string or if number is not within 0-10, return error if (cstrtok(0x00, " ") != 0x00) goto error; if (tol < 0 || tol > 10) goto error; tolerance = tol; WriteI2C(slaveAddress, 0xA1, 2, (setTemp + tolerance), 0x00); // Sets high temp to be setTemp + tolerance WriteI2C(slaveAddress, 0xA2, 2, (setTemp - tolerance), 0x00); // Sets low temp to be setTemp - tolerance updateLCD = TRUE; // Update the LCD } else if (strlen(cmd) == 1 && cmd[0] == 'm') // If the person entered m { char mode; params = cstrtok(0x00, " "); // Read next word // If next word isnt 1 character long, return error if (strlen(params) != 1 || csscanf(params, "%c", &mode) != 1) goto error; // If there is additional data at end of string, return error if (cstrtok(0x00, " ") != 0x00) goto error; mode = tolower(mode); // Lowercase the character switch (mode) { case 'h': thermostatMode = 1; // Set mode to heating WriteI2C(slaveAddress,0xAC, 1, 0x00); // Change access config on DS1621 to heating(POL = 0) break; case 'c': thermostatMode = 2; // Set mode to cooling WriteI2C(slaveAddress, 0xAC, 1, 0x02); // Change access config on DS1621 to cooling(POL = 1) break; case 'f': thermostatMode = 0; // Set mode to off break; default: goto error; // Invalid character entered, goto error } CheckFan(); // Check the fan to see if it should be on } else if (strlen(cmd) == 1 && cmd[0] == 'f') // If the person entered f { char mode; char speed; params = cstrtok(0x00, " "); // Read next word // If next word isnt 1 character long, then return error if (strlen(params) != 1 || csscanf(params, "%c", &mode) != 1) goto error; params = cstrtok(0x00, " "); // Read next word // If next word isnt 1 character long, then return error if (strlen(params) != 1 || csscanf(params, "%c", &speed) != 1) goto error; // If there is additional data at end of string, return error if (cstrtok(0x00, " ") != 0x00) goto error; speed = tolower(speed); // Lowercase the speed and mode characters entered mode = tolower(mode); switch (mode) { case 'm': fanMode = 0; // Set fan mode to manual break; case 'a': fanMode = 1; // Set fan mode to automatic break; default: // Otherwise go to error goto error; } MotorDriver_Stop(); // Stop the motor to change the period values switch (speed) { case 'l': fanSpeed = 0; // Set fan speed to low MotorDriver_WritePeriod(49999); // See report for where these numbers came from MotorDriver_WriteCompareValue(25000); break; case 'm': fanSpeed = 1; // Set fan speed to medium MotorDriver_WritePeriod(9999); // See report for where these numbers came from MotorDriver_WriteCompareValue(5000); break; case 'h': fanSpeed = 2; // Set fan speed to high MotorDriver_WritePeriod(1999); // See report for where these numbers came from MotorDriver_WriteCompareValue(1000); break; default: // Otherwise go to error if invalid input entered goto error; } CheckFan(); // Check the fan to see if it should be on } else goto error; } if (checkTemp) // Check the temperature { char buf[2]; ReadI2C(slaveAddress, 0xAA, 2, buf); // Read the temperature from IC, returns 2 bytes curTemp = buf[0]; // We just care about the first byte checkTemp = FALSE; // Turn flag off so it doesnt keep doing this } if (updateLCD) // Update the LCD { char buf[3]; NumToStr(buf, curTemp, 2); // Convert current temp to str LCD_Position(0, 5); LCD_PrString(buf); // Print it LCD_Position(0, 8); switch(thermostatMode) // Print thermostat mode { case 0: LCD_PrCString("OFF "); break; case 1: LCD_PrCString("HEAT"); break; case 2: LCD_PrCString("COOL"); break; } NumToStr(buf, setTemp, 2); // Convert set temp to str LCD_Position(1, 5); LCD_PrString(buf); // Print it LCD_Position(1, 12); if (fanMode == 1 && thermostatMode == 0) LCD_PrCString("OFF"); // Print current fan state else if (fanSpeed == 0) LCD_PrCString("LOW"); else if (fanSpeed == 1) LCD_PrCString("MED"); else if (fanSpeed == 2) LCD_PrCString("HI "); updateLCD = FALSE; } continue; error: UART_CPutString("# Invalid format entered. Valid formats are:\r\n\ # S ##\r\n\ # S - Set the desired Temperature\r\n\ # ## - Desired temperature in celsius\r\n\ #\r\n\ # T ##\r\n\ # T - Set the desired tolerance\r\n\ # ## - Desired tolerance in celsius\r\n\ #\r\n\ # M X\r\n\ # M - Change the mode of the thermostat\r\n\ # X - C is for cool, H is for heat, F is for off\r\n\ #\r\n\ # F X S\r\n\ # F - Change the mode of the fan\r\n\ # X - A is for automatic fan control, M is for always on\r\n\ # S - Speed of the fan, H = high, M = medium, L = low\r\n\ #####################################################################\r\n"); } }
void readTempSensor(unsigned char *byte1, unsigned char *byte2, unsigned char *slope, unsigned char *counter) { StartI2C(); IdleI2C(); WriteI2C(0x9E); // slave address + W IdleI2C(); WriteI2C(0xAA); // read two bytes command IdleI2C(); RestartI2C(); IdleI2C(); WriteI2C(0x9F); // slave address + R IdleI2C(); (*byte1) = ReadI2C(); IdleI2C(); AckI2C(); IdleI2C(); (*byte2) = ReadI2C(); IdleI2C(); NotAckI2C(); IdleI2C(); StopI2C(); Delay10KTCYx(1); // read counter StartI2C(); IdleI2C(); WriteI2C(0x9E); // slave address + W IdleI2C(); WriteI2C(0xA8); // read counter command IdleI2C(); RestartI2C(); IdleI2C(); WriteI2C(0x9F); // slave address + R IdleI2C(); (*counter) = ReadI2C(); IdleI2C(); NotAckI2C(); IdleI2C(); StopI2C(); Delay10KTCYx(1); // read slope StartI2C(); IdleI2C(); WriteI2C(0x9E); // slave address + W IdleI2C(); WriteI2C(0xA9); // read slope command IdleI2C(); RestartI2C(); IdleI2C(); WriteI2C(0x9F); // slave address + R IdleI2C(); (*slope) = ReadI2C(); IdleI2C(); NotAckI2C(); IdleI2C(); StopI2C(); Delay10KTCYx(1); }