Hi all, i have a problem which i was hoping someone could help me with. I am attempting a task that creates a set of 3 random numbers which are stored in three separate characters. These numbers are only between 0 and 3 so will only ever use a max of 2 bits. Given this my problem, i need to display each of these numbers on a set of 8 LEDS simultaneously using a global interrupt every 250ms. Each random number is generated on 2 LEDS with a space between each, which the space LED is not active. Eg. 11011011 where the 11's are where i display each random number. My interrupt works fine, the only problem is i have no idea how to display 3 different values on specific bits of the Port they are on at the same time. Any suggestions/help would be appreciated? I'm using an ATMEGA8 device, and my knowledge of C is relatively basic but i do have have an understanding of pretty much everything else im doing at the moment.
Do you know the bitwise operators? If not, you should learn them.
Yes i do, but still unsure how to go about it.
So you've got three steps to do:
1. Build a number that has your first random number in bits 6 and 7 and the rest zero.
2. Now change bits 3 and 4 to be your second number.
3. Now change bits 0 and 1 to be your third number.
The first one is pretty easy; the second one might take a bit more thinking, so look at your truth tables. The third is the same as the second, more or less, so that won't take long either.
Then put the three together in order and you're done.
Thanks for the help, i managed to do it in a similar way but i was a little unsure how to manipulate singular bits to a value, but i just set or unset bits based on what the random value was.