//Calibrate HF Clock and AGCs byte ucal_ufo(void) { byte check = NO_ERROR; int calc; uint16 hf1,hf2; PWRDWN = OFF; uset_reg(REG0, RESET_UFO); //If no reset, last reg values are used uset_reg(REG0, UNSET_UFO); uagc(UP_DIR); HFConst = 1; //Avoid div by 0 uset_reg(REG15, HF_CAL ); //Set Hf cal bit,leave Pcycle bit INTF = OFF; //Clear INTF flag us_start(UP_DIR); //Cal using up direction delay(TIMEOUT); if(!INTF) check = ERROR; else { TRISC = TRISC_IN; //Set PortC for input CCP1CON = CCP1CON_DATA; //Disble PWM while reading data hf1 = (uf_read(REG6) << 8 | uf_read(REG5))*10; hf2 = (uf_read(REG8) << 8 | uf_read(REG7))*10; TRISC = TRISC_OUT; //Restore PortC to output CCP1CON = CCP1CON_PWM; //Renable PWM mode calc = hf2 -hf1; if(calc<0) { calc *= -1; } //Set HFConst HFConst = (double)calc; //Set HFConst } uset_reg(REG15, PCYCL); //Reset HFcal if((HFConst > 1200) || (HFConst < 700)) check = ERROR; return(check); }
void uagc(byte dir) { asm("clrwdt"); Check = NO_ERROR; uset_reg(REG11,REG11_RESET); //Without this, a call to ustart_ufo() after uset_reg(REG11,PTRIG_AGC); //an iduced measurement failure will fail continuously?? INTF = OFF; //Clear INTF flag Delay_ticks = TIMEOUT; us_start(dir | AGC_CAL); //Start an AGC cal in passed direction while(!INTF && Delay_ticks); //Wait for ufo interrupt or time out if(!Delay_ticks || !INTF) Check = ERROR; //Missed AGC uset_reg(REG11, Pt); }
int main() { time_t now; int i, times; int sum, *arg; stpool_t *pool; /** Creat a task pool */ pool = stpool_create("mypool", /** pool name */ eCAP_F_DYNAMIC|eCAP_F_SUSPEND|eCAP_F_ROUTINE|eCAP_F_WAIT_ALL, 50, /** max servering threads */ 0, /** 0 servering threads that reserved for waiting for tasks */ 1, /** suspend the pool */ 0); /** default number of priority queue */ /** * Add tasks */ times = 90000; arg = (int *)malloc(times * sizeof(int)); for (i=0; i<times; i++) { /** * It may take a long time to load a large amount of tasks * if the program is linked with the debug library */ if (i % 4000 == 0 || (i + 1) ==times) { printf("\rLoading ... %.2f%% ", (float)i * 100/ times); fflush(stdout); } arg[i] = i; stpool_add_routine(pool, "sche", task_run, NULL, (void *)&arg[i], NULL); } printf("\nAfter having executed @stpool_add_routine for %d times:\n" "--------------------------------------------------------\n%s\n", times, stpool_stat_print(pool)); /** * Wait for all tasks' being done. */ { unsigned us; /** * Wake up the pool to schedule tasks */ stpool_resume(pool); us_start(); stpool_wait_all(pool, -1); us = us_end(); printf("Test costs %.2f ms \n", (double)us / 1000); } /** * Get the sum */ for (i=0, sum=0; i<times; i++) sum += arg[i]; free(arg); now = time(NULL); printf("--OK. finished. <arg: %d> %s\n%s\n", sum, ctime(&now), stpool_stat_print(pool)); #if 0 /** * You can use debug library to watch the status of the pool */ { int c; while ('q' != getchar()) { for (i=0; i<40; i++) stpool_add_routine(pool, "debug", task_run, NULL, &sum, NULL); } /** * Clear the stdio cache */ while ((c=getchar()) && c != '\n' && c != EOF) ; } #endif getchar(); /** * Release the pool */ printf("Shut down the pool now.\n"); stpool_release(pool); getchar(); return 0; }