Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

DSPIC30F4011 PWM Control

Status
Not open for further replies.
Okay, so after a lot of pouring over the internet, I've figured out how to work the Configuration Bits in the high tech c compiler. It's now in my code below - except that I have not been able to figure out how take off code protection. Unfortunately, even in simulation, when I try to write anything to PORTB, nothing happens. I'm not going to move back to PWM until I can turn an LED on. Does anyone have any ideas why RB0 - and later, RB2 - refuse to go high?

//title "Project Zero"

#include <dspic.h>
#include <dspic30f4011.h>
#include <hitech.h>
#include <htc.h>
//-----------------------------------------------------------------------------

__CONFIG(FOSC, EC & FSCMDIS);
__CONFIG(FWDT,WDTDIS);
__CONFIG(FBORPOR, BORDIS & MCLREN & HPOL);

//#define FOSC 10000000 // 10Mhz
//#define FCY (FOSC / 4) // 1 inscruction cycle = 4 clock cycles



void init(void)
{
TRISB = 0b000000000;
RB0=1;
}

void initpwm(void)
{
PTEN = 0; //Disable MCPWM
PWMCON1_PEN1H = 1; //PWM1H enabled for PWM output
PWMCON1_PEN2H = 1; //PWM2H enabled for PWM output
PTCKPS0 = 1; //PTCKPS<1:0> set to 01. 1:4 Prescale.
PTCKPS1 = 0;
PTOPS0 = 0; //PTOPS<3:0> set to 0s. 1:1 Postscale
PTOPS1 = 0;
PTOPS2 = 0;
PTOPS3 = 0;
PTSIDL = 1; // STOP in Idle Mode: Yes
PTMOD0 = 0; //PTMOD<1:0> set to 00. Free running mode.
PTMOD1 = 0;
PTEN = 1; //ENABLE MCPWM

PTPER = 11250;
}

void main(void)
{

initpwm();
init();

while(1) //Infinite loop
{
PDC1 = 1875; //should set PMW1 duty cycle.
PORTB = 100;
}

}

Thanks in advance.
 
Last edited:
O.K. I doubt very much that you are using EC for your oscillator (i.e. clock). That would imply that an external resistor/cap is being used. Keep in mind that is probably the least used option. More commonly the internal osc (FRC) and external crystal, or resonator (i.e. XT and HS) are used. Check chapter 21 of the data sheet for explanations of the oscillator and other config settings. Why not use the internal osc for now, and add the crystal later if more precise timing is required?

When writing and toggling port pins, the LATBbits.B0 ^= 1 expression could be used to toggle the led in main. If the PortB shorthand works, than fine, just not sure offhand. Setting the PORTB=100 would be b'01100100' so it's just going to set B0 off and stay off, B2 should come on. There is no delay introduced, which is always good when flashing leds. Hitech has delay macro's in the delay.h library, could start using them instead of the timer rollover in the example below.

Below is the getting started with 30F led blink example from Microchip, with some hitech config bits thrown in to start the internal oscillator up. It compiles and simulates. Put the breakpoint on the LATDbits.D0 ^=1 line and watch/view the Special Function Registers PORTD and LATD toggle. Remember I'm using the htc.h include, which implicity includes the device selected in MPLAB.

Thought I explained the hitech config bits in previous post:confused:, oh well.



Code:
/*******************************************************************************
*
* Use Timer 1 to flash LED
*
******************************************************************************/
#include <htc.h>
//-----------------------------------------------------------------------------
//Configuration bits
//Internal osc is 7.37Mhz
__CONFIG (FOSC, CLKSWDIS & FSCMDIS & FRCPLL4);
__CONFIG (FBORPOR, MCLRDIS); 
__CONFIG (FWDT, WDTDIS);
//-----------------------------------------------------------------------------
//Program Specific Constants
#define FCY 7370000	//Instruction cycle rate (Osc x PLL / 4)
//=============================================================================
//Main routine
//Set up LEDs and timer, wait for timer periods, and flash one of the two LEDs
//int main(void);
void main(void){
LATD = 0xfffe; //Initialize LED pin data to off state
TRISD = 0xfffe; //Set LED pin as output
LATDbits.LATD0 = 1; //Turn LED on
T1CON = 0; //Turn off Timer1 and clear settings
TMR1 = 0; //Start Timer1 at zero
PR1 = FCY/256/5; //Set period register value for 1/5 second
T1CON = 0x8030; //Turn on Timer1 with 1:256 prescaler

while(1) //Loop forever
{
while(!IFS0bits.T1IF){} //Wait for timer period
IFS0bits.T1IF = 0; //Clear timer flag for next period
LATDbits.LATD0 ^= 1; //Toggle LED
}
} //End of main()
 
Last edited:
I see you're right, Kent. I could have saved myself some time if I'd read your previous post more thoroughly. I'll adopt your configuration and try out this code. I'm not familiar with the LATD/LATC/LATE though. How does it differ from PORTD/C/E, and why do you turn it on and off PORTD0 instead of LATD0?

Thanks again.
 
Using LATX.x over PORTX.x has to do with read modify write problem that can be incurred under certain circumstances. Look it up in the I/O Port chapter of data sheet, or look at a couple of links. I admit to not strictly abiding by this rule, but is a good habit.

RMW and solutions for it

I really hope you get this going, it's time to move on to the fun stuff.
 
I know right? I agree.

I just compiled your code and tested it in the debugger/simulator. Like you said, I could see RD0 (pin 23) going high and low. For whatever reason, PORTB will not go high or low no matter how I write to it (RB0, LATB0, LATBbits.LATB0, whatever), but that must be another issue. Anyway, seeing some control, even in simulation, was great. However, when I wrote to the chip, pin 23 wasn't going high. I even backed up and just wrote LATDbits.LATD0 = 1, just to see if my oscilloscope would show the pin as high. No luck.

Out of ideas again. My professor suggested something could be wrong with the chips, so I ordered several new samples off microchip.com. Hopefully when they get here I'll have better luck. Updated code below.

Thanks again.

Code:
//title "Project Zero"


#include <htc.h>
//-----------------------------------------------------------------------------

__CONFIG(FOSC, FRC & FSCMDIS);
__CONFIG(FWDT,WDTDIS);
__CONFIG(FBORPOR, BORDIS & MCLREN & HPOL);

//#define FOSC 10000000  // 10Mhz
//#define FCY (FOSC / 4) // 1 inscruction cycle = 4 clock cycles



void init(void)
{
	TRISB = 0xfffe;
	TRISD = 0xfffe;
	PORTD = 0xfffe;
	PORTB = 0xfffe;
	LATD = 0xfffe;
	LATB = 0xfffe;
}
	
void initpwm(void)
{
	PTEN = 0; //Disable MCPWM
	PWMCON1_PEN1H = 1; //PWM1H enabled for PWM output
	PWMCON1_PEN2H = 1; //PWM2H enabled for PWM output
	PTCKPS0 = 1; //PTCKPS<1:0> set to 01. 1:4 Prescale.
	PTCKPS1 = 0;
	PTOPS0 = 0;  //PTOPS<3:0> set to 0s. 1:1 Postscale
	PTOPS1 = 0;
	PTOPS2 = 0;
	PTOPS3 = 0;
	PTSIDL = 1; // STOP in Idle Mode: Yes
	PTMOD0 = 0; //PTMOD<1:0> set to 00. Free running mode.
	PTMOD1 = 0;
	PTEN = 1; //ENABLE MCPWM
	PTPER = 11250;
}

void main(void)
{
	init();
	initpwm();
	
		
		while(1) //Infinite loop
		{
			PDC1 = 1875; //should set PMW1 duty cycle.
			RD0=1;
			LATDbits.LATD0=1;
			
		}
	
}
 
Which programmer? Is the programmer holding the device in reset after programming? Does the programmer verify that the hex has downloaded to the device?

On the PORTB stuff it looks like the analog pins need to be disabled. It at least simulates now.
Code:
ADPCFG = 0x1111;  //make AN(X) pins all digital

I am a newbie with this dsPIC stuff, so maybe someone has some insights on possible initialization problems.
 
A pic of the board setup would be helpful, and a description of how it is powered and/or regulated.

If a proto or vero board is being used? then a check around with a DVM is in order. Verify expected Vdd voltage (5V needed for bulk program erase), ohm out each pin to ground to check for shorts, and between all adjacent pins for solder bridges.

If it is a breadboard, check IC is fully inserted, then try changing socket location, check Vdd, and for loose/broken wires.

If there is any problem with the setup, it will just be repeated with the new chips.
 
I've finally attached a picture of the setup. The setup is the only thing I can think that may be wrong right now. As you can see it is very simple. It is nothing but the 3 VDD and 3 VSS connections. And an oscilloscope on some pin. That and I pulled MCLR (pin 1), up to 5V using a 5K resistor as my professor said I should. Is there something missing that might explain the lack of chip response? The new chips are in, but I don't want to write to them to see no response if there is a problem on the board.

Thanks again.
 

Attachments

  • IMG_0567.JPG
    IMG_0567.JPG
    59.4 KB · Views: 216
Things look like they are hooked up O.K. Hard to tell from the picture, but presumably the green wire on the left side of the chip is hooked from the vertical positive rail to the horizontal positive rail of the breadboard. Double check that 5V is getting to the chip.

You are showing the breadboard without the programmer hookup? Again which programmer?, has it been successfully used on other chips? Make sure the programmer doesn't have the device in reset box ticked, that would keep the chip from running the program. Check to make sure that hex has been downloaded in the program view box when a device read operation is performed.

Otherwise, where is the programmer hookup? The programmer is needed to install the compiled hex code from MPLAB to the chip. Should have picked up a Pickit 2 programmer while you were at it? Attached is my setup for the dspic30f1010. From top to bottom the Pickit 2 hookup is MCLR, Vdd, Gnd, PGD, PGC.

I also hooked up the analog AVdd and AVss pins to the Vdd and Vss pins as a precaution. Shown is an led on the RD0 pin to show a successful outcome after programming the blinky. Having the 0.1uf cap close to the microcontroller supply pins is always a good idea.

Is the power supply regulated at 5V? If it is a variable supply, then you are living dangerously without a lm7805 regulator ic. Accidently overvolting, or shorting Vdd to Vss can kill a PIC.

For my dsPic30f1010 the osc needs the PLL enabled in config for the Analog and Pwm modules to work. Not sure if that applies to the dsPic 30f4011, could be yet another oddity of the 30f1010.

Then it's time to try the old chip again, no luck?, then try a new one.
 

Attachments

  • dspic30f.jpg
    dspic30f.jpg
    132.8 KB · Views: 255
FYI, I'm using an MPLAB..um..PM 3 burner? I'll check exactly soon. Finally heading back to the lab, really hope these new chips work. Or else I'm out of ideas. I'll try activating PLL too, though.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top