PWM sur un Pic 16f886 - Besoin d'une autre paire d'yeux...
Répondre à la discussion
Affichage des résultats 1 à 5 sur 5

PWM sur un Pic 16f886 - Besoin d'une autre paire d'yeux...



  1. #1
    invite08c749d2

    Question PWM sur un Pic 16f886 - Besoin d'une autre paire d'yeux...


    ------

    La belle datasheet nous donne les indications suivantes.

    11.5.7 SETUP FOR PWM OPERATION
    The following steps should be taken when configuring
    the CCP module for PWM operation:
    1. Disable the PWM pin (CCPx) output drivers as
    an input by setting the associated TRIS bit.

    Donc je fait
    Code:
    TRISC |= 0b00000110 ;
    2. Set the PWM period by loading the PR2 register.

    Code:
    PR2= 255;
    3. Configure the CCP module for the PWM mode
    by loading the CCPxCON register with the
    appropriate values.

    Code:
    CCP1CON |= 0b00001111 ;
    CCP2CON |= 0b00001111 ;
    4. Set the PWM duty cycle by loading the CCPRxL
    register and DCxB<1:0> bits of the CCPxCON
    register.

    Code:
    CCPR1L=Valeur;	  // mise en place des 8 bits de poids fort, CCPR1L	
    CCPR2L=Valeur;
    5. Configure and start Timer2:
    • Clear the TMR2IF interrupt flag bit of the
    PIR1 register.
    • Set the Timer2 prescale value by loading the
    T2CKPS bits of the T2CON register.
    • Enable Timer2 by setting the TMR2ON bit of
    the T2CON register.


    Code:
    TMR2IF = 0;
    T2CKPS1=0;  // Prediviseur <5
    T2CKPS0=1;  // Prediviseur =4
    
    ...
    
    
    TMR2ON=1;
    6. Enable PWM output after a new PWM cycle has
    started:
    • Wait until Timer2 overflows (TMR2IF bit of
    the PIR1 register is set).
    • Enable the CCPx pin output driver by clearing
    the associated TRIS bit.

    Code:
    while(TMR2IF==0) ;
    TRISC&=0b11111001 ;

    Au final le code C'est :
    Code:
    #include <htc.h>
    
    void Init_PWM();
    void Rapport_cyclique_PWM(int);
    
    
    int main () 
    	{
    		Init_PWM();
    		TRISB=0;
    		PORTB=0b11111111;
    		CCPR1L=0b1111111;
    		while(1);
    		
    
    	}
    
    void Rapport_cyclique_PWM(int Valeur)
               	{	   
               	CCPR1L=Valeur;	  // mise en place des 8 bits de poids fort, CCPR1L	
               	CCPR2L=Valeur;
    			}
     
    void Init_PWM()
           		{
    			TRISC |= 0b00000110 ;
    			
               	
    			
               		{
               			PR2= 255;   // Periode Maximale
    					TMR2IF = 0;
               			T2CKPS1=0;  // Prediviseur <5
    					T2CKPS0=1;  // Prediviseur =4
               		}
    			CCP1CON |= 0b00001111 ;
    			CCP2CON |= 0b00001111 ;
    			Rapport_cyclique_PWM(1);	// configuration rapport cyclique
               	TMR2ON=1;					// activation du timer 2
    			while(TMR2IF==0) ;
    			TRISC&=0b11111001 ;
               	}
    J'utilise le Hi-Tech C Compiler en version Lite dans MPLAB, je programme mon pic 16f886 avec un Pickit3 ... fréquence de l'horloge 20Mhz.

    Je met une résistance de quelques 200 ohm et une led et entre la pin RC1 et la masse et rien ne s'allume.

    Donc, la je sais plus quoi faire... quelqu'un voit la connerie que je fais ?

    -----

  2. #2
    invite03481543

    Re : PWM sur un Pic 16f886 - Besoin d'une autre paire d'yeux...

    Bonjour,
    C'est un drain ouvert à priori donc la led entre la sortie et le +Vcc.

  3. #3
    invite08c749d2

    Re : PWM sur un Pic 16f886 - Besoin d'une autre paire d'yeux...

    C'est pas une sortie à drain ouvert, c'est une sortie CMOS classique

    RC0/T1OSO/T1CKI RC0 ST CMOS General purpose I/O.
    T1OSO — CMOS Timer1 oscillator output.
    T1CKI ST — Timer1 clock input.
    RC1/T1OSI/CCP2 RC1 ST CMOS General purpose I/O.
    T1OSI ST — Timer1 oscillator input.
    CCP2 ST CMOS Capture/Compare/PWM2.
    RC2/P1A/CCP1 RC2 ST CMOS General purpose I/O.
    P1A — CMOS PWM output.
    CCP1 ST CMOS Capture/Compare/PWM1.
    RC3/SCK/SCL RC3 ST CMOS General purpose I/O.
    SCK ST CMOS SPI clock.
    SCL ST OD I2C™ clock.
    Du coup, ca ne change rien.

  4. #4
    invitecb49b6c0

    Re : PWM sur un Pic 16f886 - Besoin d'une autre paire d'yeux...

    Salut,

    comme ca en regardant vite fait je vois pas...
    voici une partie de mon code pour gérer les PWMs.

    Code:
    /******************************************************************************/
    /*              GENERATION DES PWM et CONFIGURATION RATIO                     */
    /******************************************************************************/
    
    void VitesseMoteurs(char VMA, char VMB)
    {
        PR2     = 0b11111001;    // Periode : Frequence 20kHz
        CCPR1L  = VMB;           // Rapport cyclique pour RC2
        CCPR2L  = VMA;           // Rapport cyclique pour RC1
        T2CON   = 0b00000100;    // TIMER2 ON , Prescaler = 1
        CCP1CON = 0b00001100;    // PWM Mode pour RC2
        CCP2CON = 0b00001100;    // PWM Mode pour RC1
    }

    A+
    Laurent

  5. A voir en vidéo sur Futura
  6. #5
    invite08c749d2

    Re : PWM sur un Pic 16f886 - Besoin d'une autre paire d'yeux...

    J'ai réordonné le code et ca fonctionne...
    cela reste un mystere pour moi.
    Merci à vous deux ^^
    Pour ceux qui chercherai ....

    #include <htc.h>

    void Init_PWM();
    void Rapport_cyclique_PWM(int);


    int main ()
    {
    Init_PWM();
    TRISB=0;
    PORTB=0b11111111;
    while(1)
    {
    TMR2IF=0;
    while(TMR2IF==0);
    Rapport_cyclique_PWM(120);
    TMR2IF=0;
    while(TMR2IF==0);
    Rapport_cyclique_PWM(0);
    };


    }

    void Rapport_cyclique_PWM(int Valeur)
    {
    CCPR1L=Valeur; // mise en place des 8 bits de poids fort, CCPR1L
    CCPR2L=Valeur;
    }

    void Init_PWM()
    {
    TRISC |= 0b00000110 ; // Disable the PWM pin (CCPx) output drivers as an input by setting the associated TRIS bit.
    PR2= 255; // Set the PWM period by loading the PR2 register.
    CCP1CON |= 0b00001111 ; // Configure the CCP module for the PWM mode
    CCP2CON |= 0b00001111 ; // by loading the CCPxCON register with the appropriate values.
    Rapport_cyclique_PWM(0); // Set the PWM duty cycle by loading the CCPRxL registe
    //Configure and start Timer2:
    TMR2IF = 0; // Clear the TMR2IF interrupt flag bit of the PIR1 register.
    T2CKPS1=0; // Set the Timer2 prescale value by loading the T2CKPS bits of the T2CON register.
    T2CKPS0=1; // Prediviseur =4
    TMR2ON=1; // Enable Timer2 by setting the TMR2ON bit of the T2CON register.
    // Enable PWM output after a new PWM cycle has started:
    while(TMR2IF==0) ; // Wait until Timer2 overflows (TMR2IF bit of the PIR1 register is set).
    TRISC&=0b11111001 ; // Enable the CCPx pin output driver by clearing the associated TRIS bit.
    }

Discussions similaires

  1. Aide pour LCD graphique + PIC 16F886
    Par invite1817c4c0 dans le forum Électronique
    Réponses: 7
    Dernier message: 27/01/2010, 20h52
  2. [PIC 16f886] Un simple code ne marche pas
    Par blinkseb dans le forum Électronique
    Réponses: 2
    Dernier message: 01/02/2009, 14h34
  3. Besoin de 3 PWM avec un PIC
    Par HULK28 dans le forum Électronique
    Réponses: 24
    Dernier message: 25/07/2007, 11h35
  4. pwm sur pic
    Par noisyboxes dans le forum Électronique
    Réponses: 3
    Dernier message: 28/04/2007, 19h07
  5. Besoin urgent d'une paire de ciseaux virtuels pour couper des vidéos mpeg2
    Par invitee8de67aa dans le forum Logiciel - Software - Open Source
    Réponses: 6
    Dernier message: 28/12/2005, 18h17
Découvrez nos comparatifs produits sur l'informatique et les technologies.