Forum Discussion
Altera_Forum
Honored Contributor
14 years agoHi cris,
here is my count_The_Frequency_Of_The_FAD() method: double count_The_Frequency_Of_The_FAD() { double count_timeH_before=0; double count_timeL_before=0; 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); //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); 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("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); //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; } And it works if I call it in the main function without any crashes.