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