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 » [initialload.hex] Error 2

January 21, 2012
by ffogarty5
ffogarty5's Avatar

I can't seem to get past [initialload.hex] Error 2 when I hit run in the command window. The COM port setting in the program was changed to the port listed in the DM. (com4 in my case). Any Ideas ?

Thanks, Chris

January 22, 2012
by hevans
(NerdKits Staff)

hevans's Avatar

Hi ffogarty5,

That doesn't give me enough info about the error you are getting to know which direction to go in. If you can post a screenshot of the full error you are getting it will give us more information and I'm sure we can get up and running in no time.

Humberto

March 13, 2012
by Guerrieri
Guerrieri's Avatar

I am getting the same thing. I do not know how to compile the program. This is where I believe I am stuck.

C:Documents and Settingsuser>cd my documents

C:Documents and SettingsuserMy Documents>ls

C:Documents and SettingsuserMy Documents>cd initialload

C:Documents and SettingsuserMy Documentsinitialload>ls Makefile initialload.c

C:Documents and SettingsuserMy Documentsinitialload>make make -C ../libnerdkits make: ../libnerdkits: No such file or directory. Stop. make: [initialload.hex] Error 2

March 13, 2012
by Rick_S
Rick_S's Avatar

You have to keep the folder structure intact. Your initial load folder should be inside the code folder with all the other project folders as well as a folder called libnerdkits. That is where the UART, LCD, and delay libraries are kept. Unzip your download again but this time keep the original structure, then edit your makefile per the guide. I think you'll find then that it will compile fine.

Rick

March 13, 2012
by pcbolt
pcbolt's Avatar

Hi Guerrieri -

Looks like the "makefile" program cannot find the libnerdkits directory so either it isn't installed on your machine or it is in a place "makefile" isn't told to look for. The way it is written now it is searching in a directory that shares the same parent directory as the one listed at your command prompt. So it should look something like this:

  • NK_Stuff
    • led_blink
    • libnerdkits
    • my_new_project
      • my_new_project.c
      • makefile

If you are in command prompt mode it should look like:

C:\Some_other_directories\NK_Stuff\my_new_project\make

Any other configuration and the "makefile" program will be looking in the wrong place. You can either re-configure your directory structure to match this - or - replace the relative path to the "libnerdkits" directory (../libnerdkits) with the absolute path to the "libnerdkits" directory (C:\Some_other_directories\NK_Stuff\libnerkits).

March 13, 2012
by pcbolt
pcbolt's Avatar

Rick -

You must type a little faster than I do...you beat me to the punch :-)

March 13, 2012
by Ralphxyz
Ralphxyz's Avatar

Guerrieri, did you maintain the structure of the Nerdkits download?

The initiaload folder has to be in the Code folder!!

To show the path with \ just indent the line four spaces or use the "Indent Selection as Code Block" button on this page.

C:\Documents and Settings\My Documents\initialload  (Which will not work!!)

Or you can escape the \ with \\ (two \ instead of one).

Ralph

March 14, 2012
by Guerrieri
Guerrieri's Avatar

The initialload folder was NOT in the code folder. I downloaded the code again, and saved it in my docs, but how do I open it with word pad or a text editor? I am missing something simple. Before, when I save just the initialload folder, I was prompted to open with a list of programs. Thanks for the quick response. Joe

March 14, 2012
by pcbolt
pcbolt's Avatar

Joe -

I think I need a little more info. I assume after you downloaded the .zip file, you were able to extract the whole "code.zip" file all at once. If so, everything should get placed in the right location. If you extract just a sub-directory you're going to have trouble putting all the pieces together. Looks like you're using a recent version of windows so you should be able to right-click on the "code.zip" file and a default decompression program should be on the context menu (right-click menu). Another way is to open WinZip or 7-Zip then navigate to your "Code.zip" file, the use the "extract all". Hopefully this is where you got stuck...if not please describe your steps further.

March 15, 2012
by Ralphxyz
Ralphxyz's Avatar

Guerrieri said:

I downloaded the code again, and saved it in my docs, but how do I open it with word pad or a text editor? I am missing something simple.

What are you "trying" to open with word pad or a text editor?

You do not open a folder with a text editor.

Instead of using My Documents you ought to copy/move/unzip the code folder to C:\Nerdkits.

That will give you C:\Nerdkits\Code.

Then you can open C:\Nerdkits\Code\Initialload\initialload.c in your text editor (never use NotePad).

Actually My Documents works but getting there from the command line is harder then simple

cd C:\\Nerdkits\\Code\\Initialload

Ralph

March 15, 2012
by pcbolt
pcbolt's Avatar

Ralph -

I just use a batch file kept in the default cmd prompt directory to do that.

March 15, 2012
by Ralphxyz
Ralphxyz's Avatar

You have a batch file for all of your projects?

That certainly would work, or is your batch file just to get to the Code folder and then you cd from there?

Ralph

March 15, 2012
by pcbolt
pcbolt's Avatar

It just gets me from "Documents and Settings\ETC" to "Nerdkits\Code". Then I can pick and choose from there. Works like a charm.

March 30, 2012
by Jer
Jer's Avatar

My problem is, I keep getting an Exit Code 2.

"Line 414=> Isim = log(temp2) * Ik;"

BUT If I place a letral like:

Isim = log(.7) * Ik;"

It will Compile no problems.

Here is the full source code:

Thanks For your Help.

// ZSenSim.c is based on tempsensor.c // for NerdKits with ATmega328 // mrobbins@mit.edu

define F_CPU 14745600

include <stdio.h>

//#include <math.h>

include "c:WinAVR-20090313avrincludemath.h"

include <avr/io.h>

include <avr/interrupt.h>

include <avr/pgmspace.h>

include <inttypes.h>

include "../libnerdkits/delay.h"

include "../libnerdkits/lcd.h"

include "../libnerdkits/uart.h"

include "../libnerdkits/io_328p.h"

include "../libnerdkits/io_328p.h"

//--------------------------------------------------------------------------------------------- void master_SPI_init() {

// Set MOSI,SCK,SS as outputs.
// Pins  17,         19,         16
DDRB |= (1<<PB3) | (1<<PB5) | (1<<PB2);

// Initiate the SPI module in master mode, data rate clk/128
// Bit 7 – SPIE: SPI Interrupt Enable = TRUE
// Bit 4 – MSTR: Master = TRUE /Slave Select = FALSE
// Bit[1,0] - SPR1,SPR0
//               0,0 = clk/4
//               0,1 = clk/16
//               1,0 = clk/24
//               1,1 = clk/128

//   SPI MODE: 0, CPOL=0, CPHA=0, Sample (Rising) Setup (Falling).
//SPCR |= (1<<SPE) | (1<<MSTR) | (1<<SPR1) | (1<<SPR0);

//   SPI MODE: 1, CPOL=0, CPHA=1, Setup (Rising) Sample (Falling).
SPCR |= (1<<SPE) | (1<<MSTR) | (1<<CPOL) | (1<<SPR1) | (1<<SPR0);// <= For keypad (74HC165).

//   SPI MODE: 2, CPOL=1, CPHA=0, Sample (Falling) Setup (Rising).
//SPCR |= (1<<SPE) | (1<<MSTR) | (1<<CPHA) | (1<<SPR1) | (1<<SPR0);

//   SPI MODE: 3, CPOL=1, CPHA=1, Setup (Falling) Sample (Rising).
//SPCR |= (1<<SPE) | (1<<MSTR) | (1<<CPOL) | (1<<CPHA) | (1<<SPR1) | (1<<SPR0);

// Set the SS pins we are using as outputs
// PINS=>    23,        24,         25,         26,        27,         28         
DDRC = (1<<PC1) | (1<<PC2) | (1<<PC3) | (1<<PC4) | (1<<PC5);
DDRB |= (1<<PB1);

// set the SS pins logic high (slave not active)
// PINS=>     23,        24,         25,         26,        27,         28  
PORTC = (1<<PC1) | (1<<PC2) | (1<<PC3) | (1<<PC4) | (1<<PC5);
PORTB |= (1<<PB1);

//set up timer, when this fires all the slave panels are updated
//timer set to clk/1024 - aprox 56Hz

// TCCR0B |= (1<<CS02) | (1<<CS00); // TIMSK0 |= (1<<TOIE0);

}

//--------------------------------------------------------------------------------------------- uint16_t SPI_MasterReceive() { // Initz Return value. uint16_t data = 0;

// Pull SS line Low to initiate data transmission. 
PORTB &= ~(1<<PB2);

// Delay to alow SS to go low.
delay_us(50);

// Dummy write a byte of data to SPDR to startup SCLK. 
SPDR = 0xFF;

// Wait for reception to complete, MSB 
while (!(SPSR & (1<<SPIF)))  
{
    // do nothing.
}

// Record byte, shifted 8 bits to left. 
data = (SPDR<<8);

// Dummy write a byte of data to SPDR to startup SCLK. 
SPDR = 0xFF;

// Wait for reception to complete, LSB   
while ( !(SPSR & (1<<SPIF)))   
{
    // do nothing.
}

// Record byte add it to current data value.
data = data + SPDR;

// Pull SS high to synchronise Slave 
PORTB |= (1<<PB2);

// Delay after making SS High for some time to garranty setup before next SPI I/O.
delay_us(50);

// Return Data Register 
return data;

}

//--------------------------------------------------------------------------------------------- void SPI_Master_Outp(uint16_t OutWord) {

// Pull SS line Low to initiate data transmission. 
PORTB &= ~(1<<PB2);

// Delay to alow SS to go low.
delay_us(50);

// write the High byte 1st to SPDR to SPI-output.
SPDR = (OutWord>>8);

// Wait for reception to complete, MSB 
while (!(SPSR & (1<<SPIF)))  
{

}

// write the LOW byte 2nd to SPDR to SPI-output.
SPDR = OutWord & 255;

// Wait for reception to complete, LSB   
while ( !(SPSR & (1<<SPIF)))   
{

}

// Pull SS high to synchronise Slave 
PORTB |= (1<<PB2);

// Delay after making SS High for some time to garranty setup before next SPI I/O.
delay_us(50);

}

//--------------------------------------------------------------------------------------------- void SET_VDAC(uint8_t Chnl, double Vout, double Vref) { uint16_t DAC_Word = 0;

if((Chnl>=0) && (Chnl<4))
{

    //   SPI MODE: 0, CPOL=0, CPHA=0, Sample (Rising) Setup (Falling).
    //SPCR |= (1<<SPE) | (1<<MSTR) | (1<<SPR1) | (1<<SPR0);

    //   SPI MODE: 1, CPOL=0, CPHA=1, Setup (Rising) Sample (Falling).
    //SPCR |= (1<<SPE) | (1<<MSTR) | (1<<CPOL) | (1<<SPR1) | (1<<SPR0);// <= For keypad (74HC165).

    //   SPI MODE: 2, CPOL=1, CPHA=0, Sample (Falling) Setup (Rising).
    //SPCR |= (1<<SPE) | (1<<MSTR) | (1<<CPHA) | (1<<SPR1) | (1<<SPR0);

    //   SPI MODE: 3, CPOL=1, CPHA=1, Setup (Falling) Sample (Rising).
    SPCR |= (1<<SPE) | (1<<MSTR) | (1<<CPOL) | (1<<CPHA) | (1<<SPR1) | (1<<SPR0);

    // Vref = 0 then set Vref to a default value.
    if(Vref==0)
    {
        Vref = 2.5;// Volts.
    }

    // Chnl = 0 to 3.
    DAC_Word = (Chnl<<14);

    // Calculate the 16bits for the DAC = (ADDRESS + COUNT).
    DAC_Word |= (uint16_t)((Vout/Vref) * 4095);

    // 1) Select DAC's SPI-ADDRESS.
    PORTC &= ~(1<<PC3);

    // Delay after selecting the DAC's SPI address to allow it to settle.
    delay_us(50);// <= this can be set lower to speed things up, later.

    // SPI-Outp DAC_Word.
    SPI_Master_Outp(DAC_Word);

    // 6) De-SelectDAC's SPI-ADDRESS.
    PORTC |= (1<<PC3);

    // Delay after deselection of the SPI-ADDRESS.
    delay_us(50);

    // 1) LOAD DAC's new value. Make DAC_Load LOW.
    PORTC &= ~(1<<PC2);

    // Delay after deselection of the SPI-ADDRESS.
    delay_us(50);

    // 1) LATCH DAC's new value. Make DAC_Load HIGH.
    PORTC |= (1<<PC2);
}

}

//--------------------------------------------------------------------------------------------- uint16_t Inp_Keypad() {

//   SPI MODE: 1, CPOL=0, CPHA=1, Setup (Rising) Sample (Falling).
SPCR |= (1<<SPE) | (1<<MSTR) | (1<<CPOL) | (1<<SPR1) | (1<<SPR0);// <= For keypad (74HC165).

uint16_t cKeyValue = 65535;

// Parrallel Load Keypad data.
// Make 74HC165.PL go Lo then back to High.

// 1) Make -PL Low.
PORTC &= ~(1<<PC5);

// 2) Delay, holding -PL low.
delay_us(50);

// 3) Make PL High again.
PORTC |= (1<<PC5);

// Let -PL stay High before Reading SPI.
delay_us(50);

// 4) Select Keypad SPI address.
PORTC &= ~(1<<PC4);

// 5) Read in the 16bits of the Keypad SPI.
cKeyValue = SPI_MasterReceive();

// 6) De-Select Keypad SPI address by making Keypad address High.
PORTC |= ~(1<<PC4);

// Allow SS Address to be deselected.
delay_us(50);

//cKeyValue !=cKeyValue;

//return (65535 - cKeyValue);
switch(65535 - cKeyValue)
{
    case 0:
        // No Key.
        return 65535;
        break;
    case 1:
        // [*]
        return 10;
        break;
    case 2:
        // [7]
        return 7;
        break;
    case 4:
        // [4]
        return 4;
        break;
    case 8:
        // [1]
        return 1;
        break;
    case 16:
        // [0]
        return 0;
        break;  
    case 32:
        // [8] 
        return 8;
        break;
    case 64:
        // [5] 
        return 5;
        break;
    case 128:
        // [2] 
        return 2;
        break;
    case 256:
        // [#] 
        return 11;
        break;
    case 512:
        // [9] 
        return 9;
        break;
    case 1024:
        // [6] 
        return 6;
        break;
    case 2048:
        // [#] 
        return 3;
        break;
    case 4096:
        // [C] 
        return 12;
        break;
    case 8192:
        // [D] 
        return 13;
        break;
    case 16384:
        // [E] 
        return 14;
        break;
    case 32768:
        // [F] 
        return 15;
        break;
    default:
        // Invalid Key.
        return 65535;
        break;
}

}

//--------------------------------------------------------------------------------------------- void adc_init() { // set analog to digital converter // for external reference (5v), single ended input ADC0 ADMUX = 0;

// Set analog to digital converter to 
// be enabled, with a clock prescale of 1/128
// so that the ADC clock runs at 115.2kHz.
ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0);

// fire a conversion just to get the ADC warmed up
ADCSRA |= (1<<ADSC);

}

//--------------------------------------------------------------------------------------------- uint16_t Inp_ADC() { // read from ADC, waiting for conversion to finish // (assumes someone else asked for a conversion.) // wait for it to be cleared while(ADCSRA & (1<<ADSC)) { // do nothing... just hold your breath. } // bit is cleared, so we have a result.

// read from the ADCL/ADCH registers, and combine the result
// Note: ADCL must be read first (datasheet pp. 259)
uint16_t result = ADCL;
uint16_t temp = ADCH;
result = result + (temp<<8);

// set ADSC bit to get the *next* conversion started
ADCSRA |= (1<<ADSC);

return result;

}

//--------------------------------------------------------------------------------------------- double sampleToFahrenheit(uint16_t sample) { // conversion ratio in DEGREES/STEP: // (5000 mV / 1024 steps) * (1 degree / 10mV) // ^^^ ^^ // from ADC from LM34 return sample * (5000.0 / 1024.0 / 10.0);
}

//--------------------------------------------------------------------------------------------- double Calc_VZout(double Read_Sim, double Ik, double Vref_uut, double Zout) {

 double Isim = 0;
double Vzsim = 0;
 double temp = 1;
 double temp2 = 0;

temp2 = (temp - Read_Sim);

Isim = log(temp2) * Ik;

Vzsim = (Isim * Zout);

return (Vref_uut - Vzsim);

}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ int main() {

uint8_t  Range_index = 0;
uint8_t   main_state = 0;
uint16_t    KeyValue = 0;
uint16_t last_sample = 0;

double Read_sim_inp = 2;
double         Vref = 2.5085;
double     Vref_uut = 1.1;
double        VZout = 0;
double     Range[3] = {.25,.5,.95};
double      Zout[3] = {15000,30000,7000};
double        Ik[3] = {-0.000037,-0.000037,-0.000037};
//double         Isim = 0;

// Main Loop.
while(1) 
{

    switch (main_state)
    {
        case 0:/* INITZ-Hardware. */

            //Set next State.
            main_state = 1;

            // Initz SPI hardware.
            master_SPI_init();

            // Start up the LCD.
            lcd_init();
            FILE lcd_stream = FDEV_SETUP_STREAM(lcd_putchar, 0, _FDEV_SETUP_WRITE);
            lcd_home();

            // Start up the Analog to Digital Converter.
            adc_init();

            // Start up the serial port.
            uart_init();
            FILE uart_stream = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
            stdin = stdout = &uart_stream;

            break;

        case 1:/*  Input Key. */

            // Set to next State.
            main_state = 2;

            // Get the current key pressed.
            KeyValue = Inp_Keypad();

            break;

        case 2: /* Get User's Desired Reading from ADC input. */

            // Set to next State.
            main_state = 3;

            last_sample = Inp_ADC()-40;

            // Calculate the desired Simulated Reading.
            Read_sim_inp = Range[Range_index] * (last_sample/(double)800);

            break;

        case 3:/* Calculate Vzout. */

            // Set to next State.
            main_state = 4;

            //Calculate VZout.
            VZout = Calc_VZout(Read_sim_inp, Ik[Range_index], Vref_uut, Zout[Range_index]);

            break;

        case 4:/* set the DAC for the simulated output. */

            // Set to next State.
            main_state = 5;

            //SET DAC
            SET_VDAC(0,VZout,Vref);

            break;

        case 5:/* Update Display. */

            main_state = 1;

            // write message to LCD
            lcd_home();

            fprintf_P(&lcd_stream, PSTR("Range = %.2f"), Range[Range_index] * 100);
            lcd_write_string(PSTR("%  "));

            lcd_line_two();
            fprintf_P(&lcd_stream, PSTR("Sim-Read = %.2f"), Read_sim_inp*100);
            lcd_write_string(PSTR("%  "));

            lcd_line_three();
            fprintf_P(&lcd_stream, PSTR("VZout = %.4f"), VZout);
            lcd_write_string(PSTR("V  "));

            lcd_line_four();
            fprintf_P(&lcd_stream, PSTR("ADC =%u"), last_sample);
            fprintf_P(&lcd_stream, PSTR(" Key =%u"), KeyValue);
            lcd_write_string(PSTR("  "));

            break;

        default:

            main_state = 0;

            break;

        break;

    }// END switch main_state(main_state)

} // END Main Loop.

return 0;

} //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

March 30, 2012
by pcbolt
pcbolt's Avatar

Hi Jer -

It looks like the compiler may be assuming "temp2" does not get changed after it is initialized to 0. Since log(0) is undefined, it does not want to compile.

  • You could initialize "temp2" to 1.

  • You could change the optimization level in your "Makefile" from -Os to -O0 (that's letter 'O' followed by number '0').

Either way you might want to add code to test if temp2 is zero before using the log() function.

March 31, 2012
by Jer
Jer's Avatar

Thanks for the help!

I initialized temp2 to EQL 1 and changed -Os to -O0

neither change the out come.

Jer

March 31, 2012
by Jer
Jer's Avatar

I FOUND THE SOLUTION!!!

THANKS to mrobbins and Ralphxyz!!

Microcontroller Programming » Trouble with the log10 function

June 02, 2010 by mrobbins (NerdKits Staff)

Hi Rufus, Does your compiler error say something like "relocation truncated to fit"? If so, give this a try: 1) In the Makefile, remove "-lm" from the end of the LINKFLAGS line:

1 LINKFLAGS=-Wl,-u,vfprintf -lprintf_flt -Wl,-u,vfscanf -lscanf_flt View Original 2) In the Makefile, add "-lm" to the end of the LINKOBJECTS line:

1 LINKOBJECTS=../libnerdkits/delay.o ../libnerdkits/lcd.o ../libnerdkits/uart.o -lm View Original 3) Run "make". This simply rearranges the order in which the compiler/linker bundles the math code with your code, ensuring that the math code comes last. For reasons that are a little bit voodoo, this tends to make it work. (There's a bug report about this issue from a while ago, but not too much detail in explaining it clearly.) Please let me know if that works! And if not, please post the compiler's error message. Mike

March 31, 2012
by Jer
Jer's Avatar

Also Thanks to Mike and pcbolt!

Post a Reply

Please log in to post a reply.

Did you know that 20 LEDs can be controlled from 11 microcontroller pins, to make a twinkling heart outline? Learn more...