PIC PWM length capture

Discussion in 'Embedded Systems and Microcontrollers' started by Shreder001, Aug 23, 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;
    PORTA = 0x00;
    CMCON0 = 0b00000111;
    ANSEL = 0x00;
    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
    {
    ;
    }
    TMR1H = PORTC;

    }
    }


    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? Thanks!!
     
  2. rjenkins

    AAC Fanatic!

    Nov 6, 2005
    1,015
    69
    I don't know about the rest of it (not enough coffee yet to think properly), but you have the output line backwards - it is copying the port to the counter rather than the counter to the port.

    TMR1H = PORTC;
    should be
    PORTC = TMR1H;

    Note with some PICs you may have to read the low half of the counter before you read the high half - The high read is from a latch, saved by the low read so the counter changing between the two reads does no t mess up the data.
     
  3. Shreder001

    Thread Starter Member

    Mar 7, 2009
    22
    0
    I switched them, still nothing. I forgot to mention some of the program I included is for future sections of it (PWM output to motor).
     
  4. rjenkins

    AAC Fanatic!

    Nov 6, 2005
    1,015
    69
    Did you add a dummy read from TMR1L before reading TMR1H?
     
  5. Shreder001

    Thread Starter Member

    Mar 7, 2009
    22
    0
    What is a dummy read? (I still have alot to learn :D)
     
  6. rjenkins

    AAC Fanatic!

    Nov 6, 2005
    1,015
    69
    It just means you are reading it as that's what the chip needs, rather than because you need the result.

    If you were using a variable to store the counter high byte, you could have
    char countval;
    ...
    countval = TMR1L;
    countval = TMR1H;

    The first one is the dummy, the data is overwritten by the second one, but it does the operation the chip needs, storing the counter high value in the latch.

    You don't mention exactly which PIC you are using, so I cannot be sure of the specific requirements for your device.
     
  7. Shreder001

    Thread Starter Member

    Mar 7, 2009
    22
    0
    I'm using the PIC16F684.
     
    Last edited: Aug 25, 2009
  8. rjenkins

    AAC Fanatic!

    Nov 6, 2005
    1,015
    69
    OK, that chip appears to have only a simple timer without the latching system.
    A simple read should work..

    I don't have time to work through it just now, I will try to get back later.
     
  9. Shreder001

    Thread Starter Member

    Mar 7, 2009
    22
    0
    Take your time :).
     
Loading...