Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[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
Divers test avec 18F46K22 Q=20MHz x 4
RS232 UART1 avec interface 2T
Test calcul cosinus Angle
Cos 0 1.00000
Cos 1 0.99985
Cos 2 0.99939
Cos 3 0.99863
Cos 4 0.99756
Cos 5 0.99619
Cos 6 0.99452
Cos 7 0.99255
Cos 8 0.99027
Cos 9 0.98769
Cos 10 0.98481
Cos 11 0.98163
Cos 12 0.97815
Cos 13 0.97437
Cos 14 0.97030
Cos 15 0.96593
Cos 16 0.96126
Cos 17 0.95630
Cos 18 0.95106
Cos 19 0.94552
Cos 20 0.93969
Cos 21 0.93358
Cos 22 0.92718
Cos 23 0.92050
Cos 24 0.91355
Cos 25 0.90631
Cos 26 0.89879
Cos 27 0.89101
Cos 28 0.88295
Cos 29 0.87462
Cos 30 0.86603
Cos 31 0.85717
Cos 32 0.84805
Cos 33 0.83867
Cos 34 0.82904
Cos 35 0.81915
Cos 36 0.80902
Cos 37 0.79864
Cos 38 0.78801
Cos 39 0.77715
Cos 40 0.76604
Cos 41 0.75471
Cos 42 0.74315
Cos 43 0.73135
Cos 44 0.71934
Cos 45 0.70711
Cos 46 0.69466
Cos 47 0.68200
Cos 48 0.66913
Cos 49 0.65606
Cos 50 0.64279
Cos 51 0.62932
Cos 52 0.61566
Cos 53 0.60182
Cos 54 0.58779
Cos 55 0.57358
Cos 56 0.55919
Cos 57 0.54464
Cos 58 0.52992
Cos 59 0.51504
Cos 60 0.50000
Cos 61 0.48481
Cos 62 0.46947
Cos 63 0.45399
Cos 64 0.43837
Cos 65 0.42262
Cos 66 0.40674
Cos 67 0.39073
Cos 68 0.37461
Cos 69 0.35837
Cos 70 0.34202
Cos 71 0.32557
Cos 72 0.30902
Cos 73 0.29237
Cos 74 0.27564
Cos 75 0.25882
Cos 76 0.24192
Cos 77 0.22495
Cos 78 0.20791
Cos 79 0.19081
Cos 80 0.17365
Cos 81 0.15644
Cos 82 0.13917
Cos 83 0.12187
Cos 84 0.10453
Cos 85 0.08716
Cos 86 0.06976
Cos 87 0.05234
Cos 88 0.03490
Cos 89 0.01745
 

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,

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

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 :D

THANKS
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top