task main() { while(true) { //Cmd: // 1- pitch detect // 2- sound power level // 3- FFT cmd=3; mode=0; if(st==0) { i2c_write_registers(0x01, 0x02, 0,cmd,mode); st=1; writeDebugStreamLine("Starting"); //wait1Msec(4000); writeDebugStreamLine("Ok"); } else if(cmd==1) i2c_read_registers_text(0x01, 0, 2); else if(cmd==2) i2c_read_registers_text(0x01, 0, 1); else if(cmd==3) i2c_read_registers_text(0x01, 0, 15); wait1Msec(2); } }
/* Will auto-adjust the stored year if required */ void pcf8583_read(struct tm *time) { union { uint8_t bytes[5]; uint16_t words[2]; } tmp; /* Set to default value in case we abort */ memcpy_P(time, &rtc_default_date, sizeof(struct tm)); if (rtc_state != RTC_OK) return; if (i2c_read_registers(PCF8583_ADDR, REG_SECONDS, 5, &tmp)) return; time->tm_sec = bcd2int(tmp.bytes[0]); time->tm_min = bcd2int(tmp.bytes[1]); time->tm_hour = bcd2int(tmp.bytes[2]); time->tm_mday = bcd2int(tmp.bytes[3] & 0b00111111); time->tm_mon = bcd2int(tmp.bytes[4] & 0b00011111)-1; time->tm_wday = bcd2int(tmp.bytes[4] >> 5); /* Check for year rollover */ tmp.bytes[4] = tmp.bytes[3] >> 6; i2c_read_registers(PCF8583_ADDR, REG_YEAR1, 4, &tmp); if ((tmp.words[0] & 3) != tmp.bytes[4]) { /* Year rolled over, increment until the lower two bits match */ while ((tmp.words[0] & 3) != tmp.bytes[4]) tmp.words[0]++; tmp.words[1] = tmp.words[0] ^ 0xffffU; /* Store new year to RTC ram */ i2c_write_registers(PCF8583_ADDR, REG_YEAR1, 4, &tmp); } time->tm_year = tmp.words[0]-1900; }
/* Set the time in the RTC */ void pcf8583_set(struct tm *time) { union { uint8_t bytes[5]; uint16_t words[2]; } tmp; if (rtc_state == RTC_NOT_FOUND) return; i2c_write_register(PCF8583_ADDR, REG_CONTROL, CTL_STOP_CLOCK); tmp.bytes[0] = int2bcd(time->tm_sec); tmp.bytes[1] = int2bcd(time->tm_min); tmp.bytes[2] = int2bcd(time->tm_hour); tmp.bytes[3] = int2bcd(time->tm_mday) | ((time->tm_year & 3) << 6); tmp.bytes[4] = int2bcd(time->tm_mon+1)| ((time->tm_wday ) << 5); i2c_write_registers(PCF8583_ADDR, REG_SECONDS, 5, &tmp); tmp.words[0] = time->tm_year + 1900; tmp.words[1] = (time->tm_year + 1900) ^ 0xffffU; i2c_write_registers(PCF8583_ADDR, REG_YEAR1, 4, &tmp); i2c_write_register(PCF8583_ADDR, REG_CONTROL, CTL_START_CLOCK); rtc_state = RTC_OK; }
/* Set the time in the RTC */ void dsrtc_set(struct tm *time) { uint8_t tmp[7]; if (rtc_state == RTC_NOT_FOUND) return; tmp[REG_SECOND] = int2bcd(time->tm_sec); tmp[REG_MINUTE] = int2bcd(time->tm_min); tmp[REG_HOUR] = int2bcd(time->tm_hour); tmp[REG_DOW] = int2bcd(time->tm_wday+1); tmp[REG_DOM] = int2bcd(time->tm_mday); tmp[REG_MONTH] = int2bcd(time->tm_mon+1) | 0x80 * (time->tm_year >= 2100); tmp[REG_YEAR] = int2bcd(time->tm_year % 100); i2c_write_registers(RTC_ADDR, REG_SECOND, 7, tmp); i2c_write_register(RTC_ADDR, REG_CONTROL, 0); // 3231: enable oscillator on battery, interrupts off // 1307: disable SQW output #ifdef CONFIG_RTC_DS3231 i2c_write_register(RTC_ADDR, REG_CTLSTATUS, 0); // clear "oscillator stopped" flag #endif rtc_state = RTC_OK; }
void display_send_prefixed(uint8_t cmd, uint8_t prefixbyte, uint8_t len, const uint8_t *buffer) { displaybuffer[0] = prefixbyte; memcpy(displaybuffer+1, buffer, min(sizeof(displaybuffer)-1, len)); i2c_write_registers(DISPLAY_I2C_ADDR, cmd, min(len+1,sizeof(displaybuffer)), displaybuffer); }
uint8_t display_init(uint8_t len, uint8_t *message) { display_intrq_init(); display_found = !i2c_write_registers(DISPLAY_I2C_ADDR, DISPLAY_INIT, len, message); return display_found; }
void display_send_cmd(uint8_t cmd, uint8_t len, const void *buf) { if (display_found) /* repeat until successful - the display may be busy */ while (i2c_write_registers(DISPLAY_I2C_ADDR, cmd, len, buf)) ; }