pic16f877 DC Motor Control

Discussion in 'Embedded Systems and Microcontrollers' started by Nanocore, May 11, 2011.

  1. Nanocore

    Thread Starter New Member

    May 8, 2011
    1
    0
    Hello guys!

    I would like to seek your expertise with regards to ADC and PWM control of a dc motor. I am using a pic16f877 and mikroC as my compiler. The code compiles successfully but does not work on my circuit. Hope you guys could give me a hand. Here are the following parameters:

    PIC16F877 -runs at 4Mhz
    RA3(AN3) -pot command
    RC2 - Motor Forwards High
    RC1 - Motor Reverse PWM (on Low)
    RE2 - Motor Reverse High
    RE1 - Motor Forwads PWM (on Low)

    Attached is my code and the schematic.. Thank you.. Godbless.. ^^

    Code ( (Unknown Language)):
    1.  
    2.  
    3. unsigned short current_duty, old_duty, current_duty1, old_duty1;
    4. int Dlay;
    5. char ADCValue;
    6.  
    7. main()
    8. {
    9.  
    10.       PORTC=0;
    11.       PORTE=0;
    12.       PORTA= 255;
    13.       TRISA= 255;
    14.       PWM1_Init(4000);
    15.       PWM2_Init(4000);
    16.      
    17.       current_duty  = 16;
    18.       current_duty1 = 16;
    19.      
    20.       PWM1_Start();
    21.       PWM2_Start();
    22.       PWM1_Set_Duty(current_duty);
    23.       PWM2_Set_Duty(current_duty1);
    24.      
    25.       ADCON0=0b01011001;
    26.      
    27.       TMR2=0;
    28.       PR2=64;
    29.  
    30.       T2CON=0b00000100;
    31.       CCPR1L=0;
    32.      
    33.       while(1 == 1)
    34.       {
    35.  
    36.              delay_ms(100);
    37.              ADCON0.B2 = 1;
    38.              while (ADCON0.B2);
    39.  
    40.               ADCValue=ADRESH;
    41.               if (ADCValue > 0x80)
    42.               {
    43.                  CCPR1L = (ADCValue - 80) >> 1;
    44.                  CCP1CON = 0b00101110;
    45.                  current_duty++;
    46.                  PWM1_Set_Duty(current_duty);
    47.                  current_duty1--;
    48.                  PWM2_Set_Duty(current_duty1);
    49.                  TRISC = 0b00000110;
    50.                  TRISE = 0b00000110;
    51.               }
    52.               else
    53.               {
    54.                   CCPR1L = (ADCValue ^ 0x7F) >> 1;
    55.                   CCP1CON = 0b00111110;
    56.                   current_duty++;
    57.                   PWM1_Set_Duty(current_duty);
    58.                   current_duty1--;
    59.                   PWM2_Set_Duty(current_duty1);
    60.                   TRISC = 0b00000110;
    61.                   TRISE = 0b00000110;
    62.               }
    63.       }
    64. }
     
Loading...