NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Project Help and Ideas » Can you program the Atmel128 to do the project "Music with a microcontroler" Please help ASAP

March 14, 2010
by 787pilot
787pilot's Avatar

I have the USB nerdkit and I dont have any money for Atmel ATtiny26L so I was wondering If you could program the AtmeL128 for the project " Music with a microcontroler"? please help

March 14, 2010
by hevans
(NerdKits Staff)

hevans's Avatar

Hi 787pilot,

You can absolutely program the Atmega168 to do the Music with a microcontroller program, it just takes a few modifications. If you take a look through the code there are only a few things that need some modifying. You need to change the lcd and delay.h includes at the top to point to the libnerdkits folder. You need to change the F_CPU define at the top to 14745600, since we are using a 14.7Mhz crystal as opposed to an 8Mhz crystal. In the code on our site we use PA1 for the Buzzer and PA7 for the button that starts the music. You just need to wire up the button and the Buzzer wherever you choose, and change the parts of the code accordingly. Hope that gets you started.

Humberto

March 14, 2010
by 787pilot
787pilot's Avatar

What do I need to change in icd/delay.h?

March 14, 2010
by mcai8sh4
mcai8sh4's Avatar

I think, although I've not done it myself - you just need to alter the location of the delay.h and lcd.h to match there positions. So rather than

#include "delay.h"

alter it to

#include "../libnerdkits/delay.h"

Something along those lines.

Let us know how you get on.

-Steve

March 14, 2010
by 787pilot
787pilot's Avatar

It is not working! please help

March 14, 2010
by 787pilot
787pilot's Avatar

It is not working! please help

March 14, 2010
by hevans
(NerdKits Staff)

hevans's Avatar

Hi 787pilot,

We need more information before we can help you. Please let us know what errors you are getting, or where you are getting confused, and include a description of how you are hooking your circuit it up (if you think that is where the problem is). Describing a problem thoroughly is a great skill to learn =)

Humberto

March 14, 2010
by 787pilot
787pilot's Avatar

I upload every thing then when i flip the switch i just get black bars

March 14, 2010
by hevans
(NerdKits Staff)

hevans's Avatar

Hi 787pilot,

This usually means that you either have the chip in programming mode, or the wires to the LCD are loose. Check those two things to make sure that is not the problem. Are you able to compile and upload the code successfully?

Humberto

March 14, 2010
by 787pilot
787pilot's Avatar

Yes humbarto i can compile it sucessfully but the swithc is down I will also cheak the conections

March 14, 2010
by 787pilot
787pilot's Avatar

The LCD wires are in great I dont know what is wrong!

March 14, 2010
by hevans
(NerdKits Staff)

hevans's Avatar

Hi 787pilot,

Can you load another program onto your MCU that also uses the LCD, like initialload onto your chip. This will make sure that you have programming process correct, and that your LCD is hooked up correctly. Remember to take it slow, change only one thing at a time, and you will methodically be able to solve the problem.

Humberto

March 14, 2010
by 787pilot
787pilot's Avatar

I uploaed tempsensor and it worked but the music procram will only give me black lines

March 14, 2010
by hevans
(NerdKits Staff)

hevans's Avatar

Hi 787pilot,

A quick read through of the code reveals that the musicbox1.c code is using lcd_blank_line(), which is no longer a function in lcd.c. Your best bet is to replace lcd_quick_clear_and_home() in the main function with lcd_clear_and_home() which is part of the lcd.c found in libnerdkits. Hopefully that does the trick. Make sure you watch the output of the command line when you are compiling and watch for any errors or warnings that come up.

Humberto

March 14, 2010
by 787pilot
787pilot's Avatar

The LCD still has 2 black lines and I did what you said any other options

March 14, 2010
by hevans
(NerdKits Staff)

hevans's Avatar

Well that just means there is something else causing the problem that we are not anticipating. Could you provide a link to the musicbox1.c file you are using (or just copy the text into a post, make sure you put 4 spaces in front of each line so it it shows up as code) . As well as the Makefile you are using to upload it. Perhaps there is something subtly wrong there.

Humberto

March 15, 2010
by 787pilot
787pilot's Avatar

here is musicbox1.c (under it is the makefile)Hope this helps

// musicbox1.c // for NerdKits with ATtiny26L // mrobbins@mit.edu

// F_CPU defined for delay.c

define F_CPU 14745600UL // 8MHz

include <avr/io.h>

include <avr/interrupt.h>

include <avr/pgmspace.h>

include <util/delay.h>

include <inttypes.h>

include <stdlib.h>

include "../libnerdkits/Icd.h"

include "../libnerdkits/delay.h"

// PIN DEFINITIONS: // // PA0 -- temperature sensor analog input // PA1 -- piezo buzzer // PA4 -- LCD RS (pin 4) // PA5 -- LCD E (pin 6) // PA7 -- button (pullup high) // PB3-6 -- LCD DB4-7 (pins 11-14)

void lcd_clear_and_home() { lcd_write_string(PSTR(" ")); }

void lcd_clear_and_home() { lcd_home(); lcd_clear_and_home() ; lcd_line_two(); lcd_clear_and_home() ; lcd_home(); }

void play_tone(uint16_t delay, uint8_t duration) { // delay is half-period in microseconds // duration is in 10ms increments

// example: 440Hz --> delay=1136

// duration = 2*delay * cycles (all in same units) // cycles = 10000 * duration / delay / 2 // cycles = 100 * duration / (delay/50) uint16_t tmp = 100 * duration; uint16_t delaysm = delay / 50; uint16_t cycles = tmp / delaysm;

while(cycles > 0) { DDRC &= ~(1<<PC0); delay_us(delay); DDRC &= ~(1<<PC0); delay_us(delay); cycles--; } }

// define some notes // Frequencies from http://www.phy.mtu.edu/~suits/notefreqs.html // converted to half-periods (us) by calculating // 1000000/2/frequency // where frequency is in Hz

define D5 851

define E5 758

define Fsh5 675

define G5 637

define A5 568

define B5 506

define C6 477

define D6 425

define DUR 40

int main() { // internal RC oscillator calibration for 8MHz. OSCCAL = 176;

// enable the piezo as output DDRC |= (1<<PC4);

// enable internal pullup on PA7 (the button) DDRC &= ~(1<<PC5);

// fire up the LCD lcd_init(); lcd_home();

// loop forever! while(1) { lcd_clear_and_home() ; lcd_write_string(PSTR("Press to play a song..."));

// wait for button press...
while(PINB & (1<<PC5)){ 
  // do nothing
}

lcd_clear_and_home() ;
lcd_write_string(PSTR("  Happy ")); play_tone(D5, DUR);
lcd_write_string(PSTR("birth"));    play_tone(E5, DUR);
lcd_write_string(PSTR("day ")); play_tone(D5, DUR);
lcd_write_string(PSTR("to "));  play_tone(G5, DUR);
lcd_write_string(PSTR("you ")); play_tone(Fsh5, 2*DUR);

lcd_line_two();
lcd_write_string(PSTR("  Happy ")); play_tone(D5, DUR);
lcd_write_string(PSTR("birth"));    play_tone(E5, DUR);
lcd_write_string(PSTR("day ")); play_tone(D5, DUR);
lcd_write_string(PSTR("to "));  play_tone(A5, DUR);
lcd_write_string(PSTR("you ")); play_tone(G5, 2*DUR);

lcd_clear_and_home() ;

lcd_write_string(PSTR("Happy "));   play_tone(D5, DUR);
lcd_write_string(PSTR("birth"));    play_tone(D6, DUR);
lcd_write_string(PSTR("day ")); play_tone(B5, DUR);
lcd_write_string(PSTR("dear "));    play_tone(G5, DUR);
lcd_write_string(PSTR("__"));   play_tone(Fsh5, DUR);
lcd_write_string(PSTR("__"));   play_tone(E5, DUR);

lcd_line_two();
lcd_write_string(PSTR(" Happy "));  play_tone(C6, DUR);
lcd_write_string(PSTR("birth"));    play_tone(B5, DUR);
lcd_write_string(PSTR("day ")); play_tone(G5, DUR);
lcd_write_string(PSTR("to "));  play_tone(A5, DUR);
lcd_write_string(PSTR("you! "));    play_tone(G5, 2*DUR);

// delay a bit
delay_ms(500);

}

return 0; }

here is the makefile

GCCFLAGS=-g -Os -Wall -mmcu=atmega168 LINKFLAGS=-Wl,-u,vfprintf -lprintf_flt -Wl,-u,vfscanf -lscanf_flt -lm AVRDUDEFLAGS=-c avr109 -p m168 -b 115200 -P COM5 LINKOBJECTS=../libnerdkits/delay.o ../libnerdkits/lcd.o ../libnerdkits/uart.o

all: musicbox1.c-upload

musicbox1.hex: musicbox1.c :make -C ../libnerdkits avr-gcc ${GCCFLAGS} ${LINKFLAGS} -o delay.o musicbox1.c ${LINKOBJECTS} avr-objcopy -j .text -O ihex delay.o musicbox1.hex

musicbox1.ass: musicbox1.hex avr-objdump -S -d delay.o > musicbox1.ass

musicbox1.c-upload: musicbox1.hex avrdude ${AVRDUDEFLAGS} -U flash:w:musicbox1.hex:a

March 15, 2010
by bretm
bretm's Avatar

I haven't read the whole thing, but I noticed that the line

include "../libnerdkits/Icd.h"

should instead be

include "../libnerdkits/lcd.h"

Unfortunately these two lines look exactly the same with the font that they use in these forums. Copy and paste them into a word processor and change to a font with serifs and you'll see that the second one actually says Lcd.h.

If you start the lines with four spaces, then the "#" character will cause it to use a better font, instead of making the line bold. Then you'll see the difference:

#include "../libnerdkits/Icd.h"
#include "../libnerdkits/lcd.h"
March 15, 2010
by 787pilot
787pilot's Avatar

sorry here is it correctly

  // musicbox1.c
 // for NerdKits with ATtiny26L
// mrobbins@mit.edu

// F_CPU defined for delay.c
#define F_CPU 14745600UL    // 8MHz

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include <inttypes.h>
#include <stdlib.h>

#include "../libnerdkits/lcd.h"

#include "../libnerdkits/delay.h"

// PIN DEFINITIONS:
//
// PA0 -- temperature sensor analog input
// PA1 -- piezo buzzer
// PA4 -- LCD RS (pin 4)
// PA5 -- LCD E (pin 6)
// PA7 -- button (pullup high)
// PB3-6 -- LCD DB4-7 (pins 11-14)

void lcd_clear_and_home()  {
  lcd_write_string(PSTR("                        "));
}

void lcd_clear_and_home()  {
lcd_home();
lcd_clear_and_home() ;
lcd_line_two();
lcd_clear_and_home() ;
lcd_home();
}

void play_tone(uint16_t delay, uint8_t duration) {

// delay is half-period in microseconds // duration is in 10ms increments

// example: 440Hz --> delay=1136

// duration = 2*delay * cycles (all in same units)
// cycles = 10000 * duration / delay / 2
// cycles = 100 * duration / (delay/50)
uint16_t tmp = 100 * duration;
uint16_t delaysm = delay / 50;
uint16_t cycles = tmp / delaysm;

while(cycles > 0) {
DDRC &= ~(1<<PC0);
delay_us(delay);
DDRC &= ~(1<<PC0);
delay_us(delay);
cycles--;
}
}

// define some notes
// Frequencies from http://www.phy.mtu.edu/~suits/notefreqs.html
// converted to half-periods (us) by calculating
//  1000000/2/frequency
// where frequency is in Hz
#define D5 851
#define E5 758
#define Fsh5 675
#define G5 637
#define A5 568
#define B5 506
#define C6 477
#define D6 425
#define DUR 40

int main() {

// internal RC oscillator calibration for 8MHz. OSCCAL = 176;

// enable the piezo as output
DDRC |= (1<<PC4);

// enable internal pullup on PA7 (the button)
DDRC &= ~(1<<PC5);

// fire up the LCD
lcd_init();
lcd_home();

// loop forever!
 while(1) {
 lcd_clear_and_home() ;
 lcd_write_string(PSTR("Press to play a song..."));

// wait for button press...
while(PINB & (1<<PC5)){ 
 // do nothing
}

lcd_clear_and_home() ;
lcd_write_string(PSTR("  Happy ")); play_tone(D5, DUR);
lcd_write_string(PSTR("birth"));    play_tone(E5, DUR);
lcd_write_string(PSTR("day ")); play_tone(D5, DUR);
lcd_write_string(PSTR("to "));  play_tone(G5, DUR);
lcd_write_string(PSTR("you ")); play_tone(Fsh5, 2*DUR);

lcd_line_two();
lcd_write_string(PSTR("  Happy ")); play_tone(D5, DUR);
lcd_write_string(PSTR("birth"));    play_tone(E5, DUR);
lcd_write_string(PSTR("day ")); play_tone(D5, DUR);
lcd_write_string(PSTR("to "));  play_tone(A5, DUR);
lcd_write_string(PSTR("you ")); play_tone(G5, 2*DUR);

lcd_clear_and_home() ;

lcd_write_string(PSTR("Happy "));   play_tone(D5, DUR);
lcd_write_string(PSTR("birth"));    play_tone(D6, DUR);
lcd_write_string(PSTR("day ")); play_tone(B5, DUR);
lcd_write_string(PSTR("dear "));    play_tone(G5, DUR);
lcd_write_string(PSTR("__"));   play_tone(Fsh5, DUR);
lcd_write_string(PSTR("__"));   play_tone(E5, DUR);

lcd_line_two();
lcd_write_string(PSTR(" Happy "));  play_tone(C6, DUR);
lcd_write_string(PSTR("birth"));    play_tone(B5, DUR);
lcd_write_string(PSTR("day ")); play_tone(G5, DUR);
lcd_write_string(PSTR("to "));  play_tone(A5, DUR);
lcd_write_string(PSTR("you! "));    play_tone(G5, 2*DUR);

// delay a bit
delay_ms(500);
}

return 0;
}

here is the makefile GCCFLAGS=-g -Os -Wall -mmcu=atmega168 LINKFLAGS=-Wl,-u,vfprintf -lprintf_flt -Wl,-u,vfscanf -lscanf_flt -lm AVRDUDEFLAGS=-c avr109 -p m168 -b 115200 -P COM5 LINKOBJECTS=../libnerdkits/delay.o ../libnerdkits/lcd.o ../libnerdkits/uart.o

all: musicbox1.c-upload

musicbox1.hex:  musicbox1.c 
:make -C ../libnerdkits
avr-gcc ${GCCFLAGS} ${LINKFLAGS} -o delay.o musicbox1.c ${LINKOBJECTS}
avr-objcopy -j .text -O ihex delay.o musicbox1.hex

musicbox1.ass:  musicbox1.hex
avr-objdump -S -d delay.o > musicbox1.ass

musicbox1.c-upload: musicbox1.hex
avrdude ${AVRDUDEFLAGS} -U flash:w:musicbox1.hex:a
March 18, 2010
by 787pilot
787pilot's Avatar

can some one please help!

March 18, 2010
by hevans
(NerdKits Staff)

hevans's Avatar

Hi 787pilot,

After fixing what bretm suggested you fix, did that change anything? Can you try to upload the program again. Then after that, switch the MCU into running mode and reset the power to the breadboard. Then try to describe as detailed as you can what the problem is you are having.

Humberto

March 20, 2010
by 787pilot
787pilot's Avatar

this is about as deatiled as I can put it The command prompt says it was up-loaded but (I did what you said) The Lcd is just showing 2 black lines and the button when pressed wont do any thing

March 20, 2010
by hevans
(NerdKits Staff)

hevans's Avatar

Hi 787pilot,

I took a close look through the second version of the code you posted. Take a look at the definitions for lcd_clear_and_home() you have. You have defined the exact same function twice in the same file. This will result in a compile time error, which you should see on the command line when you try to compile it. You say that the command line says it uploaded the file, but that is not possible with the code you posted above, since it would immediately give you a compiler error.

The first thing to figure out is why you are able to see no errors and the code being uploaded. This probably means you are in the wrong folder in your command line and you are just uploading the old file over and over.

The second thing to fix is the actual error. You can't define the exact same function twice, because then your program doesn't know which one to use when you call it. lcd.c in your libnerdkits folder already has a definition for lcd_clear_and_home() so you can just delete both of those declarations in your C file.

Does all this make sense?

Humberto

March 20, 2010
by 787pilot
787pilot's Avatar

delete lcd_clear_and_home() from where in the make file?

March 20, 2010
by 787pilot
787pilot's Avatar

sorry not make file I mean musicbox1.c

March 21, 2010
by 787pilot
787pilot's Avatar

Do I need to change any thing in Icd.o/Delay.o?

March 22, 2010
by hevans
(NerdKits Staff)

hevans's Avatar

Hi 787pilot,

You should not need to change anything in lcd.c or delay.c (until you want to start modifying the libraries of course, but this is not the time). You need to delete the function declarations. That is, delete the partrs where lcd_cleaer_and_home() are being defined.

You need to get clear in your head the difference between defining a function, and calling a function. This is a concept that can be tricky for beginners to programming. When you define a function you are defining what will happen when the function gets called (you can only define a function once), you can later call the function as many times as you want.

If you look at your code the parts where you wirte:

void lcd_clear_and_home(){
    // some code here
}

That is a function definition. It is merely describing the code that will execute when you call the function.

Later, in your main function, you have lines that call lcd_clear_and_home() that look like this:

lcd_clear_and_home();

This is calling a function, when your program gets to that line, it jumps to the function definition and executes the code.

In this case you need to remove the definitions lcd_clear_and_home(), since it is already defined in lcd.c as part of libnerdkits. You still need to call lcd_clear_and_home() from your makefile where appropriate though since you want to clear the LCD and move the pointer to the home position.

Does all that make sense?

Humberto

March 22, 2010
by 787pilot
787pilot's Avatar

I still dont really get it

March 22, 2010
by hevans
(NerdKits Staff)

hevans's Avatar

Hi 787pilot,

If you go through and let me know which parts you do get, and what is still a bit fuzzy I can go ahead and try to clear those points up. Feel free to just write down your general understanding of how defining functions and calling functions works, and I will let you know if your understanding correct.

Humberto

March 23, 2010
by 787pilot
787pilot's Avatar

why do I need to put lcd_clear_and_home() in the makefile?

March 23, 2010
by 787pilot
787pilot's Avatar

Do I need to make Icd.o musicbox.hex or do I need to make delay.o be musicbox.hex

March 24, 2010
by Farmerjoecoledge
Farmerjoecoledge's Avatar

Hello, mind if I jump in?

Chip-Atmega168, check. Code-musicbox1.c, check. Changed- 8mhz to 14745600, check. Add- "../libnerdkits, directory, check. Subtract- quick_, 4 places, check. Delete- void lcd_quick_clear_and_home(), check. Delete-lcd_quick_clear_and_home(),3-places, check. Makefile- converted reg to musicbox1, check. Make- command line gcc, check.

Next...this does not compile, there's a PORTA error on line 54.

747pilot, the musicbox1.hex is made by compiling musicbox1.c. The gcc makes a .hex and .o file in the same folder, if it's working.

I don't have a attiny either, are you guy's talking the same chip? 128, 168?

fjc

March 25, 2010
by 787pilot
787pilot's Avatar

I think that the fault is im the c file not the make file I think it is a compile error here is the C file

    // musicbox1.c
 // for NerdKits with ATtiny26L
 // mrobbins@mit.edu

 // F_CPU defined for delay.c
 #define F_CPU 14745600UL    // 8MHz

 #include <avr/io.h>
 #include <avr/interrupt.h>
 #include <avr/pgmspace.h>
 #include <util/delay.h>
 #include <inttypes.h>
 #include <stdlib.h>

 #include "../libnerdkits/lcd.h" 
 #include "../libnerdkits/delay.h"
 #include "../libnerdkits/uart.h"

// PIN DEFINITIONS:
//
// PA0 -- temperature sensor analog input
// PA1 -- piezo buzzer
// PA4 -- LCD RS (pin 4)
// PA5 -- LCD E (pin 6)
    // PA7 -- button (pullup high)
// PB3-6 -- LCD DB4-7 (pins 11-14)

void   {
 lcd_write_string(PSTR("                        "));
}

void   {
lcd_clear_and_home()
lcd_home();
lcd_line_two();
lcd_home();
}

void play_tone(uint16_t delay, uint8_t duration) {
// delay is half-period in microseconds // duration is in 10ms increments

// example: 440Hz --> delay=1136

// duration = 2*delay * cycles (all in same units)
// cycles = 10000 * duration / delay / 2
// cycles = 100 * duration / (delay/50)
uint16_t tmp = 100 * duration;
uint16_t delaysm = delay / 50;
uint16_t cycles = tmp / delaysm;

while(cycles > 0) {
DDRC &= ~(1<<PC0);
delay_us(delay);
DDRC &= ~(1<<PC0);
delay_us(delay);
cycles--;
}
}

 // define some notes
// Frequencies from http://www.phy.mtu.edu/~suits/notefreqs.html
// converted to half-periods (us) by calculating
 //  1000000/2/frequency
// where frequency is in Hz
#define D5 851
#define E5 758
#define Fsh5 675
#define G5 637
#define A5 568
#define B5 506
#define C6 477
#define D6 425
#define DUR 40

int main() {
// internal RC oscillator calibration for 8MHz. 
OSCCAL = 176;

// enable the piezo as output
DDRC |= (1<<PB1);

// enable internal pullup on PA7 (the button)
DDRC &= ~(1<<PC5);

// fire up the LCD
lcd_init();
lcd_home();

// loop forever!
while(1) {
lcd_write_string(PSTR("Press to play a song..."));

// wait for button press...
while(PINB & (1<<PC5)){ 
// do nothing
}

lcd_clear_and_home()

lcd_write_string(PSTR("  Happy "));   play_tone(D5, DUR);
lcd_write_string(PSTR("birth"));  play_tone(E5, DUR);
lcd_write_string(PSTR("day "));   play_tone(D5, DUR);
lcd_write_string(PSTR("to "));    play_tone(G5, DUR);
lcd_write_string(PSTR("you "));   play_tone(Fsh5, 2*DUR);

lcd_line_two();
lcd_write_string(PSTR("  Happy "));   play_tone(D5, DUR);
lcd_write_string(PSTR("birth"));  play_tone(E5, DUR);
lcd_write_string(PSTR("day "));   play_tone(D5, DUR);
lcd_write_string(PSTR("to "));    play_tone(A5, DUR);
lcd_write_string(PSTR("you "));   play_tone(G5, 2*DUR);

lcd_clear_and_home()

lcd_write_string(PSTR("Happy ")); play_tone(D5, DUR);
lcd_write_string(PSTR("birth"));  play_tone(D6, DUR);
lcd_write_string(PSTR("day "));   play_tone(B5, DUR);
lcd_write_string(PSTR("dear "));  play_tone(G5, DUR);
lcd_write_string(PSTR("__")); play_tone(Fsh5, DUR);
lcd_write_string(PSTR("__")); play_tone(E5, DUR);

lcd_line_two();
lcd_write_string(PSTR(" Happy "));    play_tone(C6, DUR);
lcd_write_string(PSTR("birth"));  play_tone(B5, DUR);
lcd_write_string(PSTR("day "));   play_tone(G5, DUR);
lcd_write_string(PSTR("to "));    play_tone(A5, DUR);
lcd_write_string(PSTR("you! "));  play_tone(G5, 2*DUR);

// delay a bit
delay_ms(500);
}

return 0;
}
March 25, 2010
by Farmerjoecoledge
Farmerjoecoledge's Avatar

I've been having gcc problems for awhile. But check this out and see if it works for you. http://www.nerdkits.com/forum/thread/231/ The last entry.

March 26, 2010
by hevans
(NerdKits Staff)

hevans's Avatar

Hi 7878pilot,

There is an actual compilation error in your code. Take a look at the following lines in your code:

void   {
     lcd_write_string(PSTR("                        "));
}

void   {
    lcd_clear_and_home()
    lcd_home();
    lcd_line_two();
    lcd_home();
}

You removed the names of the function definitoins but not the whole definition. This left you with invalid code. You need to remove this entire chunk of code. All 10 lines, just remove them. It is not necessary at all, since this function is defined for you in lcd.c (which you don't need to modify at all).

Humberto

March 26, 2010
by 787pilot
787pilot's Avatar

here is the make file. in folder(witch musicbox1.c is in) is delay.o,delay.h,lcd.o,lcd.h,uart.o,uart.h,musicbox1.c,makefile

GCCFLAGS=-g -Os -Wall -mmcu=atmega168
LINKFLAGS=-Wl,-u,vfprintf -lprintf_flt -Wl,-u,vfscanf -lscanf_flt -lm
AVRDUDEFLAGS=-c avr109 -p m168 -b 115200 -P COM5
LINKOBJECTS==../libnerdkits/delay.o ../libnerdkits/lcd.o../libnerdkits/uart.o

all: musicbox1.c-upload

lcd.o:  musicbox1.c 
: make -C ../libnerdkits
avr-gcc ${GCCFLAGS} ${LINKFLAGS} -o delay.o musicbox1.c  ${LINKOBJECTS}
avr-objcopy -j .text -O ihex delay.o lcd.o

delay.o: lcd.o
avr-objdump -S -o delay.o > lcd.o

 musicbox1.c-upload: lcd.o
avrdude ${AVRDUDEFLAGS} -U flash:w:lcd.o:a
March 26, 2010
by Farmerjoecoledge
Farmerjoecoledge's Avatar

Holy Makarel!... Who showed you how to do that? Do it the easy way. Just put a copy of initialload makefile in a folder with the musicbox1.c code and just change the name of musicbox1.c to initialload. It's a short cut, all you have to do is remember that the initialload.c is really musicbox1.c.

March 28, 2010
by 787pilot
787pilot's Avatar

I dont really under stand this could you please help me under stand this (it came from command prompt)

C:Documents and SettingsMaxMy DocumentsnerdkitNew Folder (2)>make make -C ../code2/code/libnerdkits make[1]: Entering directory C:/Documents and Settings/Max/My Documents/nerdkit/ code2/code/libnerdkits' make[1]: Nothing to be done forall'. make[1]: Leaving directory `C:/Documents and Settings/Max/My Documents/nerdkit/c ode2/code/libnerdkits' avr-gcc -g -Os -Wall -mmcu=atmega168 -Wl,-u,vfprintf -lprintf_flt -Wl,-u,vfscan f -lscanf_flt -lm -o musicbox1.o musicbox1.c ../libnerdkits/delay.o ../libnerdki ts/lcd.o ../libnerdkits/uart.o avr-gcc: ../libnerdkits/delay.o: No such file or directory avr-gcc: ../libnerdkits/lcd.o: No such file or directory avr-gcc: ../libnerdkits/uart.o: No such file or directory musicbox1.c:15:34: error: ../libnerdkits/delay.h: No such file or directory musicbox1.c:16:33: error: ../libnerdkits/lcd.h: No such file or directory musicbox1.c: In function 'play_tone': musicbox1.c:43: error: 'PORTA' undeclared (first use in this function) musicbox1.c:43: error: (Each undeclared identifier is reported only once musicbox1.c:43: error: for each function it appears in.) musicbox1.c:43: error: 'PA1' undeclared (first use in this function) musicbox1.c:44: warning: implicit declaration of function 'delay_us' musicbox1.c: In function 'main': musicbox1.c:71: error: 'DDRA' undeclared (first use in this function) musicbox1.c:74: error: 'PORTA' undeclared (first use in this function) musicbox1.c:77: warning: implicit declaration of function 'lcd_init' musicbox1.c:78: warning: implicit declaration of function 'lcd_home' musicbox1.c:82: warning: implicit declaration of function 'lcd_write_string' musicbox1.c:85: error: 'PINA' undeclared (first use in this function) musicbox1.c:95: warning: implicit declaration of function 'lcd_line_two' musicbox1.c:117: warning: implicit declaration of function 'delay_ms' make: *** [musicbox1.hex] Error 1

March 28, 2010
by hevans
(NerdKits Staff)

hevans's Avatar

Hi 787pilot,

The errors you are seeing point to several things being wrong. The first couple of them, the ones that have 'No such file or directory' mean that Make cannot find the libnerdkits directory and all the files in it. Remember that to compile a project (the way we have our makefiles set up) is to have the Code folder with a bunch of different project folders it. One of those folder should be libnerdkits, there should be others in there like initialload, and tempsensor. To create your own project you should make another folder inside the Code folder, like musicbox. Then put your C files in there.

The second set of errors, the ones that relate to not finding PINA are there because this code was origninally written for a different chip than the one you are using. That chip had some pins that were named PAn which do not exist on the Atmega168 you are using. That is why you need to change the code to make it work with your chip.

You were very close with the code you had pasted above, did you revert back to the one you downloaded from our site?

Humberto

March 28, 2010
by 787pilot
787pilot's Avatar

what does this mean?

Connecting to programmer: . Found programmer: Id = "ôÿ#"; type = Software Version = «. ; Hardware Version = ». avrdude: error: buffered memory access not supported. Maybe it isn't a butterfly/AVR109 but a AVR910 device? make: *** [musicbox-upload] Error 1

March 28, 2010
by hevans
(NerdKits Staff)

hevans's Avatar

That could mean any number things. It most likely means your chip is not in programming mode, and is receiving junk from the chip. It could also mean your power is running low, or there is some other problem with the communication between your computer and the chip.

Just a note, if you put 4 spaces in front of every line in the error messages or code you post, it will keep the forum software from formatting it strangely, and it might make it easier for everyone to read. For example:

There are four spaces in front
of every line here. So the line breaks are all
where I put them.
March 28, 2010
by Farmerjoecoledge
Farmerjoecoledge's Avatar

747pilot...from above.

I'm tagin along, so far were just about the same, just haven't got the above error, yet, but reboot and stuff too. To straighten out the connect.

March 28, 2010
by Farmerjoecoledge
Farmerjoecoledge's Avatar

Godgiveusbetterforumsoftware

March 29, 2010
by 787pilot
787pilot's Avatar

here is the code again every thing but the button will work how can I make the button work? (this is the main part)

int main() {
// internal RC oscillator calibration for 8MHz.
OSCCAL = 176;

// enable the piezo as output
DDRC |= (1<<PB1);

// enable internal pullup on PA7 (the button)
DDRC &= ~(1<<PC4);

// fire up the LCD
lcd_init();
lcd_home();

// loop forever!
while(1) {
lcd_write_string(PSTR("Press to play a song..."));

// wait for button press...
while(1) DDRC |= (1<<PC4);
  // do nothing

lcd_clear_and_home(); 
lcd_write_string(PSTR("  Happy ")); play_tone(D5, DUR);
lcd_write_string(PSTR("birth"));    play_tone(E5, DUR);
lcd_write_string(PSTR("day ")); play_tone(D5, DUR);
lcd_write_string(PSTR("to "));  play_tone(G5, DUR);
lcd_write_string(PSTR("you ")); play_tone(Fsh5, 2*DUR);

lcd_line_two();
lcd_write_string(PSTR("  Happy ")); play_tone(D5, DUR);
lcd_write_string(PSTR("birth"));    play_tone(E5, DUR);
lcd_write_string(PSTR("day ")); play_tone(D5, DUR);
lcd_write_string(PSTR("to "));  play_tone(A5, DUR);
lcd_write_string(PSTR("you ")); play_tone(G5, 2*DUR);

lcd_clear_and_home(); 
lcd_write_string(PSTR("Happy "));   play_tone(D5, DUR);
lcd_write_string(PSTR("birth"));    play_tone(D6, DUR);
lcd_write_string(PSTR("day ")); play_tone(B5, DUR);
lcd_write_string(PSTR("dear "));    play_tone(G5, DUR);
lcd_write_string(PSTR("__"));   play_tone(Fsh5, DUR);
lcd_write_string(PSTR("__"));   play_tone(E5, DUR);

lcd_line_two();
lcd_write_string(PSTR(" Happy "));  play_tone(C6, DUR);
lcd_write_string(PSTR("birth"));    play_tone(B5, DUR);
lcd_write_string(PSTR("day ")); play_tone(G5, DUR);
lcd_write_string(PSTR("to "));  play_tone(A5, DUR);
lcd_write_string(PSTR("you! "));    play_tone(G5, 2*DUR);
lcd_clear_and_home();
March 29, 2010
by hevans
(NerdKits Staff)

hevans's Avatar

Hi pilot,

You are getting very close. I think you modified a part too much that you didn't intend to. Take a close look at this part of your code:

// wait for button press... while(1) DDRC |= (1<<PC4); // do nothing

You have while(1) here, which means that as long as 1 evaluates to true, perform the next statement. As it happens the next statement is set PC4 as an output (which I also don't think is what you want to do. I think you are looking for something like this

// wait for button press...
while(PINC & (1<<PC4)) {
  // do nothing
}

This is assuming the button on PC4. Notice how this piece of code will stay in the while loop doing nothing until PC4 goes low (when you push the button). Then the the code is allowed to continue past the while loop.

Humberto

March 29, 2010
by 787pilot
787pilot's Avatar

Now it will say it on the lcd after I press the button but it won't play on the pieazo

March 29, 2010
by Farmerjoecoledge
Farmerjoecoledge's Avatar

Just to make sure, 747 can you post the complete working code. The one that you flashed,thx.

March 30, 2010
by hevans
(NerdKits Staff)

hevans's Avatar

Hi pilot,

The fact that is writing the song to the LCD means the code is running correctly (or at least running). The first thing to make sure is that the buzzer is connected in the right place. That is, make sure it is connected to the pin that is driving the output. Also make sure you are setting that pin as an output pin in your code. I'm sure with a little bit of digging you can find the issue.

Humberto

March 30, 2010
by 787pilot
787pilot's Avatar

her is the code again

// musicbox1.c
// for NerdKits with ATtiny26L
// mrobbins@mit.edu

// F_CPU defined for delay.c
#define F_CPU 14745600UL    // 8MHz

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include <inttypes.h>
#include <stdlib.h>

#include "../libnerdkits/delay.h"
#include "../libnerdkits/lcd.h"

 // PIN DEFINITIONS:
//
// PA0 -- temperature sensor analog input
// PA1 -- piezo buzzer
// PA4 -- LCD RS (pin 4)
// PA5 -- LCD E (pin 6)
// PA7 -- button (pullup high)
// PB3-6 -- LCD DB4-7 (pins 11-14)

void play_tone(uint16_t delay, uint8_t duration) {
// delay is half-period in microseconds
// duration is in 10ms increments

// example: 440Hz --> delay=1136

 // duration = 2*delay * cycles (all in same units)
 // cycles = 10000 * duration / delay / 2
 // cycles = 100 * duration / (delay/50)
 uint16_t tmp = 100 * duration;
 uint16_t delaysm = delay / 50;
 uint16_t cycles = tmp / delaysm;

 while(cycles > 0) {
 PORTC |= ~(1<<PC0);
 delay_us(delay);
 PORTC &= ~(1<<PC0);
 delay_us(delay);
 cycles--;
 }
 }

// define some notes
// Frequencies from http://www.phy.mtu.edu/~suits/notefreqs.html
// converted to half-periods (us) by calculating
//  1000000/2/frequency
// where frequency is in Hz
 #define D5 851
 #define E5 758
 #define Fsh5 675
 #define G5 637
 #define A5 568
  #define B5 506
 #define C6 477
 #define D6 425
 #define DUR 40

 int main() {
  // internal RC oscillator calibration for 8MHz.
 OSCCAL = 176;

 // enable the piezo as output
 PORTB |= (1<<PB1);

 // enable internal pullup on PA7 (the button)
 DDRC &= ~(1<<PC5);

// fire up the LCD
lcd_init();
lcd_home();

// loop forever!
while(1) {
lcd_write_string(PSTR("Press to play a song..."));

// wait for button press...
while(PINC & (1<<PC5)) {
// do nothing
}

lcd_clear_and_home(); 
lcd_write_string(PSTR("  Happy ")); play_tone(D5, DUR);
lcd_write_string(PSTR("birth"));    play_tone(E5, DUR);
lcd_write_string(PSTR("day ")); play_tone(D5, DUR);
lcd_write_string(PSTR("to "));  play_tone(G5, DUR);
lcd_write_string(PSTR("you ")); play_tone(Fsh5, 2*DUR);

lcd_line_two();
lcd_write_string(PSTR("  Happy ")); play_tone(D5, DUR);
lcd_write_string(PSTR("birth"));    play_tone(E5, DUR);
lcd_write_string(PSTR("day ")); play_tone(D5, DUR);
lcd_write_string(PSTR("to "));  play_tone(A5, DUR);
lcd_write_string(PSTR("you ")); play_tone(G5, 2*DUR);

lcd_clear_and_home(); 
lcd_write_string(PSTR("Happy "));   play_tone(D5, DUR);
lcd_write_string(PSTR("birth"));    play_tone(D6, DUR);
lcd_write_string(PSTR("day ")); play_tone(B5, DUR);
lcd_write_string(PSTR("dear "));    play_tone(G5, DUR);
lcd_write_string(PSTR("__"));   play_tone(Fsh5, DUR);
lcd_write_string(PSTR("__"));   play_tone(E5, DUR);

lcd_line_two();
lcd_write_string(PSTR(" Happy "));  play_tone(C6, DUR);
lcd_write_string(PSTR("birth"));    play_tone(B5, DUR);
lcd_write_string(PSTR("day ")); play_tone(G5, DUR);
lcd_write_string(PSTR("to "));  play_tone(A5, DUR);
lcd_write_string(PSTR("you! "));    play_tone(G5, 2*DUR);
lcd_clear_and_home();

// delay a bit
delay_ms(500);
}

 return 0;
}
March 30, 2010
by Farmerjoecoledge
Farmerjoecoledge's Avatar

Thanks 747,

This doesn't have much to do with you it's just the boy's might like to see this, again. By changing the .c to .py and edit with IDLE, you can see the error. This is on both of my windows machines. This is a clue, but not to me.AVRDUDE MYCOMPS You can see it's making it's own errors.

Not to fret, I'm getting close to writing my own, my very own, it's guaranteed not to work for anyone else ;D

March 31, 2010
by 787pilot
787pilot's Avatar

what do you want me to do exatly

March 31, 2010
by Farmerjoecoledge
Farmerjoecoledge's Avatar

Well you can trade me comps straight across, hell I bet I could go buy you a new one and I'd have the same problem. You can't get any closer to having a new one. Their older but both have their own installation disks with brand new installs. There doesn't seem to be an answer to my plight, so not to worry.

Kinda gettin away from your question. Did you get your speaker to work?

April 01, 2010
by bretm
bretm's Avatar

This part will result in no sound:

while(cycles > 0) {
  PORTC |= ~(1<<PC0);  // this turns on all PORTC pins except PC0
  delay_us(delay);
  PORTC &= ~(1<<PC0);  // this turns off pin PC0
  delay_us(delay);
  cycles--;
}

Get rid of the "~" on the PORTC |= (1<<PC0) line.

April 01, 2010
by bretm
bretm's Avatar

And somewhere during initialization you need to do DDRC |= 1<<PC0 to configure PC0 as an output pin. This assumes PC0 is connected to the piezo.

April 03, 2010
by 787pilot
787pilot's Avatar

sorry but I accdintly fried my microcontroler so I wont be able to give you resualts until about wensday sorry

April 03, 2010
by Farmerjoecoledge
Farmerjoecoledge's Avatar

This is baffling, this code should work http://www.nerdkits.com/forum/thread/231/ Following along and can't help asking, what's happening here? Bretm, your addition totally baffles it. You see the guy who posted the working code knows his stuff so why all the changes? Why doesn't the code pbfy0 posted just work. Could one of you please paste one musicbox.c file tested and working so us rookies don't go nuts, I think I already am, but that's ok.

April 04, 2010
by bretm
bretm's Avatar

You already posted the link to working code, back on the 25th. The OP apparently wants to figure it out themselves instead of copying and pasting. He's almost there.

Post a Reply

Please log in to post a reply.

Did you know that NerdKits believes in the importance of a mixture of meaningful topics, clear instruction, and engaging projects? Learn more...