FRDM-K22F: Working with arrays

cancel
Showing results for 
Search instead for 
Did you mean: 

FRDM-K22F: Working with arrays

156 Views
Contributor III

Hi, I'm looking for an example of how to use arrays in my code. 

What I currently have is a CD4094 8-bit shift register connected to the FRDM-K22.

I'm using PTE0 as a clock, and PTE1 as my Data pin.

All the outputs of the shift register are tied to leds so I can confirm it works.

Below is a picture of the shift register if anyone's interested. 

This is more of a fun project for me.  I'm using KDS 3.2.0 and PE.

 pastedImage_1.png

Below this statement is how it would be connected, IF, I was using an ATtiny 13.

Also included is the code for the ATtiny below that.  The syntax used to create the array I assume is different than in KDS.  My biggest problem is correctly writing the code PRIOR to "int main(void) {...", i.e. initializing the array,

followed by what to write within  "int main(void) {...}" in order to use the array.

I did try, however, I haven't been successful.

Thanks,

Brian

pastedImage_2.png

pastedImage_3.png

Labels (1)
0 Kudos
3 Replies

53 Views
Specialist V

Hi Brian

It may be an issue with the compiler C programming language standard dialect (eg. C90, C99, C11) used by KDS (it may also be configurable if you play with it) that will not allow you to declare variables in a routine after code.

If you do this instead it may build:

int main(void) {
int i;
int data[] = {1,0,1,1,0,0,0,1};
DDRB |= ((1<<DDB0)|(1<<DDB4));
...

or if you put the second part in brackets it may also be OK:

int main(void) {
DDRB |= ((1<<DDB0)|(1<<DDB4));
...
    {
        int i;
        int data[] = {1,0,1,1,0,0,0,1};
        for (....
    }
}

I would also make int data[]
static const int data[]
to keep it in flash.


Also, the array looks to be a bit overkill in this case whereby

static const data = 0xb1;
for (i=0x80;i!=0;i>>=1) {
    if ((data & i) == 0) {PORTB = 0;}
    else {PORTB = (1<<DATA);}
    PORTB |= (1<<REG_CLK);
}


is both more efficient and more logical "when thinking in the byte value" to be sent.

Regards

Mark


uTasker developer and supporter (+5'000 hours experience on +60 Kinetis derivatives in +80 product developments)
Kinetis: http://www.utasker.com/kinetis.html

0 Kudos

53 Views
Contributor III

Hi Mark,

Thanks, that's put on the correct track.

I now see me clock signal that I created.

I would like to output each value in the array on PT1 but I'm not sure how to do that.

Here's what I have so far in my code, I'm not comfortable with my choice taking data[i] and passing it to Data_GetRawVal().

I've tried others but still not sucess.

pastedImage_1.png

pastedImage_2.png

0 Kudos

53 Views
Specialist V

Brian

I am sorry but I don't follow your code:

1. data[] has a size of 8 ints but your loop is using 16 entries and so over-running the end of the array.
2. data[i] (when i is in valid range) can be either 1 or 0 so the if (data[i] == 8) can never be true.
3. Data_GetRawVal() == data[i] is comparing the function's return value with the array entry but the compare is useless.

Check the console output when you compile this since I would expect the compiler to already generate one or two warnings due to this.
Run lint over the program and it will probably give half a dozen warnings.

Regards

Mark

0 Kudos