Important Note: In order for our digital dice to work correctly the internal oscillator of the ATMega8515 microcontroller must be enabled and set to 4MHz.
Software for the Digital Dice
Below is the AVR C implementation of the entire code for the ATMega8515 based double digit digital dice. The code was implemented and built using AVR Studio 5. Be reminded that the internal clock of the ATMega8515 microcontroller should be enabled and programmed to operate at 4MHz for the AVR C code to operate correctly. A video of the digital dice in operation is presented at the end of this page.
Please see the Interfacing a Seven Segment Display with the AVR Microcontroller tutorial for more information on the seven segment display.
Below is the AVR C code for the ATMega8515 Based Double Digit Digital Dice. This code was written in AVR Studio 5.
/* * DigitalDice_DoubleDigit.asm * Written in AVR Studio 5 * Compiler: AVR GNU C Compiler (GCC) * * Created: 10/24/2011 4:34:14 PM * Author: AVR Tutorials * Website: AVR-Tutorials.com * * Description: AVR C program for ATMega8515 Seven Segment double digit * digital dice */ #define DicePort PORTC #define DicePortDDR DDRC #define CntrlPortDDR DDRE #define CntrlPort PORTE #define CntrlPin PINE #define Dice1CntrlPin 0 #define Dice2CntrlPin 1 #define RollCntrlPin 2 #define F_CPU 4000000UL #include <avr/io.h> #include <util/delay.h> #include <avr/interrupt.h> unsigned char seed = 0; /*Declaration of functions. The implementation of these */ /*function are done below the main() function*/ unsigned char digit_to_7segval(unsigned char digit); void roll_dice(unsigned char times); void init(); ISR(TIMER0_OVF_vect); int main(void) { unsigned char digit = 6; unsigned char digit2 = 6; init(); // Configure the Microcontroller /*Do forever*/ while(1) { if(!(CntrlPin & (1<<RollCntrlPin))) { /*Roll the dice 2 times*/ CntrlPort = ~(1<<Dice1CntrlPin | 1<<Dice2CntrlPin); roll_dice(2); /*Generate two integer based random number between */ /*1 and 6 inclusive and store it in digit & digit2 */ seed = (seed*17)+21; digit = seed % 6; digit++; seed = (seed*17); digit2 = seed % 6; digit2++; } /*Get the Seven Segment values of the random numbers*/ /*and output it to the pins of port B*/ CntrlPort = ~(1<<Dice1CntrlPin); DicePort = digit_to_7segval(digit); _delay_ms(10); CntrlPort = ~(1<<Dice2CntrlPin); DicePort = digit_to_7segval(digit2); _delay_ms(10); } } /*Configuration for the microcontroller*/ void init() { /*Configure DicePort as an output port*/ DicePortDDR = 0xFF; /*Configure CntrlPort as an input port*/ CntrlPortDDR = ~(1<<RollCntrlPin); /*Enable RollCntrlPin pull-up resistors*/ CntrlPort = 1<<RollCntrlPin; TCCR0 = 1<<CS00; // Enable Timer0 with no prescalar TIMSK = 1<<TOIE0; // Enable Timer0 interrupt sei(); // Enable Global Interrupt } /*This function takes an character value and return the */ /*and return the value to be outputted to display that */ /*character on a seven segment display*/ unsigned char digit_to_7segval(unsigned char digit) { unsigned char segval = '0'; if(digit == 1) segval = 0x06; // 7-Seg Value for Digit 1 else if (digit == 2) segval = 0x5B; // 7-Seg Value for Digit 2 else if (digit == 3) segval = 0x4F; // 7-Seg Value for Digit 3 else if (digit == 4) segval = 0x66; // 7-Seg Value for Digit 4 else if (digit == 5) segval = 0x6D; // 7-Seg Value for Digit 5 else if (digit == 6) segval = 0x7D; // 7-Seg Value for Digit 6 return segval; } /*This function simulates the rolling of dice*/ void roll_dice(unsigned char times) { unsigned char i; for(i=0; i<times; i++) { DicePort = 0x06; // Display #1 on dice _delay_ms(200); DicePort = 0x5B; // Display #2 on dice _delay_ms(200); DicePort = 0x4F; // Display #3 on dice _delay_ms(200); DicePort = 0x66; // Display #4 on dice _delay_ms(200); DicePort = 0x6D; // Display #5 on dice _delay_ms(200); DicePort = 0x7D; // Display #6 on dice _delay_ms(200); } } /*Interrupt Service Routine for timer overflow*/ ISR(TIMER0_OVF_vect) { seed++; // Increment the value in the variable seed }
Video of the ATMega16 Digital Dice in Operation
AVR Tutorials hopes this AVR project on the ATMega8515 digital dice was benificial to you and looks forward to your continued visits for all your AVR microcontroller projects needs.