So, I'm trying to achieve a 1HZ PWM frequency with a Duty Cycle of about 70%.
The problem that I encountered was with the minimum PWM frequency that the PIC18F14K50 can reach, which is about 1.892HZ, using PR2=255(max),FOSC equals to 31kHZ(changed from 16MHZ) and prescaler of TIMER2=16:
PWM FREQUENCY= (PR2+1)*4*TOSC*prescaler
So my question resides in a manner to achieve that 1HZ for the PWM Frequency with a Duty Cycle of 70%.
Notes: Can't use delays(function) and can't change the hardware(PIC), so the only option is via Software.
Bellow is the code used to achieve a frequency of 1.892HZ:
//Variables
int DC; // Duty cycle
//
OSCCON=0; // 31kHZ
// Configure PWM Module 1.892 HZ
OpenTimer2(T2_PS_1_16); // Prescaler 16
OpenPWM1(0xFF); //Configure PWM module and initialize 1 HZ ; PR2 =255
SetDCPWM1(0); // set duty cyle
SetOutputPWM1(SINGLE_OUT, PWM_MODE_1);
void LED(void) {
DC=70; // 70%
SetDCPWM1((int) DC * 10.23); //set the duty cycle 70% in 10bits//
}
EDIT:
Trying to follow the comments of all of you I tried this with TIMER0 and now with FOSC= 16MHZ :
void highISR(void) {
// Check if there was an overflow in TIMER0
if (INTCONbits.TMR0IF == 1) {
LATCbits.LATC5=1; // LED ON
INTCONbits.TMR0IF = 0;
WriteTimer0(Ktimer);
}
}
#pragma code HighInterruptVector=0x0008
void HighInterruptVector(void) {
_asm
goto highISR
_endasm
}
#pragma code
// CONFIG TIMER0
OpenTimer0(TIMER_INT_ON & // Use interruptions
T0_16BIT & // 16 bit timer
T0_SOURCE_INT & // Use internal clock as source
T0_EDGE_FALL & // Update on falling edge
T0_PS_1_128); // 128 prescaler (lowest period possible)
// FOSC = 16 MHz ==> FTY = 16/4 = 4MHz ==> -------TCY = 250 ns
// Timer 0 prescaler = 128 ==> Count on every--------- 250 ns * 128 = 32 us
// 1 seg counting = --------1s / 32u = 31250
//31250*0,7 =21875 70% DC
Ktimer = 65536 - (21875);
//WriteTimer0(Ktimer);
// So the flag occurs
While(1){
WriteTimer0(Ktimer)
LATCbits.LATC5=0; // LED OFF
}
But led wont turn on. 100% certain that the fault is in how the code is constructed.
Best Answer
Final answer to anyone wondering here's the code that worked for me. It's simple , because I really don't need a very specific code or precision for the work I'm doing. Wanna thank you all that commented and helped me get the right ideas.