int main(void) { __init__(); test_7seg(); int i; uchar value = 0; while(true) { i=0; do{ send2display(toBCD(value)); delay(refresh_rate_SSD); }while(++i < (refresh_counter_rate/refresh_rate_SSD)); if((value = counter_8bits(value)) == 100) { value = 0; int j; for(j=0; j < 5; j++) { i=0; do{ send2display(toBCD(value)); delay(refresh_rate_SSD); }while(++i < half_second/refresh_rate_SSD); LATB &= 0xFC00; delay(half_second); // LATB |= 0x0200; } } } return 0; }
void toBCDTime(U8 *d, int time) { //time is given in second //convert to hour, minutes, seconds d[0] = toBCD(time / 3600); d[1] = toBCD((time % 3600) / 60); d[2] = toBCD((time % 3600) % 60); }
void toDVBTime(U8 *d, const time_t t) { tm *time = gmtime(&t); int l = 0; int month = time->tm_mon + 1; if (month == 1 || month == 2) l = 1; int mjd = 14956 + time->tm_mday + (int)((time->tm_year - l) * 365.25) + (int)((month + 1 + l*12) * 30.6001); d[0] = mjd >> 8; d[1] = mjd & 0xFF; d[2] = toBCD(time->tm_hour); d[3] = toBCD(time->tm_min); d[4] = toBCD(time->tm_sec); }
void write_rtc(unsigned char hour, unsigned char minute, unsigned char second, unsigned char day, unsigned char month, unsigned char year, unsigned char century) { outb(cmos_address, 0x00); // second outb(cmos_data, toBCD(second)); outb(cmos_address, 0x02); // minute outb(cmos_data, toBCD(minute)); outb(cmos_address, 0x04); // hour outb(cmos_data, toBCD(hour)); outb(cmos_address, 0x07); // day outb(cmos_data, toBCD(day)); outb(cmos_address, 0x08); // month outb(cmos_data, toBCD(month)); outb(cmos_address, 0x09); // year outb(cmos_data, toBCD(year)); outb(cmos_address, century_register); // century outb(cmos_data, toBCD(century)); }
void EPlaybackWing::sendPageData() { QByteArray sendData(42, char(0)); sendData.replace(0, sizeof(EWING_HEADER_INPUT), EWING_HEADER_INPUT); sendData[EWING_PLAYBACK_INPUT_BYTE_VERSION] = EWING_PLAYBACK_INPUT_VERSION; sendData[EWING_PLAYBACK_INPUT_BYTE_PAGE] = toBCD(m_page); QUdpSocket sock(this); sock.writeDatagram(sendData, address(), EWing::UDPPort); }
void ShortcutWing::sendPageData() { QByteArray sendData(42, char(0)); sendData.replace(0, sizeof(WING_HEADER_INPUT), WING_HEADER_INPUT); sendData[WING_SHORTCUT_INPUT_BYTE_VERSION] = WING_SHORTCUT_INPUT_VERSION; sendData[WING_SHORTCUT_INPUT_BYTE_PAGE] = toBCD(page()); QUdpSocket sock(this); sock.writeDatagram(sendData, address(), Wing::UDPPort); }
void _int_(12) isr_T3(void) { static int counter = 0; send2displays(toBCD(value2display)); // Send "value2display" global variable to displays if(++counter==100){ counter = 0; puts_("V: "); putc(value2display/10 + 48); putc((value2display - (value2display/10)*10) + 48); putc('\n'); } IFS0bits.T3IF = 0; // Reset T3IF flag }
int main(void) { // Configure A/D module; int i = 0, V = 0; double average = 0; double sum = 0; TRISBbits.TRISB14 = 1; // RB4 digital output disconnected AD1PCFGbits.PCFG14 = 0; // RB4 configured as analog input (AN4) AD1CON1bits.SSRC = 7; // Conversion trigger selection bits: in this // modeaninternalcounterendssamplingand AD1CON1bits.CLRASAM = 1; // Stop conversions when the 1st A/D converter AD1CON1bits.ON = 1; AD1CON3bits.SAMC = 16; // Sample time is 16 TAD (TAD = 100 ns) AD1CHSbits.CH0SA = 14; AD1CON2bits.SMPI = 3; // uma conversao consecutiva while(1){ delay(10); // Wait 10 ms using the core timer if(i++ == 25) // 250 ms { AD1CON1bits.ASAM = 1; // Start conversion while(IFS1bits.AD1IF == 0 );// Wait while conversion not done (AD1IF == 0) int *p = (int *)(&ADC1BUF0); for( i = 0; i < 16; i++ ){ printInt10(p[i*4]); sum += p[i*4]; printStr("\n"); } average = sum/4; printf("%s %f","Média dos valores lidos: ", average ); V = (average*33+511)/1023; printStr("\n"); printf("%s %d","Tensão: ", V ); i = 0; sum = 0; average = 0; IFS1bits.AD1IF = 0; // Reset AD1IF V = toBCD(V); } send2displays(V); } return 0; // Consoante o número de conversões consecutivas, aparecerão os valores até 15 conversões consecutivas }
void setTime(TimeVal* timeVal) { /* WRITE: 0xD0 ACK<< DS1307 write address WRITE: 0x00 ACK<< location to write WRITE: 0x00 ACK<< 0 seconds, reset CH bit to 0 WRITE: 0x30 ACK<< 30 minutes WRITE: 0x09 ACK<< 9 hours, 24 hour clock mode WRITE: 0x02 ACK<< Monday (day 2) WRITE: 0x07 ACK<< 7th (date) WRITE: 0x09 ACK<< September (month) WRITE: 0x09 ACK<< '09 (year) WRITE: 0x00 ACK<< Set control register to 0' I2C STOP BIT */ // Compute the BCD data uint8_t yearByte = toBCD(timeVal->year - 2000); uint8_t monthByte = toBCD(timeVal->month); uint8_t dateByte = toBCD(timeVal->date); uint8_t dayByte = toBCD(getWeekday(timeVal) + 1); uint8_t hourByte = toBCD(timeVal->hour); uint8_t minByte = toBCD(timeVal->min); uint8_t secByte = toBCD(timeVal->sec); // Set device address and write mode i2c_start_wait(DEV_DS1307 + I2C_WRITE); // Write at address 0 i2c_write(0x00); i2c_write(secByte); // Seconds, CH bit i2c_write(minByte); // Minutes i2c_write(hourByte); // Hours, 12/24h clock mode i2c_write(dayByte); // Weekday i2c_write(dateByte); // Date i2c_write(monthByte); // Month i2c_write(yearByte); // Year // Set control register to 0 i2c_write(0x00); // Stop, release bus i2c_stop(); }
int main(void){ unsigned int i; unsigned char counter = 0x00; // RB0-RB9 will be Outputs TRISB = TRISB & 0xFC00; while(1){ i = 0; do{ // wait 10ms (100 Hz) delay(10); // call send2displays with counter value as argument send2displays(toBCD(counter)); } while(++i <4); counter = (counter+1)%60 ; } return 0; }
void writeRTC(uint8_t *ptr) { uint32_t year; RtcConfig[0] = 0x80 | toBCD(*ptr++); RtcConfig[1] = toBCD(*ptr++); RtcConfig[2] = toBCD(*ptr++); RtcConfig[3] = 0x08; RtcConfig[4] = toBCD(*ptr++); RtcConfig[5] = toBCD( *ptr++); year = *ptr++; year |= *ptr << 8; RtcConfig[6] = toBCD(year - 2000); RtcConfig[7] = MFPsetting; Rtc_write_ptr = RtcConfig; Rtc_write_count = 8; __disable_irq(); Rtc_write_pending |= 1; i2cCheck(); __enable_irq(); }
static void rtcRecv() { //INFO("RTC Read command 0x%02X\n", (rtc.cmd >> 1)); memset(&rtc.data[0], 0, sizeof(rtc.data)); switch (rtc.cmd >> 1) { case 0: // status register 1 //INFO("RTC: read regstatus1 (0x%02X)\n", rtc.regStatus1); rtc.regStatus1 &= 0x0F; rtc.data[0] = rtc.regStatus1; //rtc.regStatus1 &= 0x7F; break; case 1: // status register 2 //INFO("RTC: read regstatus2 (0x%02X)\n", rtc.regStatus1); rtc.data[0] = rtc.regStatus2; break; case 2: // date & time { //INFO("RTC: read date & time\n"); DateTime tm = rtcGetTime(); rtc.data[0] = toBCD(tm.get_Year() % 100); rtc.data[1] = toBCD(tm.get_Month()); rtc.data[2] = toBCD(tm.get_Day()); //zero 24-apr-2010 - this is nonsense. //but it is so wrong, someone mustve thought they knew what they were doing, so i am leaving it... //rtc.data[3] = (tm.tm_wday + 6) & 7; //if (rtc.data[3] == 7) rtc.data[3] = 6; //do this instead (gbatek seems to say monday=0 but i don't think that is right) //0=sunday is necessary to make animal crossing behave //maybe it means "custom assignment" can be specified by the game rtc.data[3] = tm.get_DayOfWeek(); int hour = tm.get_Hour(); if (!(rtc.regStatus1 & 0x02)) hour %= 12; rtc.data[4] = ((hour < 12) ? 0x00 : 0x40) | toBCD(hour); rtc.data[5] = toBCD(tm.get_Minute()); rtc.data[6] = toBCD(tm.get_Second()); break; } case 3: // time { //INFO("RTC: read time\n"); DateTime tm = rtcGetTime(); int hour = tm.get_Hour(); if (!(rtc.regStatus1 & 0x02)) hour %= 12; rtc.data[0] = ((hour < 12) ? 0x00 : 0x40) | toBCD(hour); rtc.data[1] = toBCD(tm.get_Minute()); rtc.data[2] = toBCD(tm.get_Second()); break; } case 4: // freq/alarm 1 /*if (cmdBitsSize[0x04] == 8) INFO("RTC: read INT1 freq\n"); else INFO("RTC: read INT1 alarm1\n");*/ //NDS_makeARM7Int(7); break; case 5: // alarm 2 //INFO("RTC: read alarm 2\n"); break; case 6: // clock adjust //INFO("RTC: read clock adjust\n"); rtc.data[0] = rtc.regAdjustment; break; case 7: // free register //INFO("RTC: read free register\n"); rtc.data[0] = rtc.regFree; break; } }
static void rtcRecv() { //INFO("RTC Read command 0x%02X\n", (rtc.cmd >> 1)); memset(&rtc.data[0], 0, sizeof(rtc.data)); switch (rtc.cmd >> 1) { case 0: // status register 1 //INFO("RTC: read regstatus1 (0x%02X)\n", rtc.regStatus1); rtc.regStatus1 &= 0x0F; rtc.data[0] = rtc.regStatus1; //rtc.regStatus1 &= 0x7F; break; case 1: // status register 2 //INFO("RTC: read regstatus2 (0x%02X)\n", rtc.regStatus1); rtc.data[0] = rtc.regStatus2; break; case 2: // date & time { //INFO("RTC: read date & time\n"); time_t tm; time(&tm); struct tm *tm_local= localtime(&tm); tm_local->tm_year %= 100; tm_local->tm_mon++; rtc.data[0] = toBCD(tm_local->tm_year); rtc.data[1] = toBCD(tm_local->tm_mon); rtc.data[2] = toBCD(tm_local->tm_mday); rtc.data[3] = (tm_local->tm_wday + 6) & 7; if (!(rtc.regStatus1 & 0x02)) tm_local->tm_hour %= 12; rtc.data[4] = ((tm_local->tm_hour < 12) ? 0x00 : 0x40) | toBCD(tm_local->tm_hour); rtc.data[5] = toBCD(tm_local->tm_min); rtc.data[6] = toBCD(tm_local->tm_sec); break; } case 3: // time { //INFO("RTC: read time\n"); time_t tm; time(&tm); struct tm *tm_local= localtime(&tm); { if (!(rtc.regStatus1 & 0x02)) tm_local->tm_hour %= 12; rtc.data[0] = ((tm_local->tm_hour < 12) ? 0x00 : 0x40) | toBCD(tm_local->tm_hour); rtc.data[1] = toBCD(tm_local->tm_min); rtc.data[2] = toBCD(tm_local->tm_sec); break; } } case 4: // freq/alarm 1 /*if (cmdBitsSize[0x04] == 8) INFO("RTC: read INT1 freq\n"); else INFO("RTC: read INT1 alarm1\n");*/ //NDS_makeARM7Int(7); break; case 5: // alarm 2 //INFO("RTC: read alarm 2\n"); break; case 6: // clock adjust //INFO("RTC: read clock adjust\n"); rtc.data[0] = rtc.regAdjustment; break; case 7: // free register //INFO("RTC: read free register\n"); rtc.data[0] = rtc.regFree; break; } }
void getRTCTime(char* timeString) { //get the time from RTC char i; unsigned char dumpTime[8]; char dump[6]; unsigned char dateUpdate,BCDDate,registerAdr; for(i=1;i<7;i++) { //read the flag of each commmand for checking updates if(getReadyState(i)) { //If a new command for RTc has been received dateUpdate = getCdeValue(i); //get the command parameter registerAdr = WRITE_CDE; //preload the registerAdr with WRITE_CDE data switch (i) { case YEARS : if(dateUpdate>99) //clamp value for each field dateUpdate = 99; time.year = dateUpdate; registerAdr |= REG_YEAR_ADR; //complete with the register adress break; case MONTHS : if(dateUpdate>12) dateUpdate = 12; time.month = dateUpdate; registerAdr |= REG_MONTH_ADR; break; case DAYS : if(dateUpdate>31) dateUpdate = 31; time.day = dateUpdate; registerAdr |= REG_DATE_ADR; break; case HOURS : if(dateUpdate>24) dateUpdate = 24; time.hour = dateUpdate; registerAdr |= REG_HOURS_ADR; break; case MINUTES : if(dateUpdate>60) dateUpdate = 60; time.min = dateUpdate; registerAdr |= REG_MINUTES_ADR; break; case SECONDS : if(dateUpdate>60) dateUpdate = 60; time.sec = dateUpdate; registerAdr |= REG_SECONDS_ADR; break; default : break; } BCDDate = toBCD(dateUpdate); //convert to BCD code setDeviceSPI(RTC); //set the SPI device transferSPI(registerAdr); //write the register data transferSPI(BCDDate); //send the value setDeviceSPI(NONE); } } setDeviceSPI(RTC); //set selected RTC as device transferSPI(0); //send the read command for(i=0;i<8;i++) dumpTime[i]=transferSPI(0xff); //send dummy AA byte and get time setDeviceSPI(NONE); //deselect RTC convertTime(dumpTime,timeString); //convert raw data to ASCII string /* toChar(time.csec,dump); timeString[14] = dump[3]; timeString[15] = dump[4]; toChar(time.sec,dump); timeString[12] = dump[3]; timeString[13] = dump[4]; toChar(time.min,dump); timeString[10] = dump[3]; timeString[11] = dump[4]; toChar(time.hour,dump); timeString[8] = dump[3]; timeString[9] = dump[4]; toChar(time.day,dump); timeString[6] = dump[3]; timeString[7] = dump[4]; toChar(time.month,dump); timeString[4] = dump[3]; timeString[5] = dump[4]; toChar(time.year,dump); timeString[0] = '2'; timeString[1] = '0'; timeString[2] = dump[3]; timeString[3] = dump[4];*/ }