// delay = N*T
for(i = 1, i < N, ++i) ; //Loop for N*T that does no useful work
Timer 0 depends on the oscillator frequency which can be external or internal on a PIC. The term RTC is ambiguous at best. It can be a Time of Day Clock Calender device or in a RTOS it can be any timer, like Timer 0, that provides a periodic interrupt. The literature is neither clear nor precise on this point. I guess the OP/TS will need to provide us with some clarification on this point.Timer0 interrupt does not depend on an external clock; RTC or otherwise.
http://www.microcontrollerboard.com/pic-timer0-tutorial.html
int sampletime_ms=0,present_time=0,previous_time=0,diff_time=0;
sampletime_ms = 1000;
void run_PID()
{
present_time = get_time_from_rtc();
diff_time = (present_time - previous_time);
if(diff_time > sampletime_ms)
{
//run control loop
previous_time = present_time;
}
}
if(TMR1 == TMR2)
{
//generate interrupt
//disable pwm.
}
So using OCx is not an option for me :!When the OCx module is enabled, the Input/Output (I/O) pin direction is controlled by the Output
Compare x module. The Output Compare x module returns the I/O pin control back to the appropriate LATx and TRISx control bits when it is disabled. When the Simple PWM with Fault Protection
Input mode is enabled, the OCFA/OCFB Fault pins must be configured as inputs by setting the
respective TRISx bits. Enabling this special PWM mode does not configure the OCFA/OCFB Fault
pins as inputs.
That is not quite the way it works. You don't compare two timers to each other, but rather you compare a timer to a COMPARE REGISTER with a fixed value. Each time there is an interrupt you can adjust the value to adapt to changing circumstances.Hmm, yes the output compare module will do the trick,but i have a few basic doubts regarding this module,i generally use a high speed pwm modules for pwm generation and ignore this completely.This is my first time attempting to use it so please correct me if i am wrong anywhere.
Firstly all my MCU pins are already assigned,currently the output compare module pins are digital outputs for my LCD,it is possible to only compare ,and not generate any PWM after comparison?,something like this:
I am assuming that if i set the OC1CON1bits.OCM = 0x00; would this disable the PWM generation and i can continue using the pin as a digital output for my LCD?Code:if(TMR1 == TMR2) { //generate interrupt //disable pwm. }
while(counter < counter_limit)
{
if(counter % 1000 == 0) //executes the loop exactly after 1000 cycles
{
//run my control loop
run_PID();
}
counter++
//continue with the rest of the operations
}
You need an interrupt: what you've shown here will be executed every 1000 trips through the loop, sure, but its timing/rep-rate will be at the mercy of all other operations.i my have found a suitable alternative,its quiet simple actually,
Code:while(counter < counter_limit) { if(counter % 1000 == 0) //executes the loop exactly after 1000 cycles { //run my control loop run_PID(); } counter++ //continue with the rest of the operations }
short counter = 0;
if (counter % 5 == 0) //updates every second
{
if(counter > 100)
counter = 0;
//run the control function
run_PID();
}
//ISR
void __attribute__((__interrupt__, no_auto_psv)) _T3Interrupt(void)
{
/* Interrupt Service Routine code goes here */
IFS0bits.T3IF = 0; //Clear Timer3 interrupt flag
counter++;
}