Autor Tema: Quadcopter PIC 16F84 en CCS  (Leído 1990 veces)

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado Abner_emanuel

  • PIC16
  • ***
  • Mensajes: 160
Quadcopter PIC 16F84 en CCS
« en: 22 de Enero de 2016, 23:03:17 »
Aquí les dejo un code que me encontré de un cuadcopter por si a alguno de ustedes les interesa. Yo e querido realizar uno aunque tengo poca experiencia en microcontroladores.
Código: C
  1. #include <16F84.h>
  2.  
  3. #FUSES NOWDT                    //No Watch Dog Timer
  4. #FUSES HS                       //High speed Osc (> 4mhz)
  5. #FUSES NOPUT                    //No Power Up Timer
  6. #FUSES NOPROTECT                //Code not protected from reading
  7.  
  8. #use delay(clock=20000000)
  9. //#use rs232(baud=9600,xmit=PIN_B6,rcv=PIN_B7)  //debug
  10. #byte portb=0x06
  11. INT8  i,j,indata,indatb,TimerS,templateA;
  12. INT16 TimerL;
  13.  
  14. INT R[4];
  15. INT16 T[4];
  16. INT16 PWM[4];
  17.  
  18. //------------------------
  19.  
  20. void main(){
  21.  
  22. // 0   Init
  23.  
  24. setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
  25. templateA= 0b00001111  ;           // input 4 channels
  26.  
  27. PWM[0]=227;
  28. PWM[1]=151;  //init throttle at lowerst position
  29. PWM[2]=227;
  30. PWM[3]=227;
  31.      
  32.   //LDHUng   ,Inital Gyro esky 0704,It take about 15 second
  33.    set_tris_b(0);
  34.    for(TimerL=1;TimerL<=710; TimerL++)
  35.         {
  36.             portb = 0b00001111;
  37.             delay_us(1500); //want servo to move to 90 degrees,center point servo
  38.             portb = 0b00000000;
  39.             if(TimerL%20<=10)
  40.                portb = 0b00010000;
  41.           delay_us(18500);
  42.        }
  43.    output_low(pin_B4);
  44.    //LDHUng
  45.    delay_mS(100);                  // wait
  46. // wait an inpit
  47. // To find an interval > 2 mS ,the syn single of PPM
  48.  
  49. TimerS =0;
  50. do{
  51.       indatb = INPUT_A();
  52.       indata=(indatb & templateA);}
  53.       while(indata==0);
  54.    
  55. while(TimerS < 40) {
  56.       indatb = INPUT_A();
  57.       indata=(indatb & templateA);
  58.       if(indata ==0) {TimerS++ ; delay_uS(50);} //50uS*40 =2mS
  59.       else TimerS =0;
  60.                      }
  61.  
  62. // A   Determine the input sequence
  63. // Had find the begin of PPM string
  64. // store first channel of PWM in R0, second in R1..
  65.  
  66. i=0;
  67. do {
  68.      do{
  69.      indatb = INPUT_A();
  70.      indata=(indatb & templateA );}
  71.      while (indata ==0);
  72.      
  73.      if (indata ==1){  R[i]=1;  templateA=(templateA ^0b00000001);}
  74.      if (indata ==2){  R[i]=2;  templateA=(templateA ^0b00000010);}  
  75.      if (indata ==4){  R[i]=4;  templateA=(templateA ^0b00000100);}  
  76.      if (indata ==8){  R[i]=8;  templateA=(templateA ^0b00001000);}  
  77.  
  78.       i++;
  79.      
  80.       do{
  81.       indatb = INPUT_A();
  82.       indata=(indatb & templateA );}
  83.       while (! indata ==0);                  
  84.    }
  85. while (! templateA==0);    // all 4in were detect and sort
  86.    
  87.  
  88. //------------------------
  89. // B   Read 4 input one by one
  90.  
  91. // Begin of Endless loop
  92. // Read PWM one by one, R1~R4, take 5.5~9.5 mS
  93. // T0~T3 = 151~302 = 1mS~2mS +- 10%
  94. // delay_cycles(24) =33cycles 6.6uS,  
  95.  
  96. do{
  97.    output_low(pin_B4);    // Valid input indecator
  98.  
  99.    for  (i=0;i<4;++i){
  100.          timerL=0;
  101.          j=R[i];
  102.      
  103.       if (j==1){  do {}                         //read first channel
  104.                   while (! input(pin_A0) );
  105.                   do { TimerL ++;               // first channel rise
  106.                           delay_cycles(24);     // make total 33 cycles
  107.                       }
  108.                   while ( input(PIN_A0) );      
  109.                   T[0]=TimerL;                  //first channel down
  110.                  }
  111.                  
  112.       if (j==2){  do {}
  113.                   while (! input(pin_A1) );
  114.                   do { TimerL ++;
  115.                           delay_cycles(24);
  116.                       }
  117.                   while ( input(PIN_A1) );
  118.                   T[1]=TimerL;
  119.                   }  
  120.                  
  121.       if (j==4){  do {}
  122.                   while (! input(pin_A2) );
  123.                   do { TimerL ++;
  124.                           delay_cycles(24);
  125.                       }
  126.                   while ( input(PIN_A2) );
  127.                   T[2]=TimerL;
  128.                   }  
  129.                  
  130.       if (j==8){  do {}
  131.                   while (! input(pin_A3) );
  132.                   do { TimerL ++;
  133.                           delay_cycles(24);
  134.                       }
  135.                   while ( input(PIN_A3) );
  136.                   T[3]=TimerL;
  137.                   }          
  138.  
  139.       output_high(pin_B4);          // Valid input
  140.  
  141. }    
  142.  
  143.  
  144. //---------------------------
  145. // C   Calculating
  146. // Calculating must less than 2.5mS
  147.  
  148.  
  149.  
  150. /*PWM[0] = T[1] +(T[3]/2) -(T[0]/2)-1;
  151. PWM[1] = T[1] +227 -(T[0]/2) -(T[3]/2)+2;
  152. PWM[2] = T[1] +(T[0]/2) -(T[2]/2)-3;
  153. PWM[3] = T[1] +(T[0]/2) +(T[2]/2) -227 -3 ;
  154. */
  155. PWM[0] = T[1] +T[3] -T[0]-1;
  156. PWM[1] = T[1] +454 -T[0] -T[3]+5;
  157. PWM[2] = T[1] +T[0] -T[2]-6;
  158. PWM[3] = T[1] +T[0] +T[2] -454 -9 ;
  159.  
  160.  
  161. for  (i=0;i<4;++i){
  162.       if (PWM[i] < 130) PWM[i] = 130;        // safe range control, 90%~110%
  163.       if (PWM[i] > 325) PWM[i] = 325;
  164.       }
  165.  
  166. //---------------------------
  167. // D   Out put 4 PWM
  168. // output 4 PWM, take 2.5mS
  169. // 1 unit = 33 cycles, 6.6 uS
  170.  
  171. output_high(pin_B0);
  172. delay_cycles(10)  ;    //delay to match output_low
  173. output_high(pin_B1);
  174. delay_cycles(10)  ;
  175. output_high(pin_B2);
  176. delay_cycles(10)  ;
  177. output_high(pin_B3);
  178.  
  179. timerL=0;
  180.  
  181. while (timerL < 330){
  182.       timerL++;
  183.  
  184.       if (timerL == PWM[0]) output_low(pin_B0);
  185.       if (timerL == PWM[1]) output_low(pin_B1);
  186.       if (timerL == PWM[2]) output_low(pin_B2);
  187.       if (timerL == PWM[3]) output_low(pin_B3);
  188.       delay_cycles(1);                //total 33 cycles
  189.       }
  190. }
  191. while (true);       //End of endless loop
  192.  
  193. }  // End of Main

Este es el autor: http://ilufa.orgfree.com/4MIXER_E.htm
http://www.clbmohinh.com/forum/tm.aspx?m=382664

Haber que me pueden compartir de este codigo... Saludos
« Última modificación: 22 de Enero de 2016, 23:07:40 por Abner_emanuel »
El señor es mi pastor,  nada me faltará.

Desconectado Chaly29

  • Moderador Global
  • DsPIC33
  • *****
  • Mensajes: 4315
Re:Quadcopter PIC 16F84 en CCS
« Respuesta #1 en: 23 de Enero de 2016, 10:39:18 »
Hola Abner_emanuel, se ruega por favor no repetir el mismo tema, esta prohibido dentro de las reglas del foro, que puedes y se agradecería las leas en este linck:

http://www.todopic.com.ar/foros/index.php?action=announcements;aid=1

Desde ya muchas gracias. Un saludo.

Atte. CARLOS.

La teoría es cuando se sabe todo y nada funciona. La práctica es cuando todo funciona y nadie sabe por qué.

Desconectado Abner_emanuel

  • PIC16
  • ***
  • Mensajes: 160
Re:Quadcopter PIC 16F84 en CCS
« Respuesta #2 en: 23 de Enero de 2016, 11:02:47 »
Enterado. Gracias.
El señor es mi pastor,  nada me faltará.


 

anything