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.

CC2530 - subtract values

uint8 adcVal=0;
adcVal = HalAdcRead(HAL_ADC_CHANNEL_7, HAL_ADC_RESOLUTION_8); //read values ​​from the potentiometer from 0 to 127
HalLcdWriteStringValue ("Temperature:", adcVal, 10, 1); print read values ​​adcVal
adcVal=adcVal-67; //Read the value subtract the value of 67
HalLcdWriteStringValue ("Temperature:", adcVal, 10, 2); print read values ​​adcVal-67

If you read the value of 127 works OK, adcVal will be 60, but when I read the value that is less than 67, does not work as expected.
Example: If you read the value of 40, adcVal will be 228? How? 40-67=-27, so 255-27=228
If you read the value of 27, adcVal will be 215? 27-67=-40, so 255-40=215,
If you read the value of , adcVal will be 188? 0-67=-67, so 255-67=188


Whenever you read a value less than the value of 67, adcVal subtracted from the value of 255, why, please, do you know someone?

How to solve the problem?

Thanks on advance

  • You declare adcVal as uint8 and let it minus? This is the problem.

  • When the declaration int8 adcVal, the same happens, only subtracts from 6553? And when uint8 adcVal,  subtracts from 255?

  • 1. You should program like the followings;

           int int_adcVal=;

           int_adcVal=(int)adcVal-67;

          Use debugger to check if int_adcVal is correct now.

    2. The second parameter of HalLcdWriteStringValue is declared as uint16 which means it only display positive value.

  • What   int int_adcVal=;?
    You should      int int_adcVal;? without =?

  • Sorry, it is typo. Please remove ?

  • Yes, I removed.

    I did as what you said, but still does not work. :((

    If you read the value of 127 works OK, adcVal will be 60, but when I read the value that is less than 67, does not work as expected.
    Example: If you read the value of 41, adcVal will be 6551.
    If you read the value of 12, adcVal will be 6548.
    If you read the value of 0 , adcVal will be 6546.

  • If you read the value of 66, adcVal will be 6553, but If you read the value of 67, adcVal will be 0.

  • You describe "Example: If you read the value of 41, adcVal will be 6551." Do you mean it shows 6551 on LCD?

  • I had told you that in my first reply.

    The second parameter of HalLcdWriteStringValue is declared as uint16 which means it only display positive value.

  • Do you know whether it is possible to check whether the number (example 59) subtract the number 67 without printing the LCD? 59 is read value.

  • You can refer to my post at http://e2e.ti.com/support/wireless_connectivity/f/158/p/345659/1210929.aspx#1210929

  • If the value of 50 can be deducted the value of 67 and the value is 17, I will print the negative  value of the number 17 so I'm going to function HalLcdWriteStringValue add minus in print.

    Example:
    HalLcdWriteStringValue ("Temperature:-", adcVal, 10, 2);

  • Your alternative should work. Try it.

  • I tried it, but I  can not subtract  from the value 50 value 67.

  • If I can get 50-67 = 17, so in function HalLcdWriteStringValue I paint sign minus.

  • You can revise your code like

    if (adcVal>=67){

    adcVal=adcVal-67;

    HalLcdWriteStringValue ("Temperature:", adcVal, 10, 2);

    } else {

    adcVal=67-adcVal;

    HalLcdWriteStringValue ("Temperature:-", adcVal, 10, 2);

    }

  • Hello YiKai Chen,

    I have a problem with negative values.

    I use sample light/switch aplication. My aplication is simulation temperature.
    On one side, I simulate the temperature via the potentiometer as follows:
     adcVal = HalAdcRead(HAL_ADC_CHANNEL_7, HAL_ADC_RESOLUTION_8);

    And I read values 0 to 127.
    But, the temperature in real terms could be negative. Do you have any idea how to do this?

    Thanks in advance

  • According to my experience, temperature sensor doesn't work like you expect. It won't make your ADC reading negative. Would you mind to let me know which temperature sensor you use so i can give you suggestion?

  • I do not use real sensor, I need to simulate the temperature via ADC.
    On one side I need to read temperature, example -60 to 60, and I need to send temperature to another side. And that is temperature which I set. After setting, I should to send those temperature.

    For sending values, I use:
    zclSendNewClusterCommand(SAMPLESW_ENDPOINT, &zclSampleSw_DstAddr, TRUE, 0, &adcVal  );

    I read 0 to 127, and I send  those values over the upper line code, but how negative values to send?

    First, how to read negative values, and then how to send the value?

  • Function zclSendNewClusterCommand uses function  zcl_SendCommand which is defined in ZStack.

  • In read case, you need to do some mapping from ADC reading to get temperature. The ADC reading should be positive and might be mapped to negative temperature. THere is a standard ZCL command to send temperature report like the followings:

    static void zclSampleTemperatureSensor_SendTemp( void )
    {
    #ifdef ZCL_REPORT
      zclReportCmd_t *pReportCmd;

      pReportCmd = osal_mem_alloc( sizeof(zclReportCmd_t) + sizeof(zclReport_t) );
      if ( pReportCmd != NULL )
      {
        pReportCmd->numAttr = 1;
        pReportCmd->attrList[0].attrID = ATTRID_MS_TEMPERATURE_MEASURED_VALUE;
        pReportCmd->attrList[0].dataType = ZCL_DATATYPE_INT16;
        pReportCmd->attrList[0].attrData = (void *)(&zclSampleTemperatureSensor_MeasuredValue);

        zcl_SendReportCmd( SAMPLETEMPERATURESENSOR_ENDPOINT, &zclSampleTemperatureSensor_DstAddr,
                           ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT,
                           pReportCmd, ZCL_FRAME_SERVER_CLIENT_DIR, TRUE, zclSampleTemperatureSensorSeqNum++ );
      }

      osal_mem_free( pReportCmd );
    #endif  // ZCL_REPORT
    }

  • And is there any other way to send a negative value to use zcl_SendCommand?
    Everything I've done so far fall.  I used the function zcl_SendCommand to send positive values.
    zcl_SendCommand do not send negative values?

  • As you can see that pReportCmd->attrList[0].dataType = ZCL_DATATYPE_INT16, it is no problem to put negative value in zclSampleTemperatureSensor_MeasuredValue.

  • Would this be a good way?

    int8 adcVal;
     adcVal = HalAdcRead(HAL_ADC_CHANNEL_7, HAL_ADC_RESOLUTION_10); //read 0 to 255
    Int8 offset = -127;

    adcVal=adcVal+offset; //example when I read 0 then 0+(-127)=-127; when I read 255 then 255+(-127)=128
    After that I convert int8 to uint8 and I use zcl_SendCommand which defined as:

    /*********************************************************************
    * @fn zcl_SendCommand
    *
    * @brief Used to send Profile and Cluster Specific Command messages.
    *
    * NOTE: The calling application is responsible for incrementing
    * the Sequence Number.
    *
    * @param srcEp - source endpoint
    * @param destAddr - destination address
    * @param clusterID - cluster ID
    * @param cmd - command ID
    * @param specific - whether the command is Cluster Specific
    * @param direction - client/server direction of the command
    * @param disableDefaultRsp - disable Default Response command
    * @param manuCode - manufacturer code for proprietary extensions to a profile
    * @param seqNumber - identification number for the transaction
    * @param cmdFormatLen - length of the command to be sent
    * @param cmdFormat - command to be sent
    *
    * @return ZSuccess if OK
    */
    ZStatus_t zcl_SendCommand( uint8 srcEP, afAddrType_t *destAddr,
    uint16 clusterID, uint8 cmd, uint8 specific, uint8 direction,
    uint8 disableDefaultRsp, uint16 manuCode, uint8 seqNum,
    uint16 cmdFormatLen, uint8 *cmdFormat )

     

     

  • No, it is not good. Since there is already standard way to send temperature, I suggest you to use zcl_SendReportCmd.

  • Ok,

    I have another question.
    This is my  code:

    int8 adcVal;
    adcVal = HalAdcRead(HAL_ADC_CHANNEL_7, HAL_ADC_RESOLUTION_8);//read 0 to 255 values
    HalLcdWriteStringValue ("read:", adcVal, 10, HAL_LCD_LINE_1);

    int8 offset=-67;
    adcVal=adcVal+offset;

    if(adcVal<0){
    HalLcdWriteStringValue ("value:-", adcVal, 10, HAL_LCD_LINE_2);
    }
    else if(adcVal>0){
    HalLcdWriteStringValue ("value:", adcVal, 10, HAL_LCD_LINE_2);
    }

    When I read 127 over potentiometer,  after that adcVal =127 +(-67)=60 , then adcVal>0, print 60, it is ok.
    But I read example 60, adcVal = 60+(-67)=-7,but will be print -65529 WHY? You should print 7, and function HalLcdWriteStringValue with added  "-"  you should -7.


    Another example is I read 20,  adcVal = 20+(-67)=-7, then adcVal<0, after that  wil be print -65489 WHY? Becouse 20-67=-47, after that 65536-47=65489.
    How do I solve this problem?

    Thanks in advance

  • I think it still is data type casting problem of your code. Try the following code

    u int8 adcVal;
    adcVal = HalAdcRead(HAL_ADC_CHANNEL_7, HAL_ADC_RESOLUTION_8);//read 0 to 255 values
    HalLcdWriteStringValue ("read:", adcVal, 10, HAL_LCD_LINE_1);

    int8 offset=-67;
    adcVal=adcVal+offset;

    if(adcVal<67){
    HalLcdWriteStringValue ("value:-", (67-adcVal), 10, HAL_LCD_LINE_2);
    }
    else if(adcVal>=67){
    HalLcdWriteStringValue ("value:", (adcVal-67), 10, HAL_LCD_LINE_2);
    }

  • I tried it.

    When uint8 adcVal:
    When I read 0,  wil be display:
    read :0
    value:122

    read 16
    value:138

    read: 101
    value:-33

    read: 25
    value:147

    read: 127
    value:-7


    When int8 adcVal:
    When I read 0,  wil be display: 

    read :0
    value:-134

    read: 16
    value:-118

    read :21
    value:-113

    read: 101
    value:-33

    read: 25
    value:147

    read: 127
    value:-7

    It is not OK, you should:
    read:0
    value:-127

    read: 40
    value: -27

    read: 127
    value: 60

    Thanks in advance

  • I made a mistake:
    When int8:

    read:25
    value: -109

  • Do you replace your whole source code with my revisions or you just change int8 adcVal to uint8 adcVal?

  • I put int8 adcVal; 
    Just in my_event where it's happening.

  • Please check my revision again. I modify not  only uint8.