Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Clarification on the following page pwm

Status
Not open for further replies.

electronicsman

Full Member level 5
Joined
May 4, 2012
Messages
291
Helped
11
Reputation
22
Reaction score
12
Trophy points
1,298
Activity points
3,737
i was referring to the following web page http://www.ocfreaks.com/sine-wave-generator-using-pwm-lpc2148-microcontroller-tutorial/ and bit confused with the source code.
Code:
/*
(C) Umang Gajera | Power_user_EX - www.ocfreaks.com 2011-13.
More Embedded tutorials @ www.ocfreaks.com/cat/embedded

Source for LPC2148 Sine PWM Tutorial
License : GPL.
*/
#include <lpc214x.h>
#include 
#define PLOCK 0x00000400
#define PWMPRESCALE 30 //30 PCLK cycles to increment TC by 1 i.e 0.5 Micro-second 
#define ANGLE_STEP_RATE 1.8 //i.e the Angle Resolution
#define PI 3.14159

void initPWM(void);

void initClocks(void);
void setupPLL0(void);
void feedSeq(void);
void connectPLL0(void);
void computeSineLookupTable(void);
void PWM_ISR(void) __irq;

char sineLookupTable[200];
int count=0;

int main(void)
{
	initClocks(); //Initialize CPU and Peripheral Clocks @ 60Mhz
	
	computeSineLookupTable(); //Compute Sine Lookup Table with 200 Divisions
	
	VICIntEnable |= (1<<8) ; 
	VICVectCntl0 = (1<<5) | 8 ;
	VICVectAddr0 = (unsigned) PWM_ISR;
	
	initPWM(); //Initialize PWM

	//Sinusoidal PWM Generation is Active Now!
   
	while(1){}
	
	//return 0; //normally this wont execute ever
}

void computeSineLookupTable(void)
{
	float angle;
	int i;
	for(i=0; i<200; i++)
	{
		angle = ( ((float)i) * ANGLE_STEP_RATE );
		sineLookupTable[i] = rint(100 + 99* sin( angle * (PI/180) )); //converting angle to radians
	}
}

void PWM_ISR(void) __irq
{
	long regVal = PWMIR;									
	
	PWMMR1 = sineLookupTable[count]; //current amplitude
	PWMLER = (1<<1); // set Latch Bit to High 
	count++;
	if(count>199) count = 0;
	
	PWMIR = regVal; // Clear Flags
	VICVectAddr = 0xFF; //End of ISR
}

void initPWM(void)
{
	/*Assuming that PLL0 has been setup with CCLK = 60Mhz and PCLK also = 60Mhz.*/

	PINSEL0 = (1<<1); // Select PWM1 output for Pin0.0
	PWMPCR = 0x0; //Select Single Edge PWM - by default its single Edged so this line can be removed
	PWMPR = PWMPRESCALE-1; // 0.5 micro-second resolution
	PWMMR0 = 200; // 200 ticks i.e 200x0.5=100ms period duration
	PWMMR1 = 100; // Initial pulse duration i.e width
	PWMMCR = (1<<1) | (1<<0); // Reset PWMTC & Interrupt on PWMMR0 match
	PWMLER = (1<<1) | (1<<0); // update MR0 and MR1
	PWMPCR = (1<<9); // enable PWM output
	PWMTCR = (1<<1) ; //Reset PWM TC & PR

	//Now , the final moment - enable everything
	PWMTCR = (1<<0) | (1<<3); // enable counters and PWM Mode

	//PWM Generation goes active now!!
	//Now you can get the PWM output at Pin P0.0!
}

specifically regarding the following register PWMPR = PWMPRESCALE-1; // 0.5 micro-second resolution. Do we have any similar thing in Microchip dspic30f4011 family to control the resolution. I am doing only the following settings
Fcy = 20Mhz, required pwm frequency = 1kHz then PTPER = (20Mhz/1khz) - 1 = 19999. Which register should i change to get the resolution as suggested in the web page? In the code i am writing for micro chip what is the resolution i am getting? Please advise.
 

dsPIC has only discrete prescaler values (1,4 16, 64), it's not possible to get the intended resolution and period combination with 20 MHz Fcy. Review Section 15. Motor Control PWM of the dsPIC30 family reference manual for details.
 

I am really sorry I am completely off on this. Let me explain my calculations Let us say the prescaler is 1:1 and my Fcy is 20mhz or 20000000. Let us say i want the pwm frequency of 1khz. Hence i use the formula and calculate the ptper as 20000000/1000 = 20000 as of now i will exclude the subtraction of 1. Hence for 20000 my duty is 100% hence for a count of 1 is it (1/20000)*100% duty that is 0.005% duty? This corresponds to the resolution time of Total Period * duty = (1/1000)*(0.005/100) = 0.00000005 seconds. Really confusing. Am I correct on this? Please help.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top