+ Reply to Thread
Results 1 to 1 of 1
  1. #1
    Join Date
    Mar 2012
    Posts
    2

    Unhappy Input Capture CAP1 using PIC 18F help !

    Hello people, this my first post . I am trying to understand the Input Capture. Please have a look and understand that I am a newbie and it's difficult to me.
    I use 18f4331 PIC and a 10MHz crystal.
    You see, I 've got this problem. I used CAP1 of the 18F Microchip's PIC to toggle a led connected to RC0. I also use another led connected to RC0. I've connected the RA2 pin to RC3. When the RC3 changes state, the CAP1 detects this and the Leds toggle simultaneously. I've done this for a first indication. My problem is this ;
    I want to change the PWM state in PWM 1 and PWM 3 but it seems my code is wrong because it doesn't understand that. It just preserves the initial state of my bootup function 50% PWM.What do I forget to activate so as to change the PWM state? I use the ic_flag for the interrupt, and when it is set, the main executes the code under the ic_flag.

    I've attached my code, please have a look and let's talk about it, so as to have a brainstorming. I guess someone might have done something similar. I use Power control mode to send the PWM.


    Hi, we are pleased to see that you are using our forum.

    We'd like you to become a member of our community. Membership is FREE, please REGISTER and view our forums without these annoying reminders!

    Once registered you may post on the forums, download from the file areas and use the WIKI without interruption.






    I've tried to discuss this with other people too, at other forums, and I hope I find some advice here. thank you !

    Code:
    
    
    Code:
     #include <htc.h>
    #include <pic18f4331.h>
    #define    _XTAL_FREQ    10000000
    unsigned int counter=0;
    unsigned int counter2=0;
    unsigned int counter3=0;
    unsigned char ic_flag;
    unsigned char flag_5;
    unsigned char flag_4;
    unsigned int vel_max=1036; // max timi tis taxytitas se rad/sec antistoixei se 10000 rpm
    unsigned int vel_ref= 837; // se rad/sec  estw 8000 rpm
    // input control signal to the pwm driver
    unsigned int pwm_command = 1023 ;        // Duty Cycle command
    unsigned int pwm_command1=1023;
    // Friction compensation
    unsigned int friction_compensation = 0;
    
    
    void boot_up_pic(void);
    void interrupt my_isr(void);
    void set_pwm(void);
    
    
    void main() {
    boot_up_pic();
    ic_flag=0;
    flag_5=0;
    flag_4=0;
      while (1)
      {
       // ******************* If optocoupler interrupt is triggered *****************************
    // ******************************* Timer 5 is used ***************************************
       if(ic_flag==1) {
         // when input capture interrupt happens (when it detects the RC3 on or off) indicate with the RC0.
         //RC0 and RC3 toggle simultaneously
        if (RC0==0)
         RC0=1;
         else
         RC0=0;
    
    
        // Addittionaly when you get an inpute capture interrupt, change the pwm
         if (pwm_command==520){
        OVDCOND=0b00001010; //  PWM1 & PWM3 controlled by OVDCOND  , PAGE 197 Datasheet!
        OVDCONS=0b00000000;// deactivate PWM0 and PWM2
        pwm_command=800;
        PDC0L=((++pwm_command)<<2)&(0xFC);
        PDC0H=((++pwm_command)>>6);
        PDC1L=(pwm_command1<<2)&0xFC;
        PDC1H = pwm_command1>>6;
        }
        else if(pwm_command==800){
        OVDCOND=0b00001010;
        OVDCONS=0b00000000;
        pwm_command=520;
        PDC0L=((++pwm_command)<<2)&(0xFC);
        PDC0H=((++pwm_command)>>6);
        PDC1L=(pwm_command1<<2)&0xFC;
        PDC1H = pwm_command1>>6;
        }
         ic_flag=0;
             } // end if
    } // end while
    }//end main
    void boot_up_pic(){
    // set PORTS
    PORTA = 0b00001000;
    TRISA = 0b00011100;  // RA3 for Input Capture CAP1
    ANSEL0= 0b00000000; // RA4 IS ANALOG INPUT ,REST DIGITAL
    
    
    TRISB=0b00000000;
    PORTB=0b00000000;
    
    
    PORTC=0b00000000;
    TRISC=0b11110110;  // RC3 and RC0 are outputs
    
    
    PORTD= 0b00010000;
    TRISD= 0b00000100;
    
    
    // TIMER 5 MODULE !
    T5CON=0b00010101;  //prescaler = 1:4
    PR5H=0x00;
    PR5L=0xFF;  // 255 overflow
    // Tper= 400ns -> Tper x PR5 x prescaler = 400ns x 255 x 4 = 408 usec
    
    
    INTCON=0b11000010; // ensure global and peripheral interrupts enabled.
    IPR3=0b00010011; //
    PIE3=0b00000011;  //
    PIR3=0b00000011;
    
    
    // PWM MODULE !
    /* PTCON0 module
    * 1:1 prescaling
    * 1:1 postscale
    * Free Running Mode
    */
    PTCON0 = 0b00000000;  // prescale 1:64  period Duty Cycle = 6.5 msec
    
    
    /* PTCON1 module
    *  PTEN =1 (PWM time base in on)
    *  PTDIR =0 (PWM time base counts up)
    *  the rest unimplemented read as 0
    */
    PTCON1=0b10000000;
    
    
    //  PWMCON0 module
    //    bit 7 = unimplemented,
    //    100 PWM0, PWM1, PWM2, PWM3,PWM4, PWM5 pins are enabled for PWM output
    //    h 010 PWM0 PWM1 enabled
    //    PMOD <3:0> 1111 pair (PWM0, PWM1)se independent mode  */
    PWMCON0=0b01011111;
    // Independent mode
    
    
    // PWMCON1 module - ayta vazei kai o panagiwtis
    //    bit 7-4 1:1 postscale 0000
    //    bit 3 =0 special event trigger when counting upwards
    //    bit2 unimplemented
    //    bit 1= 0 updates from Duty Cycle and Period Buffer registers are enebled
    //    bit 0= 0 outputs overrides via the OVDCON register are asynchronous
    PWMCON1=0b00000000;
    
    
    OVDCOND=0b00001010; //  PWM1 & PWM3 controlled by OVDCOND  DES PAGE 197 Datasheet!
    OVDCONS=0b00000000; //
    
    
    FLTCONFIG = 0b00000000; // Fault configuration register
    
    
    PTPERL=0xFF;
    PTPERH=0x03; // time base period ragister high byte for 1023 pwm
    
    
    pwm_command=520; // Initially pwm 50%
    PDC0L=((++pwm_command)<<2)&(0xFC); //PDC takes the value of PWM
    PDC0H=((++pwm_command)>>6);
    pwm_command1=520;
    PDC1L=(pwm_command1<<2)&0xFC;
    PDC1H = pwm_command1>>6;
    /* Input Capture MODULE tou Motion Feedback Module*/
    CAP1CON=0b01001000; // capture at any state (on or off) of the RC3 you detect
    ei();
    }
    /**********************INTERRUPTS*********************************/
    void interrupt my_isr(void){
    /****************** LED interrupt ***********************/
    // toggle RC3 every 4 (almost) seconds . 4 secs ON and 4 secs OFF
    if(TMR5IE==1 && TMR5IF==1) {
    counter3++;
    if  (counter3==10000){ //last for 4 seconds
        flag_4=1;
    if (RC3==0)
         RC3=1; //ON for 4 seconds
    else
        RC3=0; //OFF for 4 seconds
    counter3=0;
    }
    TMR5IF=0;
    }
    
    
    /******************** CAP - IC1  interrupt ****************************************/
        if(IC1IE && IC1IF){   //interrupt every changing state of the led RC3 set the ic_flag
              ic_flag=1;
              IC1IF=0;
           }
    }
    Last edited by aredhel; - 28th March 2012 at 17:23.

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts