electronicsman
Full Member level 5
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.
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.
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.