Generation of PWM using External Interrupts and Timers on a scope...

Status
Not open for further replies.

Anoop

Newbie level 1
Joined
Dec 5, 2012
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,286
Hello,

I'm trying to generate a continuous PWM wave on a scope from the C8051F330 micro-controller using external interrupts and timers only. I have put up a piece of code. Could anybody please look at it and spot any mistake/mistakes that I've been doing which is eventually restricting the output...? The code is as follows:

Code:
//-----------------------------------------------------------------------------
// Include Files
//-----------------------------------------------------------------------------

#include <compiler_defs.h>			   	// Macro definitions for 8051 Compiler //
#include <C8051F336_defs.h>            		// SFR declarations //

//-----------------------------------------------------------------------------
// Global Constants
//-----------------------------------------------------------------------------

#define SYSCLK             								24500000/12              // Clock speed in Hz //


SBIT (SW1, SFR_P0, 7);                 	// SW1==0 means switch depressed


//-----------------------------------------------------------------------------
// Function Prototypes
//-----------------------------------------------------------------------------

void Oscillator_Init (void);           		// Configure the system clock //
void Port_Init (void);                 		// Configure the Crossbar and GPIO //
void Timer2_Init (int counts); 	           // Configure Timer2 //
void PCA_Init (void);	               	// Configure PCA for PWM mode //
void Ext_Interrupt_Init (void);                   // Configure External Interrupts (INT0) //

                                       
INTERRUPT_PROTO (INT0_ISR, INTERRUPT_INT0);


//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------
void main (void)
{
   PCA0MD &= ~0x40;                    		// Disable Watchdog timer //
   int RELOAD_VALUE = 0;
   Oscillator_Init();                  	              // Initialize the system clock //
   Port_Init ();				// Initialize crossbar and GPIO //
   Timer2_Init(SYSCLK/100);	                           // Initialize Timer2 to generate interrupts at a 20kHz rate//
   PCA_Init ();		                          // Initialize PCA for PWM //		
   Ext_Interrupt_Init();                        	// Initialize External Interrupts //
   
	 EA = 1;

   while(1)                        // Infinite while loop waiting for an interrupt //
{
		 if(TF2H == 1)
		 {
			 if (RELOAD_VALUE<65535)
					{
						TF2H = 0;
						PCA0CP0 = RELOAD_VALUE;
						RELOAD_VALUE ++;
					}
			 else
					{
						TF2H = 0;
						RELOAD_VALUE =0;
					}
			}

}
	 
}


//-----------------------------------------------------------------------------
// Oscillator_Init
//-----------------------------------------------------------------------------
// This routine initializes the system clock to use the precision internal
// oscillator as its clock source.
//-----------------------------------------------------------------------------
void Oscillator_Init (void)
{
	CLKSEL = 0x00;				// Clock Select. SYSCLK is derived from the Internal High Frequency Oscillator and scaled per IFCN bits in register OSCICN //
	OSCICN = 0x83;                      			// Internal H-F Oscillator enabled and SYSCLK derived from Internal H-F Oscillator divided by 1 //
}


//-----------------------------------------------------------------------------
// Port_Init
//-----------------------------------------------------------------------------

void Port_Init (void)
{
	
   XBR1      = 0x40;  		              // Enable crossbar and weak pull ups //                  							
   P0SKIP    = 0x7F;			// Skip unused pins on P0 and P1 //
   P1SKIP    = 0xFF; 
	  
}


//-----------------------------------------------------------------------------
// Timer2_Init
//-----------------------------------------------------------------------------
 
void Timer2_Init (int counts)   
{
	
   TMR2CN  = 0x00;                 // Stop Timer2. Clear T2SPLIT.
	                               // Use SYSCLK/12 as timebase, 16-bit auto-reload					
   CKCON  |= 0x30;                // Timer2 High and Low byte uses the SYSCLK //
   TMR2RL  = -counts;  	  // Initialize reload value 
   TMR2    = 0xFFFF;               // Set to reload immediately 
	
}


//-----------------------------------------------------------------------------
// PCA_Init
//-----------------------------------------------------------------------------

void PCA_Init(void)
{
    	
	PCA0MD    = 0x00;		// PCA Counter/Timer Idle Control enabled, Watchdog timer disabled, Watchdog timer enable unlocked, Unused, System clock divided by 12, Disable CF Interrupt // 
	PCA0CPM0  = 0x82;	// PWM 16, ECOM, TOG, PWM, ECCF bits set // 
  PCA0PWM	&= ~0x80;  
}


//-----------------------------------------------------------------------------
// Ext_Interrupt_Init
//-----------------------------------------------------------------------------

void Ext_Interrupt_Init (void)
{
	
   TCON   = 0x01;                       	// INT 0 is edge triggered //
   IT01CF = 0x07;                      	// INT0 active low; INT0 on P0.7 //
   EX0 = 1;                             	// Enable INT0 interrupts //  
	
}  


//-----------------------------------------------------------------------------
// Interrupt Service Routines
//-----------------------------------------------------------------------------

INTERRUPT(INT0_ISR, INTERRUPT_INT0)
{
   TR2  = 1;			// Start Timer2 //					
}

Many Thanks.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…