AVR code for dice

A six sided die simulator

//
// dice_next_steps.c
//
//
// Created by daniel on 10/27/14.
//
//
#include <stdio.h>
#include <stdlib.h>
#include <avr/io.h>
#include <util/delay.h>
#include <avr/pgmspace.h>
void showPips(int value) { // present the outcome of the die toss

switch(value) {
case 0: PORTA = 0b00001000; PORTB = 0; break;
case 1: PORTA = 0b00101000; PORTB = 0; break;
case 2: PORTA = 0b00001000; PORTB = 0b00000100; break;
case 3: PORTA = 0b00101000; PORTB = 0b00000100; break;
case 4: PORTA = 0b01001000; PORTB = 0b00000100; break;
case 5: PORTA = 0b01101000; PORTB = 0b00000100; break;
case 6: PORTA = 0b11001000; PORTB = 0b00000100; break;
case 7: PORTA = 0b11101000; PORTB = 0b00000100; break;
}
}

void pinwheel(void){
int j;
int pinwhirls = 4;
for(j=1; j<=pinwhirls; j++){
PORTA = 0b00101000; PORTB = 0b00000100;
_delay_ms(10);
PORTA = 0b10101000; PORTB = 0;
_delay_ms(10);
PORTA = 0b01101000; PORTB = 0;
_delay_ms(10);
PORTA = 0b00101000; PORTB = 0b00000000;
_delay_ms(10);
PORTA = 0b11001000; PORTB = 0b00000100;
_delay_ms(10);
}
}

/* return a uniform random value in the range 0..n-1 inclusive from Yale CS http://www.cs.yale.edu/homes/aspnes/pinewiki/C(2f)Randomization.html*/

int
randRange(int n)
{
int limit;
int r;

limit = RAND_MAX – (RAND_MAX % n);

while((r = rand()) >= limit);

return r % n;
}

int main(void){
DDRA = 0b11100000; //sets output lines
DDRB = 0b00000100;
PORTA = 0b00001000; PORTB = 0b00000000; //Turns off LEDs and sets switch pin internal pull-up.

int temp;
int value1;
int value2;
int seed = 0;

while(1){
temp = (PINA & 0b00001000);
srand(seed++);

while(temp == 0){
int i;
for(i=0;i<=50;i++){
showPips(randRange(8));
_delay_ms(1);
};
showPips(value1 = randRange(6)+1);
_delay_ms(80);

for (i=0; i<=7; i++){
showPips(i);
_delay_ms(7);
};
showPips(value2 = randRange(6)+1);
_delay_ms(80);

if (value1+value2 == 7){
pinwheel();
}

showPips(0);
temp = (PINA & 0b00001000);
}
}
return(0);
}

PewPewPew code

pwpwpw_side
//Daniel Rosenberg for MAS863.14
//a program to run a realistic 6 shot ray gun.
//press the button for single shot, hold for six, slowly slowing.
//11/18/2014
const int HBin1 = 2;
const int HBin2 = 1;
const int buttonPin = 3;
int buttonPushCounter = 0;  //unused
int buttonState = 0;         
int lastButtonState = 1;
int pews = 1;
int n = 0;
int m = 10;
int o = 1;
void setup(){
    pinMode(buttonPin, INPUT);
    pinMode(HBin1, OUTPUT);
    pinMode (HBin2, OUTPUT);
}
void loop(){
  buttonState = digitalRead(buttonPin);    // read the pushbutton input pin
  delay(1);     //debounce
  if (buttonState != lastButtonState) {pews = 0; lastButtonState = buttonState;}    //reset pew counter
  if (buttonState == 0) {        //button is pressed
      if (pews <= 5) {
         for(o = 100; o >= 40; o -= 10){        //yeah, I used “o” as a counter variable.
           for(m = 1; m <= 100; m++){
              digitalWrite(HBin2,1);
              digitalWrite(HBin1,0);
              delayMicroseconds(m);         //push the current one way for m microseconds
              digitalWrite(HBin2,1);
              digitalWrite(HBin1,1);          //turns on the brake
              delay(m/o);                            //turn off for ten to twenty five times longer.
              digitalWrite(HBin2,0);
              digitalWrite(HBin1, 1);
              delayMicroseconds(m);     //push the current t’other way for m microseconds.
              digitalWrite(HBin2,1);
              digitalWrite(HBin1,1);     //brakes
              delay(m/o);      //same delay for symmetry
              }// end for m
            }//end for o
            delay(100*pews);     //slow down successive shots
        }    //end if pews less than number of shots
        pews++;    //increment for back to if (button is pressed) test.
        delay(10);    //small delay for timing three seconds button pressed beyond six pews
        if(pews >= 300){     //oh,no, you’ve overheated the coils
          for (n=1; n<= 2000; n++){     //make some noise
            digitalWrite(HBin2,1);
              digitalWrite(HBin1,0);
              delayMicroseconds(random(20));
              digitalWrite(HBin2,1);
              digitalWrite(HBin1,1);
              delay(random(10)/5);
              digitalWrite(HBin2,0);
              digitalWrite(HBin1,1);
              delayMicroseconds(random(20));
              digitalWrite(HBin2,1);
              digitalWrite(HBin1,1);
              delay(random(10)/5);
          }   //end for n
        }   //end if pews > 300
    }    //end if button is pressed.
}   //end loop
   

Final Project Code

fandrive_board

Fan Driver software

//
// fan driver software
//
//
// cobbled together by daniel on 12/13/14.
//
//

#include <avr/io.h>
#include <util/delay.h>
#include <avr/power.h>
#include
#include <avr/pgmspace.h>
#include

#define output(directions,pin) (directions |= pin) // set port direction for output
#define input(directions,pin) (directions &= (~pin)) // set port direction for input
#define set(port,pin) (port |= pin) // set port pin
#define clear(port,pin) (port &= (~pin)) // clear port pin
#define pin_test(pins,pin) (pins & pin) // test for port pin
#define bit_test(byte,bit) (byte & (1 << bit)) // test for bit set
#define bit_delay_time 100 // bit delay for 9600 with overhead
#define bit_delay() _delay_us(bit_delay_time) // RS232 bit delay
#define half_bit_delay() _delay_us(bit_delay_time/2) // RS232 half bit delay
#define led_delay() _delay_ms(100) // LED flash delay
#define char_delay() _delay_ms(10) // char delay

#define serial_port_in PORTB //modified defines from hello.
#define serial_direction_in DDRB
#define serial_pins PINB
#define serial_port_out PORTA
#define serial_direction_out DDRA
#define serial_pin_in (1 << PB2)
#define serial_pin_out (1 << PA7)

#define other_port PORTA // defines for board communication
#define other_direction DDRA
#define other_pins PINA
#define other_pin_in (1 << PA6) //reverse of tempsense definition
#define other_pin_out (1 << PA4)

#define node_id ’99’
#define max_buffer 25

void get_char(volatile unsigned char *pins, unsigned char pin, char *rxbyte) {
//
// read character into rxbyte on pins pin
// assumes line driver (inverts bits)

*rxbyte = 0;
while (pin_test(*pins,pin)); // wait for start bit

half_bit_delay();
bit_delay(); // delay to middle of first data bit

if pin_test(*pins,pin) // unrolled loop to read data bits
*rxbyte |= (1 << 0);
else
*rxbyte |= (0 << 0);
bit_delay();
if pin_test(*pins,pin)
*rxbyte |= (1 << 1);
else
*rxbyte |= (0 << 1);
bit_delay();
if pin_test(*pins,pin)
*rxbyte |= (1 << 2);
else
*rxbyte |= (0 << 2);
bit_delay();
if pin_test(*pins,pin)
*rxbyte |= (1 << 3);
else
*rxbyte |= (0 << 3);
bit_delay();
if pin_test(*pins,pin)
*rxbyte |= (1 << 4);
else
*rxbyte |= (0 << 4);
bit_delay();
if pin_test(*pins,pin)
*rxbyte |= (1 << 5);
else
*rxbyte |= (0 << 5);
bit_delay();
if pin_test(*pins,pin)
*rxbyte |= (1 << 6);
else
*rxbyte |= (0 << 6);
bit_delay();
if pin_test(*pins,pin)
*rxbyte |= (1 << 7);
else
*rxbyte |= (0 << 7);

bit_delay(); // wait for stop bit
half_bit_delay();
}

void put_char(volatile unsigned char *port, unsigned char pin, char txchar) {
//
// send character in txchar on port pin
// assumes line driver (inverts bits)

clear(*port,pin); // start bit
bit_delay();

if bit_test(txchar,0) // unrolled loop to write data bits
set(*port,pin);
else
clear(*port,pin);
bit_delay();
if bit_test(txchar,1)
set(*port,pin);
else
clear(*port,pin);
bit_delay();
if bit_test(txchar,2)
set(*port,pin);
else
clear(*port,pin);
bit_delay();
if bit_test(txchar,3)
set(*port,pin);
else
clear(*port,pin);
bit_delay();
if bit_test(txchar,4)
set(*port,pin);
else
clear(*port,pin);
bit_delay();
if bit_test(txchar,5)
set(*port,pin);
else
clear(*port,pin);
bit_delay();
if bit_test(txchar,6)
set(*port,pin);
else
clear(*port,pin);
bit_delay();
if bit_test(txchar,7)
set(*port,pin);
else
clear(*port,pin);
bit_delay();

set(*port,pin); // stop bit

bit_delay(); // char delay
bit_delay();
}

void put_string(volatile unsigned char *port, unsigned char pin, char *str) {
//
// print a null-terminated string
//
static int index;
index = 0;
do {
put_char(port, pin, str[index]);
++index;
} while (str[index] != 0);
}

void initLEDS(void){
DDRB |= (1 << PB2); //green led
DDRA |= (1 << PA7); //red led
}

void multi_flash(void){ //wagging the dog

uint8_t n = 0;

for (n = 0; n <= 4; n++){
PORTA = 0xff;
PORTB = 0;
_delay_ms(100);
PORTA = 0;
PORTB = 0xff;
_delay_ms(100);
}
}

void initSerialPins(void){ // had to specify port_in and port_out as they are different on my board

set(serial_port_out, serial_pin_out);
output(serial_direction_out, serial_pin_out);
set(serial_port_in, serial_pin_in);
input(serial_direction_in, serial_pin_in);

}

void initOtherPins(void){ // inter[-board communication

set(other_port, other_pin_out);
output(other_direction, other_pin_out);
set(other_port, other_pin_in);
input(other_direction, other_pin_in);

}

void initFan(void){

DDRA |= (1 << PA3);

}

int main(void){

clock_prescale_set(clock_div_1);

//static char chr;
//uint8_t i = 0;
initLEDS();
multi_flash();

initSerialPins();
initOtherPins();

initFan();

while(1){
if (PINA & (1 << PA6)) //When the tempsense module reads over 130 a.u.
PORTA |= (1 << PA3);
else
PORTA &= ~(1 << PA3);
_delay_ms(1000);
}
return(0);
}

tempsens_board

Temperature sensor software

//
// temperature sensor software
//
//
// lifted and altered by daniel on 12/4/14.
//
//

#include <avr/io.h>
#include <util/delay.h>
#include <avr/power.h>
#include
#include <avr/pgmspace.h>
#include

#define output(directions,pin) (directions |= pin) // set port direction for output
#define input(directions,pin) (directions &= (~pin)) // set port direction for input
#define set(port,pin) (port |= pin) // set port pin
#define clear(port,pin) (port &= (~pin)) // clear port pin
#define pin_test(pins,pin) (pins & pin) // test for port pin
#define bit_test(byte,bit) (byte & (1 << bit)) // test for bit set
#define bit_delay_time 100 // bit delay for 9600 with overhead
#define bit_delay() _delay_us(bit_delay_time) // RS232 bit delay
#define half_bit_delay() _delay_us(bit_delay_time/2) // RS232 half bit delay
#define led_delay() _delay_ms(100) // LED flash delay
#define char_delay() _delay_ms(10) // char delay

#define serial_port_in PORTB
#define serial_direction_in DDRB
#define serial_pins PINB
#define serial_port_out PORTA
#define serial_direction_out DDRA
#define serial_pin_in (1 << PB2)
#define serial_pin_out (1 << PA7)
#define other_port PORTA
#define other_direction DDRA
#define other_pins PINA
#define other_pin_in (1 << PA4)
#define other_pin_out (1 << PA6)

#define node_id ’99’
#define max_buffer 25

void get_char(volatile unsigned char *pins, unsigned char pin, char *rxbyte) {
//
// read character into rxbyte on pins pin
// assumes line driver (inverts bits)

*rxbyte = 0;
while (pin_test(*pins,pin)); // wait for start bit

half_bit_delay();
bit_delay(); // delay to middle of first data bit

if pin_test(*pins,pin) // unrolled loop to read data bits
*rxbyte |= (1 << 0);
else
*rxbyte |= (0 << 0);
bit_delay();
if pin_test(*pins,pin)
*rxbyte |= (1 << 1);
else
*rxbyte |= (0 << 1);
bit_delay();
if pin_test(*pins,pin)
*rxbyte |= (1 << 2);
else
*rxbyte |= (0 << 2);
bit_delay();
if pin_test(*pins,pin)
*rxbyte |= (1 << 3);
else
*rxbyte |= (0 << 3);
bit_delay();
if pin_test(*pins,pin)
*rxbyte |= (1 << 4);
else
*rxbyte |= (0 << 4);
bit_delay();
if pin_test(*pins,pin)
*rxbyte |= (1 << 5);
else
*rxbyte |= (0 << 5);
bit_delay();
if pin_test(*pins,pin)
*rxbyte |= (1 << 6);
else
*rxbyte |= (0 << 6);
bit_delay();
if pin_test(*pins,pin)
*rxbyte |= (1 << 7);
else
*rxbyte |= (0 << 7);

bit_delay(); // wait for stop bit
half_bit_delay();
}

void put_char(volatile unsigned char *port, unsigned char pin, char txchar) {
//
// send character in txchar on port pin
// assumes line driver (inverts bits)

clear(*port,pin); // start bit
bit_delay();

if bit_test(txchar,0) // unrolled loop to write data bits
set(*port,pin);
else
clear(*port,pin);
bit_delay();
if bit_test(txchar,1)
set(*port,pin);
else
clear(*port,pin);
bit_delay();
if bit_test(txchar,2)
set(*port,pin);
else
clear(*port,pin);
bit_delay();
if bit_test(txchar,3)
set(*port,pin);
else
clear(*port,pin);
bit_delay();
if bit_test(txchar,4)
set(*port,pin);
else
clear(*port,pin);
bit_delay();
if bit_test(txchar,5)
set(*port,pin);
else
clear(*port,pin);
bit_delay();
if bit_test(txchar,6)
set(*port,pin);
else
clear(*port,pin);
bit_delay();
if bit_test(txchar,7)
set(*port,pin);
else
clear(*port,pin);
bit_delay();

set(*port,pin); // stop bit

bit_delay(); // char delay
bit_delay();
}

void put_string(volatile unsigned char *port, unsigned char pin, char *str) {
//
// print a null-terminated string
//
static int index;
index = 0;
do {
put_char(port, pin, str[index]);
++index;
} while (str[index] != 0);
}

void initLEDS(void){
DDRB |= (1 << PB2); //green led
DDRA |= (1 << PA7); //red led
}

void runADC(void){
ADCSRA |= (1 << ADEN);
ADCSRA |= (1 << ADATE);
ADCSRA |= (1 << ADSC);
}

void multi_flash(void){

uint8_t n = 0;

for (n = 0; n <= 4; n++){
PORTA = 0xff;
PORTB = 0;
_delay_ms(100);
PORTA = 0;
PORTB = 0xff;
_delay_ms(100);
}
}

void initADC23(void){
ADMUX |= ((0 << REFS1)|(0 << REFS0)); //VCC ref.
ADMUX |= 0b00000011; //PA3 no gain
ADCSRA |= ((1 << ADPS2)|(1 << ADPS1)|(1 << ADPS0)); //ADC clock /128
ADCSRA |= (1 << ADEN); //enable!
ADCSRB &= ~(1 << BIN); //not bipolar
ADCSRB |= (1 << ADLAR);
DIDR0 |= ((1 << ADC2D)|(1 << ADC3D)); //turn off digital buffers on PA2 and PA3
initLEDS();
multi_flash();
}

void initSerialPins(void){ //modularizing example code

set(serial_port_out, serial_pin_out);
output(serial_direction_out, serial_pin_out);
set(serial_port_in, serial_pin_in);
input(serial_direction_in, serial_pin_in);

}

void initOtherPins(void){ //modifying and modularizing example code

set(other_port, other_pin_out);
output(other_direction, other_pin_out);
set(other_port, other_pin_in);
input(other_direction, other_pin_in);

}

void sendDecimal(uint8_t number){ //from Make:AVR programming
put_char(&serial_port_out, serial_pin_out, ‘0’ + (number/100));
put_char(&serial_port_out, serial_pin_out, ‘0’+ (number/10) % 10);
put_char(&serial_port_out, serial_pin_out, ‘0’ + (number % 10));
put_char(&serial_port_out, serial_pin_out, 32);
}

int main(void){

clock_prescale_set(clock_div_1); /* I can see why coding is so much like working in the machine shop. There are so many ways of getting to product. My code guru, Allen Crockett, is not happy with my mixing up styles. I respect his aesthetic, but must stay true to my own, which I confess could be called erratic. */

initADC23();

static char chr;

initSerialPins();
initOtherPins();
//uint16_t Tdata,T32 = 0;
// uint8_t i = 0;
//char what = 25;
// initLEDS();
// runADC();

while (1) {
//
// send framing
//
/* put_char(&serial_port_out, serial_pin_out, ‘1’);
char_delay();
put_char(&serial_port_out, serial_pin_out, ‘2’);
char_delay();
put_char(&serial_port_out, serial_pin_out, ‘3’);
char_delay();
put_char(&serial_port_out, serial_pin_out, ‘4’);
char_delay();
*/
_delay_ms(1000);

ADCSRA |= (1 << ADSC); // initiate conversion

while (ADCSRA & (1 << ADSC)); // send result chr = ADCH; sendDecimal(chr); char_delay(); if(ADCH > 130) {
PORTA &= ~(1 << PA6);
} //thermo-switch by arb.units: freezer=52,RT~120,finger=142
else PORTA |= (1 << PA6);
}

return(0);
}