1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Start/Stop Timer

Discussion in 'Microcontrollers' started by TucsonDon, Aug 30, 2017.

  1. TucsonDon

    TucsonDon Member

    Joined:
    Sep 4, 2016
    Messages:
    45
    Likes:
    0
    Code (text):

    //RTCC free RAM addresses for pump start-stop timers
    #define PumpTime0   0x15
    #define PumpTime1   0x1B
    #define PumpTime2   0x21
    #define PumpTime3   0x27
    #define PumpTime4   0x2D
    #define PumpTime5   0x33
    #define PumpTime6   0x39
    #define PumpTime7   0x3F

    typedef unsigned char bool;
    union
    {
        struct
        {
            bool pumptimer0:1;      /*using the bits to flag if */
            bool pumptimer1:1;      /*a run timer is in use*/
            bool pumptimer2:1;
            bool pumptimer3:1;
            bool pumptimer4:1;
            bool pumptimer5:1;
            bool pumptimer6:1;
            bool pumptimer7:1;
        };
    }pmptmr;        //pump timer status
     
    I am trying to setup a timer to turn a pump on-off. I am using pmptmon bits to flag which timer is in use and need to point to the correct address on th RTCC to read the information but don't know how to do it.
     
    Last edited: Aug 31, 2017
  2. TucsonDon

    TucsonDon Member

    Joined:
    Sep 4, 2016
    Messages:
    45
    Likes:
    0
    Someone got to know how to do this
     
  3. valknut

    valknut New Member

    Joined:
    Aug 7, 2017
    Messages:
    17
    Likes:
    0
    Your question is too vague. Post the source code you are using and describe the exact issues you are having with it.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. TucsonDon

    TucsonDon Member

    Joined:
    Sep 4, 2016
    Messages:
    45
    Likes:
    0

    Code (text):

    void PumpStartStop(void)
    {
        pdata = PmpLoadBuffer[2];
        cc = 0;
        nCount = 5;
        I2C1_MESSAGE_STATUS status;
     
        PmpTimer = (pmptmr.pumptimer0)|(pmptmr.pumptimer1<<1)|(pmptmr.pumptimer2<<2)
                |(pmptmr.pumptimer3<<3)|(pmptmr.pumptimer4<<4)|(pmptmr.pumptimer5<<5)
                |(pmptmr.pumptimer6<<6)|(pmptmr.pumptimer7<<7);
     
        PmpTimerOn = (pmptmon.pmpon0)|(pmptmon.pmpon1<<1)|(pmptmon.pmpon2<<2)|
                (pmptmon.pmpon3<<3)|(pmptmon.pmpon4<<4)|(pmptmon.pmpon5<<5)|
                (pmptmon.pmpon6<<6)|(pmptmon.pmpon7<<7);
     
     
        //bit shift to the left "bb" times
        timer = 0x01<<bb;
     
        /*Masking the bits to see if the timer is in use then*/
        /*masking again to determine which timer to set the RTC RAM address*/
        if (PmpTimer & timer ? On : Off)
        {
            TimerUse = (PmpTimer & timer);
         
            switch (TimerUse)
            {
                case 0x01:
                {
                    PmpLoadBuffer[0] = PumpTime0;
                    break;
                }
             
                case 0x02:
                {
                    PmpLoadBuffer[0] = PumpTime1;
                    break;
                }
                case 0x04:
                {
                    PmpLoadBuffer[0] = PumpTime2;
                    break;
                }
             
                case 0x08:
                {
                    PmpLoadBuffer[0] = PumpTime3;
                    break;
                }
             
                case 0x10:
                {
                    PmpLoadBuffer[0] = PumpTime4;
                    break;
                }
             
                case 0x20:
                {
                    PmpLoadBuffer[0] = PumpTime5;
                    break;
                }
             
                case 0x40:
                {
                    PmpLoadBuffer[0] = PumpTime6;
                    break;
                }
             
                case 0x80:
                {
                    PmpLoadBuffer[0] = PumpTime7;
                    break;
                }
             
                default:
                    break;
            }/*switch*/
         
            for (counter = 0; counter < nCount; counter++)
            {
                while(status != I2C1_MESSAGE_FAIL)
                {
                    I2C1_MasterRead(*pdata,1,RTCC,status);
                 
                    //wait for message to be sent
                    while(status == I2C1_MESSAGE_PENDING);
                 
                    if (status == I2C1_MESSAGE_COMPLETE)break;
                 
                }
                PmpLoadBuffer[1]= PumpRun[cc];
                cc++;
            }
             bb++;
        }
        else bb=0;
    }
     
    the only way that I could come up with was to use a "switch" statement but was hoping there was an easier way to do it.
     
  6. valknut

    valknut New Member

    Joined:
    Aug 7, 2017
    Messages:
    17
    Likes:
    0
    It doesn't help much if we can't see how all of the variables you're using were declared and initialized. Post the entire program.

    That said, I was able to spot a few problems from what you have provided.

    1) The bits within the pmptmr bitfield don't need to get shifted around; they're already "in position" (the whole purpose of bitfields is to allow you manipulate the bits of an object without shifts!). Also, most bitfields are packed into a union that defines another variable for accessing all the bits at once.

    Here's an example:

    Code (text):

    #include <stdio.h>

    typedef unsigned uint;

    #define SHOW(uint_expression)((void)printf("%s: 0x%x\n", #uint_expression, uint_expression))

    typedef union
    {
       struct
       {
            uint index_0: 1;  
            uint index_1: 1;  
            uint index_2: 1;
            uint index_3: 1;
            uint index_4: 1;
            uint index_5: 1;
            uint index_6: 1;
            uint index_7: 1;
       };
       uint all;
    }
       sample;

    void show_sample_bits(sample data)
    {
       puts("- all bits -");
       SHOW(data.all);
       puts("- individual bits -");
       SHOW(data.index_0);
       SHOW(data.index_1);
       SHOW(data.index_2);
       SHOW(data.index_3);
       SHOW(data.index_4);
       SHOW(data.index_5);
       SHOW(data.index_6);
       SHOW(data.index_7);
    }

    int main(void)
    {
       sample data;
       puts("Unsetting all bits...");
       data.all = 0;
       show_sample_bits(data);
       puts("Setting seventh bit...");
       data.index_7 = 1;
       show_sample_bits(data);
       puts("Directly setting zeroth and fourth bit (seventh will be erased)...");
       data.all = (1 << 0) | (1 << 4);
       show_sample_bits(data);
       puts("Unsetting zeroth bit...");
       data.index_0 = 0;
       show_sample_bits(data);
       return 0;
    }
     
    2) I2C1_MasterRead is expecting a pointer to an array of bytes as it's first parameter and a pointer to an I2C1_MESSAGE_STATUS object as it's last parameter. Unless you've declared "pdata" as a pointer-to-a-pointer, that's being passed in incorrectly, and for certain you should be passing the address of "status" to the function.

    3) It looks like you might be doing some unnecessary unpacking/packing of data bits, although it's hard to say for sure without seeing more code.
     
    Last edited: Aug 31, 2017

Share This Page