简体   繁体   中英

How to store MCU-GPIO pin status (value) to an array of 10 numbers in Embedded C?

I want to read the MCU GPIO pin status and store it to an array of 10 numbers repeatedly. When the array is full, it should left shift the value and store the new value to a [9]th subscript position and continue.

How can i implement this as code in Embedded C ?

Given that you're only storing 1 bit each time, an array isn't the only solution:

static uint16_t buffer = 0;

void gpio_add(bool pin_value)
{
    buffer >>= 1;
    if(pin_value) {buffer |= 0x0200;}
}

bool gpio_get_entry(uint8_t index)
{
    return !!(buffer & (1 << index));
}

Note that if you're taking this approach, you may as well store either 8 or 16 values.

If the purpose of this is to implement a simple debouncer (ie to determine if the pin level has remained stable for a while), then we can simply see if buffer is zero or 0x3FF.

If I understood well, it should look like something like this:

void insert(int* arr, int size, int value){
    int i = 0;
    for(int i=0; i < size-1; i++){
        arr[i] = arr[i+1];
    }
    arr[size-1] = value;
}

With size, the size of you're array (10 for you). You should initialize you're array with 0 or -1 to be sure. However I don't know the difference between ansi C and embedded C. Maybe "for(int i = ;...)" doesn't work and you'll have to create the variable i just before. Not sure if that's what you wanted.

Not sure what you mean exactly by 'Embedded C', but I write C for embedded systems, and I'd use something like the following. (The GpioPrint() function is really only for (my) debugging.)

#include <stdio.h>
#include <string.h>

// Returns the size of a[].
#define ARRAY_SZ(a) (sizeof a / sizeof a[0])

// Holds MCU-GPIO pin values.
typedef struct
{
    unsigned a[10]; // replace unsigned with your 'pin value' type
    unsigned n;     // values are in a[0..n-1]
} Gpio_t;

// Initialise the pin value store.
static void GpioInit(Gpio_t *gpio)
{
    gpio->n = 0;
}

// Add the new value to the pin store, removing the oldest if the store
// is full.
static void GpioAdd(Gpio_t *gpio, unsigned newVal)
{
    if (gpio->n < ARRAY_SZ(gpio->a))
    {
        gpio->a[gpio->n++] = newVal;
    }
    else
    {
        memmove(&gpio->a[0], &gpio->a[1], (gpio->n - 1) * sizeof gpio->a[0]);

        gpio->a[gpio->n - 1] = newVal;
    }
}

// Output the pin store contents.
static void GpioPrint(const Gpio_t *gpio)
{
    unsigned i;

    for (i = 0; i < gpio->n; i++)
    {
        printf("%2u: %10u\n", i, gpio->a[i]);
    }
}

// Test the functions.
int main(void)
{
    Gpio_t   gpio;
    unsigned newVal;

    GpioInit(&gpio);

    // Add 20 values to the pin store, checking the contents after each
    // addition.
    for (newVal = 0; newVal < 20; newVal++)
    {
        printf("add %u:\n", newVal);

        GpioAdd(&gpio, newVal);
        GpioPrint(&gpio);
    }

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM