librairie <math.h> resultats bizarre... (atmel 8535)
Répondre à la discussion
Affichage des résultats 1 à 22 sur 22

librairie <math.h> resultats bizarre... (atmel 8535)



  1. #1
    invite5e1e868f

    librairie <math.h> resultats bizarre... (atmel 8535)


    ------

    Bonsoir

    Voilà j'ai un petit problème lorsque j'utilise certaines fonctions mathématiques de la librairie <math.h>

    Exemple :


    lorsque je fais afficher sur mon afficheur lcd cos 0, j'ai 1 bon jusque là aucun problème.Mais quand je fais cos 3, par exemple, j'obtient ...28709 !!

    De la même façon,

    cos 1=20803
    cos 2=4386

    Pareil pour la fonction sinus : sin 0=0, mais sin 1=27302

    Bref je trouve ça étrange, et cela me gène car je ne peut pas faire de calculs correctement. Quelqu'un sait-il d'où celà vient??

    Merci d'avance

    -----

  2. #2
    invite2562e666

    Re : librairie <math.h> resultats bizarre... (atmel 8535)

    oulala

    Attention à la manière dont tu déclares tes variables !
    La fonction cos ou ou cosf doivent avoir des arguments de type float (cosf) ou double (cos), et retournent aussi des floats ou double floats.

    essaye x=cosf(1.0); (où x est déclaré comme float)

    th.

  3. #3
    invite5e1e868f

    Re : librairie <math.h> resultats bizarre... (atmel 8535)

    ok merci, je suis un gros débutant, donc tout est nouveau pour moi^^

    Par contre, j'ai fait ce que tu m'as dit et codevisionavr me dit : undefined symbol cosf

    Au cas ou tu voudrais jeter un oeil à ce que j'ai fait:

    /****************************** ***********************
    This program was produced by the
    CodeWizardAVR V1.25.3 Professional
    Automatic Program Generator
    © Copyright 1998-2007 Pavel Haiduc, HP InfoTech s.r.l.
    http://www.hpinfotech.com

    Project :
    Version :
    Date : 12/05/2008
    Author : F4CG
    Company : F4CG
    Comments:


    Chip type : ATmega8535
    Program type : Application
    Clock frequency : 8,000000 MHz
    Memory model : Small
    External SRAM size : 0
    Data Stack size : 128
    ****************************** ***********************/

    #include <mega8535.h>

    // Alphanumeric LCD Module functions
    #asm
    .equ __lcd_port=0x15 ;PORTC
    #endasm
    #include <lcd.h>
    #include <math.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <delay.h>

    float x=3;
    float y;
    char chaine[256];

    // Declare your global variables here

    void main(void)
    {
    // Declare your local variables here

    // Input/Output Ports initialization
    // Port A initialization
    // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
    // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
    PORTA=0x00;
    DDRA=0x00;

    // Port B initialization
    // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
    // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
    PORTB=0x00;
    DDRB=0x00;

    // Port C initialization
    // Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
    // State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
    PORTC=0x00;
    DDRC=0xFF;

    // Port D initialization
    // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
    // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
    PORTD=0x00;
    DDRD=0x00;

    // Timer/Counter 0 initialization
    // Clock source: System Clock
    // Clock value: Timer 0 Stopped
    // Mode: Normal top=FFh
    // OC0 output: Disconnected
    TCCR0=0x00;
    TCNT0=0x00;
    OCR0=0x00;

    // Timer/Counter 1 initialization
    // Clock source: System Clock
    // Clock value: 8000,000 kHz
    // Mode: Normal top=FFFFh
    // OC1A output: Discon.
    // OC1B output: Discon.
    // Noise Canceler: Off
    // Input Capture on Falling Edge
    // Timer 1 Overflow Interrupt: Off
    // Input Capture Interrupt: Off
    // Compare A Match Interrupt: Off
    // Compare B Match Interrupt: Off
    TCCR1A=0x00;
    TCCR1B=0x01;
    TCNT1H=0x00;
    TCNT1L=0x00;
    ICR1H=0x00;
    ICR1L=0x00;
    OCR1AH=0x00;
    OCR1AL=0x00;
    OCR1BH=0x00;
    OCR1BL=0x00;

    // Timer/Counter 2 initialization
    // Clock source: System Clock
    // Clock value: Timer 2 Stopped
    // Mode: Normal top=FFh
    // OC2 output: Disconnected
    ASSR=0x00;
    TCCR2=0x00;
    TCNT2=0x00;
    OCR2=0x00;

    // External Interrupt(s) initialization
    // INT0: Off
    // INT1: Off
    // INT2: Off
    MCUCR=0x00;
    MCUCSR=0x00;

    // Timer(s)/Counter(s) Interrupt(s) initialization
    TIMSK=0x00;

    // Analog Comparator initialization
    // Analog Comparator: Off
    // Analog Comparator Input Capture by Timer/Counter 1: Off
    ACSR=0x80;
    SFIOR=0x00;

    // LCD module initialization
    lcd_init(16);

    while (1)
    {
    // Place your code here
    lcd_init(16) ;
    y=cosf(x) ;

    sprintf(chaine,"%d",y);
    lcd_puts(chaine);

    delay_ms(3000) ;
    };
    }

  4. #4
    invite2562e666

    Re : librairie <math.h> resultats bizarre... (atmel 8535)

    Et avec cos()?


    Le mieux est de regarder le mode d'emploi d'avr-gcc, tu trouveras la réponse. Je ne l'ai actuellement pas sous la main.
    Il y faut aussi les librairies libm.a dans le link.

    th.

  5. A voir en vidéo sur Futura
  6. #5
    invite5e1e868f

    Re : librairie <math.h> resultats bizarre... (atmel 8535)

    avec cos(), j'obtient le problème du premier post avec les valeurs bizarre

    Sinon pour la librairie que tu me propose, elle sert à quoi??Parce que codevisionavr ne la trouve pas quand je compile.

    Tiens, dans la rubrique mathematical functions de l'aide j'ai ça (c'est du copier coller):

    float sin(float x)

    returns the sine of the floating point number x, where the angle is expressed in radians.

    float cos(float x)

    returns the cosine of the floating point number x, where the angle is expressed in radians.


    float tan(float x)

    returns the tangent of the floating point number x, where the angle is expressed in radians.

  7. #6
    eSb`

    Re : librairie <math.h> resultats bizarre... (atmel 8535)

    A mon avis, c'est ton sprintf qui est mauvais :
    sprintf(chaine,"%d",y); (d c'est pour un entier)

    Essaie :
    sprintf(chaine,"%f",y);

  8. #7
    invite5e1e868f

    Re : librairie <math.h> resultats bizarre... (atmel 8535)

    j'ai fais comme tu m'as dis eSb , mais je sais pas pourquoi, quand j'ai changé "%d" par "%f" et que j'ai chargé le programme dans le mc , l'afficheur lcd n'aficahait plus rien. Je comprends pas ce qui se passe ^^

  9. #8
    eSb`

    Re : librairie <math.h> resultats bizarre... (atmel 8535)

    Jamais essayé de programmer un LCD, donc je ne sais pas t'aider à ce niveau. (je sais pas comment ça gère des float, on pourra sûrement t'aider ici)

    Essaie ça pour voir (je n'ai jamais programmé en C pour un hors Windows, donc je ne sais pas si c'est acceptable):
    sprintf(chaine,"%d",(int)y);

    (normalement tu devrais avoir 0 non stop vu que le nombre "à virgule" sera arrondi à l'entier le plus proche)

  10. #9
    invite5e1e868f

    Re : librairie <math.h> resultats bizarre... (atmel 8535)

    en effet, cela fonctionne, j'obtien 0 quelque soit la valeur de x, donc quelquechose fonctionne correctement au moins, mais toujours le même problème dès que j'enlève (int) devant y.Le pire c'est qu'il n'y a pas de ressemblance entre ce qui est afiché et la valeur réèlle du cos calculé.

    Exemple:

    Sur ma carte :

    cos 3 = 21807

    valeur de la calculatrice : cos 3= -0.9899924966

    Donc aucune ressemblance.Que peut-il bien se passer qui pose problème?

  11. #10
    eSb`

    Re : librairie <math.h> resultats bizarre... (atmel 8535)

    C'est simplement le fait que tu travailles avec des float (codés sur 4 octets) et qu'après tu lui demandes de les interpréter comme des int (codés sur 2 octets signés).

    Tel quel ça me paraît normal que ça foire ; ).

    Mais je ne sais pas comment on gère les float sur un LCD. Le coup du 0 fonctionne vu que tu fais un (int) sur le float. (donc tu lui dis clairement d'interpréter/de convertir le nombre en un entier, donc pour lui 0.999=0)

  12. #11
    DAUDET78

    Re : librairie <math.h> resultats bizarre... (atmel 8535)

    Une idée idiote. On fait 1000*cos(3) et après, on fait un Int sur le résultat. On devrait afficher 989 pour cos(3) et 540 pour cos(1)
    J'aime pas le Grec

  13. #12
    eSb`

    Re : librairie <math.h> resultats bizarre... (atmel 8535)

    Idée encore plus idiote :
    sprintf("0.%d",(int)(cos(x)*10 00));

    On recrée le nombre ;d. (avec le 0.)

    Mais il doit y avoir plus élégant, non ?

  14. #13
    DAUDET78

    Re : librairie <math.h> resultats bizarre... (atmel 8535)

    Citation Envoyé par eSb` Voir le message
    Idée encore plus idiote :
    Ouais ESB, mais ça marche pas ton truc si tu as un cosinus plus grand que 1
    OK, OK ...... je vais me coucher .....

    PS : par contre, si tu as un cosinus négatif, ma remarque n'est pas trop idiote (faut changer le sprintf)
    J'aime pas le Grec

  15. #14
    invite5e1e868f

    Re : librairie <math.h> resultats bizarre... (atmel 8535)

    ben assez bizarrement je trouve avec 1000*cos(3) que la partie entière vaut -653 qu'est-ce que cela veut dire??

    Je suis en train de chercher des infos sur comment afficher un float, mais pour l'instant je trouve rien. Je vous tiens au courant.

  16. #15
    invite5e1e868f

    Re : librairie <math.h> resultats bizarre... (atmel 8535)

    le code en question

    J'ai mis un incrementation de x pour tester plusieurs valeur sans avoir a reprogrammer à chaques fois:

    /****************************** ***********************
    This program was produced by the
    CodeWizardAVR V1.25.3 Professional
    Automatic Program Generator
    © Copyright 1998-2007 Pavel Haiduc, HP InfoTech s.r.l.
    http://www.hpinfotech.com

    Project :
    Version :
    Date : 12/05/2008
    Author : F4CG
    Company : F4CG
    Comments:


    Chip type : ATmega8535
    Program type : Application
    Clock frequency : 8,000000 MHz
    Memory model : Small
    External SRAM size : 0
    Data Stack size : 128
    ****************************** ***********************/

    #include <mega8535.h>

    // Alphanumeric LCD Module functions
    #asm
    .equ __lcd_port=0x15 ;PORTC
    #endasm
    #include <lcd.h>
    #include <math.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <delay.h>

    float x=3;
    float y;
    char chaine[256];

    // Declare your global variables here

    void main(void)
    {
    // Declare your local variables here

    // Input/Output Ports initialization
    // Port A initialization
    // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
    // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
    PORTA=0x00;
    DDRA=0x00;

    // Port B initialization
    // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
    // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
    PORTB=0x00;
    DDRB=0x00;

    // Port C initialization
    // Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
    // State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
    PORTC=0x00;
    DDRC=0xFF;

    // Port D initialization
    // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
    // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
    PORTD=0x00;
    DDRD=0x00;

    // Timer/Counter 0 initialization
    // Clock source: System Clock
    // Clock value: Timer 0 Stopped
    // Mode: Normal top=FFh
    // OC0 output: Disconnected
    TCCR0=0x00;
    TCNT0=0x00;
    OCR0=0x00;

    // Timer/Counter 1 initialization
    // Clock source: System Clock
    // Clock value: 8000,000 kHz
    // Mode: Normal top=FFFFh
    // OC1A output: Discon.
    // OC1B output: Discon.
    // Noise Canceler: Off
    // Input Capture on Falling Edge
    // Timer 1 Overflow Interrupt: Off
    // Input Capture Interrupt: Off
    // Compare A Match Interrupt: Off
    // Compare B Match Interrupt: Off
    TCCR1A=0x00;
    TCCR1B=0x01;
    TCNT1H=0x00;
    TCNT1L=0x00;
    ICR1H=0x00;
    ICR1L=0x00;
    OCR1AH=0x00;
    OCR1AL=0x00;
    OCR1BH=0x00;
    OCR1BL=0x00;

    // Timer/Counter 2 initialization
    // Clock source: System Clock
    // Clock value: Timer 2 Stopped
    // Mode: Normal top=FFh
    // OC2 output: Disconnected
    ASSR=0x00;
    TCCR2=0x00;
    TCNT2=0x00;
    OCR2=0x00;

    // External Interrupt(s) initialization
    // INT0: Off
    // INT1: Off
    // INT2: Off
    MCUCR=0x00;
    MCUCSR=0x00;

    // Timer(s)/Counter(s) Interrupt(s) initialization
    TIMSK=0x00;

    // Analog Comparator initialization
    // Analog Comparator: Off
    // Analog Comparator Input Capture by Timer/Counter 1: Off
    ACSR=0x80;
    SFIOR=0x00;

    // LCD module initialization
    lcd_init(16);

    while (1)
    {
    // Place your code here
    lcd_init(16);
    x=x+1;
    y=1000*cos(x) ;

    sprintf(chaine,"0.%d",(int)y);
    lcd_puts(chaine);

    delay_ms(5000) ;
    };
    }

  17. #16
    eSb`

    Re : librairie <math.h> resultats bizarre... (atmel 8535)

    Citation Envoyé par DAUDET78 Voir le message
    Ouais ESB, mais ça marche pas ton truc si tu as un cosinus plus grand que 1
    OK, OK ...... je vais me coucher .....

    PS : par contre, si tu as un cosinus négatif, ma remarque n'est pas trop idiote (faut changer le sprintf)
    J'avais même pas pensé à ça .
    Même afficher 1, ça va être foireux.

    Donc, on peut faire des tests et changer le sprintf en conséquence (mais ça me paraît lourd à mettre en place).

  18. #17
    invite5e1e868f

    Re : librairie <math.h> resultats bizarre... (atmel 8535)

    bon aparement sprintf(chaine,"%f",y); est censé marcher, mais je ne sais pas pourquoi, l'afficheur lcd reste vierge. ça fait peut-être buguer le mc?

  19. #18
    eSb`

    Re : librairie <math.h> resultats bizarre... (atmel 8535)

    Autre technique, je sais pas si c'est génial et il faudrait réfléchir un peu au niveau de la taille du char mais voilà :

    char cY[4];
    sprintf(cY,"%f",y);
    printf("%s",cY);

    Idée, on met le float dans une chaine de caractères et on affiche la chaine. ; )

  20. #19
    invite5e1e868f

    Re : librairie <math.h> resultats bizarre... (atmel 8535)

    mais c'est pas ce que je fais déjà (voir code plus haut). Et quelle est l'utilité de printf("%s",cY) ?

  21. #20
    eSb`

    Re : librairie <math.h> resultats bizarre... (atmel 8535)

    Tu as essayé ?

    En fait, sprintf recopie l'élément dans une chaîne, ici on l'utilise pour recopie ton float dans cette chaîne de caractère. On ne l'affiche pas encore.
    Mon printf permet d'afficher la chaîne de caractère contenant le float. (%s cette fois-ci), il faut sûrement la remplacer par sprintf. (j'ai testé sur un bête compilateur C chez moi et j'ai oublié de changer)

  22. #21
    invite5e1e868f

    Re : librairie <math.h> resultats bizarre... (atmel 8535)

    je viens d'éssayer et j'ai toujours cet ecran vierge, mais comment cela se fait t'il qu'il n'affiche rien?

  23. #22
    eSb`

    Re : librairie <math.h> resultats bizarre... (atmel 8535)

    Et tu arrives à afficher une bête chaîne de caractère par lcd_puts("Test") ?
    (en supposant que cette fonction fonctionne comme ça)

    Et quand tu dis que tu as essayé, tu as modifié le printf en sprintf(chaine,"%s",cY) ?
    Dernière modification par eSb` ; 15/05/2008 à 22h09.

Discussions similaires

  1. Programmation ATMEL 8535
    Par invite199eaed0 dans le forum TPE / TIPE et autres travaux
    Réponses: 1
    Dernier message: 10/07/2008, 09h03
  2. can atmega 8535
    Par invitea2f29347 dans le forum Logiciel - Software - Open Source
    Réponses: 1
    Dernier message: 25/04/2008, 13h47
  3. Equation avec des resultats bizarre
    Par invitede6f3928 dans le forum Électronique
    Réponses: 5
    Dernier message: 03/11/2005, 21h10
  4. Vref de l'ATMEGA 8535
    Par invite7e9cb89a dans le forum Électronique
    Réponses: 2
    Dernier message: 15/04/2005, 13h02
  5. AVR 8535 et Port
    Par zanzeoo dans le forum Électronique
    Réponses: 6
    Dernier message: 30/07/2004, 16h30
Découvrez nos comparatifs produits sur l'informatique et les technologies.