Shop OBEX P1 Docs P2 Docs Learn Events
adc3208 - c code - problems... — Parallax Forums

adc3208 - c code - problems...

JavalinJavalin Posts: 892
edited 2009-09-02 21:15 in Propeller 1
Hello all,

Hopefully somebody can spot what I can't see!

Channel 0 works 100% ok - the others return gibberish.· Checked the circuit and the inputs as far as I can.· I guess something is going out of sequence here - but why then is channel 0 working all the time?· The loops appear to be outputing/inputting the correct number of bits.

Thanks!



[color=#008000][color=#008000]// pins...[/color][/color]
[color=black]int adc_clk = 15;
int adc_di = 14; 
int adc_do = 13; 
int adc_cs = 12;[/color]
 
[color=#000000]//code[/color]
[color=black]void main(void)[/color]
[color=black]{[/color]
[color=black]
    int i = 0;
    int result = 0;
    int spiData = 0;
    int adcMUX = 0; 
    int adcData = 0;
    int adcChannel[noparse][[/noparse]8];

[/color]    SET (DIRA, adc_cs); [color=#008000][color=#008000]// output [/color][/color]
    SET (DIRA, adc_do); [color=#008000][color=#008000]// output [/color][/color]
    SET (DIRA, adc_clk); [color=#008000][color=#008000]// output [/color][/color]
    CLR (DIRA, adc_di); [color=#008000][color=#008000]// input[/color][/color]

    CLR (OUTA,adc_cs); [color=#008000][color=#008000]// low[/color][/color]
    msleep(10);        [color=#008000][color=#008000]// wait a tick[/color][/color]
    SET (OUTA,adc_cs); [color=#008000][color=#008000]// high [/color][/color]

[color=#008000][color=#008000]    // ADC![/color][/color]
    while(TRUE)
    {
        printf([i]"is -> "[/i]);
        putchar(32); 

        for (i=0; i<8; i++)
        {
            adcMUX = 0b00011000 + i; [color=#008000][color=#008000]// command byte[/color][/color]

            CLR (OUTA,adc_cs); [color=#008000][color=#008000]// low [/color][/color]
            CLR (OUTA,adc_do); [color=#008000][color=#008000]// low[/color][/color]
            CLR (OUTA,adc_clk); [color=#008000][color=#008000]// low[/color][/color]

            shiftout (adcMUX,8); [color=#008000][color=#008000]// output the cmd byte [/color][/color]

            CLR (OUTA,adc_clk); [color=#008000][color=#008000]// low[/color][/color]
            SET (OUTA,adc_clk); [color=#008000][color=#008000]// high [/color][/color]

            CLR (OUTA,adc_clk); [color=#008000][color=#008000]// low[/color][/color]
            SET (OUTA,adc_clk); [color=#008000][color=#008000]// high[/color][/color]

            CLR (OUTA,adc_clk); [color=#008000][color=#008000]// low [/color][/color]

            adcChannel[noparse][[/noparse]i] = shiftin(12); [color=#008000][color=#008000]// clock in the data[/color][/color]
   
            SET (OUTA,adc_cs); [color=#008000][color=#008000]// high [/color][/color]

            FdSerial_dec(adcChannel[noparse][[/noparse]i]); 
            putchar(32);

            msleep(10); 
       } 

       putchar(13);
       msleep(500);
     }
}

int shiftin(short bitCnt)
{
    short i=0;
    int data=0;
    int inputpinMask = 1 << adc_di;

    for(i=0; i<bitCnt; i++)
    {
         CLR (OUTA,adc_clk); [color=#008000][color=#008000]// low[/color][/color]
         SET (OUTA,adc_clk); [color=#008000][color=#008000]// high [/color][/color]

         data <<= 1; [color=#008000][color=#008000]// shift-left the data [/color][/color]
[color=#008000][color=#008000]         //data |= ((INA & inputpinMask) != 0); // read the port[/color][/color]
         if (INA & inputpinMask)
         {
             data |= 1;
         } 
   } 
   return data; 
}

void shiftout(int spiData,short bitCnt)
{
    short i=0;

    for(i=0; i<bitCnt; i++)
    {
[color=#008000][color=#008000]        // test for state of bit (n) [/color][/color]
        if (spiData & (1 << i))
        {
             SET (OUTA,adc_do);
        }
        else
        {
             CLR (OUTA,adc_do);
        } 

[color=#008000][color=#008000]        // pulse the clock[/color][/color]
        SET (OUTA,adc_clk); [color=#008000][color=#008000]// high[/color][/color]
        CLR (OUTA,adc_clk); [color=#008000][color=#008000]// low [/color][/color]
    }
}


James

Edit -> Included variable declarations & pins.

Post Edited (Javalin) : 9/2/2009 5:44:13 AM GMT

Comments

  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2009-09-01 20:45
    Using the SPIN/PASM version can always help: obex.parallax.com/objects/488/ smilewinkgrin.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    April, 2008: when I discovered the answers to all my micro-computational-botherations!
  • Nick MuellerNick Mueller Posts: 815
    edited 2009-09-01 21:00
    I don't see a bug, but I see that this can't be the complete programm.

    And I'll kill you[noparse][[/noparse]tm] if you are using a global 'i' for the counter in main.

    I'll kill you: smile.gif
    <


    Nick

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Never use force, just go for a bigger hammer!

    The DIY Digital-Readout for mills, lathes etc.:
    YADRO
  • PavelPavel Posts: 43
    edited 2009-09-01 21:40
    You seem to have the DO and DI pins swapped. You shift out data on DO and shift in on DI. You have the pins configured right in your code but check your circuit (DO pin is data-out of the ADC, so it should be input to the MCU). Reverse for DI pin.

    There's no way to tell whether this is a problem without seeing your circuit. The mystery why the channel #0 works remains unexplained so I'd expect that you have the wires right.

    BTW, you need to have analog signal connected to a channel pin, if you just leave the pin unconnected it won't show 0 (or close to 0), it will produce numbers all over the range. Could this be the case?
  • PavelPavel Posts: 43
    edited 2009-09-01 21:44
    One more thing: there's a MCP3208 C-object in OBEX (http://obex.parallax.com/objects/499/). You could try to run that and if you get good readings, then you can be reasonably sure that the problem is in your code (and keep digging). On the other hand, if the OBEX code shows the same gibberish, the chip may be fried (or at least some of the ports).
  • Erik FriesenErik Friesen Posts: 1,071
    edited 2009-09-01 23:41
    I don't see where i is declared in the main function. I am unclear what your compiler will do with a global i and a local i.

    Also I am not sure you have your pre clocks correct. If I were you I would let the shift in routine take care of the null bit by adcChannel = shiftin(13); and dropping the extra clocks between.

    Last time I debugged communication with this chip I used jazzed's prop based logic program to look at the pattern.
  • ImageCraftImageCraft Posts: 348
    edited 2009-09-02 02:03
    Erik Friesen said...
    I don't see where i is declared in the main function. I am unclear what your compiler will do with a global i and a local i.

    ..

    I have no clue about hardware issues, but regarding global i vs. local i, the compiler "does the right thing" (tm) smile.gif. That is, per C standard, if a local variable is visible, that will be used instead of a global variable.

    The original program is incomplete so we can only guess that main() does in fact use a global i. Regardless, that should not cause any issue either way.

    // richard
  • JavalinJavalin Posts: 892
    edited 2009-09-02 05:49
    Nick
    I'm not!· The code isn't finished either way!

    Erik
    Tried that - no joy.

    All,
    The output is weird - ch0 works nicely goes from 0 (ish) to 4095 but the other 7 channels all seems to move with the change when I vary the input to channel 3 for example!
    is ->  4094 0 0 0 24 0 63 119
    is ->  4095 0 0 0 26 0 63 127
    is ->  4095 0 4 0 26 0 63 127                        // channel 3 is "0"
    is ->  4094 2155 4 2858 63 0 59 127                  // start to increase 3
    is ->  4095 9122 10 10786 112 0 63 119
    is ->  4094 15542 4 16190 85 0 59 119
    is ->  4095 5065 4 5768 95 0 59 127
    is ->  4095 12066 4 12948 106 0 59 127
    is ->  4095 15644 0 16375 56 0 63 127
    is ->  4095 15843 4 16383 26 0 63 127
    is ->  4095 4480 4 4736 10 0 63 127
    is ->  4095 10945 4 10723 7 0 59 119                  // ch3 back to 0
    is ->  4095 0 0 0 24 0 63 119
    is ->  4095 0 4 0 26 0 63 127
    

    Hardware wise its on·a PPDB board - I currently have a pot on ch0 and 3.· Tried 3.3v and 5v voltage sources - didn't help.· Tried different I/O pins.

    Its almost like its reading differential channels or reading the LSB first data somehow.

    I'll re-write the code in SPIN to test the circuit.· I have some PASM that works well - that this is based on.· Found some weirdness looking at the I/O via an O-scope.

    Thanks all,

    James

    Post Edited (Javalin) : 9/2/2009 7:19:58 AM GMT
  • JavalinJavalin Posts: 892
    edited 2009-09-02 05:52
    >I have no clue about hardware issues, but regarding global i vs. local i, the compiler "does the right thing" (tm) . That is, per C standard, if a local variable is visible, that will be used instead of a global variable.
    Richard - if I use local variables are they kept within the cog that piece of code runs under? I.e. like PASM? I ask cause SPIN obviously has no local variables.

    Cheers,

    James
  • jazzedjazzed Posts: 11,803
    edited 2009-09-02 06:11
    You're right about i global, but people would look at you funny [noparse]:)[/noparse] I've never seen coding style death threats before now ....

    Spin has local variables in methods ... i.e. pub start(codep, parmp) | j, k ... guess which ones are local (normally kept in the stack).

    There is nothing in the code you posted that suggests you are using multiple cogs. What do you have connected to the ADC channels?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --Steve

    Propeller Tools
  • Nick MuellerNick Mueller Posts: 815
    edited 2009-09-02 07:07
    > I've never seen coding style death threats before now ....

    I think Achmed, the dead terrorist is funny enough for that quote. Hope none took it too serious. smile.gif
    But a global counter is really something that needs punishment. If he would have used the same i in the other functions, the result would have been quite confusing.

    @James:
    Use #defines for adc_clk etc.
    #define adc_clk 15 /* or whatever */
    
    


    These are constants, not variables.


    Nick

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Never use force, just go for a bigger hammer!

    The DIY Digital-Readout for mills, lathes etc.:
    YADRO

    Post Edited (Nick Mueller) : 9/2/2009 7:12:28 AM GMT
  • Erik FriesenErik Friesen Posts: 1,071
    edited 2009-09-02 11:23
    OK, because you are shifting out MSB, and you have (spiData & (1 << i)) , you are in reality shifting out garbage now, right? Shouldn't it be (spiData & (0b10000000 >> i))
    (yeah the 0b is probably a microchip thing for binary)
  • JavalinJavalin Posts: 892
    edited 2009-09-02 12:37
    Thanks Erik - i've just worked that out! I was re-writing in SPIN and noticed when I debugged DO pin state's the "shiftout" routine I was getting 10011000 rather than 00011001 - i'd done similar in C but debuged the creation of the value in the adcMUX - so I knew it was being created ok!

    It explains why channel0 works - as the code is the same both ways arround (i.e. 00011000)!

    Doh. Good spot.

    I'll post the fixed C code and the SPIN equivalent tonight

    Cheers again all,

    James
  • JavalinJavalin Posts: 892
    edited 2009-09-02 12:38
    Nick,

    the compiler doesn't like:

    #define adc_clk 15 /* or whatever */

    Help?

    james
  • Nick MuellerNick Mueller Posts: 815
    edited 2009-09-02 14:17
    > the compiler doesn't like:

    Did he say "I don't like that"?


    Nick

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Never use force, just go for a bigger hammer!

    The DIY Digital-Readout for mills, lathes etc.:
    YADRO
  • JavalinJavalin Posts: 892
    edited 2009-09-02 14:27
    funny enough - how did you know?

    I'll post the exact error tonight

    James
  • jazzedjazzed Posts: 11,803
    edited 2009-09-02 14:48
    What happens if you remove the comment ?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --Steve

    Propeller Tools
  • JavalinJavalin Posts: 892
    edited 2009-09-02 14:50
    tried without the comment - which is when I got the error.

    James
  • PavelPavel Posts: 43
    edited 2009-09-02 16:21
    Regarding the #define compile error, could you post the code snippet where it occurs? It maybe triggered by something before the actual line. Also check that the '#' character is in the leftmost column (some preprocessors will report error if it's not, not sure about ICC). You can still indent the directives, but # character should stay in column 1.

    #ifdef SOMETHING
    #    define SOMETHING_ELSE
    #endif
    
    
  • JavalinJavalin Posts: 892
    edited 2009-09-02 16:25
    Ok - my mistake I tried:

    #define adc_cs=15;
    #define adc_cs 15;

    and then (which worked)

    #define adc_cs 15

    (no ";")

    James
  • PavelPavel Posts: 43
    edited 2009-09-02 16:40
    Yep, that's one of the worst traps in C. Everything after the first token in #define is copied verbatim into code where the token occurs. It's really worth one's time to read carefully about how #define directive works in the preprocessor because it can cause a lot of headaches (en.wikipedia.org/wiki/C_preprocessor). You may even hear that "macros are pure evil" and there's some truth to that claim.
  • JavalinJavalin Posts: 892
    edited 2009-09-02 16:44
    OK - thanks all - here is the working code for those that are interested!· Simple MCP3208 example if nothing else.· I've included the SPIN version too. The C example below uses Steve Densons FDSerial C object - i've omitted most of it for clarity.·

    Out of interest I took some timing's via O'scope from when CS goes LOW to when it returns to HIGH - a single channel read!
    C·= 175us
    SPIN = 1.89ms
    #include <propeller.h>
    #include <stdio.h>
    #include "FdSerial.h"
    
    [color=#008000][color=#008000]// define pins (constants) [/color][/color]
    const short adc_clk=15;
    const short adc_di=14; 
    const short adc_do=13; 
    const short adc_cs=12;
    
    
    [color=green]// fwd declarations[/color]
    void shiftout(int spiData,short bitCnt);
    int shiftin(short bitCnt);
    
    
    void main(void)
    {
        int result = 0;
        int spiData = 0;
        int adcMUX = 0; 
        int adcData = 0;
        int adcChannel[noparse][[/noparse]8]; 
        int channel=0;
    
        FdSerial_start(31,30,0,115200);
    
        putchar(0); // clear screen
    
    [color=#008000][color=#008000]    // pins...[/color][/color]
        SET (DIRA, adc_cs); [color=#008000][color=#008000]// output [/color][/color]
        SET (DIRA, adc_do); [color=#008000][color=#008000]// output [/color][/color]
        SET (DIRA, adc_clk); [color=#008000][color=#008000]// output [/color][/color]
        CLR (DIRA, adc_di); [color=#008000][color=#008000]// input[/color][/color]
    
        CLR (OUTA,adc_cs); [color=#008000][color=#008000]// low[/color][/color]
        msleep(10); [color=#008000][color=#008000]// wait a tick[/color][/color]
        SET (OUTA,adc_cs); [color=#008000][color=#008000]// high [/color][/color]
    
    [color=#008000][color=#008000]    // ADC![/color][/color]
        while(TRUE)
        { 
            for (channel=0; channel<8; channel++)
            {
                adcMUX = 24 + channel; [color=#008000][color=#008000]// mux [/color][/color]
    
                printf([i]"ch %d\t"[/i], channel);
    
                FdSerial_bin(adcMUX,8);
                putchar([i]' '[/i]); 
    
                CLR (OUTA,adc_cs); [color=#008000][color=#008000]// low [/color][/color]
                CLR (OUTA,adc_do); [color=#008000][color=#008000]// low[/color][/color]
                CLR (OUTA,adc_clk); [color=#008000][color=#008000]// low[/color][/color]
    
                shiftout (adcMUX,8); [color=#008000][color=#008000]// output the cmd byte [/color][/color]
    
                CLR (OUTA,adc_clk); [color=#008000][color=#008000]// low[/color][/color]
                SET (OUTA,adc_clk); [color=#008000][color=#008000]// high [/color][/color]
    
                CLR (OUTA,adc_clk); [color=#008000][color=#008000]// low[/color][/color]
                SET (OUTA,adc_clk); [color=#008000][color=#008000]// high[/color][/color]
    
                CLR (OUTA,adc_clk); [color=#008000][color=#008000]// low [/color][/color]
    
                adcChannel[noparse][[/noparse]channel] = shiftin(12); [color=#008000][color=#008000]// clock in the data [/color][/color]
    
                SET (OUTA,adc_cs); [color=#008000][color=#008000]// high [/color][/color]
     
                printf([i]"\t%d"[/i], adcChannel[noparse][[/noparse]channel]);
                printf([i]"\r"[/i]);
            }
    
            printf([i]"\r"[/i]);  [color=#008000][color=#008000]// new line [/color][/color]       
            msleep(500);   [color=green]// wait a tick[/color]
        }
    }
    
    int shiftin(short bitCnt)
    {
        short i3=0;
        int data=0;
        int inputpinMask = 1 << adc_di;
    
    [color=#008000][color=#008000]    // shift in the data (MSB first) [/color][/color]
        for(i3=0; i3<bitCnt; i3++)
        {
             CLR (OUTA,adc_clk); [color=#008000][color=#008000]// low[/color][/color]
             SET (OUTA,adc_clk); [color=#008000][color=#008000]// high [/color][/color]
    
             data <<= 1; [color=#008000][color=#008000]// shift-left the data (MSB First) [/color][/color]
             data |= ((INA & inputpinMask) != 0); [color=#008000][color=#008000]// read the port[/color][/color]
        } 
        return data; 
    }
    
    void shiftout(int spiData,short spiBitCnt)
    {
        short i2=0;
    
    [color=#008000][color=#008000]    // shift out the data (MSB first)[/color][/color]
        for(i2=spiBitCnt; i2>=0; i2--)
        {
    [color=#008000][color=#008000]         // test for state of bit (n) [/color][/color]
             if (spiData & (1 << i2))
             {
                 SET (OUTA,adc_do); 
             }
             else
             {
                 CLR (OUTA,adc_do);
             } 
    
    [color=#008000][color=#008000]         // pulse the clock[/color][/color]
             SET (OUTA,adc_clk); [color=#008000][color=#008000]// high[/color][/color]
             CLR (OUTA,adc_clk); [color=#008000][color=#008000]// low [/color][/color]
        }
    }
    
    

    The output (Parallax Serial Terminal) looks like:
    ch 0    00011000        1770
    ch 1    00011001        13
    ch 2    00011010        14
    ch 3    00011011        4094
    ch 4    00011100        10
    ch 5    00011101        12
    ch 6    00011110        8
    ch 7    00011111        11
    

    And the SPIN code:

    '' **************************************************************************************
    '' **************************************************************************************
    '' * editor-tabs: 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60              *
    '' **************************************************************************************
    CON
        _clkmode            = xtal1 + pll16x
        _xinfreq            = 5_000_000
        _stack              = 500
         
         ' ADC MCP3208 pins
        mcp3208clk          = 15
        mcp3208di           = 14
        mcp3208do           = 13
        mcp3208cs           = 12    
        ' debug - USE onboard pins
        pcDebugRX           = 31
        pcDebugTX           = 30
         
        ' serial baud rates  
        pcDebugBaud         = 115200  ' 57600, 115200 or 256000 only
         
      
    OBJ
        debug             :     "Debug_PC" 
      
    VAR
        long    channel, adcMUX, data1
     
      
    PUB Start
        ' start the PC debug object
        debug.startx(pcDebugRX,pcDebugTX,pcDebugBaud)
        dira[noparse][[/noparse]mcp3208clk]~~
        dira[noparse][[/noparse]mcp3208cs]~~
        dira[noparse][[/noparse]mcp3208do]~~
        dira[noparse][[/noparse]mcp3208di]~
        outa[noparse][[/noparse]mcp3208cs]~
        waitcnt(clkfreq/5+cnt)
        outa[noparse][[/noparse]mcp3208cs]~~
        repeat
            repeat channel from 0 to 7
                debug.str(string("is -> ch"))
                debug.dec(channel)
                debug.str(string(", "))
                        
                adcMUX := %0001_1000 + channel
                debug.bin(adcMUX,8)            
                outa[noparse][[/noparse]mcp3208clk]~
                outa[noparse][[/noparse]mcp3208cs]~
                outa[noparse][[/noparse]mcp3208do]~
                shiftout(adcMUX,8)
                debug.str(string(", "))            
                outa[noparse][[/noparse]mcp3208clk]~
                outa[noparse][[/noparse]mcp3208clk]~~
                outa[noparse][[/noparse]mcp3208clk]~
                outa[noparse][[/noparse]mcp3208clk]~~
                outa[noparse][[/noparse]mcp3208clk]~
                data1 := shiftin(12)
                
                outa[noparse][[/noparse]mcp3208cs]~~
                debug.str(string(", "))
                debug.dec(data1)
                debug.putc(13)
            debug.putc(13)                                  
                                                                
     
    PRI ShiftIn(spiBitCnt) : result | i3, spidata1
        spidata1 := 0
        
        repeat i3 from 0 to spiBitCnt-1
            ' clock
            outa[noparse][[/noparse]mcp3208clk]~
            outa[noparse][[/noparse]mcp3208clk]~~
            ' data
            spidata1 <<= 1        
            spidata1 |= ina[noparse][[/noparse]mcp3208di]
            
        return spiData1
                
            
    PRI ShiftOut(spiData,spiBitCnt) | i2, data, mask
        mask := %1 << spiBitCnt
     
        debug.putc(" ")
        
        repeat i2 from 0 to spiBitCnt-1
            ' data
            mask >>= 1        
            if spiData & mask
                debug.putc("x")
                outa[noparse][[/noparse]mcp3208do]~~
            else
                debug.putc("o")        
                outa[noparse][[/noparse]mcp3208do]~                             
            ' clock
            outa[noparse][[/noparse]mcp3208clk]~~
            outa[noparse][[/noparse]mcp3208clk]~            
                
        
    

    output is:
    is -> ch0, 00011000 oooxxooo, , 1769
    is -> ch1, 00011001 oooxxoox, , 16
    is -> ch2, 00011010 oooxxoxo, , 17
    is -> ch3, 00011011 oooxxoxx, , 4094
    is -> ch4, 00011100 oooxxxoo, , 15
    is -> ch5, 00011101 oooxxxox, , 16
    is -> ch6, 00011110 oooxxxxo, , 14
    is -> ch7, 00011111 oooxxxxx, , 15
    

    I've left the debug in the SPIN object.
    James

    Post Edited (Javalin) : 9/3/2009 8:03:03 PM GMT
  • Nick MuellerNick Mueller Posts: 815
    edited 2009-09-02 17:45
    > #define adc_cs 15

    But I wrote that.

                printf("ch"); // serial debug
                putchar(32);
                FdSerial_dec(channel);
                putchar(9);
                FdSerial_bin(adcMUX,8);
    
    



    That would be:
      printf("ch %d\t0X%x", channel, adcMUX);
    
    



    You shouldn't mix printf and FdSerial_bin etc. Albeit it doesn't matter here, the is no guaranty that the two make some buffering or not.
    OK, %x doesn't print a binary, only a hex. And the stdio with format specifiers (length etc.) is quite expensive size-wise.

                putchar(9); // tab
    
                FdSerial_dec(adcChannel[noparse][[/noparse]channel]); // serial debug
                putchar(32);
                putchar(13);
    
    


    thats:
      printf("\t%d \n", adcChannel[noparse][[/noparse]channel]);
    
    


    putchar(32); would be putchar(' '); and putchar(9); putchar('\t');


    Nick

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Never use force, just go for a bigger hammer!

    The DIY Digital-Readout for mills, lathes etc.:
    YADRO
  • jazzedjazzed Posts: 11,803
    edited 2009-09-02 18:26
    Nick Mueller said...
    You shouldn't mix printf and FdSerial_bin etc. Albeit it doesn't matter here, the is no guaranty that the two make some buffering or not.
    In this case it doesn't matter because both use exactly the same fundamental output code.

    One of my biggest ICC frustrations is that some format specifiers just don't work at all ... I could be mistaken, but I don't remember %04x working at all in any printf mode.

    Using printf, sprintf, snprintf, vfprintf, yada yada carries a big cost. If you are using it, make sure you take full advantage of it. It's not entirely clear how ICC interprets the format specifiers (perhaps the ICC on-line manual addresses this), but you can get a feel by looking at a sprintf man page like this: linux.die.net/man/3/sprintf

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --Steve

    Propeller Tools
  • JavalinJavalin Posts: 892
    edited 2009-09-02 19:01
    Hello all,

    Thanks for the formatting suggestions - hadn't thought of that!

    That would be:
      printf("ch %d\t0X%x", channel, adcMUX);
    
    

    Is there a binary formatter for printf?· Google wasn't my friend on that one!

    Post updated with the rest though!

    James
  • Nick MuellerNick Mueller Posts: 815
    edited 2009-09-02 20:29
    > In this case it doesn't matter because both use exactly the same fundamental output code.

    But I wrote that, right?


    > One of my biggest ICC frustrations is that some format specifiers just don't work at all ... I could be mistaken,
    > but I don't remember %04x working at all in any printf mode.

    No, that works! You have to select the printf-lib one level higher (project settings IIRC). There are three: with float (not that one), full format specifiers (or something like this) and "primitive". Default is primitive, and this one doesn't understand field lengths.
    But, as I said, it is quite bigger in code-size!


    > Is there a binary formatter for printf?

    Officially not. Maybe ICC has one but I really don't remember. I guess not. But programmers can translate hex to bin anyhow. wink.gif


    Nick

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Never use force, just go for a bigger hammer!

    The DIY Digital-Readout for mills, lathes etc.:
    YADRO
  • Nick MuellerNick Mueller Posts: 815
    edited 2009-09-02 20:44
    Have to add that:
    The '#define someConstant' is one way. An other way (and more up to date) is to write 'const int 4711'.
    Some compilers might emmit less code with '#define'. I didn't check with ICC.

    But anyhow, you have to learn that #define, #ifdef, #endif stuff, it's good for debugging, production code, different versions (language, platform, etc).


    Nick

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Never use force, just go for a bigger hammer!

    The DIY Digital-Readout for mills, lathes etc.:
    YADRO
  • jazzedjazzed Posts: 11,803
    edited 2009-09-02 20:56
    Nick Mueller said...
    In this case it doesn't matter because both use exactly the same fundamental output code.

    But I wrote that, right?

    smile.gif No, you wrote:
    > You shouldn't mix printf and FdSerial_bin etc. Albeit it doesn't matter here, the is no guaranty that the two make some buffering or not.

    I prefer using const int ...; over #define since you avoid "label" collision problems in large projects. I prefer static const int ...; where possible of course.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --Steve

    Propeller Tools
  • Nick MuellerNick Mueller Posts: 815
    edited 2009-09-02 21:12
    > since you avoid "label" collision problems

    Label!? That sounds like a four letter word, like 'goto'. I'm shocked!


    Nick

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Never use force, just go for a bigger hammer!

    The DIY Digital-Readout for mills, lathes etc.:
    YADRO
  • jazzedjazzed Posts: 11,803
    edited 2009-09-02 21:15
    I meant "symbol" ... label was a bad choice of words. I'd rather not use goto, but I have as you know. It is a common thing in Linux device drivers for some reason.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --Steve

    Propeller Tools
Sign In or Register to comment.