Shop OBEX P1 Docs P2 Docs Learn Events
Proper syntax for INA command in C — Parallax Forums

Proper syntax for INA command in C

brantleyrabrantleyra Posts: 4
edited 2010-10-16 09:57 in Propeller 1
I'm trying to access a single pin's state with the INA command using C. The Imagecraft compiler software has almost no examples of the correct format of this or any of the other supported propellor commands. I would appreciate any help I can get with the correct syntax as trial and error is time consuming.

Comments

  • jazzedjazzed Posts: 11,803
    edited 2010-10-15 14:10
    brantleyra wrote: »
    I'm trying to access a single pin's state with the INA command using C. The Imagecraft compiler software has almost no examples of the correct format of this or any of the other supported propellor commands. I would appreciate any help I can get with the correct syntax as trial and error is time consuming.
    You should be able to find examples in the include folder/file propeller.h
  • brantleyrabrantleyra Posts: 4
    edited 2010-10-15 14:29
    Propeller.h has no examples of the INA command. Only defines address space used by INA.
  • RossHRossH Posts: 5,520
    edited 2010-10-15 17:05
    Hi brantleyra

    Below is Catalina's "wrapper" file that hides the difference between Catalina and ICC - in it you will find examples of the ICC syntax for all the Parallax special functions (as well as the equivalent Catalina syntax).

    However, please note that while these should compile, and I believe the syntax is therefore correct, I have not done ANY actual testing with ICC.

    If you find any problems, I'd appreciate it if you'd let me know.

    Ross.
    // +================+
    // | catalina_icc.h |
    // +================+
    //
    // This file contains set of macros to allow C programs to be compiled to  
    // run under either Catalina or ICC. 
    // 
    // This include file configures itself automatically by detecting whether
    // the symbol __CATALINA__ (defined by the catalina compiler) is defined.
    //
    
    #ifndef _CATALINA_ICC_H
    
    #define _CATALINA_ICC_H
    
    #ifdef __CATALINA__
    #include <catalina_cog.h>
    #else
    #include <propeller.h>
    #endif
    
    //
    // set_dira(bits) : set bits in DIRA
    //
    #ifdef __CATALINA__
    #define set_dira(bits) _dira((bits), (bits))
    #else
    #define set_dira(bits) DIRA |= (bits)
    #endif
    
    //
    // set_dirb(bits) : set bits in DIRB
    //
    #ifdef __CATALINA__
    #define set_dirb(bits) _dirb((bits), (bits))
    #else
    #define set_dirb(bits) DIRB |= (bits)
    #endif
    
    //
    // clr_dira(bits) : clear bits in DIRA
    //
    #ifdef __CATALINA__
    #define clr_dira(bits) _dira((bits), 0)
    #else
    #define clr_dira(bits) DIRA &= ~(bits)
    #endif
    
    //
    // clr_dirb(bits) : clear bits in DIRB
    //
    #ifdef __CATALINA__
    #define clr_dirb(bits) _dirb((bits), 0)
    #else
    #define clr_dirb(bits) DIRB &= ~(bits)
    #endif
    
    //
    // set_outa(bits) : set bits in OUTA
    //
    #ifdef __CATALINA__
    #define set_outa(bits) _outa((bits), (bits))
    #else
    #define set_outa(bits) OUTA |= (bits)
    #endif
    
    //
    // set_outb(bits) : set bits in OUTB
    //
    #ifdef __CATALINA__
    #define set_outb(bits) _outb((bits), (bits))
    #else
    #define set_outb(bits) OUTB |= (bits)
    #endif
    
    //
    // clr_outa(bits) : clear bits in OUTA
    //
    #ifdef __CATALINA__
    #define clr_outa(bits) _outa((bits), 0)
    #else
    #define clr_outa(bits) OUTA &= ~(bits)
    #endif
    
    //
    // clr_outb(bits) : clear bits in OUTB
    //
    #ifdef __CATALINA__
    #define clr_outb(bits) _outb((bits), 0)
    #else
    #define clr_outb(bits) OUTB &= ~(bits)
    #endif
    
    //
    // get_ina() : get the value of INA
    //
    #ifdef __CATALINA__
    #define get_ina() _ina()
    #else
    #define get_ina() INA
    #endif
    
    //
    // get_inb() : get the value of INB
    //
    #ifdef __CATALINA__
    #define get_inb() _inb()
    #else
    #define get_inb() INB
    #endif
    
    //
    // get_cnt() : get the value of CNT
    //
    #ifdef __CATALINA__
    #define get_cnt(bits) _cnt()
    #else
    #define get_cnt(bits) CNT
    #endif
    
    
    //
    // cogid() : get the current cog id
    //
    #ifdef __CATALINA__
    #define cogid() _cogid()
    #else
    #define cogid() COGID()
    #endif
    
    //
    // cogstop(cog) : stop a cog
    //
    #ifdef __CATALINA__
    #define cogstop(cog) _cogstop(cog)
    #else
    #define cogstop(cog) COGSTOP(cog)
    #endif
    
    //
    // coginit(val) : start a program in a cog (val is the PASM coginit val)
    //
    #ifdef __CATALINA__
    #define coginit(val) _coginit(((val) & 0xFFFC0000)>>18, ((val) & 0x3FFF0)>>4, (val) & 0xF)
    #else
    #define coginit(val) COGINIT(val)
    #endif
    
    //
    // locknew(lock) : clear a lock
    //
    #ifdef __CATALINA__
    #define locknew(lock) _locknew(lock)
    #else
    #define locknew(lock) LOCKNEW(lock)
    #endif
    
    
    //
    // lockclr(lock) : clear a lock
    //
    #ifdef __CATALINA__
    #define lockclr(lock) _lockclr(lock)
    #else
    #define lockclr(lock) LOCKCLR(lock)
    #endif
    
    //
    // lockset(lock) : set a lock
    //
    #ifdef __CATALINA__
    #define lockset(lock) _lockset(lock)
    #else
    #define lockset(lock) LOCKSET(lock)
    #endif
    
    //
    // lockret(lock) : return a lock
    //
    #ifdef __CATALINA__
    #define lockret(lock) _lockret(lock)
    #else
    #define lockret(lock) LOCKRET(lock)
    #endif
    
    //
    // waitcnt(count, ticks) : wait for cnt to equal count, then add ticks to count
    //
    #ifdef __CATALINA__
    #define waitcnt(count,ticks) { _waitcnt(count); count += (ticks); }
    #else
    #define waitcnt(count,ticks) WAITCNT(count,ticks)
    #endif
    
    //
    // waitvid(colors, pixels) : wait for video generator
    //
    #ifdef __CATALINA__
    #define waitvid(colors, pixels) _waitvid(colors, pixels)
    #else
    #define waitvid(colors, pixels) WAITVID(colors, pixels)
    #endif
    
    //
    // waitpne(mask, pins) : wait for pins not equal
    //
    #ifdef __CATALINA__
    #define waitne(mask, pins) _waitpne(mask, pins, 0)
    #else
    #define waitpne(mask, pins) waitpne(mask, pins)
    #endif
    
    //
    // waitpeq(mask, pins) : wait for pins equal
    //
    #ifdef __CATALINA__
    #define waitne(mask, pins) _waitpne(mask, pins, 0)
    #else
    #define waitpne(mask, pins) waitpne(mask, pins)
    #endif
    
    //
    // clockfreq() : fetch the clock frequency
    //
    #ifdef __CATALINA__
    #define clockfreq() _clockfreq()
    #else
    #define clockfreq() CLKFREQ
    #endif
    
    //
    // clockmode() : fetch the clock mode
    //
    #define clockmode() CLKMODE
    
    //
    // clkset(mode, frequency) : set the clock mode and frequency
    //
    #ifdef __CATALINA__
    #define clkset(mode, frequency) _clockinit(mode, frequency)
    #endif
    
    //
    // wait(ticks) : wait for the specified number of ticks
    //
    #ifdef __CATALINA__
    #define wait(ticks) _waitcnt(_cnt() + (ticks))
    #endif
    
    //
    // msleep(ticks) : wait for the specified number of milliseconds
    //
    #if __CATALINA__
    #define msleep(ticks) wait((ticks)*(clockfreq()/1000))
    #endif
    
    //
    // sleep(ticks) : wait for the specified number of seconds
    //
    #ifdef __CATALINA__
    #define sleep(ticks) wait((ticks)*clockfreq())
    #endif
    
    #endif
    
  • jazzedjazzed Posts: 11,803
    edited 2010-10-15 18:00
    brantleyra wrote: »
    Propeller.h has no examples of the INA command. Only defines address space used by INA.

    Ooops, you're right. Try this:

    long value = INA;

    BTW, several drivers are ported for ICC. They can be found at:
    http://obex.parallax.com/objects/search/?q=ICC&csrfmiddlewaretoken=d9ed9ece14ac88d521ef456555f4ddae

    Hopefully those examples, the examples ICC includes, and the ImageCraft on-line help will get you answers you need. I ported several examples because ICC was the first "native Propeller" C tool-chain. I'm not affiliated with ImageCraft in anyway other than having produced code.
  • brantleyrabrantleyra Posts: 4
    edited 2010-10-16 07:06
    Thanks for the help. What I'm looking for is a simple command like this from the propeller manual.

    Temp := INA[16]

    Reads I/O pin 16 and stores its state (0 or 1) in the lowest bit of Temp; all other bits of Temp are cleared.

    As I plan to read serveral pins at different times I didn't want to have to set-up a bit mask for each pin or a whole subroutine that strips out the pin.
  • brantleyrabrantleyra Posts: 4
    edited 2010-10-16 07:24
    Support at Imagecraft got back to me with this

    temp = INA & 0x80;

    will read the 15th bit (pin 16) of the register.

    This looks like it will solve my problem. Simple and short.
  • jazzedjazzed Posts: 11,803
    edited 2010-10-16 09:57
    brantleyra wrote: »
    Support at Imagecraft got back to me with this

    temp = INA & 0x80;

    will read the 15th bit (pin 16) of the register.

    This looks like it will solve my problem. Simple and short.

    I guess you left out some 0's. It should be "temp = INA & 0x8000;"

    Spin is good about using array notation for pins if you're used to it.
    A simple C function to read one pin "by number" would be:
    /*
     * read state of pin number as 1 or 0
     * pin - pin number to read.
     * returns 1 if pin is high or 0 if pin is low
     */
    int readpin(int pin)
    {
    // make sure pin number is in range
       pin &= 0x1f;
    // return state of the pin number
       return (INA & (1<<pin)) ? 1 : 0;
    }
    
    Good luck on your C journey.
Sign In or Register to comment.