Forum > Embedded - AVR
debugging code for te ACS712, with ATMEGA328P
pascalbythree:
Can somebody help me out debugging this piece of source code? To read the mA from the ACS712.
PS: A underhand unit for this is maybe a better help.
Greets, Wouter van Wegen, PascalByThree
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program WVW_ACS712; uses consoleio, uart, delay; const mVperAmp = 100; // 100; // use 100 for 20A Module and 66 for 30A Module ACSoffset = 2500; var RawValue : DWord; voltage : DWord; Amps: Dword; counter : integer; //****************************************************************************************************************procedure ModePortB(Pin: byte; Value: Boolean); begin if Value then begin DDRB := DDRB or (1 shl pin); end else begin DDRB := DDRB and not (1 shl pin); end; end;procedure WritePortB(Pin: byte; Value: Boolean); begin if Value then begin PORTB := PORTB or (1 shl pin); end else begin PORTB := PORTB and not (1 shl pin); end; end;procedure ModePortD(Pin: byte; Value: Boolean); begin if Value then begin DDRD := DDRD or (1 shl pin); end else begin DDRD := DDRD and not (1 shl pin); end; end;procedure WritePortD(Pin: byte; Value: Boolean); begin if Value then begin PORTD := PORTD or (1 shl pin); end else begin PORTD := PORTD and not (1 shl pin); end; end;function ReadPortD(bit: byte): Boolean; begin Result := PIND and (1 shl bit) <> 0; end;function ReadPortB(bit: byte): Boolean; begin Result := PINB and (1 shl bit) <> 0; end;function IntToStr(const i: Integer): string; begin Str(i, Result); end;function StrToInt(const s: string): Integer; var code: Integer; begin Val(s, Result, code); end;//**************************************************************************************************************** procedure ADC_Init;const Port = 0;begin ADMUX := (1 shl REFS) or (Port and $0F); ADCSRA := %111 or (1 shl ADEN);end; function ADC_Read(Port : byte) : integer;begin ADMUX := (1 shl REFS) or (Port and $0F); // Specify port ADCSRA := ADCSRA or ( 1 shl ADSC ); // Start measuring while (ADCSRA and (1 shl ADSC)) <> 0 do // Wait until measured begin end; Result := ADC; // Read out the measured valueend; function WriteChar(ACh: char; AUserData: pointer): boolean;begin WriteChar := true; uart.uart_transmit(ord(Ach));end; function ReadChar(var ACh: char; AUserData: pointer): boolean;begin ReadChar:=true; ACh := char(uart.uart_receive);end; procedure initIO;begin uart_init1(115200, true); OpenIO(Input, @WriteChar, @ReadChar, fmInput, nil); OpenIO(Output, @WriteChar, @ReadChar, fmOutput, nil); OpenIO(ErrOutput, @WriteChar, @ReadChar, fmOutput, nil); OpenIO(StdOut, @WriteChar, @ReadChar, fmOutput, nil); OpenIO(StdErr, @WriteChar, @ReadChar, fmOutput, nil);end; begin initIO; ModePortB(0,True); // Initialize ADC ADC_Init; counter := 0; repeat counter := counter + 1; if counter = 100 then counter := 1; //***************** PART TO DEBUG FORUM ************************ RawValue := ADC_Read(0); voltage := (RawValue * 5000) div 1024; Amps := Voltage - ACSoffset; Amps := Amps div mVperAmp;//***************************************************************************** WriteLn('***************************'); WriteLn('counter: '+IntToStr(counter)); WriteLn('RawValue: '+IntToStr(RawValue)); WriteLn('Amps: '+IntToStr(Amps)); WriteLn('***************************'); WritePortB(0,True); // Status LED ON delay_ms(25); WritePortB(0,False); delay_ms(25); // Status LED OFF until 1=2; end.
Laksen:
What's the problem?
Khrys:
The only thing that caught my eye at first glance is that ADC_Read returns a signed integer even though ADC is unsigned, but that shouldn't matter since it's never greater than 1023 anyway... what exactly is the problem you're having?
ccrause:
--- Quote from: pascalbythree on January 22, 2025, 12:43:15 pm ---Can somebody help me out debugging this piece of source code? To read the mA from the ACS712.
--- End quote ---
What problem do you observe? What tests have you done, and what measurements were obtained?
MarkMLl:
--- Quote from: ccrause on January 22, 2025, 01:25:54 pm ---
--- Quote from: pascalbythree on January 22, 2025, 12:43:15 pm ---Can somebody help me out debugging this piece of source code? To read the mA from the ACS712.
--- End quote ---
What problem do you observe? What tests have you done, and what measurements were obtained?
--- End quote ---
Have you successfully loaded the code onto the target?
Have you verified- e.g. by a startup message- that the expected code is running?
How does the structure of your code compare to examples written in e.g. C?
Have you read and complied with the device datasheet?
MarkMLl
Navigation
[0] Message Index
[#] Next page