This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CCS/TMS320F28377S: Wrong results from SIN() and COS() functions

Part Number: TMS320F28377S

Tool/software: Code Composer Studio

Hello, please help me with the following issue:

In my program I use the sin() and cos() functions from the trigonometric math unit of the TMS320F28377S processor.

I am surprised to see that the values given by the processor are wrong. To illustrate that, I obtained many values in order to plot the results from the functions, as shown below, along with the true values calculated with a spreadsheet:

Results are kind of shifted (different shift for sin and cos), but other times they are just plain wrong.

I saw a post that may be related to this issue: TMS320F28069FPFPQ have error in calculating sin(x) and cos(x)

I am using the 2837xS_Generic_FLASH_lnk.cmd file.

What is wrong? how can it be fixed?

Thank you.

  • Hello,

    Do you mind sharing a snippet of code that you used to get the numbers you have graphed above?

    Whitney

  • Hello, thanks for reading my post.

    Below is the piece of my code that uses SIN and COS, it is called on each ADC interrupt. "GuardarEnVector_AuxFloat1" is a small function I use to store the results form the SIN and COS calculations inside a fixed size array:
    ********************************************************************************
    void CalculoSenoCoseno(void)
    {
    motor_sin = sin(motor_theta) ;
    motor_cos = cos(motor_theta) ;
    GuardarEnVector_AuxFloat1(1234567.8) ;
    GuardarEnVector_AuxFloat1(motor_theta) ;
    GuardarEnVector_AuxFloat1(motor_sin) ;
    GuardarEnVector_AuxFloat1(motor_cos) ;
    }
    ********************************************************************************

    The disassembly view:


    The corresponding assembler from the .lst file:

    ********************************************************************************
    1629 ;***************************************************************
    1630 ;* FNAME: _CalculoSenoCoseno FR SIZE: 0 *
    1631 ;* *
    1632 ;* FUNCTION ENVIRONMENT *
    1633 ;* *
    1634 ;* FUNCTION PROPERTIES *
    1635 ;* 0 Parameter, 0 Auto, 0 SOE *
    1636 ;***************************************************************
    1637
    1638 0000018e _CalculoSenoCoseno:
    1639 .dwcfi cfa_offset, -2
    1640 .dwcfi save_reg_to_mem, 26, 0
    1641 .dwpsn file "../aa_motor.c",line 271,column 5,is_stmt,isa 0
    1642 0000018e 761F- MOVW DP,#_motor_theta ; [CPU_U]
    0000018f 0000
    1643 00000190 E2AF- MOV32 R0H,@_motor_theta ; [CPU_] |271|
    00000191 002E
    1644 $C$DW$108 .dwtag DW_TAG_TI_branch
    1645 .dwattr $C$DW$108, DW_AT_low_pc(0x00)
    1646 .dwattr $C$DW$108, DW_AT_name("___relaxed_sin")
    1647 .dwattr $C$DW$108, DW_AT_TI_call
    1648
    TMS320C2000 Assembler PC v16.9.1 Thu Oct 05 09:28:53 2017

    Tools Copyright (c) 1996-2015 Texas Instruments Incorporated
    C:\Users\DOCTOR~1\AppData\Local\Temp\0966410 PAGE 35

    1649 00000192 7640' LCR #___relaxed_sin ; [CPU_] |271|
    00000193 0000
    1650 ; call occurs [#___relaxed_sin] ; [] |271|
    1651 00000194 E203- MOV32 @_motor_sin,R0H ; [CPU_] |271|
    00000195 000C
    1652 .dwpsn file "../aa_motor.c",line 272,column 5,is_stmt,isa 0
    1653 00000196 E2AF- MOV32 R0H,@_motor_theta ; [CPU_] |272|
    00000197 002E
    1654 $C$DW$109 .dwtag DW_TAG_TI_branch
    1655 .dwattr $C$DW$109, DW_AT_low_pc(0x00)
    1656 .dwattr $C$DW$109, DW_AT_name("___relaxed_cos")
    1657 .dwattr $C$DW$109, DW_AT_TI_call
    1658
    1659 00000198 7640' LCR #___relaxed_cos ; [CPU_] |272|
    00000199 000B
    1660 ; call occurs [#___relaxed_cos] ; [] |272|
    1661 0000019a E203- MOV32 @_motor_cos,R0H ; [CPU_] |272|
    0000019b 0012
    1662 .dwpsn file "../aa_motor.c",line 273,column 5,is_stmt,isa 0
    1663 0000019c E802 MOVIZ R0H,#18838 ; [CPU_] |273|
    0000019d 4CB0
    1664 0000019e E80D MOVXI R0H,#46142 ; [CPU_] |273|
    0000019f A1F0
    1665 $C$DW$110 .dwtag DW_TAG_TI_branch
    1666 .dwattr $C$DW$110, DW_AT_low_pc(0x00)
    1667 .dwattr $C$DW$110, DW_AT_name("_GuardarEnVector_AuxFloat1")
    1668 .dwattr $C$DW$110, DW_AT_TI_call
    1669
    1670 000001a0 7640! LCR #_GuardarEnVector_AuxFloat1 ; [CPU_] |273|
    000001a1 0000
    1671 ; call occurs [#_GuardarEnVector_AuxFloat1] ; [] |273|
    1672 .dwpsn file "../aa_motor.c",line 274,column 5,is_stmt,isa 0
    1673 000001a2 761F- MOVW DP,#_motor_theta ; [CPU_U]
    000001a3 0000
    1674 000001a4 E2AF- MOV32 R0H,@_motor_theta ; [CPU_] |274|
    000001a5 002E
    1675 $C$DW$111 .dwtag DW_TAG_TI_branch
    1676 .dwattr $C$DW$111, DW_AT_low_pc(0x00)
    1677 .dwattr $C$DW$111, DW_AT_name("_GuardarEnVector_AuxFloat1")
    1678 .dwattr $C$DW$111, DW_AT_TI_call
    1679
    1680 000001a6 7640! LCR #_GuardarEnVector_AuxFloat1 ; [CPU_] |274|
    000001a7 0000
    1681 ; call occurs [#_GuardarEnVector_AuxFloat1] ; [] |274|
    1682 .dwpsn file "../aa_motor.c",line 275,column 5,is_stmt,isa 0
    1683 000001a8 761F- MOVW DP,#_motor_sin ; [CPU_U]
    000001a9 0000
    1684 000001aa E2AF- MOV32 R0H,@_motor_sin ; [CPU_] |275|
    000001ab 000C
    1685 $C$DW$112 .dwtag DW_TAG_TI_branch
    1686 .dwattr $C$DW$112, DW_AT_low_pc(0x00)
    1687 .dwattr $C$DW$112, DW_AT_name("_GuardarEnVector_AuxFloat1")
    1688 .dwattr $C$DW$112, DW_AT_TI_call
    1689
    1690 000001ac 7640! LCR #_GuardarEnVector_AuxFloat1 ; [CPU_] |275|
    TMS320C2000 Assembler PC v16.9.1 Thu Oct 05 09:28:53 2017

    Tools Copyright (c) 1996-2015 Texas Instruments Incorporated
    C:\Users\DOCTOR~1\AppData\Local\Temp\0966410 PAGE 36

    000001ad 0000
    1691 ; call occurs [#_GuardarEnVector_AuxFloat1] ; [] |275|
    1692 .dwpsn file "../aa_motor.c",line 276,column 5,is_stmt,isa 0
    1693 000001ae 761F- MOVW DP,#_motor_cos ; [CPU_U]
    000001af 0000
    1694 000001b0 E2AF- MOV32 R0H,@_motor_cos ; [CPU_] |276|
    000001b1 0012
    1695 $C$DW$113 .dwtag DW_TAG_TI_branch
    1696 .dwattr $C$DW$113, DW_AT_low_pc(0x00)
    1697 .dwattr $C$DW$113, DW_AT_name("_GuardarEnVector_AuxFloat1")
    1698 .dwattr $C$DW$113, DW_AT_TI_call
    1699
    1700 000001b2 7640! LCR #_GuardarEnVector_AuxFloat1 ; [CPU_] |276|
    000001b3 0000
    1701 ; call occurs [#_GuardarEnVector_AuxFloat1] ; [] |276|
    1702 .dwpsn file "../aa_motor.c",line 277,column 1,is_stmt,isa 0
    1703 $C$DW$114 .dwtag DW_TAG_TI_branch
    1704 .dwattr $C$DW$114, DW_AT_low_pc(0x00)
    1705 .dwattr $C$DW$114, DW_AT_TI_return
    1706
    1707 000001b4 0006 LRETR ; [CPU_]
    1708 ; return occurs ; []
    1709 .dwattr $C$DW$107, DW_AT_TI_end_file("../aa_motor.c")
    1710 .dwattr $C$DW$107, DW_AT_TI_end_line(0x115)
    1711 .dwattr $C$DW$107, DW_AT_TI_end_column(0x01)
    1712 .dwendentry
    1713 .dwendtag $C$DW$107
    ********************************************************************************

  • Have you placed a breakpoint in your function and read the motor_sin and motor_cos values in the Expressions window in CCS? Do they appear to be calculated correctly when you do that or are they off also incorrect like in your graph?

    Whitney
  • ...I see now, I think the values are correct. I believe I was misinterpreting the information shown by CodeComponerStudio. When I place a breakpoint or suspend the execution of the program, the values I collect in my code, or that I see in the Expressions table in CCS, are fine and correspond to the true values of SIN and COS functions. But when I stop refreshing the variables (but the program keeps running), the values shown are -I guess- from different moments in time, so they seem wrong, since they don't match the value of the argument given by CCS at that moment.

    I understand that all the values can not be show in real time, at least for values updating at kilohertz rates. Now it seems very obvious, and I feel a little silly, but at that time I was assuming it worked that way...

    Thanks for the time you have spent helping me.