Making A Cylon Eye - Page 1
Forum section: electronics / projects

author: Lance
author_rank: Frequent Contributor
timestamp: February 20, 2011, 05:32:07 am
content: I'm trying to make 8 leds run like a cylon eye. Because the leds cover two different port ranges, I decided to try and make a little bitfield which I could then set the ports with. It failed horribly.The idea is to have 8 unsigned integers, one representing each light. Then just use some bitwise shifts to move the light around.Code:void Cylon(void){unsigned int counter=0;unsigned long i=0;struct light{unsigned int light0:1;unsigned int light1:1;unsigned int light2:1;unsigned int light3:1;unsigned int light4:1;unsigned int light5:1;unsigned int light6:1;unsigned int light7:1;}union light_union{struct light x;} Lights;Lights.x=(0b00000000u);}Actually trying to set the bits is being problematic, I get this error message:Code:illegal conversion between types unsigned int -> struct lightI'm not sure what to do here.

author: RayJones
author_rank: Frequent Contributor
timestamp: February 20, 2011, 06:29:07 am
content: You are close, but not quite right! (as you've probably figured!)You need to add an int to your union, and keep your struct as you have already.AlaCode:union light_union {int x;struct light bits;} Lights;You can then access x as you desire, and the magic of the union will set the bits broadside as you desire, likewise read them back.If you want change a bit, useCode:Lights.bits.light0 = 1; (or 0)

author: Lance
author_rank: Frequent Contributor
timestamp: February 20, 2011, 06:37:27 am
content: Quote from: RayJones on February 20, 2011, 06:29:07 amI see. Could you please explain to me why this is necessary?

author: RayJones
author_rank: Frequent Contributor
timestamp: February 20, 2011, 06:39:23 am
content: BTW, I've been coding for far too long, and still the union caught me with my pants down the other day.I knew what I wanted to achieve, but still made minced meat of it.The secret is each line in the union specifies a element, the largest element determines the end size of the union (space used in memory).Other elements will occupy the same memory space, but be interpreted according to their type, ie a bit field in your application.Other tricks could beCode:union {int AsInt;float AsFloat;}This allows you to write a float value, then grab the byte form equivalent for transfer/storage.When digging down to byte level though you need to be careful of the endian order of your processor as to where the LSB lives in memory for a 16 or 32 bit value.My personal fave isCode:union {int Word;struct {char LSB;char MSB;} bytes;}This is used to rip a 16 bit value into the HIWORD/LOWORD far more efficiently than the typical mask and shift operations used.Once again, take care with the endianess of your processor.

author: RayJones
author_rank: Frequent Contributor
timestamp: February 20, 2011, 06:44:32 am
content: I should also add, using int is processor specific as to how many bits it is.The native register size of the processor is usually adopted.ie for a 16 bit processor, ints will be 16 bits, likewise 32 bits for a 32 bit processor.to control the sizes better, you should use shorts or longs (16 or 32)char is invariably 8 bits, but not always!Having said this, you should realise my examples above are flawed, instead of int I should use long in the float example, and short in the HIWORD/LOWORD example.

author: Lance
author_rank: Frequent Contributor
timestamp: February 20, 2011, 06:51:15 am
content: Yeah, and for my specific program the x should be an unsigned integer. Well then, now I just need to make the shift logic work.If I remember correctly it should go something like this:Code:var<<var;If I want to shift bits one to the left.

author: RayJones
author_rank: Frequent Contributor
timestamp: February 20, 2011, 06:53:10 am
content: Actually, I should have used unsigned char in the first answer.This would be an unsigned 8 bit value - which is the same size as your structure.When it comes to bit fields, they have been a traditional sore point in compilers of old, hopefully better now, especially when some processors have bit wise opcodes built in.Also I'd suspect a lot of compliers would only be happy with an "int : X" specification (or "unsigned int : X")Hopefully you now have a bitt more knowledge to get the job done, but it may take a while for the details to settle in the brain.The combination of bit fields and unions is indeed pretty ambitious - many points for trying

author: RayJones
author_rank: Frequent Contributor
timestamp: February 20, 2011, 06:54:45 am
content: Quote from: Lance on February 20, 2011, 06:51:15 amNo,useCode:var<<1;your example would shift the bits by whatever value var was - definitely not what you want

author: Lance
author_rank: Frequent Contributor
timestamp: February 20, 2011, 07:02:44 am
content: Quote from: RayJones on February 20, 2011, 06:54:45 amYeah, that didn't work out to well for me. On the plus side between that failure and your reply I made a little binary counter. It's all red and pretty.I tried what you suggested, and it's currently content to sit there with the first light on:Code:if(first){Lights.x=0x01;first=0;}Lights.x<<1;for(i=0;i<10000;i++){}It's like it's not even shifting...

author: RayJones
author_rank: Frequent Contributor
timestamp: February 20, 2011, 07:04:17 am
content: Actually, if you do the right shift, you will see the difference between unsigned and signed chars:If var is a signed char (normal char) and you you start with the MSB set (ie -ve value), you will propagate the MSB to the right using var>>1;eg (char)0x80 >> 1 = 0xc0;If var is unsigned char, it will work as you expect, a zero being shifted into the MSBeg (unsigned char)0x80 >> 1 = 0x40;Your simple cylon eye has plenty of "traps for young players" (and old ones too)Will your cylon look like Tricia Helfer(#6) or Alessandra Toressani (Zoe Graystone)?

author: RayJones
author_rank: Frequent Contributor
timestamp: February 20, 2011, 07:07:13 am
content: Quote from: Lance on February 20, 2011, 07:02:44 amYou are only manipulating the internal memory.To change the LEDs you will need to write to the port with the new X value....

author: Lance
author_rank: Frequent Contributor
timestamp: February 20, 2011, 07:15:47 am
content: Quote from: RayJones on February 20, 2011, 07:04:17 amI wish.It's gonna look more like this:I know I have to set the ports, I omitted that part of the code. Incidentally it's still not working:Code:union light_union{unsigned int x;struct light bits;} Lights;if(!first){Lights.x=0x01;first=1;}Lights.x>>1;for(i=0;i<6000;i++){}As usual it sits there with the first light on. I checked to make sure it wasn't running through the if(first) statement, and it's not. Oh, and the variables I'm working with are unsigned integers.EDIT: I GOT IT!Lights.x=Lights.x<<1;That's how it should be! The bit shift is an operation like addition or subtraction. It was doing the calculation, just not storing the result anywhere.

author: RayJones
author_rank: Frequent Contributor
timestamp: February 20, 2011, 07:22:50 am
content: Yeah not as sexy as the on screen models!First off, change to an unsigned char. I presume you are accessing an 8 bit port?As stated earlier, this will always correctly line up with your structure's internal memory representation.There is always the possibility that the bit fields and union used in conjunction are conspiring against you, as I alluded to earlier.Can your compiler give you ASM code for what it has generated, and does that gel with your intents?

author: Lance
author_rank: Frequent Contributor
timestamp: February 20, 2011, 07:28:54 am
content: Quote from: RayJones on February 20, 2011, 07:22:50 amIt's an 8 bit port. I'm using a PIC16F917. I'm not sure if my compiler can export assembly, how to do such a thing, and I don't really want to at this moment. I don't know assembly. Although I would like to learn. I just don't have the spare time now.I guess you must have missed the edit, but it's working now. It's all red and pretty and does not want to kill all humans. Thanks for the help!Code:/******************************************************************************Header Includes******************************************************************************/#include "main.h"#include <pic.h>//#include <pic16f917.h>#include <killallhumans.h>Oops...