Help with Coding dsPIC30F4011 PWM Module

Status
Not open for further replies.

jrmazur

New Member
Hey guys,
I am trying to use the dsPIC30F4011's PWM Module to control the brightness of of 2 different LED's. I have the ADC taking in and converting in a sample voltage from a PoT and storing the 2 different values into 2 different registers. (value it takes in is from 0 to 1024, and the 2nd LED gets the remaining value. Like LED1 = 724/1024, and LED2 = 300/1024).

I am still pretty new to using these chips and I've read through the reference manuals a few times now and i'm still confused on how to do this. I know the Duty Cycle is what will control the Brightness of the LED but i guess im not sure how to use the numbers i get to change the Duty cycle.

What my code does is just lights up the 2 LED's at same brightness and they don't change at all when I turn the PoT.

Here is the Code i have got:

/* Import dsPIC30F4011 */
#include "p30f4011.h"

/* Configure Oscillator to use External Clock */
_FOSC(EC);


/* Function Prototypes */
void InitializeADCModule();
void InitializePWMModule();

/* Global Variables*/
unsigned LED1=0;
unsigned LED2=0;
unsigned LED1delay=0;
unsigned LED2delay=0;


int main(void)
{
InitializeADCModule(); /*Call ADC Initialization Module*/
InitializePWMModule(); //Call PWM Initializaton Module

while(1);
return 0;
}

/* Configure the ADC module to sample
* the 2k POT in 10 bit mode once every
* X milliseconds in decimal form. Will
* give a value 0-1024
*/
void InitializeADCModule(void)
{
/*ADCON1 Register*/
ADCON1bits.FORM = 0; //Data output Format set to Unsigned Integer
ADCON1bits.SSRC = 7; //Set for Auto-Convert
ADCON1bits.SIMSAM =0; //Sample Sequentially
ADCON1bits.ASAM = 1; //Set for Auto-Sampling
ADCON1bits.ADSIDL = 0; //Continue module operation in Idle Mode

/*ADCON2 Register*/
ADCON2bits.VCFG = 0; //+VREF Connected to AVdd, -VREF Connected to AVss
ADCON2bits.CSCNA = 0; //Don't Scan Inputs, Only 1 input
ADCON2bits.CHPS = 0; //Converts channel CH0
ADCON2bits.SMPI = 0; //Generate an interrupt after every Sample/Convert
ADCON2bits.ALTS = 0; //Always use MUX A input Multiplexer settings

/*ADCON3 Register*/
ADCON3bits.SAMC = 1; //Auto-Sample uses 1 TAD
ADCON3bits.ADRC = 0; //A/D conversion clock derived from system clock
ADCON3bits.ADCS = 2; //Uses just TCY, Conversion clock period TAD = 133nsec

/*ADCHS*/
ADCHS =0x0005; //Channel 0 positive input is AN5

/*ADCSSL Register*/
ADCSSL = 0x0000; //Skip ANx for input scan

/*ADPCFG Register*/
ADPCFG = 0xFFFF; //Set all Analog input pins to Digital Mode
ADPCFGbits.PCFG5 = 0; //Set AN5 to Analog Input, A/D samples Pin Voltage
IFS0bits.ADIF = 0; //Clear ADC interrupt flag
IPC2bits.ADIP = 6; //Set Priority of ADC Interrupt to 6(greater than Timer Interuppt)
IEC0bits.ADIE = 1; //Enable ADC Interrupt
ADCON1bits.ADON = 1; //Turn on ADC Module
return;
}


// Read ADC values here
void __attribute__((__interrupt__, __no_auto_psv__)) _ADCInterrupt (void)
{
LED1 = ADCBUF0; //Grab input Sample Voltage from Buffer 0
LED2 = 1024-LED1; //Calculate second Sample voltage for 2nd LED
LED1delay =LED1*64; //Scale up the sample voltage # to match 16-bit Timer #
LED2delay =LED2*64; //Scale up the sample voltage # to match 16-bit Timer #
PDC1 = LED1delay; //Set brightness of LED1 using Duty Cycle 1
PDC2 = LED2delay; //Set brightness of LED2 using Duty Cycle 2
IFS0bits.ADIF = 0; /*Clear the Interrupt Flag*/
}


//Initialize the PWM Module
void InitializePWMModule()
{
PTCONbits.PTEN = 0; //Turn off PWM Module
PTPER = 1332; //PWM period is 50µS (20kHz)
PTMR = 0; //Time Base Register, cleared
SEVTCMP = PTPER; // Generate special Event for ADC conversion Start
PWMCON1 = 0x0777; //Enable PWM outputs in independent mode
PTCONbits.PTCKPS = 3; //PWM Time Base Input Clock prescale set to 1:64
PTCONbits.PTMOD = 0; //PWM runs in Free Running Mode
PWMCON1bits.PEN1L = 1; //PWM1L pin is enabled for PWM output
PWMCON1bits.PEN2L = 1; //PWM2L pin is enabled for PWM output
PTCONbits.PTOPS = 0; // Output Clock Postscale bits: 1:1
PTCONbits.PTSIDL = 0; // Stop in Idle Mode: NO

//PWMCON2 Register
PWMCON2bits.UDIS = 0; //Duty Cycle, period register updates enabled
PWMCON2bits.OSYNC = 0; //Output override sync'ed to TCY boundry
PWMCON2bits.IUE = 0; //Active PDC register updates sync'ed to PWM
PWMCON2bits.SEVOPS = 0; //Special event trigger output post scale is 1:1

OVDCON = 0x0000; //Outputs controlled by POUTxx and set inactive
PDC1 = 0; //Duty Cycle register 1
PDC2 = 0; //Duty Cycle register 2
PDC3 = 0; //Duty Cycle register 3
IFS2bits.PWMIF = 0; //clear interrupt flag
IEC2bits.PWMIE = 0; //Turn on PWM interrupts
PTCONbits.PTEN = 1; //Turn on PWM Module

return;
}


Any and All Help would be appreciated!
Thanks!
 
Status
Not open for further replies.