[SOLVED] mikroc math lib dont work

Status
Not open for further replies.
But its a precompiled lib and its not the uart the probleme cause whitout any uart in project it will crash when a will call function like sin,cos,tan. The uart line just after the function cos "resolve" the crash so that why i dont understand why it crash whitout uart cause they arr no link between this 2 function.
 

Check if the included MikroC math.h library give support to the function ftoa(), and try something like this:
Code:
[I]UART1_Write_Text( &ftoa(doub) );[/I] // not tested !
 

I think i express me wrong. I try to do my best in englis so sorry if it hard to understand. Forget all about the uart. Its realy the math lib where i got crash.


Code C - [expand]
1
2
3
4
5
6
7
8
9
Void main
{
  do
  {
    //pic will excute all the code over
    Cos (2.0);//pic will crash and reboot here
    //pic will never go here
  }while (1);
}



Juste this will crash. I write this on my cellphone on the fly but it juste an simple exemple.
 
Last edited by a moderator:

At the line 6 above, you are not assigning Cos (2.0) to any variable.
 

Hello,


An example with a PIC18F46K22 MikroC pro 6.50 and Math library
calcul of cos 0 to 90°
degre converted in radians before calling COS function
Use of double df1,df2;
#define PI 3.14159


Code:
 txt=&TEXTE[0];
  UART1_Init(19200);   // RS232
  UART_Set_Active(&UART1_Read, &UART1_Write, &UART1_Data_Ready, &UART1_Tx_Idle); // set UART1 active
  UART1_Write(CLS);
  UART1_Write_CText(mesg0);

  strConstRamCpy(txt,mesg1); UART1_Write_Text(txt);
   // inclue Maths.lib
  UART1_Write_CText("Test calcul cosinus Angle\r\n ");
  for (i=0;i<90;i++)
   {
   df1=(double)i  ;
   df1=df1 *  PI /180.0;
   df2=cos(df1);
   UART1_Write_CText("Cos ");
   WordToStr(i, txt);
   UART1_Write_Text(txt);
   UART1_Write(TAB);
   Float2Ascii (df2,CRam1,5);
   UART1_Write_Text(CRam1);
   CRLF();
   }
   while(1);

result
 

Ok i try your code and it work but if i delete the 2 line of the uart_write it crash


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
double df1,df2;
 
 
char txt[25],i;
 
void Main()
{
  Unlock_IOLOCK();
  RPOR7 =   0b0000001100000000; //OUTPUT TX UART 1 PIN RB15
  RPINR18 = 0b0000000000001110; //INPUT RX UART 1 PIN RB14
  lock_IOLOCK();
   //UART1
  TRISB.B15 = 0; //OUTPUT RX UART 1
  TRISB.B14 = 1; //INPUT TX UART 1
 
  UART1_Init(57600);
  //delay_ms(100);
 
  AD1PCFGL = 0xFFFF;                           // digital not analog
  CMCON = 7;
 
  TRISA.B0 = 0;
  PORTA.B0 = 1;
 
 
   // inclue Maths.lib
  UART1_Write_Text("Test calcul cosinus Angle\r\n "); //NEED it if not there my pic will reboot in loop at the line df1 = 57.295.....
  for (i=0;i<90;i++)
   {
   df1=(double)i  ;
   df1= cos(df1);
   UART1_Write_Text("Cos "); //NEED it to ----------------------------------------------------------------------
   FloatToStr(df1,txt);
   UART1_Write_Text(txt);
   UART1_Write(13);
   UART1_Write(10);
 
   }
   while(1);
 
}



so why i need to use uart to avoid a crash where is the link between the function ?

- - - Updated - - -

i make a video for me more easy to understand

 
Last edited by a moderator:

(By the way, your video just shows the text "This video is private" for me.)

Can I suggest that you generate a full link map and have a look at that. It should list the library modules that are being brought in and the addresses of the functions. I'm just wondering if a wrong library is being loaded, there is some spurious function with a name that is confusing the linker so something like that occurring. Also, step though the code and into the 'cos' and UART functions - while you should only see the assembler code, you will see the address and be able to compare that with the address from the link map to make sure they are the same.
As far as I know I would agree that there should not be a connection between the functions you are using. However when such things occur, it is often caused by pointers going wrong, arrays being addressed beyond their limits, stacks being corrupted, interrupt routines messing with variables. Looking at your code, you have defined 'txt' to be a 25 element character array; is there any chance that the 'FloatToStr; function could try to write more than 25 characters (including the trailing null) or that the trailing null is not added and so the next UART write text function will not stop when expected.
Also, you have defined 'i' as a character (and not used it beyond 0 to 90 so it should be OK), but used it in a "for" loop that has a "while(1)" at the end of the for loop's block. I'm a little surprised that this does not throw up a syntax error as a while instruction at the end of a loop normally requires a "do" at the start (so the compiler knows where to put the branch label).
Susan
 

hello

i don't use the UART1_Write_Text function because , it uses to much RAM memory
=> MikroC send only alarm message ..and can compile successfully.. but problem is there if not enough RAM.
with this UART1_Write_CText("Test calcul cosinus Angle\r\n ");
TEXTE is stored in FLASH ..



Code:
void UART1_Write_CText(const char *txt1)
 {
   while (*txt1)
      UART1_Write(*txt1++);
}

- - - Updated - - -

Hello,


on my original code , i use integer for I variable.
With MikroC ( not 100% ANSI C !) we can write an empty loop to stop the programe
like
Code:
while(1);

or 
while(1)
{
]

i agree that the following form is more compliant to ANSI C
do
{

}while(1);

here is all my code test (with subroutines Float2Str and variables definitions.

View attachment 18F46K22_uart_test_COSinus.zip
 

thank to everyone but i take the code from paulfjujo and its work now i dont know why but it work i modify the code for my pic and get data from my gyroscope and it work now i know the angle of my drone

THANKS
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…