// ** Grundinitialisierung ** void coldstart () { setup_adc_ports(sAN0|sAN1|sAN2|sAN3|sAN4|VSS_VDD); setup_adc(ADC_CLOCK_INTERNAL|ADC_TAD_MUL_0); setup_oscillator(OSC_8MHZ|OSC_INTRC); setup_comparator(NC_NC_NC_NC); output_a (0b00001000); output_b (0); output_c (0); output_d (0); output_e (0); set_tris_a (TRISA_INIT); // Datenrichtung Port A set_tris_b (TRISB_INIT); // Datenrichtung Port B set_tris_c (TRISC_INIT); set_tris_d (TRISD_INIT); set_tris_e (TRISE_INIT); port_b_pullups(TRUE); setup_timer_0(RTCC_INTERNAL|RTCC_DIV_32|RTCC_8_BIT); // Timer0 intern, Takt 20.00/4/64 = 78.125 KHz // Interrupt alle 256/15.625 = 3.2768 ms (305Hz) // Korrekturwert für 10 ms: 156 Timerclicks // -> Timer wird auf 256-156=100 vorgestellt set_timer0 (Timerstartwert_K); // Timerwert auf Startwert setzen enable_interrupts(INT_TIMER0); setup_timer_1(T1_DISABLED); // Nur Timer0 Interrupt delay_ms (200); }
void PowerOnSetProc() { port_b_pullups(TRUE); output_b(0xff); output_c(0xff); set_tris_A(0b00000011); set_tris_B(0b01111111); set_tris_D(0b00000000); set_tris_E(0b00000000); set_tris_C(0b10010000); setup_adc_ports(NO_ANALOGS); setup_adc(ADC_OFF); setup_comparator(NC_NC_NC_NC); setup_vref(FALSE); setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); setup_timer_2(T2_DIV_BY_4,250,2); // 8,000,000 / (4 * 8 * (249 +1)) = 1,000 = 1/1000 sec set_timer2(0); enable_interrupts(INT_TIMER2); // LCD 초기화 하기 전에 대기 없으면 이상한 현상이 생김 delay_ms(100); LCD_Initialization(); setup_psp(PSP_DISABLED); delay_ms(250); SerialPortSetup(); LCD_Clear(); enable_interrupts(INT_RDA); enable_interrupts(GLOBAL); //"01234567890123456789" strcpy(st, "DIGITAL OPERAT "); PrintLCD(0,0,st); strcpy(st, "[EwDo-21] v2.60 "); PrintLCD(1,0,st); strcpy(st, "EunWho Power Electic"); PrintLCD(2,0,st); strcpy(st, "TEL 82-51-262-7532 "); PrintLCD(3,0,st); delay_ms(3250); delay_ms(3250); }
//-----------------------------------------------------------------// // Programa Principal ---------------------------------------------// //-----------------------------------------------------------------// void main() { int i; set_tris_a(0xFF); // Puerto A todo entradas port_b_pullups(FALSE); // Resistencias de polarización set_tris_b(0x38); // set_tris_c(0x00); // Puerto C todo salidas set_tris_d(0x00); // Puerto D todo salidas //Configuro los canales del ADC SETUP_ADC_PORTS(AN0_AN1_AN2_AN3_AN4); SETUP_ADC(ADC_CLOCK_DIV_8 ); SETUP_VREF(VREF_HIGH | 6 ); //Habilito interrupciones //enable_interrupts(int_rda); //enable_interrupts(global); while(1){ realizar_ensayo(); //printf("%4ld \r",md); } }
void main() { port_b_pullups(TRUE); // Pullupy pro pripojeni tlacitka setup_adc_ports(NO_ANALOGS); setup_adc(ADC_OFF); setup_spi(SPI_SS_DISABLED); setup_timer_0(RTCC_EXT_L_TO_H|RTCC_DIV_1); setup_timer_1(T1_EXTERNAL|T1_DIV_BY_8|T1_CLK_OUT); setup_timer_2(T2_DISABLED,0,1); setup_comparator(NC_NC_NC_NC); setup_vref(FALSE); enable_interrupts(INT_TIMER1); enable_interrupts(INT_TIMER0); enable_interrupts(GLOBAL); ODROID_ON_1; //zapnutí napájení odroidu delay_ms(10); ODROID_ON_0; ODROID_I=1; PCDR_ON_1; //zapnutí napájení PCDR delay_ms(10); PCDR_ON_0; PCDR_I=1; GPS_ON_1; //zapnutí napájení GPS delay_ms(10); GPS_ON_0; GPS_I=1; GPRS_ON_1; //zapnutí napájení GPRS delay_ms(10); GPRS_ON_0; GPRS_I=1; while(TRUE) { //---------------------------------------------------------------------- //---------------------------------------------------------------------- //osetruje odpojeni napajeni ODROIDU na externí pokyn if(IN_ODROID) { if(ODROID_I) { ODROID_OFF_1; // odpojí relé delay_ms(10); ODROID_OFF_0; ODROID_I=0; } else { } } else { if(ODROID_I) { } else { ODROID_ON_1; // pripojí relé delay_ms(10); ODROID_ON_0; ODROID_I=1; } } //---------------------------------------------------------------------- //---------------------------------------------------------------------- //osetruje odpojeni napajeni PCDR na externí pokyn if(IN_PCDR) { if(PCDR_I) { PCDR_OFF_1; // odpojí relé delay_ms(10); PCDR_OFF_0; PCDR_I=0; } else { } } else { if(PCDR_I) { } else { PCDR_ON_1; // pripojí relé delay_ms(10); PCDR_ON_0; PCDR_I=1; } } //---------------------------------------------------------------------- //---------------------------------------------------------------------- //osetruje odpojeni napajeni GPS na externí pokyn if(IN_GPS) { if(GPS_I) { GPS_OFF_1; // odpojí relé delay_ms(10); GPS_OFF_0; GPS_I=0; } else { } } else { if(GPS_I) { } else { GPS_ON_1; // pripojí relé delay_ms(10); GPS_ON_0; GPS_I=1; } } //---------------------------------------------------------------------- //---------------------------------------------------------------------- //osetruje odpojeni napajeni GPRS na externí pokyn if(IN_GPRS) { if(GPRS_I) { GPRS_OFF_1; // odpojí relé delay_ms(10); GPRS_OFF_0; GPRS_I=0; } else { } } else { if(GPRS_I) { } else { GPRS_ON_1; // pripojí relé delay_ms(10); GPRS_ON_0; GPRS_I=1; } } } }
void main() { TRISC=0; // Variables para controlador int16 valor; float control; //valor del PWM float a1,b1,c1; //constantes del PID float ref; //temperatura a alcanzar float rT,eT,iT,dT,yT,uT,iT0,eT0,iT_1,eT_1; //variables de ecuaciones float max,min; //límites máximo y mínimo de control. float T ,Kp1, Ti1,Td1; setup_adc_ports(RA0_ANALOG);//entrada del LM35 setup_adc(ADC_CLOCK_INTERNAL); setup_COUNTERS(RTCC_internal.rtcc_div_1); set_adc_channel(0); setup_timer_2(t2_div_by_4,500,1); //periodo de la señal PWM a 1ms setup_ccp1(ccp_pwm); //Módulo CCP a modo PWM setup_adc(ADC_CLOCK_INTERNAL); //reloj convertidor AD interno set_adc_channel(0); setup_timer_0(rtcc_ext_l_to_h|RTCC_DIV_2); //Configuración TMR0 setup_timer_1(T1_internal|T1_DIV_BY_8); //Configuración TMR1 float Temp; int c; char k; char Kp[6]; char Ki[6]; char Kd[6]; char Sp[6]; int v; port_b_pullups(true), lcd_init(); kbd_init(); inicio: for (v=0;v<=5;v++){ Kp[v]=0; Ki[v]=0; Kd[v]=0; Sp[v]=0; } lcd_gotoxy(1,1); lcd_putc("\f"); lcd_putc("Kp:"); lcd_gotoxy(9,1); lcd_putc("Ki:"); lcd_gotoxy(1,2); lcd_putc("Kd:"); lcd_gotoxy(9,2); lcd_putc("Sp:"); while(true){ K_p: lcd_gotoxy(4,1); lcd_send_byte(0,0x0f); c=0; k=0; while(c<=4){ k=kbd_getc(); if(k!=0){ if(k!='A' && k!='*' && k!='C'){ if(k=='B'){ printf(lcd_putc,"."); Kp[c]=k; lcd_gotoxy(4+c,1); } else printf(lcd_putc,"%c",k); Kp[c]=k; lcd_gotoxy(4+c,1); } if(k=='A'){ c++; Kp[c]=-92; lcd_gotoxy(4+c,1); } } if(k=='D'){ int j=0; for(;;){ Kp[j]=0; if(j==5) break; j++; } c=0; lcd_gotoxy(1,1); lcd_putc("Kp: "); lcd_gotoxy(4+c,1); } if(k=='*'){ Kp[c+1]=-92; goto K_i; } } //FUERA DEL WHILE lcd_gotoxy(4+c-1,1); lcd_send_byte(0,0x0f); k=0; for(;;){ k=kbd_getc(); if(k!=0){ if(k=='*'){ break; } } } K_i: lcd_gotoxy(12,1); lcd_send_byte(0,0x0f); c=0; k=0; ///KI while(c<=4){ k=kbd_getc(); if(k!=0){ if(k!='A' && k!='*'){ if(k=='B'){ printf(lcd_putc,"."); Ki[c]=k; lcd_gotoxy(12+c,1); } else printf(lcd_putc,"%c",k); Ki[c]=k; lcd_gotoxy(12+c,1); } if(k=='A'){ c++; Ki[c]=-92; lcd_gotoxy(12+c,1); } } if(k=='D'){ int j=0; for(;;){ Ki[j]=0; if(j==5) break; j++; } c=0; lcd_gotoxy(9,1); lcd_putc("Ki: "); lcd_gotoxy(12+c,1); } if(k=='*'){ Ki[c+1]=-92; goto K_d; } } ///FIN KI ///FUERA DEL WHILE lcd_gotoxy(12+c-1,1); lcd_send_byte(0,0x0f); k=0; for(;;){ k=kbd_getc(); if(k!=0){ if(k=='*'){ break; } } } K_d: lcd_gotoxy(4,2); lcd_send_byte(0,0x0f); c=0; k=0; while(c<=4){ k=kbd_getc(); if(k!=0){ if(k!='A' && k!='*'){ if(k=='B'){ printf(lcd_putc,"."); Kd[c]=k; lcd_gotoxy(4+c,2); } else printf(lcd_putc,"%c",k); Kd[c]=k; lcd_gotoxy(4+c,2); } if(k=='A'){ c++; Kd[c]=-92; lcd_gotoxy(4+c,2); } } if(k=='D'){ int j=0; for(;;){ Kd[j]=0; if(j==5) break; j++; } c=0; lcd_gotoxy(1,2); lcd_putc("Kd: "); lcd_gotoxy(4+c,2); } if(k=='*'){ Kd[c+1]=-92; goto S_p; } } //FUERA WHILE lcd_gotoxy(4+c-1,2); lcd_send_byte(0,0x0f); k=0; for(;;){ k=kbd_getc(); if(k!=0){ if(k=='*'){ break; } } } ///SP S_p: lcd_gotoxy(12,2); lcd_send_byte(0,0x0f); c=0; k=0; while(c<=4){ k=kbd_getc(); if(k!=0){ if(k!='A' && k!='*'){ if(k=='B'){ printf(lcd_putc,"."); Sp[c]=k; lcd_gotoxy(12+c,2); } else printf(lcd_putc,"%c",k); Sp[c]=k; lcd_gotoxy(12+c,2); } if(k=='A'){ c++; Sp[c]=-92; lcd_gotoxy(12+c,2); } } if(k=='D'){ int j=0; for(;;){ Sp[j]=0; if(j==5) break; j++; } c=0; lcd_gotoxy(9,4); lcd_putc("Sp: "); lcd_gotoxy(12+c,2); } if(k=='*'){ Sp[c+1]=-92; goto PID; } } lcd_gotoxy(12+c-1,2); lcd_send_byte(0,0x0f); k=0; for(;;){ k=kbd_getc(); if(k!=0){ if(k=='*'){ break; } } } PID: printf(lcd_putc, "\f"); // Borra la pantalla lcd_gotoxy(4, 1); printf(lcd_putc, "procesando.. "); delay_ms(600); float result; a1 = conv(Kp); b1 = conv(Ki);c1 = conv(Kd);ref = conv(Sp); // strtod(cc,NULL); lcd_init(); iT0=0.0; eT0=0.0; min=0.0; //inicialización variables uT = 0.0; max=1023; while(True){ Temp=(float)read_adc(); yT=Temp*5.0/1024.0; rT=ref; eT=rT-yT; //Cálculo error lcd_gotoxy(1,2); printf(lcd_putc,"error:%f",eT); iT=b1*eT+iT0; //Cálculo del término integral dT=c1*(eT-eT0); //Cálculo del término derivativo uT=iT+a1*eT+dT; //Cálculo de la salida PID lcd_gotoxy(1,1); printf(lcd_putc,"Out:%f",uT); if (uT>max) { //Salida PID si es mayor que el MAX uT=max;} else { if (uT<min){ //Salida PID si es menor que el MIN uT=min;} } control=uT; //Transferencia de salida PID a señal PWM lcd_gotoxy(10,1); printf(lcd_putc,"Sp:%f",ref); set_pwm1_duty(control); iT0=iT; //Guardar variables eT0=eT; delay_ms(20); } } }
void main() { int1 flag = 0; porta = 0;//all ports are zero portb = 0; portc = 0; setup_adc_ports(no_analogs|vss_vdd); //digital functions selected setup_adc(adc_off); //internal rc oscillator disabled for adc setup_wdt(wdt_off); //watch dog timer disabled setup_timer_0(rtcc_off); //all timers disabled setup_timer_1(t1_disabled); setup_timer_2(t2_disabled,0,1); setup_timer_3(t3_disabled|t3_div_by_1); setup_comparator(nc_nc_nc_nc); //comparators disabled setup_vref(false); //no reference voltage in ra2 setup_ccp1(ccp_off); //disable ccp1 setup_ccp2(ccp_off); //disable ccp2 enable_interrupts(int_rda); //uart rx interruption enabled enable_interrupts(global); //global interruptions enabled usb_cdc_init(); usb_init(); //initialize hardware usb and wait for PC conection set_tris_a(0b00111111); set_tris_b(0b11111011);//rb2 output mclr dspic port_b_pullups(false); set_tris_c(0b10111111); stateDspic = running; counterReset = 0; delay_ms(500);//wait for supply stabilization while(true) { usb_task(); manage_conection(); if (usb_cdc_kbhit()) { data_rx_usb=usb_cdc_getc();//read buffer and save in data_rx printf("%c",data_rx_usb);//send through uart if (data_rx_usb == rstKeyword[0]) { if (counterReset == 0) counterReset++; } else if (data_rx_usb == rstKeyword[1]) { if (counterReset == 1) counterReset++; else counterReset = 0; } else if (data_rx_usb == rstKeyword[2]) { if (counterReset == 2) counterReset++; else counterReset = 0; } else if (data_rx_usb == rstKeyword[3]) { if (counterReset == 3) counterReset++; else counterReset = 0; } else if (data_rx_usb == rstKeyword[4] && counterReset == 4)//here, all requirements were met { counterReset = 0; flag = 0; //reset flag for(i = 0; i < 10000; i++) //wait for the next byte { if (usb_cdc_kbhit()) //if a new byte is received { data_rx_usb = usb_cdc_getc();//read buffer and save in data_rx printf("%c",data_rx_usb);//send through uart flag = 0; break; } flag = 1; } if (flag == 1) //apply reset when no characters were received { stateDspic = stop; delay_ms(50); stateDspic = running; } } else counterReset = 0; } } }