Para saber cuanto te demora el PIC, podes crear un proyecto aparte de pruebas (esto para no involucre nada del otro codigo), crear las variables y hace la division dentro del main, de alli lo vas a simular, poniendo breakpoints en la instruccion y una mas en otra instruccion adelante. Si observas en el Stopwatch te dice cuantos ciclos le toma realizarlo, Tambien te dice el tiempo pero para que sea correcto el tiempo tenes que configurar correctamente el oscilador en el MPLAB, asi que guiate por los ciclos.
Respecto a como hacerlo mas rapido... no se que decirte, obviamente que con una division llegas al resultado, pero no se que velocidad pensas tener de respuesta desde que calculaste el valor hasta cambiar las salidas.
Ejemplo, cree un proyecto el cual posee este codigo nomas:
// PIC12F683 Configuration Bit Settings
// 'C' source line config statements
#include <xc.h>
#include <stdint.h>
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIG
#pragma config FOSC = INTOSCIO // Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF // MCLR Pin Function Select bit (MCLR pin function is digital input, MCLR internally tied to VDD)
#pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = OFF // Brown Out Detect (BOR disabled)
#pragma config IESO = OFF // Internal External Switchover bit (Internal External Switchover mode is disabled)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled)
void main (void)
{
uint16_t periodo = 6000;
uint8_t rps;
while(1)
{
rps = 1000000/periodo;
periodo += 500;
}
}
Puse el breakpoint en la instruccion rps, es decir los valores que me van a dar de ciclos van a estar separados por unos 5 ciclos masomenos ocupados por las demas instruciones.
Estos son los valores que me dieron:
Target halted. Stopwatch cycle count = 921 (76,75 µs)
Target halted. Stopwatch cycle count = 921 (76,75 µs)
Target halted. Stopwatch cycle count = 925 (77,083333 µs)
Target halted. Stopwatch cycle count = 925 (77,083333 µs)
Target halted. Stopwatch cycle count = 926 (77,166667 µs)
Target halted. Stopwatch cycle count = 963 (80,25 µs)
Target halted. Stopwatch cycle count = 963 (80,25 µs)
Target halted. Stopwatch cycle count = 963 (80,25 µs)
Target halted. Stopwatch cycle count = 963 (80,25 µs)
Target halted. Stopwatch cycle count = 963 (80,25 µs)
Target halted. Stopwatch cycle count = 963 (80,25 µs)
Target halted. Stopwatch cycle count = 1383 (115,25 µs) // Periodo es 500 aca
Target halted. Stopwatch cycle count = 1303 (108,583333 µs)
Target halted. Stopwatch cycle count = 1271 (105,916667 µs)
Target halted. Stopwatch cycle count = 1299 (108,25 µs)
Target halted. Stopwatch cycle count = 1201 (100,083333 µs)
Target halted. Stopwatch cycle count = 1187 (98,916667 µs)
Target halted. Stopwatch cycle count = 1163 (96,916667 µs)
Todo depende del valor de periodo, por lo que veo el minimo es de 925 ciclos y el maximo de 1380 aproximadamente. Es decir no es fijo.
Otra forma que probe fue:
rps = 15625/(4*(periodo>>8));
Viene de que si divido periodo por 256 deberia dividir ese millon por 256. lo cual me queda asi.. Lo bueno de esto que dividir por 256 es simplemente tomar el byte mayor, hay una perdida de presicion al dividirlo por 8 y luego multiplicarlo por 4. ya que esos 2 ultimos bits quedan en 0. Hice un excel para ver cual era el error.
La formula usada en el excel fue:
TRUNC(15625/(4*(TRUNC(periodo/256))))Y en los valores altos de periodo no hay error, pero acercandonos hacia lo que nos daria los RPS cercanos a 166 los errores aumentan en algunos hasta 3 de diferencia, es decir en ves de darte 166 me da 169, mas que eso aumentan a 5 o mas los errores. Lo bueno de todo esto es que ahora los ciclos bajaron a:
arget halted. Stopwatch cycle count = 336 (28 µs)
Target halted. Stopwatch cycle count = 337 (28,083333 µs)
Target halted. Stopwatch cycle count = 355 (29,583333 µs)
Target halted. Stopwatch cycle count = 354 (29,5 µs)
Target halted. Stopwatch cycle count = 354 (29,5 µs)
Target halted. Stopwatch cycle count = 354 (29,5 µs)
Target halted. Stopwatch cycle count = 354 (29,5 µs)
Target halted. Stopwatch cycle count = 354 (29,5 µs)
Target halted. Stopwatch cycle count = 667 (55,583333 µs) // Para cuando perdiodo es 500
Target halted. Stopwatch cycle count = 617 (51,416667 µs)
Target halted. Stopwatch cycle count = 574 (47,833333 µs)
Target halted. Stopwatch cycle count = 574 (47,833333 µs)
Target halted. Stopwatch cycle count = 532 (44,333333 µs)
Es decir la mitad a un error que podemos considerar tal ves razonable
Otro caso mas, incorporar el *4 dentro del shift:
// Programa
rps = 15625/(periodo>>6);
//Excel
=TRUNC(15625/TRUNC(periodo/64))
Ahora el error maximo es de 2 y ocurre a ese valor, este caso tiene menos error que en anterior y lso ciclos estan
Target halted. Stopwatch cycle count = 370 (30,833333 µs)
Target halted. Stopwatch cycle count = 388 (32,333333 µs)
Target halted. Stopwatch cycle count = 387 (32,25 µs)
Target halted. Stopwatch cycle count = 387 (32,25 µs)
Target halted. Stopwatch cycle count = 387 (32,25 µs)
Target halted. Stopwatch cycle count = 387 (32,25 µs)
Target halted. Stopwatch cycle count = 387 (32,25 µs) // Periodo = 65500
Target halted. Stopwatch cycle count = 693 (57,75 µs) // Periodo = 500
Target halted. Stopwatch cycle count = 638 (53,166667 µs)
Target halted. Stopwatch cycle count = 607 (50,583333 µs)
Target halted. Stopwatch cycle count = 589 (49,083333 µs)
Target halted. Stopwatch cycle count = 572 (47,666667 µs)