Author Topic: Microcontroller code problem  (Read 10650 times)

0 Members and 1 Guest are viewing this topic.

Offline snipersquad100Topic starter

  • Frequent Contributor
  • **
  • Posts: 396
  • Country: gb
    • Electrinics
Microcontroller code problem
« on: January 07, 2015, 05:22:42 pm »
Can anyone please tell me how to fix this code, Its a volt meter/analyser for a battery charger.
It's a atmel atmega48v. It doesn't use the arduino ide so I tried atmel studio 6.2.
When I press the build button I get this error message "util.c: no such file or directory".
I presume it's a library im missing if so where can I get it and where do I put it thanks.

code

#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdio.h>
#include "util.c"   
#include "adc.h"      

#define MUX_OFFSET 0x40         // MUX value for REFS0 to be enabled for AREV <---> VCC tie
#define ADC_SAVE 64             // average ADC values for sensors
#define CLR(x,y) (x&=(~(1<<y)))
#define SET(x,y) (x|=(1<<y))
#define CC_D1 PB0
#define CC_D2 PB1
#define CC_D3 PB2
#define ADC0 PC0
#define ADC_DEBUG 0      // show ADC values on 7 segement display


// globals
uint16_t   volt_out = 0;      // range 0 - 5120, used to convert adc to voltage - adc multiplier
uint16_t   volt = 0;      // battery voltage, could be mains power {120V,220VAC} if battery is not connected
uint8_t      adc_count = 0;      // counts sensor averaging
uint16_t   avg_adc[ADC_SAVE];   // store adc values for each sensor for average calc
uint8_t      volt_dig1,      // voltage reading broken broken out for 7-segment
      volt_dig2,
      volt_dig3 = 0;
uint8_t    hexcode;
uint16_t    adc_volt;
uint16_t    adc_read;
uint16_t    adc_avg_total;
uint8_t    disp_pos=1;


int main (void);
ISR(ADC_vect);
void port_init (void);
void adc2avg (void);
void adc2volt (void);
void digit_breakup (void);
void display_digits (void);
void write_sev_seg (uint8_t alphanum, uint8_t digit);
uint8_t digit_2_hex (uint8_t display_digit);


int main (void)
{

   port_init();
   adc_init();
   sei();

   while (1)
   {
      SET(ADCSRA, ADSC);      // start adc conversion

      if ( adc_count == ADC_SAVE )    // accumulate ADC readings
      {

         adc_count = 0;
         adc2avg();      // average analog voltage sensor values
         adc2volt();      // convert analog readings into voltage fahrenheit
         digit_breakup();   // break voltage into hundreds, tens and ones places

      }

      display_digits();          // update 7-segement display w/voltage
//      delay_ms(1);

   }

   return(0);
}


void port_init (void) {

   DDRD=0xFF;      // display port all outputs
   PORTD=0x00;      // disable all pull-ups

   SET(DDRB, CC_D1);   // output for display, start high, pull low to use
   SET(DDRB, CC_D2);   // output for display, start high, pull low to use
   SET(DDRB, CC_D3);   // output for display, start high, pull low to use
   SET(PORTB, CC_D1);   // output for display, start high, pull low to use
   SET(PORTB, CC_D2);   // output for display, start high, pull low to use
   SET(PORTB, CC_D3);   // output for display, start high, pull low to use

}


// collect analog values for averaging
ISR(ADC_vect)
{

   // ADC sensors:
   // adc0 - temp
   avg_adc[adc_count] = ADCW;
   adc_count++;
}


// average sensor data
void adc2avg (void)
{
   uint8_t i;

        // reset global vars to zero
        adc_count = 0;
        adc_volt = 0;
   adc_read = 0;
        adc_avg_total = 0;

        // slow down and average ADC readings to reduce noise
        // reduce ADC_SAVE to speed up response
        for(i=0;i<=ADC_SAVE;i++)
        {
                adc_avg_total += avg_adc[adc_count];
        }

        adc_read += adc_avg_total / ADC_SAVE;
   adc_volt = adc_read;

}


// convert adc values into voltage
// just diving adc by 4 gives approximate voltage
void adc2volt (void)
{
   // external spreadsheet used for calculating adc_divider
   float adc_divider = 3.50;

        adc_volt = 10 * ( adc_volt / adc_divider );

   // 5% boost on readings below 15v
   if ( adc_volt >=  150 )
      adc_volt = adc_volt * .985;

}


// reformat voltage and pot values for 7 seg
void digit_breakup (void)
{

   if ( ADC_DEBUG )
      adc_volt = adc_read;

   volt_dig1 = adc_volt / 100;                                             // hundreds place
   volt_dig2 = ( adc_volt - ( volt_dig1 * 100 ) ) / 10;                    // tens place
   volt_dig3 = adc_volt - ( ( volt_dig1 * 100 ) + ( volt_dig2 * 10 ) );    // ones place

}

uint8_t digit_2_hex ( uint8_t display_digit )
{                                                                                                                     
        uint8_t hex;                                                                                                   

        switch ( display_digit )
   {
         case (0): { hex = 0x77; break; }
         case (1): { hex = 0x12; break; }
         case (2): { hex = 0x3D; break; }
         case (3): { hex = 0x3E; break; }
         case (4): { hex = 0x5A; break; }
         case (5): { hex = 0x6E; break; }
         case (6): { hex = 0x6F; break; }
         case (7): { hex = 0x32; break; }
         case (8): { hex = 0x7F; break; }
         case (9): { hex = 0x7A; break; }
   }

        return(hex);

}



// send voltage to 7-segment
// one digit at a time
void display_digits (void)
{
       
        // run the 7-segememt display
        for ( disp_pos=1;disp_pos<=3;disp_pos++)
        {
                if ( disp_pos == 1 )
                        hexcode = digit_2_hex(volt_dig1);   // hexcode for 7-segement first digit
                else if ( disp_pos == 2 ) {
         if (ADC_DEBUG)
            hexcode = digit_2_hex(volt_dig2); // hexcode for 7-segement second digit with decimal (0x80)
         else
            hexcode = digit_2_hex(volt_dig2) | 0x80; // hexcode for 7-segement second digit with decimal (0x80)
                } else if ( disp_pos == 3 )
                        hexcode = digit_2_hex(volt_dig3);   // hexcode for 7-segement third digit                         

                write_sev_seg(hexcode,disp_pos);      // pull the cathodes low to populate display
   }
   
}


void write_sev_seg (uint8_t hexcode, uint8_t digit)
{

   if ( digit == 1 ) {

      // shutdown display - reverse logic for common cathode
      // enable digit-1
      SET(PORTB, CC_D1);
      SET(PORTB, CC_D2);
      SET(PORTB, CC_D3);
      CLR(PORTB, CC_D1);

   } else if ( digit == 2 ) {

      SET(PORTB, CC_D1);
      SET(PORTB, CC_D2);
      SET(PORTB, CC_D3);
      CLR(PORTB, CC_D2);

   } else if ( digit == 3 ) {

      SET(PORTB, CC_D1);
      SET(PORTB, CC_D2);
      SET(PORTB, CC_D3);
      CLR(PORTB, CC_D3);

   }

   PORTD = hexcode;

   delay_ms(1);      // controls display speed

   SET(PORTB, CC_D1);      // output for display, start high, pull low to use
   SET(PORTB, CC_D2);      // output for display, start high, pull low to use
   SET(PORTB, CC_D3);      // output for display, start high, pull low to use

}

Offline lapm

  • Frequent Contributor
  • **
  • Posts: 564
  • Country: fi
Re: Microcontroller code problem
« Reply #1 on: January 07, 2015, 05:26:15 pm »
You should go back to source of this code and find out what environment they used to compile this.. C-compiler is C-compiler, but where libraries and whats available depends on environment compiler runs...
Electronics, Linux, Programming, Science... im interested all of it...
 

Offline Wilksey

  • Super Contributor
  • ***
  • Posts: 1329
Re: Microcontroller code problem
« Reply #2 on: January 07, 2015, 06:08:29 pm »
Where did you get the code from?
I suspect that Util.c is just missing from the download, but it is a custom file, so it could contain anything!
 

Offline kolonelkadat

  • Regular Contributor
  • *
  • Posts: 202
  • Country: us
  • Obviously, windows are central to Windows.
    • Force Project X
Re: Microcontroller code problem
« Reply #3 on: January 07, 2015, 06:10:26 pm »
if you actually wrote this and dont know what "util.c" is, why did you include it on like the 3rd line of code?

if you just copied this from somewhere, you should go back to where you copied it from and look for a "util.c" file because it appears to be a custom file.

 

Offline 0xdeadbeef

  • Super Contributor
  • ***
  • Posts: 1576
  • Country: de
Re: Microcontroller code problem
« Reply #4 on: January 07, 2015, 06:13:45 pm »
Just remove "util.c" and see where the linker complains. From what it looks like, "delay_ms" is the only function that could be from that util module. If so, remove the call or come up with your own delay code.
Trying is the first step towards failure - Homer J. Simpson
 

Offline snipersquad100Topic starter

  • Frequent Contributor
  • **
  • Posts: 396
  • Country: gb
    • Electrinics
Re: Microcontroller code problem
« Reply #5 on: January 07, 2015, 06:14:44 pm »
The code is from here http://screwdecaf.cx/dp2.html
The link is at the bottom.

Offline 0xdeadbeef

  • Super Contributor
  • ***
  • Posts: 1576
  • Country: de
Re: Microcontroller code problem
« Reply #6 on: January 07, 2015, 07:17:51 pm »
Well, the source archive there contains a util.c and this contains delay_ms as expected. Including a C file is pretty bad style, but in this case, it should work.
Trying is the first step towards failure - Homer J. Simpson
 

Offline bingo600

  • Super Contributor
  • ***
  • Posts: 1988
  • Country: dk
Re: Microcontroller code problem
« Reply #7 on: January 07, 2015, 07:32:56 pm »
afaik for avr-gcc

delay_ms is defined in <util/delay.h> 

try to include that one instead of "util.c"

/Bingo
 

Offline snipersquad100Topic starter

  • Frequent Contributor
  • **
  • Posts: 396
  • Country: gb
    • Electrinics
Re: Microcontroller code problem
« Reply #8 on: January 07, 2015, 09:28:27 pm »
That don't seem to work either Bingo.

Offline 0xdeadbeef

  • Super Contributor
  • ***
  • Posts: 1576
  • Country: de
Re: Microcontroller code problem
« Reply #9 on: January 07, 2015, 09:37:55 pm »
Probably a problem of project setup and/ot the IDE is confused by including a c file into a C file.
Note that the project was built using a specific makefile. Can you execute the makefile directly from the command line. Might need a shell, some environment variables set etc.

Anyway, you could try to copy the content of util.c into the main c file and then delete util.c from the project.
Trying is the first step towards failure - Homer J. Simpson
 

Offline snipersquad100Topic starter

  • Frequent Contributor
  • **
  • Posts: 396
  • Country: gb
    • Electrinics
Re: Microcontroller code problem
« Reply #10 on: January 07, 2015, 09:46:45 pm »
Thanks Oxdeadbeef I'll give it a go.
I've been sent a hex file from the author but haven't a clue what to do with it.
Im shit at code as you prob can tell.

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: Microcontroller code problem
« Reply #11 on: January 07, 2015, 09:56:41 pm »
where to put it and how to put it will depend on the tools (IDE for example) you use.

It compiles flawlessly under CB/Winavr or Arduino IDE.
================================
https://dannyelectronics.wordpress.com/
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: Microcontroller code problem
« Reply #12 on: January 07, 2015, 10:06:56 pm »
Quote
#include "util.c"   

One of those "you can do but you shouldn't do" things of programming.
================================
https://dannyelectronics.wordpress.com/
 

Offline snipersquad100Topic starter

  • Frequent Contributor
  • **
  • Posts: 396
  • Country: gb
    • Electrinics
Re: Microcontroller code problem
« Reply #13 on: January 07, 2015, 10:13:30 pm »
I cant do it on my arduino ide, I cant find the atmega48 chip on that ide either.

Offline Wilksey

  • Super Contributor
  • ***
  • Posts: 1329
Re: Microcontroller code problem
« Reply #14 on: January 08, 2015, 12:35:49 am »
Hmm,

Ok, I have just compiled this from the original source download from the website you specified.

Steps to do so are:
1. Download WinAVR from Source Forge
2. Download fresh tar.gz file from website, extract to c:\dp2
3. Install WinAVR, make sure the environment paths option is checked on installation (will be obvious on WinAVR installer screens)
4. Open Command Prompt, and navigate to c:\dp2.
5. Type: make and press enter.
6. .elf and .hex will be generated in the folder.

Job Done!

You will need a programmer to write the HEX file to a chip.
 

Offline snipersquad100Topic starter

  • Frequent Contributor
  • **
  • Posts: 396
  • Country: gb
    • Electrinics
Re: Microcontroller code problem
« Reply #15 on: January 08, 2015, 11:58:14 am »
Sorry for my simple-mindedness.
I downloaded winavr.
What is tar.gz? is it the code zip file from the website I pointed to, If so I extracted that to c:\dp2
How do you navigate to c:\dp2 in command prompt? my command prompt starts with c:\useres\john>
If I type in c:\dp2 it says c:\dp2 is not recognized as an internal or external command operable program or batch file.

C:\users\john>

Thanks again


Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: Microcontroller code problem
« Reply #16 on: January 08, 2015, 12:22:00 pm »
Quote
Sorry for my simple-mindedness.

I think you will find it much easier if you just focus on burning the code to the chip.
================================
https://dannyelectronics.wordpress.com/
 

Offline AndreasF

  • Frequent Contributor
  • **
  • Posts: 251
  • Country: gb
    • mind-dump.net
Re: Microcontroller code problem
« Reply #17 on: January 08, 2015, 04:15:12 pm »
Sorry for my simple-mindedness.
I downloaded winavr.
What is tar.gz? is it the code zip file from the website I pointed to, If so I extracted that to c:\dp2
How do you navigate to c:\dp2 in command prompt? my command prompt starts with c:\useres\john>
If I type in c:\dp2 it says c:\dp2 is not recognized as an internal or external command operable program or batch file.

C:\users\john>

Thanks again

This might help for some basics: http://dosprompt.info/ 
my random ramblings mind-dump.net
 

Offline jlmoon

  • Supporter
  • ****
  • Posts: 609
  • Country: us
  • If you fail the first time, keep trying!
Re: Microcontroller code problem
« Reply #18 on: January 08, 2015, 04:47:59 pm »
how about a shortcut....

if in c:\useres\john
then

cd c:\dp2

will goto dp2 directory if it exists
assuming you're using a platform of Windows or even Dos
if you type"cd/?" or "chdir/?" at command prompt
you will see some interesting information
Recharged Volt-Nut
 

Offline Wilksey

  • Super Contributor
  • ***
  • Posts: 1329
Re: Microcontroller code problem
« Reply #19 on: January 08, 2015, 05:32:10 pm »
Lets start really simple, do you have an Atmel programmer?
 

Offline snipersquad100Topic starter

  • Frequent Contributor
  • **
  • Posts: 396
  • Country: gb
    • Electrinics
Re: Microcontroller code problem
« Reply #20 on: January 08, 2015, 05:55:14 pm »
Thanks guys for the information, I'll will give it a go now.
oh im using windows 7 and yes I have a avr progrmer.

Offline snipersquad100Topic starter

  • Frequent Contributor
  • **
  • Posts: 396
  • Country: gb
    • Electrinics
Re: Microcontroller code problem
« Reply #21 on: January 08, 2015, 06:12:23 pm »
I typed in cd c:\dp2, it found the file but nothing happened when I typed "make"

Offline Wilksey

  • Super Contributor
  • ***
  • Posts: 1329
Re: Microcontroller code problem
« Reply #22 on: January 08, 2015, 07:08:54 pm »
When in your "c:\dp2" folder, can you type "dir" and post the results, as you should have a host of files in that directory.

One more thing, when you unzip a TAR.GZ file, it will extract the .TAR file, you then need to extract this (almost double compressed) into the c:\dp2 folder.

This can be confirmed if you only have in your c:\dp2 folder one .tar file, it will need to be decompressed again (use JZip or 7Zip or something).
 

Offline snipersquad100Topic starter

  • Frequent Contributor
  • **
  • Posts: 396
  • Country: gb
    • Electrinics
Re: Microcontroller code problem
« Reply #23 on: January 08, 2015, 08:01:41 pm »
I don't think there's anything in there.

Offline snipersquad100Topic starter

  • Frequent Contributor
  • **
  • Posts: 396
  • Country: gb
    • Electrinics
Re: Microcontroller code problem
« Reply #24 on: January 08, 2015, 08:10:09 pm »
I just extracted dp2.src.tar file to C:dp2
Typed dir and this came up.


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf