ROSALES_RUIZ

CONTROL DE VELOCIDAD DE MOTOR CD CON ENCODER

Oct 17th, 2015
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.45 KB | None | 0 0
  1. ESCUELA SUPERIOR DE INGENIERIA MECANICA Y ELECTRICA
  2. UNIDAD ZACATENCO
  3. ING. EN CONTROL Y AUTOMATIZACION
  4. ASIGNATURA: INTERFASES Y MICROCONTROLADORES
  5. M.C LUIS ARTURO BENITEZ VELAZQUEZ
  6. GRUPO 7AM1
  7.  
  8. AUTORES:
  9. ROSALES HERNANDEZ JOSE EDUARDO
  10. RUIZ ESTUDILLO MARIA FERNANDA
  11. *************************************************************************
  12. *************************************************************************
  13. PRACTICA NO. 4
  14. PROGRAMA: CONTROL DE VELOCIDAD DE MOTOR CD CON ENCODER
  15.  
  16. */
  17.  
  18. //PINES DE ENTRADAS Y SALIDAS
  19.  
  20. int pushbotton=8;
  21. int led_on=10;
  22. int encoder=12;
  23. int motor=11;
  24. int pot=A0;
  25. int boton_on;
  26. int i=0; //entrada de la ranura
  27. int j=0; //salida de la ranura, entre las dos variables, funcionan como un detector de cruce por cero.
  28.  
  29. int encoder_in;
  30. int contador=0;
  31. int dip;
  32. int vel;
  33. int vel2=1;
  34. int enclave;
  35.  
  36. void setup ()
  37. {
  38.   DDRD=DDRD|B00000011; //Configura el puerto B como entradas excepto 2 pines
  39.   Serial.begin(9600); //Dipswitch 0=entrada, 1=salida
  40.  
  41. //DECLARACION DE VARIABLES
  42. pinMode(pushbotton,INPUT);
  43. pinMode(pot,INPUT);
  44. pinMode(led_on,OUTPUT);
  45. pinMode (motor,OUTPUT);
  46. pinMode(encoder,INPUT);
  47.  
  48. digitalWrite(led_on,LOW);
  49.  
  50. }
  51.  
  52. void loop ()
  53. {
  54.     boton_on=digitalRead(pushbotton); //lee posicion del boton
  55.     dip=PIND& 0b11111100; //Lee el Puerto D como binario pero lo asigna a dip como decimal
  56.  
  57.     if(boton_on==HIGH) //Código de enclave
  58.       {digitalWrite(led_on,HIGH);
  59.       enclave=1;
  60.       vel2=1;
  61.       contador=0;
  62.       }
  63.  
  64.     if(enclave==1){ //Comienzo de conteo
  65.        encoder_p();
  66.     }
  67.  
  68.     if(contador>=(dip*0.7)) //Descenso de Velocidad cuando contador llegue al 70%
  69.       vel2=2;
  70.     if(contador>=dip) //Código de Des-enclave
  71.  {
  72.    digitalWrite(led_on,LOW);
  73.    analogWrite(motor,0);
  74.    enclave=0;
  75.  }  
  76. }
  77.  
  78. void encoder_p() //Esta rutina se encarga de contar sólo cuando hay un cambio de segmento
  79. {
  80. encoder_in=digitalRead(encoder);
  81. velocidad(vel2);
  82.     if(encoder_in==1)
  83.     {i=1;}
  84.     else{i=0;}
  85.     if(i==1&&j==0)
  86.     {
  87.       contador++;
  88.       Serial.println(contador);
  89.     }
  90.  
  91.     if(encoder_in==1)
  92.     {j=1;}
  93.     else{j=0;}
  94. }
  95.  
  96. void velocidad(int a) //Esta rutina usa PWM para la velocidad del motor
  97. {  
  98.  if(a==1)
  99.     {
  100.       vel=analogRead(pot);
  101.       vel=map(vel,0,1023,175,255);
  102.       analogWrite(motor,vel);
  103.     }
  104.  
  105.     else if(a==2){ //Este caso es específico para conseguir la más baja velocidad del motor
  106.       vel=175;
  107.       analogWrite(motor,vel);
  108.     }
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment