Quiero poner las pruebas que hize con unas rutinas matemáticas de la librería MATH.H. Lo he simulado y me compila muy bien, después calculé los tiempos que estas demoran con ayuda del StopWatch y me llevé una gran sorpresa, para mal.
#include <p30f4011.h>
#include <uart.h>
#include <math.h> /* for acos */
#include <stdlib.h>
#include "utilities.c"
/* Received data is stored in array Buf */
char Buf[80];
void configUSART2()
{
// Holds the value of baud register. Holds the value of uart config reg
// Holds the information regarding uart TX & RX interrupt modes
unsigned int baudios, U1MODEvalue, U1STAvalue;
/* Configure uart1 receive and transmit interrupt */
ConfigIntUART2(UART_RX_INT_EN & UART_RX_INT_PR2 &
UART_TX_INT_DIS & UART_TX_INT_PR6);
// Configure UART1 module to transmit 8 bit data with one stopbit, 9600 baudios
baudios = 103;
//U1BRG=103; U1MODE=0x8000; U1STAbits.UTXEN=1;
U1MODEvalue = UART_EN & UART_IDLE_STOP & UART_RX_TX & UART_DIS_WAKE &
UART_DIS_LOOPBACK & UART_DIS_ABAUD & UART_NO_PAR_8BIT &
UART_1STOPBIT;
U1STAvalue = UART_INT_TX_BUF_EMPTY & UART_TX_PIN_NORMAL & UART_TX_ENABLE &
UART_INT_RX_CHAR & UART_ADR_DETECT_DIS & UART_RX_OVERRUN_CLEAR;
OpenUART2(U1MODEvalue, U1STAvalue, baudios);
}
void __attribute__((__interrupt__)) _U2RXInterrupt(void)
{
unsigned int j; //local variable
for (j=0;j<=12;j++)
{
while (DataRdyUART2()==0)
{
nop2();
}
Buf[j]=getcUART2();
}
putsUART2((unsigned int *)Buf);
IFS1bits.U2RXIF = 0;
}
int main(void)
{
char a[]="128";
char b[]="-12.85";
double x,y,z;
Buf[13]='\0';
_TRISF2=1; _TRISF3=0; _TRISD1=0;
char Txdata[] = {'M','i','c','r','o','c','h','i','p',' ','I','C','D','2',
' ','\13','\10','\0'};
configUSART2();
putsUART2((unsigned int *)Txdata);
// rutina matemática
x=2.0; y=3.0;
z=atoi(a); // demora 556 ciclos
z=atof(b); // demora 1782 ciclos
y=pow (x, y); // demora 4992 ciclos
y=2*2*2; // demora 4 ciclos
//
while (1)
{
toggle(_RD1);
delay20(); delay20(); delay20(); delay20(); delay20();
}
}
Y este es su código en asembler.
67: z=atoi(a); // demora 556 ciclos
13DA 200380 mov.w #0x38,0x0000
13DC 40000E add.w 0x0000,0x001c,0x0000
13DE 2000A2 mov.w #0xa,0x0004
13E0 EB0080 clr.w 0x0002
13E2 07F6D7 rcall 0x000192
13E4 DE80CF asr 0x0000,#15,0x0002
13E6 07FB91 rcall 0x000b0a
13E8 982760 mov.w 0x0000,[0x001c+76]
13EA 982771 mov.w 0x0002,[0x001c+78]
68: z=atof(b); // demora 1782 ciclos
13EC 2003C0 mov.w #0x3c,0x0000
13EE 40000E add.w 0x0000,0x001c,0x0000
13F0 B81160 mul.uu 0x0004,#0,0x0004
13F2 EB0080 clr.w 0x0002
13F4 07F761 rcall 0x0002b8
13F6 982760 mov.w 0x0000,[0x001c+76]
13F8 982771 mov.w 0x0002,[0x001c+78]
69: z=pow (x, y); // demora 5000 ciclos
13FA 90214E mov.w [0x001c+72],0x0004
13FC 9021DE mov.w [0x001c+74],0x0006
13FE 90202E mov.w [0x001c+68],0x0000
1400 9020BE mov.w [0x001c+70],0x0002
1402 07FBAE rcall 0x000b60
1404 982760 mov.w 0x0000,[0x001c+76]
1406 982771 mov.w 0x0002,[0x001c+78]
70: y=2*2*2; // demora 6 ciclos
1408 200000 mov.w #0x0,0x0000
140A 241001 mov.w #0x4100,0x0002
140C 982740 mov.w 0x0000,[0x001c+72]
140E 982751 mov.w 0x0002,[0x001c+74]
71: //y=asinf(x);
Lo probé varias veces para estar seguro, pero me salían la misma cantidad de instrucciones en cada operación. Como es posible que y=pow(2,3); se demore casi 5000 instrucciones e y=2*2*2; lo haga en solo 4, además los convertidores de datos se toman mucho tiempo en pasar una cadena a un número real.
Y una pregunta como puedo enviar la variable z=-12.85; por el puerto serial.
Saludos.