PIC motor control code

Discussion in 'Programmer's Corner' started by Shreder001, Sep 1, 2009.

  1. Shreder001

    Thread Starter Member

    Mar 7, 2009
    22
    0
    I am currently planning to be able to control a motor from a PWM input. Since I am very new to C programing, I am going in steps at a time and using LED's to test. Here is what I have so far:

    #include <pic.h>
    __CONFIG(FCMDIS & IESODIS & INTIO & MCLRDIS & WDTDIS & UNPROTECT);
    //Initialize
    unsigned char TIMER_COUNT_LOW; // Holding register for TMR0 low byte
    unsigned char TIMER_COUNT_HIGH; // Holding register for TMR0 high byte

    main()
    {
    OSCCON = 0b00000001; // Internal oscillator for system clock
    PORTC = 0x00;
    CMCON0 = 0b00000111;
    ANSEL = 0x00;
    TRISC = 0b00111100;
    PR2 = 0b11111111; //Set the PWM period by loading the PR2 register
    CCPR1L = 0x00;
    T2CON = 0b01011000;
    TMR2IF = 0; //clear bit for timer2 module interupt flag

    while(TMR2IF == 0) //Wait until timer2 overflows then continue
    {
    ;
    }
    TRISC = 0x00;
    ECCPASE = 1; // for future part of program
    PORTA = 0x00;
    CMCON0 = 0b00000111; // comperators off
    ANSEL = 0x00; //digital
    TRISA = 0b00101000;
    T1CON = 0b00010101;
    while(1==1) // Start MainLoop
    {
    while(RA5 == 1) //Wait until RA5 goes low then continue
    {
    ;
    }
    while(RA5 == 0) //Wait until RA5 goes high then continue
    {
    ;
    }
    TMR1H = 0x00;
    TMR1L = 0x00;
    while(RA5 == 1) //Wait until RA5 goes low again
    {
    ;
    }
    PORTC = TMR1L;

    }
    }

    It starts timer1, then waits untill a low signal on RA5 (PWM input), then a high one, then it initializes timer1 and wait for a low signal. imediatly after that, it puts the timer1 value into a variable and the displays it on PORTC (LED array). I compiled and programmed it succesfully, but it does not seem to be displaying the value, any thoughts? BTW this was in the embedded systems forum but I thought it would be better suited here Thanks!!
    [​IMG]
     
  2. elal

    New Member

    Dec 19, 2008
    6
    0
    Your code is hmmmm..... a bit messy , what Pic and what compiler are you using?
    If you tell me what you are using i may be able to give you more specific help.
    Try to use interrupts , read the pic datasheet about PWM and interrupts.
    Read your compiler manual about interrupts.
     
  3. nanovate

    Distinguished Member

    May 7, 2007
    665
    1
    Code ( (Unknown Language)):
    1. #include <pic.h>
    2.  
    3. __CONFIG(FCMDIS & IESODIS & INTIO & MCLRDIS & WDTDIS & UNPROTECT);
    4.  
    5. //Initialize
    6. unsigned char TIMER_COUNT_LOW; // Holding register for TMR0 low byte
    7. unsigned char TIMER_COUNT_HIGH; // Holding register for TMR0 high byte
    8.  
    9. main()
    10. {
    11.     OSCCON = 0b00000001; // Internal oscillator for system clock
    12.     PORTC = 0x00;
    13.     CMCON0 = 0b00000111;
    14.     ANSEL = 0x00;
    15.     TRISC = 0b00111100;
    16.     PR2 = 0b11111111; //Set the PWM period by loading the PR2 register
    17.     CCPR1L = 0x00;
    18.     T2CON = 0b01011000;
    19.     TMR2IF = 0; //clear bit for timer2 module interupt flag
    20.  
    21.     while(TMR2IF == 0) //Wait until timer2 overflows then continue
    22.     {
    23.         ;
    24.     }  
    25.     TRISC = 0x00;
    26.     ECCPASE = 1; // for future part of program
    27.     PORTA = 0x00;
    28.     CMCON0 = 0b00000111; // comperators off
    29.     ANSEL = 0x00; //digital
    30.     TRISA = 0b00101000;
    31.     T1CON = 0b00010101;
    32.    
    33.     while(1==1) // Start MainLoop
    34.     {
    35.         while(RA5 == 1) //Wait until RA5 goes low then continue
    36.         {
    37.             ;
    38.         }      
    39.         while(RA5 == 0) //Wait until RA5 goes high then continue
    40.         {
    41.             ;
    42.         }      
    43.         TMR1H = 0x00;
    44.         TMR1L = 0x00;
    45.        
    46.         while(RA5 == 1) //Wait until RA5 goes low again
    47.         {
    48.             ;
    49.         }      
    50.         PORTC = TMR1L;
    51.  
    52.     }
    53. }
     
Loading...