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 value
end;
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.