Forum Discussion
Altera_Forum
Honored Contributor
14 years agoInterval Timer Problem
Hi, I got a system with an interval timer. Now I want to use it to to count how long the time is between two positiv flanks. But the counter won't start. My code: int count_timeH=0...
Altera_Forum
Honored Contributor
14 years agoI think I have found a solution:
--- Quote Start --- double count_timeH_before=0; double count_timeL_before=0; double count_timeH_after=0; double count_timeL_after=0; double total_time_steps=0; double calculated_frequency=0; alt_u32 count_step_before=0x0; alt_u32 count_step_after=0x0; //start address of the counter IOWR_ALTERA_AVALON_TIMER_PERIODH(SYS_CLK_TIMER_BASE,0xFFFF); IOWR_ALTERA_AVALON_TIMER_PERIODL(SYS_CLK_TIMER_BASE,0xFFFF); IOWR_ALTERA_AVALON_TIMER_CONTROL(SYS_CLK_TIMER_BASE,ALTERA_AVALON_TIMER_CONTROL_STOP_MSK); //total_time is in seconds but is measured in ms //the range depends on the frequency you want to have //while((total_time<=10) & (total_time>=5)){ //while((total_time<=0.001) & (total_time>=0.00065)){ //by writing to the snapregister you get the data from the counter IOWR_ALTERA_AVALON_TIMER_SNAPL(SYS_CLK_TIMER_BASE,0x1); IOWR_ALTERA_AVALON_TIMER_SNAPH(SYS_CLK_TIMER_BASE,0x2); //start the counter IOWR_ALTERA_AVALON_TIMER_CONTROL(SYS_CLK_TIMER_BASE,0x06); printf("Control : %x\n",IORD_ALTERA_AVALON_TIMER_CONTROL(SYS_CLK_TIMER_BASE)); printf("Status: %x\n", IORD_ALTERA_AVALON_TIMER_STATUS(SYS_CLK_TIMER_BASE)); count_timeL_before= IORD_ALTERA_AVALON_TIMER_SNAPL(SYS_CLK_TIMER_BASE); printf("SnapL : %e\n", count_timeL_before); count_timeH_before= IORD_ALTERA_AVALON_TIMER_SNAPH(SYS_CLK_TIMER_BASE); printf("SnapH : %e\n", count_timeH_before); count_step_before = IORD_ALTERA_AVALON_TIMER_SNAPH(SYS_CLK_TIMER_BASE)<<16; count_step_before |=IORD_ALTERA_AVALON_TIMER_SNAPL(SYS_CLK_TIMER_BASE); //wait till the FAD gives back the right frequency int j; for ( j = 0; j < 2; ++j) { push_The_Button(FAD_CLOCK_BASE); printf("Flanke Nummer %d \n", j); } //printf("SnapH : %e\n", (double)IORD_ALTERA_AVALON_TIMER_SNAPH(SYS_CLK_TIMER_BASE)); //stop the counter IOWR_ALTERA_AVALON_TIMER_CONTROL(SYS_CLK_TIMER_BASE,0x8); //by writing to the snapregister you get the data from the counter IOWR_ALTERA_AVALON_TIMER_SNAPL(SYS_CLK_TIMER_BASE,0x1); count_timeL_after= IORD_ALTERA_AVALON_TIMER_SNAPL(SYS_CLK_TIMER_BASE); printf("SnapL : %x\n", IORD_ALTERA_AVALON_TIMER_SNAPL(SYS_CLK_TIMER_BASE)); count_timeH_after= IORD_ALTERA_AVALON_TIMER_SNAPH(SYS_CLK_TIMER_BASE); printf("SnapH : %x\n", IORD_ALTERA_AVALON_TIMER_SNAPH(SYS_CLK_TIMER_BASE)); count_step_after = IORD_ALTERA_AVALON_TIMER_SNAPH(SYS_CLK_TIMER_BASE)<<16; count_step_after =count_step_after|(IORD_ALTERA_AVALON_TIMER_SNAPL(SYS_CLK_TIMER_BASE)); total_time_steps= (count_step_before-count_step_after); printf("Zwischenergebnis:%e\n", total_time_steps); calculated_time= total_time_steps/(50000000); printf("Calculated_time: %e\n",calculated_time); calculated_frequency=1/calculated_time; printf("Frequenz: %e\n",calculated_frequency); --- Quote End --- This should be right. Isn't it?