Help with OC1

Post Reply
Posts: 1
Joined: Mon Dec 12, 2016 2:38 pm

Help with OC1

Post by Towan » Mon Dec 12, 2016 2:44 pm


I would like to send analog output through PWM by using the OC1-port, where i have connected a summer that i would like to play a sound.

My code is as follows:

Code: Select all

#include <pic32mx.h>

	void genpwm(int dutycycle) {	
		PR2 = 255; // set period to 255+1 ticks = 78.125 KHz	
		OC1RS = dutycycle; // set duty cycle, switch tone with this
		OC1CON |= 0x06; // set bits 0 through 2 to 0b110, setting the OC! to PWM-mode
		T2CON |= 0x04000; // turn on timer 2 in default mode (20 MHz, 16 bit)	
		OC1CON |= 0x04000; // turn on output compare 1 module

int main(void) {
	while(1) {
		//PORTE = (PORTF >> 1) & 0x1; 
	return 0;
Could anyone please help me out? I have no clue what to do!

User avatar
Site Admin
Posts: 2188
Joined: Wed Nov 09, 2011 7:51 pm
Location: UK

Re: Help with OC1

Post by majenko » Mon Dec 12, 2016 2:59 pm

chipKIT doesn't use "main()" - it uses "setup()" and "loop()".

There is "analogWrite(...)" which generates PWM.

It looks like you aren't using the chipKIT software, but Microchip software. We cannot directly support Microchip software, you should seek help with their forums for that.

I will say, though, that you never need to keep PWM running by constantly calling the function. You call it once to start it going then just leave it running. Your genpwm() should be before your while loop, not in it.
Why not visit my shop?
Universal IDE:
"I was trying to find out if it was possible to only eat one Jaffa Cake. I had to abandon the experiment because I ran out of Jaffa Cakes".

Post Reply