Author Topic: Fun little program I wrote for the ATMega - had to share  (Read 3784 times)

0 Members and 1 Guest are viewing this topic.

Offline HDL_CinC_DragonTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,261
  • Helpful? 5
Fun little program I wrote for the ATMega - had to share
« on: March 05, 2008, 11:04:21 PM »
I was fiddling with my bread boarded ATMega8 and decided to do some stuff with frequencies. So I put a piezo disc between 5v and pin 14(PB0) had the following code:

Code: [Select]
#include "SoR_Utils.h" //Include the SoR_Utils header that came with the $50 robot code

//21000 cycles is about 1 second
int x = 0; //Create an integer with the value of '0'

int main(void)
{
DDRB = 0xFF; // Set all B ports to outputs
DDRC = 0x00; // Set all C ports to intputs
DDRD = 0xFF; // Set all D ports to outputs

  //PORT ID 0b76543210
PORTB = 0b00000000; // Set all B ports low
PORTC = 0x00; // Set all C ports low
PORTD = 0b00010000; // Set PD4 high and the other D ports to low
while(1) // Basically just makes it loop forever - WARNING! BAD CODING STYLE! DONT DO IT! USE INTEGERS(0 or 1) OR BOOLEANS(TRUE or FALSE!
{
while(x < 100) //While x is less than 100(which it is because its 0 to start off with)
{
PORTB = 0b00000001; //Set PB0 high so the current in both directions cancel each other out(no flow)
PORTD = 0b00000000; //Set all PD ports low [The LED from the $50 schematic is on a PD(PD4 to be exact)]
delay_cycles(x*2); //Wait as many cycles as x*2 is equal to (starts off at 200 cycles, then 198, then 196, etc, etc)
PORTB = 0b00000000; //Set all PB ports low so the current flows into them
PORTD = 0b00010000; //Set PD4 high so the current cannot get into it
x++; //take x and then increment(add 1) it
}
while(x > 0) //x should now be equal to 100 which is greater than 0
{
PORTB = 0b00000001; //Set PB0 high so current cant flow through
PORTD = 0b00000000; //Set all PD ports low so current can flow
delay_cycles(x); //Delay as many cycles as x represents (starting at 100 going down to 0)
PORTB = 0b00000000; //Set all PB ports to low so current can flow
PORTD = 0b00010000; //Set PD4 high so current cannot flow
x--;
}
while(x < 100)
{
PORTB = 0b00000001;
PORTD = 0b00000000;
delay_cycles(x*2);
PORTB = 0b00000000;
PORTD = 0b00010000;
x++;
}
x = 200; //I did this instead of x=25 and x > 0 because the number 'x' represents the frequency being emitted not the amount of cycles to be delayed(in this case 25 cycles are delayed). The difference between x and the desired stop point determines the amount of cycles delayed
while(x > 175)
{
PORTB = 0b00000001;
PORTD = 0b00000000;
delay_cycles(x);
PORTB = 0b00000000;
PORTD = 0b00010000;
x--;
}
x=0; // I had to do this because 'x' is equal to 175 now which would throw off the first 3 while loops in the code
}
}

Hope you have as much fun with this as I am right now haha
Experiment with different numbers and such and try to create a cool tune :) The one reminds me of "Strict Machine" by "GoldFrapp" for some reason lol
« Last Edit: March 05, 2008, 11:14:41 PM by HDL_CinC_Dragon »
United States Marine Corps
Infantry
Returns to society: 2014JAN11

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: Fun little program I wrote for the ATMega - had to share
« Reply #1 on: March 15, 2008, 01:36:47 PM »
A client of mine has asked me to use a piezo buzzer, so I'll be fiddling around with one too.

Feel free to post any songs you write, and I'll post my tunes too probably within a month or so after I finish.

Offline HDL_CinC_DragonTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,261
  • Helpful? 5
Re: Fun little program I wrote for the ATMega - had to share
« Reply #2 on: March 15, 2008, 02:39:31 PM »
My work is going very slowly as life gets much more stressful but I definitely intend to write some tunes =)
Im hoping to fill its entire memmory block with song coding :) but first im working on writing a utility to make it easier and more efficient to write songs using a piezo :)
United States Marine Corps
Infantry
Returns to society: 2014JAN11

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: Fun little program I wrote for the ATMega - had to share
« Reply #3 on: March 21, 2008, 05:32:01 PM »
Quote
but first im working on writing a utility to make it easier and more efficient to write songs using a piezo
I've heard of people writing programs that accept MIDI files and output piezo files for PICs. Im sure you can find one if you browse around a bit.

Offline HDL_CinC_DragonTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,261
  • Helpful? 5
Re: Fun little program I wrote for the ATMega - had to share
« Reply #4 on: March 22, 2008, 01:33:03 AM »
nah, it wouldnt be as fun as writing my own :) Thanks though
United States Marine Corps
Infantry
Returns to society: 2014JAN11

 


data_list