// *********************************************************** // Project: AVR time interval counter 4 - assigns 64 bit time stamp to event --> send to host via serial port // Author: Dr Simon Schroedle // Started: 3/19/2016 // *********************************************************** #include // Most basic include files #include // Add the necessary ones #include // here #define F_CPU 10000000UL // 10 MHz #define BAUD 38400UL #define UBRR_BAUD ((F_CPU/(16UL*BAUD))-1) #define UBRR_VAL ((F_CPU+BAUD*8)/(BAUD*16)-1) #define BAUD_REAL (F_CPU/(16*(UBRR_VAL+1))) #define BAUD_ERROR ((BAUD_REAL*1000)/BAUD) #if ((BAUD_ERROR<980) || (BAUD_ERROR>1020)) #error Deviation of actual vs target baud rate exceeds 2% - not good #endif void wait_10k(void) { /*wait 10 k cycles*/ uint16_t counter; counter=10000; do { } while (--counter); } // USART initialisieren void uart_init(void) { // Baudrate einstellen (Normaler Modus) UBRRH = (uint8_t) (UBRR_BAUD>>8); UBRRL = (uint8_t) (UBRR_BAUD & 0x0ff); // Aktivieren von transmitter UCSRB |= (1< can't use timer overflow int) void usend(void) { uint8_t i; for (i=0;i<12;i++) { while ( !( UCSRA & (1<0x8000, need to increment tof to reflect actual counts tof_c=tof; //capture tof data if (timer_c&0x8000) { if (!tof_upd_c) {tof_c++;} } if ((tof_upd_c)&&(!tofupd2)) { if (otof==tof) {tof_c++;} //in case that overflow count flag has been set, but actual count not yet occured } if (timer_c<0x8000) { timer_c_adj=timer_c+0x7fff;} //need to adjust because tof counts +1 when counter reaches 0x1000 if (timer_c>=0x8000) { timer_c_adj=timer_c-0x8000;} timestamp_last=tof_c; timestamp_last<<=16; timestamp_last|=timer_c_adj; if (last_send) { missed_ts=0;} //need to ensure that missed ts info is actually transmitted, even if many missed events if (!last_send) { missed_ts=1; } last_send=0; } void send_data(void) { uint8_t i; for (i=0;i<8;i++) { ticdata[8-i]=(timestamp_last>>(i<<3))&0xff; } ticdata[0]='T'; ticdata[9]=0; //reserved for checksum ticdata[10]=0x80|missed_ts; //status code 0x80 - OK; 0x81 -missed last ts transmit (too many events) ticdata[11]='E'; //END OF TRANSMIT usend(); //sets last_send } void send_timerv(void) { uint16_t t_tmp; t_tmp=TCNT1; while ( !( UCSRA & (1<>8)&0xff; while ( !( UCSRA & (1<>15; //highest bit value if (!timer_t){ tof_upd=0; PORTC=0x00;} if ((timer_t)&&(!tof_upd)) { tofupd2=0; //after execution: tofupd2=0, tof_upd=0 --> need to increment tof otof=tof; tof_upd=1; //tofupd2=0, tof_upd=1 --> need to increment tof only if otof=tof tof++; //tofupd2=0, tof_upd=1 --> tof incremented, otof!=tof tofupd2=1; //tofupd2=1, tof_upd=1 --> tof incremented //what if interrupt happens in between flag update and actual count? - this will eliminate such error //send_timerv(); PORTC=0x02; if (!last_send){ //transmit last event data, need to send it after overflow otherwise timeout! --for fast clock send_data(); } } //asyn overflow and set upd to indicate that tof has been incremented //need to ensure that asyn update check occurs frequently enough - need to monitor data transfer time } }