Microcontroller code problem - Page 1
Forum section: electronics / general

author: snipersquad100
author_rank: Frequent Contributor
timestamp: January 07, 2015, 05:22:42 pm
content: 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// globalsuint16_t volt_out = 0; // range 0 - 5120, used to convert adc to voltage - adc multiplieruint16_t volt = 0; // battery voltage, could be mains power {120V,220VAC} if battery is not connecteduint8_t adc_count = 0; // counts sensor averaginguint16_t avg_adc; // store adc values for each sensor for average calcuint8_t volt_dig1, // voltage reading broken broken out for 7-segmentvolt_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 conversionif ( adc_count == ADC_SAVE ) // accumulate ADC readings{adc_count = 0;adc2avg(); // average analog voltage sensor valuesadc2volt(); // convert analog readings into voltage fahrenheitdigit_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 outputsPORTD=0x00; // disable all pull-upsSET(DDRB, CC_D1); // output for display, start high, pull low to useSET(DDRB, CC_D2); // output for display, start high, pull low to useSET(DDRB, CC_D3); // output for display, start high, pull low to useSET(PORTB, CC_D1); // output for display, start high, pull low to useSET(PORTB, CC_D2); // output for display, start high, pull low to useSET(PORTB, CC_D3); // output for display, start high, pull low to use}// collect analog values for averagingISR(ADC_vect){// ADC sensors:// adc0 - tempavg_adc = ADCW;adc_count++;}// average sensor datavoid adc2avg (void){uint8_t i;// reset global vars to zeroadc_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 responsefor(i=0;i<=ADC_SAVE;i++){adc_avg_total += avg_adc;}adc_read += adc_avg_total / ADC_SAVE;adc_volt = adc_read;}// convert adc values into voltage// just diving adc by 4 gives approximate voltagevoid adc2volt (void){// external spreadsheet used for calculating adc_dividerfloat adc_divider = 3.50;adc_volt = 10 * ( adc_volt / adc_divider );// 5% boost on readings below 15vif ( adc_volt >= 150 )adc_volt = adc_volt * .985;}// reformat voltage and pot values for 7 segvoid digit_breakup (void){if ( ADC_DEBUG )adc_volt = adc_read;volt_dig1 = adc_volt / 100; // hundreds placevolt_dig2 = ( adc_volt - ( volt_dig1 * 100 ) ) / 10; // tens placevolt_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 (: { hex = 0x7F; break; }case (9): { hex = 0x7A; break; }}return(hex);}// send voltage to 7-segment// one digit at a timevoid display_digits (void){// run the 7-segememt displayfor ( disp_pos=1;disp_pos<=3;disp_pos++){if ( disp_pos == 1 )hexcode = digit_2_hex(volt_dig1); // hexcode for 7-segement first digitelse if ( disp_pos == 2 ) {if (ADC_DEBUG)hexcode = digit_2_hex(volt_dig2); // hexcode for 7-segement second digit with decimal (0x80)elsehexcode = 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 digitwrite_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-1SET(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 speedSET(PORTB, CC_D1); // output for display, start high, pull low to useSET(PORTB, CC_D2); // output for display, start high, pull low to useSET(PORTB, CC_D3); // output for display, start high, pull low to use}

author: lapm
author_rank: Frequent Contributor
timestamp: January 07, 2015, 05:26:15 pm
content: 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...

author: Wilksey
author_rank: Super Contributor
timestamp: January 07, 2015, 06:08:29 pm
content: 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!

author: kolonelkadat
author_rank: Regular Contributor
timestamp: January 07, 2015, 06:10:26 pm
content: 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.

author: 0xdeadbeef
author_rank: Super Contributor
timestamp: January 07, 2015, 06:13:45 pm
content: 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.

author: snipersquad100
author_rank: Frequent Contributor
timestamp: January 07, 2015, 06:14:44 pm
content: The code is from herehttp://screwdecaf.cx/dp2.htmlThe link is at the bottom.

author: 0xdeadbeef
author_rank: Super Contributor
timestamp: January 07, 2015, 07:17:51 pm
content: 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.

author: bingo600
author_rank: Super Contributor
timestamp: January 07, 2015, 07:32:56 pm
content: afaik for avr-gccdelay_ms is defined in <util/delay.h>try to include that one instead of "util.c"/Bingo

author: snipersquad100
author_rank: Frequent Contributor
timestamp: January 07, 2015, 09:28:27 pm
content: That don't seem to work either Bingo.

author: 0xdeadbeef
author_rank: Super Contributor
timestamp: January 07, 2015, 09:37:55 pm
content: 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.

author: snipersquad100
author_rank: Frequent Contributor
timestamp: January 07, 2015, 09:46:45 pm
content: 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.

author: dannyf
author_rank: Super Contributor
timestamp: January 07, 2015, 09:56:41 pm
content: 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.

author: dannyf
author_rank: Super Contributor
timestamp: January 07, 2015, 10:06:56 pm
content: QuoteOne of those "you can do but you shouldn't do" things of programming.

author: snipersquad100
author_rank: Frequent Contributor
timestamp: January 07, 2015, 10:13:30 pm
content: I cant do it on my arduino ide, I cant find the atmega48 chip on that ide either.

author: Wilksey
author_rank: Super Contributor
timestamp: January 08, 2015, 12:35:49 am
content: 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 Forge2. Download fresh tar.gz file from website, extract to c:\dp23. 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.

author: snipersquad100
author_rank: Frequent Contributor
timestamp: January 08, 2015, 11:58:14 am
content: 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:\dp2How 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

author: dannyf
author_rank: Super Contributor
timestamp: January 08, 2015, 12:22:00 pm
content: QuoteI think you will find it much easier if you just focus on burning the code to the chip.

author: AndreasF
author_rank: Frequent Contributor
timestamp: January 08, 2015, 04:15:12 pm
content: Quote from: snipersquad100 on January 08, 2015, 11:58:14 amThis might help for some basics:http://dosprompt.info/

author: jlmoon
author_rank: Unknown
timestamp: January 08, 2015, 04:47:59 pm
content: how about a shortcut....if in c:\useres\johnthencd c:\dp2will goto dp2 directory if it existsassuming you're using a platform of Windows or even Dosif you type"cd/?" or "chdir/?" at command promptyou will see some interesting information

author: Wilksey
author_rank: Super Contributor
timestamp: January 08, 2015, 05:32:10 pm
content: Lets start really simple, do you have an Atmel programmer?

author: snipersquad100
author_rank: Frequent Contributor
timestamp: January 08, 2015, 05:55:14 pm
content: 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.

author: snipersquad100
author_rank: Frequent Contributor
timestamp: January 08, 2015, 06:12:23 pm
content: I typed in cd c:\dp2, it found the file but nothing happened when I typed "make"

author: Wilksey
author_rank: Super Contributor
timestamp: January 08, 2015, 07:08:54 pm
content: 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).

author: snipersquad100
author_rank: Frequent Contributor
timestamp: January 08, 2015, 08:01:41 pm
content: I don't think there's anything in there.

author: snipersquad100
author_rank: Frequent Contributor
timestamp: January 08, 2015, 08:10:09 pm
content: I just extracted dp2.src.tar file to C:dp2Typed dir and this came up.

author: snipersquad100
author_rank: Frequent Contributor
timestamp: January 08, 2015, 08:17:40 pm
content: ive typed make and its done some code

author: Wilksey
author_rank: Super Contributor
timestamp: January 08, 2015, 10:31:58 pm
content: There you go then, your last image shows that you compiled the C into a .hex file ready to program onto the chip.

author: dannyf
author_rank: Super Contributor
timestamp: January 08, 2015, 10:41:43 pm
content: QuoteThat's the same code contained in the download.So you are back to square 1: burn it into your chip.

author: snipersquad100
author_rank: Frequent Contributor
timestamp: January 08, 2015, 11:38:47 pm
content: so im 95% there so how do I put the hex onto the chip using a avr programmer.

author: Wilksey
author_rank: Super Contributor
timestamp: January 09, 2015, 01:31:38 am
content: You need a piece of software that is supported by your programmer, the software will allow you to "open" the .hex file and it will send it via the programmer to the chip.Using something like PonyProg or USBASP.

author: Rigby
author_rank: Super Contributor
timestamp: January 09, 2015, 02:27:21 pm
content: You folks were amazingly patient with that newbie. Well done.

author: snipersquad100
author_rank: Frequent Contributor
timestamp: January 09, 2015, 03:08:06 pm
content: Thank you so much for your help.

author: Wilksey
author_rank: Super Contributor
timestamp: January 09, 2015, 04:06:33 pm
content: We are all newbies at some point!Let us know how you get on!