使用PWM技术,pic18和mikroC编译器控制直流电机

时间:2019-05-19 18:31:01

标签: pwm mikroc

我需要使用单片机PIC18F25k22控制直流电动机。 我在MIKROC编译器中有一部分代码,但是我不知道该如何使所有这些工作正常运行,我需要改进它。 我将为该项目提供蛋白质变形模拟的图片。 电动机的速度由电位计控制。将电位计调至0级,应使电动机停止运转,并且当按下“ DIRECTIE”按钮时,我希望电动机顺时针旋转,但是释放时,电动机必须逆时针旋转。 松开“ START-STOP”按钮时,电动机必须开始旋转,按下按钮时,电动机必须停止。

提前谢谢!

https://imgur.com/a/ktTkvxI -------变形蛋白模拟

unsigned int adc_rd;
 long current_duty;

void main() {
 ANSELC = 0;  // Configure PORTC as digital I/O;CU ANSEL SETAM PT FIECARE PIN/port DACA PORTUL E ANALOGIC SAU DIGITAL, DACA E 0 E DIGITAL;DACA E 1 E ANALOPGIC
 ANSELB = 0;  // Configure PORTB as digital I/O;CU ANSEL SETAM PT FIECARE PIN/port DACA PORTUL E ANALOGIC SAU DIGITAL, DACA E 0 E DIGITAL;DACA E 1 E ANALOPGIC

  TRISA0_bit = 1;    //Configure AN0 (RA0) as input; CU TRIS CONFIGURAM DIRECTIA PINILOR, DACA SUNT INTRARI SAU IESIRI, DACA E 1 E INTRARE...
  TRISC = 0;         //PORTC pins are outputs      CU TRIS CONFIGURAM DIRECTIA PINILOR, DACA SUNT INTRARI SAU IESIRI, DACA E 0 E IESIRE...
  TRISB = 0;         //PORTB pins are outputs      CU TRIS CONFIGURAM DIRECTIA PINILOR, DACA SUNT INTRARI SAU IESIRI, DACA E 0 E IESIRE...
  TRISC3_bit = 1;  //Configuram bitul RC3 ca intrare digitala(butonul1)
  TRISC4_bit = 1;   //Configuram bitul RC4 ca intrare digitala(butonul2)


  PWM1_Init(1000);      // Initialize PWM1 module at 1KHz
  PWM1_Start();        // start PWM1
  ADC_Init();         // Initialize ADC

  LATB =  0x02;      //Turn the motor Clockwise

  while (1) {         // endless loop

  adc_rd = ADC_Read(0);     //Read Analog value from Analog channel 0

  //convert voltage level to duty cycle 0-->255
  current_duty = (long) adc_rd * 255;
  current_duty =  current_duty/1023;

  PWM1_Set_Duty(current_duty);  //change duty cycle

  Delay_ms(5);                  // slow down change pace a little
  }
}

0 个答案:

没有答案