logo elektroda
logo elektroda
X
logo elektroda
Adblock/uBlockOrigin/AdGuard mogą powodować znikanie niektórych postów z powodu nowej reguły.

"tłumaczenie" procedury C na ASM

maciejkos 06 Lis 2009 14:17 1403 7
  • #1 7222992
    maciejkos
    Poziom 10  
    Zbudowałem układ (atmega8 + czujnik TSL2561) którego zadaniem jest pomiar natężenia światła. Komunikacja z czujnikiem działa dobrze, otrzymuję wartości dwóch 16 bitowych kanałów - w zasadzie czterech 8 bitowych rejestrów - DATA0LOW, DATA0HIGH, DATA1LOW i DATA1HIGH. Teraz muszę przetworzyć wartości tych rejestrów aby otrzymać natężenie światła w luksach. Producent czujnika w jego datasheet'cie dostarczył procedury którymi można to zrobić. Niestety jest ona napisana w c(o którym mam bladziutkie pojęcie) a ja cały program obsługi mam napisany w assemblerze. Czy ktoś byłby w stanie pomóc "przetłumaczyć" taką procedurę albo chociaż dać jakieś wskazówki jak się za to zabrać?

    Dołączam skopiowaną z datasheeta procedurę. Dodam jeszcze, że są w niej elementy na pewno zbędne - czujnik jaki mam jest w obudowie T(jest jeszcze obudowa CS) a także nie potrzebne jest skalowanie - wartości GAIN oraz czas integracji są ustawione domyślnie (odpowiednio 16x i 402 ms)

    //****************************************************************************
    //
    // Copyright _ 2004-2005 TAOS, Inc.
    //
    // THIS CODE AND INFORMATION IS PROVIDED ”AS IS” WITHOUT WARRANTY OF ANY
    // KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
    // IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
    // PURPOSE.
    //
    // Module Name:
    // lux.cpp
    //
    //****************************************************************************
    #define LUX_SCALE 14 // scale by 2^14
    #define RATIO_SCALE 9 // scale ratio by 2^9
    //---------------------------------
    // Integration time scaling factors
    //---------------------------------
    #define CH_SCALE 10 // scale channel values by 2^10
    #define CHSCALE_TINT0 0x7517 // 322/11 * 2^CH_SCALE
    #define CHSCALE_TINT1 0x0fe7 // 322/81 * 2^CH_SCALE
    //---------------------------------
    // T Package coefficients
    //---------------------------------
    // For Ch1/Ch0=0.00 to 0.50
    // Lux/Ch0=0.0304-0.062*((Ch1/Ch0)^1.4)
    // piecewise approximation
    // For Ch1/Ch0=0.00 to 0.125:
    // Lux/Ch0=0.0304-0.0272*(Ch1/Ch0)
    //
    // For Ch1/Ch0=0.125 to 0.250:
    // Lux/Ch0=0.0325-0.0440*(Ch1/Ch0)
    //
    // For Ch1/Ch0=0.250 to 0.375:
    // Lux/Ch0=0.0351-0.0544*(Ch1/Ch0)
    //
    // For Ch1/Ch0=0.375 to 0.50:
    // Lux/Ch0=0.0381-0.0624*(Ch1/Ch0)
    //
    // For Ch1/Ch0=0.50 to 0.61:
    // Lux/Ch0=0.0224-0.031*(Ch1/Ch0)
    //
    // For Ch1/Ch0=0.61 to 0.80:
    // Lux/Ch0=0.0128-0.0153*(Ch1/Ch0)
    //
    // For Ch1/Ch0=0.80 to 1.30:
    // Lux/Ch0=0.00146-0.00112*(Ch1/Ch0)
    //
    // For Ch1/Ch0>1.3:
    // Lux/Ch0=0
    //-----------------------------------
    #define K1T 0x0040 // 0.125 * 2^RATIO_SCALE
    #define B1T 0x01f2 // 0.0304 * 2^LUX_SCALE
    #define M1T 0x01be // 0.0272 * 2^LUX_SCALE
    #define K2T 0x0080 // 0.250 * 2^RATIO_SCALE
    #define B2T 0x0214 // 0.0325 * 2^LUX_SCALE
    #define M2T 0x02d1 // 0.0440 * 2^LUX_SCALE
    #define K3T 0x00c0 // 0.375 * 2^RATIO_SCALE
    #define B3T 0x023f // 0.0351 * 2^LUX_SCALE
    #define M3T 0x037b // 0.0544 * 2^LUX_SCALE
    #define K4T 0x0100 // 0.50 * 2^RATIO_SCALE
    #define B4T 0x0270 // 0.0381 * 2^LUX_SCALE
    #define M4T 0x03fe // 0.0624 * 2^LUX_SCALE
    #define K5T 0x0138 // 0.61 * 2^RATIO_SCALE
    #define B5T 0x016f // 0.0224 * 2^LUX_SCALE
    #define M5T 0x01fc // 0.0310 * 2^LUX_SCALE
    #define K6T 0x019a // 0.80 * 2^RATIO_SCALE
    #define B6T 0x00d2 // 0.0128 * 2^LUX_SCALE
    #define M6T 0x00fb // 0.0153 * 2^LUX_SCALE
    #define K7T 0x029a // 1.3 * 2^RATIO_SCALE
    #define B7T 0x0018 // 0.00146 * 2^LUX_SCALE
    #define M7T 0x0012 // 0.00112 * 2^LUX_SCALE
    #define K8T 0x029a // 1.3 * 2^RATIO_SCALE
    #define B8T 0x0000 // 0.000 * 2^LUX_SCALE
    #define M8T 0x0000 // 0.000 * 2^LUX_SCALE
    //-----------------------------
    // CS package coefficients
    //---------------------------
    // For 0 <= Ch1/Ch0 <= 0.52
    // Lux/Ch0 = 0.0315-0.0593*((Ch1/Ch0)^1.4)
    // piecewise approximation
    // For 0 <= Ch1/Ch0 <= 0.13
    // Lux/Ch0 = 0.0315-0.0262*(Ch1/Ch0)
    // For 0.13 <= Ch1/Ch0 <= 0.26
    // Lux/Ch0 = 0.0337-0.0430*(Ch1/Ch0)
    // For 0.26 <= Ch1/Ch0 <= 0.39
    // Lux/Ch0 = 0.0363-0.0529*(Ch1/Ch0)
    // For 0.39 <= Ch1/Ch0 <= 0.52
    // Lux/Ch0 = 0.0392-0.0605*(Ch1/Ch0)
    // For 0.52 < Ch1/Ch0 <= 0.65
    // Lux/Ch0 = 0.0229-0.0291*(Ch1/Ch0)
    // For 0.65 < Ch1/Ch0 <= 0.80
    // Lux/Ch0 = 0.00157-0.00180*(Ch1/Ch0)
    // For 0.80 < Ch1/Ch0 <= 1.30
    // Lux/Ch0 = 0.00338-0.00260*(Ch1/Ch0)
    // For Ch1/Ch0 > 1.30
    // Lux = 0
    //----------------------------------
    #define K1C 0x0043 // 0.130 * 2^RATIO_SCALE
    #define B1C 0x0204 // 0.0315 * 2^LUX_SCALE
    #define M1C 0x01ad // 0.0262 * 2^LUX_SCALE
    #define K2C 0x0085 // 0.260 * 2^RATIO_SCALE
    #define B2C 0x0228 // 0.0337 * 2^LUX_SCALE
    #define M2C 0x02c1 // 0.0430 * 2^LUX_SCALE
    #define K3C 0x00c8 // 0.390 * 2^RATIO_SCALE
    #define B3C 0x0253 // 0.0363 * 2^LUX_SCALE
    #define M3C 0x0363 // 0.0529 * 2^LUX_SCALE
    #define K4C 0x010a // 0.520 * 2^RATIO_SCALE
    #define B4C 0x0282 // 0.0392 * 2^LUX_SCALE
    #define M4C 0x03df // 0.0605 * 2^LUX_SCALE
    #define K5C 0x014d // 0.65 * 2^RATIO_SCALE
    #define B5C 0x0177 // 0.0229 * 2^LUX_SCALE
    #define M5C 0x01dd // 0.0291 * 2^LUX_SCALE
    #define K6C 0x019a // 0.80 * 2^RATIO_SCALE
    #define B6C 0x0101 // 0.0157 * 2^LUX_SCALE
    #define M6C 0x0127 // 0.0180 * 2^LUX_SCALE
    #define K7C 0x029a // 1.3 * 2^RATIO_SCALE
    #define B7C 0x0037 // 0.00338 * 2^LUX_SCALE
    #define M7C 0x002b // 0.00260 * 2^LUX_SCALE
    #define K8C 0x029a // 1.3 * 2^RATIO_SCALE
    #define B8C 0x0000 // 0.000 * 2^LUX_SCALE
    #define M8C 0x0000 // 0.000 * 2^LUX_SCALE
    // lux equation approximation without floating point calculations
    //////////////////////////////////////////////////////////////////////////////
    // Routine: unsigned int CalculateLux(unsigned int ch0, unsigned int ch0, int iType)
    //
    // Description: Calculate the approximate illuminance (lux) given the raw
    // channel values of the TSL2560. The equation if implemented
    // as a piece-wise linear approximation.
    //
    // Arguments: unsigned int iGain - gain, where 0:1X, 1:16X
    // unsigned int tInt - integration time, where 0:13.7mS, 1:100mS, 2:402mS,
    // 3:Manual
    // unsigned int ch0 - raw channel value from channel 0 of TSL2560
    // unsigned int ch1 - raw channel value from channel 1 of TSL2560
    // unsigned int iType - package type (T or CS)
    //
    // Return: unsigned int - the approximate illuminance (lux)
    //
    //////////////////////////////////////////////////////////////////////////////
    unsigned int CalculateLux(unsigned int iGain, unsigned int tInt, unsigned int ch0,
    unsigned int ch1, int iType)
    {
    //---------------------------------
    // first, scale the channel values depending on the gain and integration time
    // 16X, 402mS is nominal.
    // scale if integration time is NOT 402 msec
    unsigned long chScale;
    unsigned long channel1;
    unsigned long channel0;
    switch (tInt)
    {
    case 0: // 13.7 msec
    chScale = CHSCALE_TINT0;
    break;
    case 1: // 101 msec
    chScale = CHSCALE_TINT1;
    break;
    default: // assume no scaling
    chScale = (1 << CH_SCALE);
    break;
    }
    // scale if gain is NOT 16X
    if (!iGain) chScale = chScale << 4; // scale 1X to 16X
    // scale the channel values
    channel0 = (ch0 * chScale) >> CH_SCALE;
    channel1 = (ch1 * chScale) >> CH_SCALE;
    //-----------------------------------
    // find the ratio of the channel values (Channel1/Channel0)
    // protect against divide by zero
    unsigned long ratio1 = 0;
    if (channel0 != 0) ratio1 = (channel1 << (RATIO_SCALE+1)) / channel0;
    // round the ratio value
    unsigned long ratio = (ratio1 + 1) >> 1;
    // is ratio <= eachBreak ?
    unsigned int b, m;
    switch (iType)
    {
    case 0: // T package
    if ((ratio >= 0) && (ratio <= K1T))
    {b=B1T; m=M1T;}
    else if (ratio <= K2T)
    {b=B2T; m=M2T;}
    else if (ratio <= K3T)
    {b=B3T; m=M3T;}
    else if (ratio <= K4T)
    {b=B4T; m=M4T;}
    else if (ratio <= K5T)
    {b=B5T; m=M5T;}
    else if (ratio <= K6T)
    {b=B6T; m=M6T;}
    else if (ratio <= K7T)
    {b=B7T; m=M7T;}
    else if (ratio > K8T)
    {b=B8T; m=M8T;}
    break;
    case 1:// CS package
    if ((ratio >= 0) && (ratio <= K1C))
    {b=B1C; m=M1C;}
    else if (ratio <= K2C)
    {b=B2C; m=M2C;}
    else if (ratio <= K3C)
    {b=B3C; m=M3C;}
    else if (ratio <= K4C)
    {b=B4C; m=M4C;}
    else if (ratio <= K5C)
    {b=B5C; m=M5C;}
    else if (ratio <= K6C)
    {b=B6C; m=M6C;}
    else if (ratio <= K7C)
    {b=B7C; m=M7C;}
    else if (ratio > K8C)
    {b=B8C; m=M8C;}
    break;
    }
    unsigned long temp;
    temp = ((channel0 * b) - (channel1 * m));
    // do not allow negative lux value
    if (temp < 0) temp = 0;
    // round lsb (2^(LUX_SCALE_1))
    temp += (1 << (LUX_SCALE_1));
    // strip off fractional portion
    unsigned long lux = temp >> LUX_SCALE;
    return(lux);
    }
    
  • #2 7223315
    august_
    Poziom 27  
    Rozrysuj sobie krok po kroku algorytm na podstawie tego kodu - będzie łatwiej.

    A najprościej to podejrzeć program memory i zobaczyć jak kompilator C przekształcił kod do asemblera.
  • #3 7223681
    tmf
    VIP Zasłużony dla elektroda
    Stworz projekt w AVR Studio, zaznacz opcje 'generate List file' i dostaniesz plik z tlumaczeniem c na assembler wraz z komentarzem ktora linia jak zostala przetlumaczona.
  • #4 7226549
    maciejkos
    Poziom 10  
    tmf napisał:
    Stworz projekt w AVR Studio, zaznacz opcje 'generate List file' i dostaniesz plik z tlumaczeniem c na assembler wraz z komentarzem ktora linia jak zostala przetlumaczona.


    tak też zrobiłem ja poradziłeś
    dodałem w tej procedurze tylko int main(void){........}
    no i w debuggerze nie za wiele się dzieje, więc stąd plik .lss też nie ma za wiele sensu.


    
    konwersjac.elf:     file format elf32-avr
    
    Sections:
    Idx Name          Size      VMA       LMA       File off  Algn
      0 .text         00000042  00000000  00000000  00000054  2**1
                      CONTENTS, ALLOC, LOAD, READONLY, CODE
      1 .debug_aranges 00000020  00000000  00000000  00000096  2**0
                      CONTENTS, READONLY, DEBUGGING
      2 .debug_pubnames 0000001b  00000000  00000000  000000b6  2**0
                      CONTENTS, READONLY, DEBUGGING
      3 .debug_info   00000078  00000000  00000000  000000d1  2**0
                      CONTENTS, READONLY, DEBUGGING
      4 .debug_abbrev 0000005b  00000000  00000000  00000149  2**0
                      CONTENTS, READONLY, DEBUGGING
      5 .debug_line   00000064  00000000  00000000  000001a4  2**0
                      CONTENTS, READONLY, DEBUGGING
      6 .debug_frame  00000020  00000000  00000000  00000208  2**2
                      CONTENTS, READONLY, DEBUGGING
      7 .debug_str    00000073  00000000  00000000  00000228  2**0
                      CONTENTS, READONLY, DEBUGGING
    
    Disassembly of section .text:
    
    00000000 <__vectors>:
       0:	12 c0       	rjmp	.+36     	; 0x26 <__ctors_end>
       2:	19 c0       	rjmp	.+50     	; 0x36 <__bad_interrupt>
       4:	18 c0       	rjmp	.+48     	; 0x36 <__bad_interrupt>
       6:	17 c0       	rjmp	.+46     	; 0x36 <__bad_interrupt>
       8:	16 c0       	rjmp	.+44     	; 0x36 <__bad_interrupt>
       a:	15 c0       	rjmp	.+42     	; 0x36 <__bad_interrupt>
       c:	14 c0       	rjmp	.+40     	; 0x36 <__bad_interrupt>
       e:	13 c0       	rjmp	.+38     	; 0x36 <__bad_interrupt>
      10:	12 c0       	rjmp	.+36     	; 0x36 <__bad_interrupt>
      12:	11 c0       	rjmp	.+34     	; 0x36 <__bad_interrupt>
      14:	10 c0       	rjmp	.+32     	; 0x36 <__bad_interrupt>
      16:	0f c0       	rjmp	.+30     	; 0x36 <__bad_interrupt>
      18:	0e c0       	rjmp	.+28     	; 0x36 <__bad_interrupt>
      1a:	0d c0       	rjmp	.+26     	; 0x36 <__bad_interrupt>
      1c:	0c c0       	rjmp	.+24     	; 0x36 <__bad_interrupt>
      1e:	0b c0       	rjmp	.+22     	; 0x36 <__bad_interrupt>
      20:	0a c0       	rjmp	.+20     	; 0x36 <__bad_interrupt>
      22:	09 c0       	rjmp	.+18     	; 0x36 <__bad_interrupt>
      24:	08 c0       	rjmp	.+16     	; 0x36 <__bad_interrupt>
    
    00000026 <__ctors_end>:
      26:	11 24       	eor	r1, r1
      28:	1f be       	out	0x3f, r1	; 63
      2a:	cf e5       	ldi	r28, 0x5F	; 95
      2c:	d4 e0       	ldi	r29, 0x04	; 4
      2e:	de bf       	out	0x3e, r29	; 62
      30:	cd bf       	out	0x3d, r28	; 61
      32:	02 d0       	rcall	.+4      	; 0x38 <main>
      34:	04 c0       	rjmp	.+8      	; 0x3e <_exit>
    
    00000036 <__bad_interrupt>:
      36:	e4 cf       	rjmp	.-56     	; 0x0 <__vectors>
    
    00000038 <main>:
    temp += (1 << (LUX_SCALE-1));
    // strip off fractional portion
    unsigned long lux = temp >> LUX_SCALE;
    return(lux);
    }
    }
      38:	80 e0       	ldi	r24, 0x00	; 0
      3a:	90 e0       	ldi	r25, 0x00	; 0
      3c:	08 95       	ret
    
    0000003e <_exit>:
      3e:	f8 94       	cli
    
    00000040 <__stop_program>:
      40:	ff cf       	rjmp	.-2      	; 0x40 <__stop_program>
    




    cały program jaki kompiluję wklejam poniżej. coś tam muszę jeszcze dopisać (trzeba jeszcze na pewno zdefiniować parametry(iType, tInt oraz iGain) ale nie za bardzo wiem jak....



    //****************************************************************************
    //
    // Copyright _ 2004-2005 TAOS, Inc.
    //
    // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
    // KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
    // IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
    // PURPOSE.
    //
    // Module Name:
    // lux.cpp
    //
    //****************************************************************************
    #define LUX_SCALE 14 // scale by 2^14
    #define RATIO_SCALE 9 // scale ratio by 2^9
    //---------------------------------
    // Integration time scaling factors
    //---------------------------------
    #define CH_SCALE 10 // scale channel values by 2^10
    #define CHSCALE_TINT0 0x7517 // 322/11 * 2^CH_SCALE
    #define CHSCALE_TINT1 0x0fe7 // 322/81 * 2^CH_SCALE
    //---------------------------------
    // T Package coefficients
    //---------------------------------
    // For Ch1/Ch0=0.00 to 0.50
    // Lux/Ch0=0.0304-0.062*((Ch1/Ch0)^1.4)
    // piecewise approximation
    // For Ch1/Ch0=0.00 to 0.125:
    // Lux/Ch0=0.0304-0.0272*(Ch1/Ch0)
    //
    // For Ch1/Ch0=0.125 to 0.250:
    // Lux/Ch0=0.0325-0.0440*(Ch1/Ch0)
    //
    // For Ch1/Ch0=0.250 to 0.375:
    // Lux/Ch0=0.0351-0.0544*(Ch1/Ch0)
    //
    // For Ch1/Ch0=0.375 to 0.50:
    // Lux/Ch0=0.0381-0.0624*(Ch1/Ch0)
    //
    // For Ch1/Ch0=0.50 to 0.61:
    // Lux/Ch0=0.0224-0.031*(Ch1/Ch0)
    //
    // For Ch1/Ch0=0.61 to 0.80:
    // Lux/Ch0=0.0128-0.0153*(Ch1/Ch0)
    //
    // For Ch1/Ch0=0.80 to 1.30:
    // Lux/Ch0=0.00146-0.00112*(Ch1/Ch0)
    //
    // For Ch1/Ch0>1.3:
    // Lux/Ch0=0
    //-----------------------------------
    #define K1T 0x0040 // 0.125 * 2^RATIO_SCALE
    #define B1T 0x01f2 // 0.0304 * 2^LUX_SCALE
    #define M1T 0x01be // 0.0272 * 2^LUX_SCALE
    #define K2T 0x0080 // 0.250 * 2^RATIO_SCALE
    #define B2T 0x0214 // 0.0325 * 2^LUX_SCALE
    #define M2T 0x02d1 // 0.0440 * 2^LUX_SCALE
    #define K3T 0x00c0 // 0.375 * 2^RATIO_SCALE
    #define B3T 0x023f // 0.0351 * 2^LUX_SCALE
    #define M3T 0x037b // 0.0544 * 2^LUX_SCALE
    #define K4T 0x0100 // 0.50 * 2^RATIO_SCALE
    #define B4T 0x0270 // 0.0381 * 2^LUX_SCALE
    #define M4T 0x03fe // 0.0624 * 2^LUX_SCALE
    #define K5T 0x0138 // 0.61 * 2^RATIO_SCALE
    #define B5T 0x016f // 0.0224 * 2^LUX_SCALE
    #define M5T 0x01fc // 0.0310 * 2^LUX_SCALE
    #define K6T 0x019a // 0.80 * 2^RATIO_SCALE
    #define B6T 0x00d2 // 0.0128 * 2^LUX_SCALE
    #define M6T 0x00fb // 0.0153 * 2^LUX_SCALE
    #define K7T 0x029a // 1.3 * 2^RATIO_SCALE
    #define B7T 0x0018 // 0.00146 * 2^LUX_SCALE
    #define M7T 0x0012 // 0.00112 * 2^LUX_SCALE
    #define K8T 0x029a // 1.3 * 2^RATIO_SCALE
    #define B8T 0x0000 // 0.000 * 2^LUX_SCALE
    #define M8T 0x0000 // 0.000 * 2^LUX_SCALE
    //-----------------------------
    // CS package coefficients
    //---------------------------
    // For 0 <= Ch1/Ch0 <= 0.52
    // Lux/Ch0 = 0.0315-0.0593*((Ch1/Ch0)^1.4)
    // piecewise approximation
    // For 0 <= Ch1/Ch0 <= 0.13
    // Lux/Ch0 = 0.0315-0.0262*(Ch1/Ch0)
    // For 0.13 <= Ch1/Ch0 <= 0.26
    // Lux/Ch0 = 0.0337-0.0430*(Ch1/Ch0)
    // For 0.26 <= Ch1/Ch0 <= 0.39
    // Lux/Ch0 = 0.0363-0.0529*(Ch1/Ch0)
    // For 0.39 <= Ch1/Ch0 <= 0.52
    // Lux/Ch0 = 0.0392-0.0605*(Ch1/Ch0)
    // For 0.52 < Ch1/Ch0 <= 0.65
    // Lux/Ch0 = 0.0229-0.0291*(Ch1/Ch0)
    // For 0.65 < Ch1/Ch0 <= 0.80
    // Lux/Ch0 = 0.00157-0.00180*(Ch1/Ch0)
    // For 0.80 < Ch1/Ch0 <= 1.30
    // Lux/Ch0 = 0.00338-0.00260*(Ch1/Ch0)
    // For Ch1/Ch0 > 1.30
    // Lux = 0
    //----------------------------------
    #define K1C 0x0043 // 0.130 * 2^RATIO_SCALE
    #define B1C 0x0204 // 0.0315 * 2^LUX_SCALE
    #define M1C 0x01ad // 0.0262 * 2^LUX_SCALE
    #define K2C 0x0085 // 0.260 * 2^RATIO_SCALE
    #define B2C 0x0228 // 0.0337 * 2^LUX_SCALE
    #define M2C 0x02c1 // 0.0430 * 2^LUX_SCALE
    #define K3C 0x00c8 // 0.390 * 2^RATIO_SCALE
    #define B3C 0x0253 // 0.0363 * 2^LUX_SCALE
    #define M3C 0x0363 // 0.0529 * 2^LUX_SCALE
    #define K4C 0x010a // 0.520 * 2^RATIO_SCALE
    #define B4C 0x0282 // 0.0392 * 2^LUX_SCALE
    #define M4C 0x03df // 0.0605 * 2^LUX_SCALE
    #define K5C 0x014d // 0.65 * 2^RATIO_SCALE
    #define B5C 0x0177 // 0.0229 * 2^LUX_SCALE
    #define M5C 0x01dd // 0.0291 * 2^LUX_SCALE
    #define K6C 0x019a // 0.80 * 2^RATIO_SCALE
    #define B6C 0x0101 // 0.0157 * 2^LUX_SCALE
    #define M6C 0x0127 // 0.0180 * 2^LUX_SCALE
    #define K7C 0x029a // 1.3 * 2^RATIO_SCALE
    #define B7C 0x0037 // 0.00338 * 2^LUX_SCALE
    #define M7C 0x002b // 0.00260 * 2^LUX_SCALE
    #define K8C 0x029a // 1.3 * 2^RATIO_SCALE
    #define B8C 0x0000 // 0.000 * 2^LUX_SCALE
    #define M8C 0x0000 // 0.000 * 2^LUX_SCALE
    
    int main (void){
    // lux equation approximation without floating point calculations
    //////////////////////////////////////////////////////////////////////////////
    // Routine: unsigned int CalculateLux(unsigned int ch0, unsigned int ch0, int iType)
    //
    // Description: Calculate the approximate illuminance (lux) given the raw
    // channel values of the TSL2560. The equation if implemented
    // as a piece-wise linear approximation.
    //
    // Arguments: unsigned int iGain - gain, where 0:1X, 1:16X
    // unsigned int tInt - integration time, where 0:13.7mS, 1:100mS, 2:402mS,
    // 3:Manual
    // unsigned int ch0 - raw channel value from channel 0 of TSL2560
    // unsigned int ch1 - raw channel value from channel 1 of TSL2560
    // unsigned int iType - package type (T or CS)
    //
    // Return: unsigned int - the approximate illuminance (lux)
    //
    //////////////////////////////////////////////////////////////////////////////
    unsigned int CalculateLux(unsigned int iGain, unsigned int tInt, unsigned int ch0,
    unsigned int ch1, int iType)
    {
    //---------------------------------
    // first, scale the channel values depending on the gain and integration time
    // 16X, 402mS is nominal.
    // scale if integration time is NOT 402 msec
    unsigned long chScale;
    unsigned long channel1;
    unsigned long channel0;
    switch (tInt)
    {
    case 0: // 13.7 msec
    chScale = CHSCALE_TINT0;
    break;
    case 1: // 101 msec
    chScale = CHSCALE_TINT1;
    break;
    default: // assume no scaling
    chScale = (1 << CH_SCALE);
    break;
    }
    // scale if gain is NOT 16X
    if (!iGain) chScale = chScale << 4; // scale 1X to 16X
    // scale the channel values
    channel0 = (ch0 * chScale) >> CH_SCALE;
    channel1 = (ch1 * chScale) >> CH_SCALE;
    //-----------------------------------
    // find the ratio of the channel values (Channel1/Channel0)
    // protect against divide by zero
    unsigned long ratio1 = 0;
    if (channel0 != 0) ratio1 = (channel1 << (RATIO_SCALE+1)) / channel0;
    // round the ratio value
    unsigned long ratio = (ratio1 + 1) >> 1;
    // is ratio <= eachBreak ?
    unsigned int b, m;
    switch (iType)
    {
    case 0: // T package
    if ((ratio >= 0) && (ratio <= K1T))
    {b=B1T; m=M1T;}
    else if (ratio <= K2T)
    {b=B2T; m=M2T;}
    else if (ratio <= K3T)
    {b=B3T; m=M3T;}
    else if (ratio <= K4T)
    {b=B4T; m=M4T;}
    else if (ratio <= K5T)
    {b=B5T; m=M5T;}
    else if (ratio <= K6T)
    {b=B6T; m=M6T;}
    else if (ratio <= K7T)
    {b=B7T; m=M7T;}
    else if (ratio > K8T)
    {b=B8T; m=M8T;}
    break;
    case 1:// CS package
    if ((ratio >= 0) && (ratio <= K1C))
    {b=B1C; m=M1C;}
    else if (ratio <= K2C)
    {b=B2C; m=M2C;}
    else if (ratio <= K3C)
    {b=B3C; m=M3C;}
    else if (ratio <= K4C)
    {b=B4C; m=M4C;}
    else if (ratio <= K5C)
    {b=B5C; m=M5C;}
    else if (ratio <= K6C)
    {b=B6C; m=M6C;}
    else if (ratio <= K7C)
    {b=B7C; m=M7C;}
    else if (ratio > K8C)
    {b=B8C; m=M8C;}
    break;
    }
    unsigned long temp;
    temp = ((channel0 * b) - (channel1 * m));
    // do not allow negative lux value
    if (temp < 0) temp = 0;
    // round lsb (2^(LUX_SCALE-1))
    temp += (1 << (LUX_SCALE-1));
    // strip off fractional portion
    unsigned long lux = temp >> LUX_SCALE;
    return(lux);
    }
    }
    
  • #5 7226841
    tmf
    VIP Zasłużony dla elektroda
    Funkcje, ktore nie sa uzyte sa przez optymalizator wywalane np. CalculateLux, uzyj jej gdzies, byc moze pomoze wylaczenie optymalizatora - -O0, ale wtedy kod bedzie cokolwiek dziwny.
  • #6 7230834
    maciejkos
    Poziom 10  
    a to nie jest użycie funkcji czasem?

    unsigned int CalculateLux(unsigned int iGain, unsigned int tInt, unsigned int ch0,
    unsigned int ch1, int iType) 


    jak już pisałem o C mam mierne pojęcie ;)
    więc jakimi komendami mam zadeklarować wymagane zmienne użyte w tej procedurze?
    // Arguments: unsigned int iGain - gain, where 0:1X, 1:16X
    // unsigned int tInt - integration time, where 0:13.7mS, 1:100mS, 2:402mS,
    // 3:Manual
    // unsigned int ch0 - raw channel value from channel 0 of TSL2560
    // unsigned int ch1 - raw channel value from channel 1 of TSL2560
    // unsigned int iType - package type (T or CS) 
  • #7 7230872
    tmf
    VIP Zasłużony dla elektroda
    Nie, to jest definicja funkcji. Uzycie polega na jej wywolaniu gdzies w swoim programie. Po prostu w main ja wywolaj z jakimis parametrami i po klopocie.
    BTW, ten program jest tak prosty, ze poznanie tych paru polecen z c juz dawno umozliwiloby ci jego przetlumaczenie. A i na przyszlosc by sie przydalo, zeby tak ciagle w tym assemblerze nie grzebac tracac czas.
  • #8 7231832
    maciejkos
    Poziom 10  
    przetłumaczyłem sobie to łopatologicznie
    działa prawidłowo więc zamykam temat
REKLAMA