Hi, I have searched the forum and could not find anything similar to what i am after.
My project is to output four separately controllable 'PWM' signals from a PIC18f4550, Controlled via a rotary encoder using the push button to cycle between channels. Firstly i am very new to PIC's in general and have learned some basic functions writing in XC8 such as blinking led's, Dimming with use of the delay() function, setting config bits, Etc.
I have generated code that reads the output from the Rotary encoder and is de-bounced enough for my purposes. However i am unsure of how to implement this into software PWM. So far my Code outputs via RD2 and RD3 to light up LED's as a visual representation of the encoder, The encoder is on RB2 and RB3. As this chip only has two 'CCP' modules i assume the four channels will be software generated. I have not been able to find examples of software PWM in XC8.
Should the encoder count to a register such as INTCON and use this a function to control duty cycle? A snippet or example of software pwm in XC8 would be very helpful. Note the frequency will be low on all channels Sub 1khz. I will include my code so far.
Any help would be great, Thank you.
Mod edit: code tags - JohnInTX
My project is to output four separately controllable 'PWM' signals from a PIC18f4550, Controlled via a rotary encoder using the push button to cycle between channels. Firstly i am very new to PIC's in general and have learned some basic functions writing in XC8 such as blinking led's, Dimming with use of the delay() function, setting config bits, Etc.
I have generated code that reads the output from the Rotary encoder and is de-bounced enough for my purposes. However i am unsure of how to implement this into software PWM. So far my Code outputs via RD2 and RD3 to light up LED's as a visual representation of the encoder, The encoder is on RB2 and RB3. As this chip only has two 'CCP' modules i assume the four channels will be software generated. I have not been able to find examples of software PWM in XC8.
Should the encoder count to a register such as INTCON and use this a function to control duty cycle? A snippet or example of software pwm in XC8 would be very helpful. Note the frequency will be low on all channels Sub 1khz. I will include my code so far.
Any help would be great, Thank you.
C:
include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#define _XTAL_FREQ 4000000
void main(void)
{
OSCCON = 0b01110010;
TRISB = 0b00000110;
TRISD = 0x00;
RCONbits.IPEN = 1;
INTCON = 0b10000000;
INTCON2 = 0b10000000;
INTCON3 = 0b11011000;
INTCON2bits.RBPU = 0;
while(1)
{
LATD = 0;
LATB = 1;
}
}
void __interrupt() rotaryISR(void)
{
if (INTCON3bits.INT1IF > INTCON3bits.INT2IF)
{
__delay_ms(100);
PORTDbits.RD2;
INTCON2bits.INTEDG1 = ~INTCON2bits.INTEDG1;
INTCON2bits.INTEDG2 = ~INTCON2bits.INTEDG2;
__delay_ms(50);
INTCON3bits.INT1IF = 0;
INTCON3bits.INT2IF = 0;
}
if (INTCON3bits.INT2IF > INTCON3bits.INT1IF)
{
__delay_ms(100);
PORTDbits.RD3;
INTCON2bits.INTEDG1 = ~INTCON2bits.INTEDG1;
INTCON2bits.INTEDG2 = ~INTCON2bits.INTEDG2;
__delay_ms(50);
INTCON3bits.INT1IF = 0;
INTCON3bits.INT2IF = 0;
}
else
{}
}
Last edited by a moderator: