//////////////////////////////////////////////////////////////////////////////////////// //初始化串口控制器 //sysclk:系统时钟(Mhz) void usmart_init(u8 sysclk) { #if USMART_ENTIMX_SCAN==1 Timer4_Init(1000,(u32)sysclk*100-1);//分频,时钟为10K ,100ms中断一次,注意,计数频率必须为10Khz,以和runtime单位(0.1ms)同步. #endif usmart_dev.sptype=1; //十六进制显示参数 }
void Main(void) { //硬件初始化 var_Init();//初始化定时器相关的参数 Port_Init();//IO端口初始化 Isr_Init();//中断初始化 Uart_Init(0,115200);//串口初始化 Uart_Select(0); _init_alloc(0x32500000,0x333fffff);//初始化堆地址:15M,使能malloc等存储分配函数 setlocale(LC_ALL,"C");//使能本地函数,如sprintf等 Timer4_Init(); lcdTest(); }
/******************************************************* * "Soft" real-time event handler for slow rate ********************************************************/ void slow_event_handler(void) { if(slow_event_count > slow_ticks_limit) { slow_event_count = 0; if(control_flags.first_scan) { putsUART((unsigned char *)WelcomeMsg,&UART1); //putsUART((unsigned char *)WelcomeMsg,&UART2); control_flags.first_scan = 0; } // (RAM) Parameters update management if(control_flags.PAR_update_req) { update_params(); control_flags.PAR_update_req = 0; } if(direction_flags.word != direction_flags_prev) { // RESET COUNTS QEI1_Init(); QEI2_Init(); Timer1_Init(); Timer4_Init(); direction_flags_prev = direction_flags.word; } // EEPROM update management if(control_flags.EE_update_req) { control_flags.EE_update_req = 0; } update_delta_joints(); update_delta_EE();//aggiornamento delle strutture dati status_flags.homing_done = home_f.done; // SACT protocol timeout manager (see SACT_protocol.c) SACT_timeout(); SACT_SendSDP(); SACT_SendSSP(); // CONTROL MODE STATE MANAGER control_mode_manager(); } // END IF slow_event_count.. }// END slow_event_handler
/******************************************************* * MAIN function, just setup some inits and loops * "soft" real-time event handlers, defined hereafter ********************************************************/ int main(void) { // configuro l'oscillatore interno che mi fornisce Tcy // Fosc = Fin (M/(N1*N2)) // FCY = Fosc/2 PLLFBD = 39; // M = 40 CLKDIVbits.PLLPOST=0; // N2 = 2 CLKDIVbits.PLLPRE=0; // N1 = 2 RCONbits.SWDTEN = 0; //disabilito il watchdog DataEEInit(); //Init Peripheral Pin Selection (QEI and UART) PPS_Init(); control_flags.first_scan = 1; slow_ticks_limit = SLOW_RATE * (FCY_PWM / 1000) - 1 ; medium_ticks_limit = MEDIUM_RATE * (FCY_PWM / 1000) - 1; mposition1 = zero_pos1;//parto dalla posizione iniziale 90 90 90 mposition2 = zero_pos2; mposition3 = zero_pos3; /*mtheta1 = 0; mtheta2 = 0; mtheta3 = 0; x_cart = 0; y_cart = 0; z_cart = 0;*/ coordinates_actual.x = 0; coordinates_actual.y = 0; coordinates_actual.z = 0; coordinates_temp.x = 0; coordinates_temp.y = 0; coordinates_temp.z = 0; angleJoints_actual.theta1 = 0; angleJoints_actual.theta2 = 0; angleJoints_actual.theta3 = 0; angleJoints_temp.theta1 = 0; angleJoints_temp.theta2 = 0; angleJoints_temp.theta3 = 0; update_params(); direction_flags_prev = direction_flags.word; // UARTs init // no need to set TRISx, they are "Module controlled" UART1_Init(); // Setup control pins and PWM module, // which is needed also to schedule "soft" // real-time tasks w/PWM interrupt tick counts DIR1 = direction_flags.motor1_dir;//0; DIR2 = direction_flags.motor2_dir;//1; DIR3 = direction_flags.motor3_dir; //BRAKE1 = 0; //BRAKE2 = 0; DIR1_TRIS = OUTPUT; DIR2_TRIS = OUTPUT; DIR3_TRIS = OUTPUT; //BRAKE1_TRIS = OUTPUT; //BRAKE2_TRIS = OUTPUT; CURRSENSE1_TRIS = INPUT; CURRSENSE2_TRIS = INPUT; CURRSENSE3_TRIS = INPUT; PWM_Init(); // MUST SETUP ALSO ANALOG PINS AS INPUTS AN0_TRIS = INPUT; AN1_TRIS = INPUT; AN2_TRIS = INPUT; ADC_Init(); DMA0_Init(); // SETUP ENCODER INPUTS // QEI inputs are "module controlled" // -> no need to set TRISx QEI1_Init(); QEI2_Init(); // Timers used to acquire Encoder 3 // corresponding PINS set as inputs T1CK_TRIS = INPUT; T4CK_TRIS = INPUT; Timer1_Init(); Timer2_Init(); Timer4_Init(); // Timer5 used to schedule POSITION loops Timer5_Init(); //Input capture IC1_Init(); IC2_Init(); // TEST PIN TEST_PIN_TRIS = OUTPUT; TEST_PIN = FALSE; while(1)//a ciclo infinito ripeto queste 2 routine { medium_event_handler(); slow_event_handler(); } return 0; //code should never get here }// END MAIN()