Thread: PWM

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    6

    PWM

    hi guys i have done some coding for a PWM project...the code shld be able to change the brightness of my LED bt for some reason its not. I tried asking my friends and trouble shooting it several times but i just cant seem to find the problem...

    below is the code i have used

    Code:
    #include <compiler_defs.h>             // compiler declarations
    #include <C8051F930_defs.h>            // SFR declarations
    
    //-----------------------------------------------------------------------------
    // Pin Declarations
    //-----------------------------------------------------------------------------
    
    SBIT (RED_LED,    SFR_P1, 5);          // '0' means ON, '1' means OFF
    SBIT (YELLOW_LED, SFR_P1, 6);          // '0' means ON, '1' means OFF
    SBIT (SW2,        SFR_P0, 2);          // SW2 == 0 means switch pressed
    SBIT (SW3,        SFR_P0, 3);          // SW3 == 0 means switch pressed
    
    // Globals for PWM
    
    //-----------------------------------------------------------------------------
    // Global CONSTANTS
    //-----------------------------------------------------------------------------
    
    #define SYSCLK       20000000/8       // SYSCLK frequency in Hz
    
    #define LED_ON           0
    #define LED_OFF          1
    
    //-----------------------------------------------------------------------------
    // Function PROTOTYPES
    //-----------------------------------------------------------------------------
    void PORT_Init (void);
    void Timer2_Init (int counts);
    //----------------------------------------
    
    
    bit pwmState=0;
    unsigned int pwmHighDuration , pwmLowDuration ;
    
    //-----------------------------------------------------------------------------
    // MAIN Routine
    //-----------------------------------------------------------------------------
    void main (void)
    {
       TMOD=0x01; //timer 0, mode 2 (0000,0010), 8-bit timer mode 
         
       TR0=1; //start timer  
       ET0=1; //allow timer 0 interrupt  	
       PCA0MD &= ~0x40;                    // WDTE = 0 (clear watchdog timer
                                           // enable)
       PORT_Init ();
    
            // Init Timer2 to generate interrupts
                                           // at a 10 Hz rate.
    
       EA = 1;        
                            // Enable global interrupts
        
    	
       
       while (1);
       
       pwmHighDuration= 0xFE40;
       pwmLowDuration= 0xFE40;               // Spin forever
    
    }
    //-----------------------------------------------------------------------------
    // PORT_Init
    //-----------------------------------------------------------------------------
    //
    // Return Value : None
    // Parameters   : None
    //
    // This function configures the crossbar and ports pins.
    //
    // P0.2   digital   open-drain    Switch 2
    // P0.3   digital   open-drain    Switch 3
    // P1.5   digital   push-pull     Red LED
    // P1.6   digital   push-pull     Yellow LED
    //-----------------------------------------------------------------------------
    void PORT_Init (void)
    {
       P0MDIN |= 0x0C;                     // P0.2, P0.3 are digital
       P1MDIN |= 0x60;                     // P1.5, P1.6 are digital
    
       P0MDOUT &= ~0x0C;                   // P0.2, P0.3 are open-drain
       P1MDOUT |= 0x60;                    // P1.5, P1.6 are push-pull
    
       P0     |= 0x0C;                     // Set P0.2, P0.3 latches to '1'
    
       XBR2    = 0x40;                     // Enable crossbar and enable
                                           // weak pull-ups
    }
    
    //-----------------------------------------------------------------------------
    // Timer2_Init
    //-----------------------------------------------------------------------------
    //
    // Configure Timer2 to 16-bit auto-reload and generate an interrupt at
    // interval specified by <counts> using SYSCLK/48 as its time base.
    //
    
    
    //-----------------------------------------------------------------------------
    // Interrupt Service Routines
    //-----------------------------------------------------------------------------
    
    //-----------------------------------------------------------------------------
    // Timer0_ISR
    //-----------------------------------------------------------------------------
    // This routine changes the state of the LED whenever Timer2 overflows.
    //
    INTERRUPT(Timer0_ISR, INTERRUPT_TIMER0)
    {
      
        TF0 = 0;
      
      if (pwmState==0)
         {
          RED_LED = 0;
          pwmState = 1;
          TH0 = pwmHighDuration/256;
          TL0 = pwmHighDuration%256;
         }
    
       else
          {
            RED_LED = 1;
            pwmState = 0;
            TH0 = pwmLowDuration/256;
            TL0 = pwmHighDuration%256; 
    }
    
    }
    
    
    
    //-----------------------------------------------------------------------------
    // End Of File
    the PWM frequency shld be within 200Hz tat means 5 micro secs,,,is there any mistake in the frequency..i would really like to know where i went worng thnak u

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    I see a couple of problems. One I don't see any value for CKCON, and your TMOD setting is setting the counter to
    16 bit mode (0x01). You should have a look at Silabs AN110 on how to use a timer for PWM.

    Jim.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM