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.

Support Forum » Cannot Make Music with the MicroController

May 28, 2010
by nerdguy
nerdguy's Avatar

Just received the nerdkit and went through the manual. Now I'm trying the "Make Music with the MicroController" project. Copied the musicbox1.c source code and created a makefile. Tried to compile the C code from the command window and the output is:


make -C ../libnerdkits
make[1]: Entering directory `C:/clients/nerdkits/Code/libnerdkits'
make[1]: Nothing to be done for `all'.
make[1]: Leaving directory `C:/clients/nerdkits/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
musicbox1.c: In function 'play_tone':
musicbox1.c:54: error: 'PORTA' undeclared (first use in this function)
musicbox1.c:54: error: (Each undeclared identifier is reported only once
musicbox1.c:54: error: for each function it appears in.)
musicbox1.c:54: error: 'PA1' undeclared (first use in this function)
musicbox1.c: In function 'main':
musicbox1.c:82: error: 'DDRA' undeclared (first use in this function)
musicbox1.c:82: error: 'PA1' undeclared (first use in this function)
musicbox1.c:85: error: 'PORTA' undeclared (first use in this function)
musicbox1.c:85: error: 'PA7' undeclared (first use in this function)
musicbox1.c:97: error: 'PINA' undeclared (first use in this function)
make: *** [musicbox1.hex] Error 1

There appears to be some undefined variables like PORTA, PA7, PINA, etc. Where are these variables being defined? I don't see them in the header files (I didn't look at all of them though).

May 28, 2010
by bretm
bretm's Avatar

That program was written for a different microcontroller. It needs some modifications to work with the Atmega168, which doesn't have an "A" port.

May 29, 2010
by nerdguy
nerdguy's Avatar

Is it a simple change? Suppose I change the code so it all references PORTC?

Has anyone gotten this to work?

May 29, 2010
by mrobbins
(NerdKits Staff)

mrobbins's Avatar

Hi nerdguy,

You're exactly right.

Yes, you can change all of the references to pins PA1 (the buzzer) and PA7 (the button), as well as registers PORTA, PINA, DDRA, to reference some other pins that do actually exist on the ATmega168.

You would also want to remove the "OSCCAL = 176;" line from the main function -- this simply isn't relevant to the ATmega168 when we're using an external crystal.

Finally, this project was designed when we were shipping kits with a 2-row, 24-character wide LCD. With the current 4-row, 20-character-wide LCD, you may want to adjust how and where the text is displayed.

I can also point you to another forum post where another member posted their code regarding this project. I haven't personally looked at it but perhaps it will be useful in combination with the above information.

Hope that helps!

Mike

May 31, 2010
by nerdguy
nerdguy's Avatar

mrobbins,

Thanks for you reply and for confirming my guesses. I replaced all references for "A" to "C", changed the CPU definition and got rid of the push button code (haven't gotten to that project yet) to start the music. This got the LCD working correctly. Had to shorten the lines from 25 chars to 20 chars.

I got the speaker working by switching the leads. Evidently, the speaker can be connected only one way for it to work correctly. This was not documented anywhere. (This is probably obvious to you but not to an inexperienced hardware person like me.)

Anyway, like I said before, I just got the nerdkit and went through the "nerdkits guide". Now I'm trying to go through the projects on your website. No doubt, I'll have other questions.

January 01, 2011
by BStory
BStory's Avatar

It's so nice to find my question already asked :)

For me the remedy for the code was to redirect the lcd.h include and the delay.h include to ../libnerdkits/lcd.h and ../libnerdkits/delay.h (Maybe the reason for the "nothing to be done for 'all'" error?)

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

Instead of:

#include "delay.h"
#include "lcd.h"

Then as mentioned above, I did away with PORTA/PINA references and used B instead.

Still working out what is going on in the play_tone function and getting the button to start the thing but hey at least I know it works lol.

January 08, 2011
by hariharan
hariharan's Avatar

I compiled my program initialload.c and i got "Make: nothing to be done for intialload.c" What did i do wrong?

January 09, 2011
by keithka
keithka's Avatar

hariharan: if you're still having trouble, please show us what's in your makefile.

March 28, 2011
by hariharan
hariharan's Avatar

i tried to modify the code for the music box, but it does not work, why? the code:

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

// F_CPU defined for delay.c
#define F_CPU 14745600

#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"

// 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<<PC4);
    delay_us(delay);
    PORTC &= ~(1<<PC4);
    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.

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

  // enable internal pullup on PA7 (the button)
  PORTC |= (1<<PC5);

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

    play_tone(D5, DUR); 
play_tone(E5, DUR);
        play_tone(D5, DUR);
        play_tone(G5, DUR);
        play_tone(Fsh5, 2*DUR);

        play_tone(D5, DUR);
        play_tone(E5, DUR);
        play_tone(D5, DUR);
    play_tone(A5, DUR);
        play_tone(G5, 2*DUR);

    play_tone(D5, DUR);
        play_tone(D6, DUR);
        play_tone(B5, DUR);
        play_tone(G5, DUR);
        play_tone(Fsh5, DUR);
        play_tone(E5, DUR);

    play_tone(C6, DUR);
    play_tone(B5, DUR);
        play_tone(G5, DUR);
        play_tone(A5, DUR);
    play_tone(G5, 2*DUR);

    // delay a bit
    delay_ms(500);
  }

  return 0;
}
March 28, 2011
by hevans
(NerdKits Staff)

hevans's Avatar

Hi Hari,

If you let us know what errors you are getting we would be glad to help you figure out why you are getting those errors, but more importantly we can start helping you figure out how to interpret the errors you are getting so you can start learning how to troubleshoot these problems for yourself.

Humberto

Post a Reply

Please log in to post a reply.

Did you know that our kits have shipped to hobbyists in 40 countries? Learn more...