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;
int count_timeL=0;
IOWR_ALTERA_AVALON_TIMER_PERIODH(SYS_CLK_TIMER_BASE,0x0);
IOWR_ALTERA_AVALON_TIMER_PERIODL(SYS_CLK_TIMER_BASE,0x0);
//start the counter
IOWR_ALTERA_AVALON_TIMER_CONTROL(SYS_CLK_TIMER_BASE,0x04);
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= IORD_ALTERA_AVALON_TIMER_SNAPL(SYS_CLK_TIMER_BASE);
printf("SnapL : %d\n", count_timeL);
count_timeH= IORD_ALTERA_AVALON_TIMER_SNAPH(SYS_CLK_TIMER_BASE);
printf("SnapH : %d\n", count_timeH);
//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);
}
//stop the counter
IOWR_ALTERA_AVALON_TIMER_CONTROL(SYS_CLK_TIMER_BASE,0x08);
printf("Status: %x\n", IORD_ALTERA_AVALON_TIMER_STATUS(SYS_CLK_TIMER_BASE));
count_timeL= IORD_ALTERA_AVALON_TIMER_SNAPL(SYS_CLK_TIMER_BASE);
printf("SnapL : %d\n", count_timeL);
count_timeH= IORD_ALTERA_AVALON_TIMER_SNAPH(SYS_CLK_TIMER_BASE);
printf("SnapH : %d\n", count_timeH);And the registers contents are: Control : 0
Status: 1
SnapL : 9216
SnapH : 3286019
Knopf drücken!
Flanke Nummer 0
Knopf drücken!
Flanke Nummer 1
Status: 0
SnapL : 9216
SnapH : 3154947So how you can see in the code I just start counting but the control register still is "0". Why the content doesn't cange? And from which registers I can read my countsize? Thanks in advance for your help!13 Replies
- Altera_Forum
Honored Contributor
Hi cris,
I'm using it as a cycle counter. So I need to change the timeout period to 1sec instead of 1 msec if I want to measure a time round about 0.0008 sec. I also want to measure it exactly as possible. Thanks is advance for your answer! - Altera_Forum
Honored Contributor
--- Quote Start --- I'm using it as a cycle counter. So I need to change the timeout period to 1sec instead of 1 msec if I want to measure a time round about 0.0008 sec. --- Quote End --- You definitely need a timeout period larger than the measured time. As I said above, if your timer is dedicated to the measuring task and doesn't serve other functions or peripherals, you can operate it freely. So, I suggest this: - set timeout period larger than the maximum expected interval to be measured - timer initialized in stopped status - reset and start the timer when you start your measure, - stop the timer at the end of measure - read the duration --- Quote Start --- I also want to measure it exactly as possible. --- Quote End --- This greatly depends on your application, namely how you perform the measurement: - poll a PIO or any other condition and start/stop/read the timer: poor precision, dependent from your polling rate, unless you can afford using the 100% of cpu time for the polling loop and don't execute any other task - use a irq triggered by start/stop conditions: common solution for greater system effinciency and good precision in the us range - use a hardware counter solution which automatically identifies start/end conditions and places the measurement in a register which you can read without effort: maximum precision at the expense of a slight increment in hw complexity (fpga resource usage) - Altera_Forum
Honored Contributor
Hi cris,
thanks for the help, now it works! I just setup the timeout period to "1sec" and changed a little bit of my code: --- Quote Start --- double count_The_Frequency() { double total_time_steps=0; double calculated_time=0; double calculated_frequency=0; alt_u32 count_step_1=0x0; alt_u32 count_step_2=0x0; int a=0; //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); //wait till the FAD gives back the right frequency int j; for ( j = 0; j < 2; ++j) { push_The_Button(FAD_CLOCK_BASE); a=a+1; IOWR_ALTERA_AVALON_TIMER_SNAPL(SYS_CLK_TIMER_BASE,0x1); if(a==1){ count_step_1 = IORD_ALTERA_AVALON_TIMER_SNAPH(SYS_CLK_TIMER_BASE)<<16; count_step_1 =count_step_1|(IORD_ALTERA_AVALON_TIMER_SNAPL(SYS_CLK_TIMER_BASE)); } if(a==2){ count_step_2 = IORD_ALTERA_AVALON_TIMER_SNAPH(SYS_CLK_TIMER_BASE)<<16; count_step_2 =count_step_2|(IORD_ALTERA_AVALON_TIMER_SNAPL(SYS_CLK_TIMER_BASE)); } } //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); //printf("1; %x\n", count_step_1); // printf("2: %x \n", count_step_2); total_time_steps= (count_step_1-count_step_2); calculated_time= total_time_steps/(50000000); calculated_frequency=1/calculated_time; // printf("Frequenz: %e\n", calculated_frequency); return calculated_frequency; } --- Quote End ---