Embedded programming

This is a discussion on Embedded programming within the C Programming forums, part of the General Programming Boards category; hi guys.... I am writing a program. In this program, i have a function which is universal. I am using ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    21

    Question Embedded programming

    hi guys....

    I am writing a program. In this program, i have a function which is universal. I am using the GPIO to do serial data sending to several devices connected to my MCU using several pins on a port. The problem now is if this function is universal, how do i pass the pins that i want to the function? i du not want to do the same function for different pins,it will be a waste of space.

    Assuming that the function will look like this

    Code:
        void DataSend(unsigned char data)
         {
           int a;     
           for (a = 0;a<8 a++)
           {
                 PORTA.Pin0 = 0;  // this will generate the low clock pulse
                 PORTA.Pin1 = data & 0x01; //this will push the data out 
                                                           //bit by bit
                 data >>=1;
                 PORTA.Pin0 = 1  //this will generate high clock pulse
          }    
        }

    If i want to make PORTA.Pinx as universal and i want to use other pin with this function...is it possible?

    can i do the following?

    Code:
        void DataSend(unsigned char data, (dunno what type to use) CLKpin ,(dunno what type to use) DATApin )
         {
           int a;     
           for (a = 0;a<8 a++)
           {
                 CLKpin = 0;  // this will generate the low clock pulse
                 DATApin = data & 0x01; //this will push the data out 
                                                           //bit by bit
                 data >>=1;
                 CLKpin = 1  //this will generate high clock pulse
          }    
        }
    if in the main i call the function as following

    Code:
        DATASend('S', PORTA.PIN2,PORTA.pin3)

    will this work as if the following?
    Code:
        PORTA.Pin2 = 0;
    and

    Code:
        PORTA.Pin3 = data & 0x01;

    As if i am latching the PORTA pin 2 and 3?

    Please advise also the type i shuld declare for the pins in the function

    thanks

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    > PORTA.Pin0
    What is the declaration of the structure which declares this PORTA variable?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    21
    Code:
    typedef unsigned char byte;
    
    
    #define PTA                             _PTA.Byte
    #define PTA_PTA0                        _PTA.Bits.PTA0
    #define PTA_PTA1                        _PTA.Bits.PTA1
    #define PTA_PTA2                        _PTA.Bits.PTA2
    #define PTA_PTA3                        _PTA.Bits.PTA3
    #define PTA_PTA4                        _PTA.Bits.PTA4
    #define PTA_PTA5                        _PTA.Bits.PTA5
    #define PTA_PTA6                        _PTA.Bits.PTA6
    #define PTA_PTA7                        _PTA.Bits.PTA7
    PORTA.Pin0 is the same with PTA_PTA0.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    if the PTAx members are bitfields then you'll probably have to use a macro, otherwise just prototype the function to take the address of the member.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    21
    I dun understand how this can be done? can you show me? Thanks

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I have no idea how you've declared your structures. create a function that takes a pointer to whatever datatype the members are - unless they are bitfields, in which case you can't form a pointer to a bit so you would probably need a macro to access the variables.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by suzanne_lim
    can i do the following?

    Code:
        void DataSend(unsigned char data, (dunno what type to use) CLKpin ,(dunno what type to use) DATApin )
         {
           int a;     
           for (a = 0;a<8 a++)
           {
                 CLKpin = 0;  // this will generate the low clock pulse
                 DATApin = data & 0x01; //this will push the data out 
                                                           //bit by bit
                 data >>=1;
                 CLKpin = 1  //this will generate high clock pulse
          }    
        }
    if in the main i call the function as following
    Code:
        DATASend('S', PORTA.PIN2,PORTA.pin3)
    Please advise also the type i shuld declare for the pins in the function
    I might suggest something like this.
    Code:
    void DataSend(unsigned char data, 
                  unsigned char DATAport, unsigned char DATApin,
                  unsigned char CLKport,  unsigned char CLKpin)
    {
       int a;     
       for ( a = 0; a < 8; a++ )
       {
          CLKport &= ~CLKpin;      // this will generate the low clock pulse
          if ( data & 1 )
          {
             DATAport |=  DATApin; // push a one out
          }
          else
          {
             DATAport &= ~DATApin; // push a zero out
          }
          data >>= 1;
          CLKport |= CLKpin;       // this will generate high clock pulse
       }    
    }
    This function would be called like this.
    Code:
       DataSend(0xAA, PTA, 0x04, PTB, 0x80);
    In this example a data value is sent using pin 2 of port A for the data and pin 7 of port B for the clock.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    21
    Thanks Dave.

    By the way, can you tell me when to use & and when to use &&. I'm a bit confuse. Let say if i want to check for whether a pin is high do i

    Code:
    if (PORT && PINMASK)
    or

    Code:
    if (PORT & PINMASK)

    Is the following

    Code:
    DATAport &= ~DATApin; // push a zero out
    similar with

    Code:
    DATAport = DATAport  &&  ~DATApin; // push a zero out
    or with the following

    Code:
    DATAport = DATAport  &  ~DATApin; // push a zero out
    Did i confuse u? Please don't tell me to read the FAQ as i have already read it but I am still confused.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    > By the way, can you tell me when to use & and when to use &&
    & is for bit-twiddling code like you're doing at the moment, where you're changing individual bits in a byte.

    && is for logical tests, like
    if ( newbie_has_question && newbie_has_code ) answer_question();


    The logical tests also implement short-circuit evaluation - see your text book.
    Basically, they stop evaluating when the truth of the statement is known.
    For example, this will never dereference NULL
    if ( p != NULL && *p != 0 )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading and writing to an embedded file
    By HLMetroid in forum C# Programming
    Replies: 4
    Last Post: 01-01-2009, 11:03 PM
  2. Embedded system board
    By ssharish2005 in forum Tech Board
    Replies: 1
    Last Post: 08-12-2007, 03:03 PM
  3. Question on embedded systems
    By ssharish2005 in forum C Programming
    Replies: 3
    Last Post: 08-12-2007, 02:28 PM
  4. embedded c++
    By fizz_uk83 in forum C++ Programming
    Replies: 4
    Last Post: 08-13-2003, 08:09 AM
  5. Embedded C
    By Sheep in forum C Programming
    Replies: 3
    Last Post: 03-27-2002, 05:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21