Hi Merlin,
my current test programm
program MC4808_Check_03; // Merlin 24.10.21
{ -------------------------- ---------------------------------------------------
Hi Dave,
Important to me:
ADC Port's 1-4 (6)
I2C Port
ISP Port
TX/RX(1) example 115,2kB
TX/RX(2) 9,6kB
(TX/RX(3))
sleep (timer)
kind regards
Toni
// ---------------- Thinary NANO Atmega 4808 (34 PIN!) ---------------------
PD6 D24
VIN 23 8-12V VIN Input
GND 22 GND
PF6 RST
+5V 21 +5V
PF5 A7
PF4 A6
PF3 A5 SCL
PF2 A4 SDA
PD3 A3
PD2 A2
PD1 A1
PD0 A0
PD7 AREF
3,3V
PC2 D13 SCK (LED)
PD5 D23
PD4 D22
PC1 D12 MISO
PC0 D11 MOSI
PC3 D10 SS
PA7 D9 CLK(ACout)
PA6 D8 PB0
PA5 D7 PD7
PA4 D6
PA3 D5 SCL
PA2 D4 SDA
PA1 D3 RX0
PA0 D2 TX0
GND GND
PF6 RST Reset
PF1 01 RX2
PF0 00 TX2
UPDI UPDI (Pin1)
// ---------------------------- Prog Einstellungen ------------------------- }
{$NOSHADOW}
{ $WG} {global Warnings off}
{ $EEPROM}
// ??????????????????????????????????????????????????????????????????
Device = mega328p, VCC = 5;
{ $BOOTRST $03800} {Reset Jump to $03800}
Define_Fuses
Override_Fuses;
COMport = USB2;
LockBits0 = [];
FuseBits0 = [CKSEL0, CKSEL1, SUT0]; // > 8MHz?
FuseBits2 = [];
ProgMode = SPI;
ProgFuses = false; // ausschalten!!!!
ProgLock = false;
ProgFlash = true;
ProgEEprom = true;
Supply = 5.0,200;
// ----------------------------- Arduino Nano BOARD-------------------------
Import SysTick,SerPort,TWIMaster,ADCPort; //BeepPort; Watchdog,
From System Import float;
Define
ProcClock = 16000000;
SysTick = 10; // msec
StackSize = $100, iData;
FrameSize = $100, iData;
// WatchDog = 6; // 1024msec
SerPort = 9600, Stop1; //
RxBuffer = 8, iData; //
TxBuffer = 8, iData; //
TWIpresc = TWI_BR400; // TWI speed max 400kHz
ADCchans = [1..6],iData; //
ADCpresc = 64; //
// BeepPort = PortB,1; // Signal
Implementation
{$IDATA}
var
Timer3 : SysTimer; {Variable vom Typ als SysTimer!}
// ??????????????????????????????????????????????????????????????????
{--------------------------------------------------------------}
{ Type Declarations }
type
{ Const Declarations }
const
// BH1750 : byte = $23; // Licht Sensor
// Lidar0 : byte = $29; // LIDAR0
// HDC1080 : byte = $40; // Temp & Feuchte HDC1080
// CO2 : byte = $5A; // CO2 Sensor $5A oder $5B ? Fehler!
// DS : byte = $68; // HW-Clock
PCA : byte = $41; // 16 x PWM 12bit(0-4095) Freq. 25Hz ....15800Hz
Mode_Reg : byte = $01;
Result_Reg : byte = $02;
Start_Reg : byte = $F4;
// -----------------------------------------------------------------------------
Text : Array[0..40] of String[8] // fängt bei 0 an!!!
= ('001','4C3','1MSB','001078xx',' ',' ',' ',' ',' ',' ',' ',
' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
' ',' ',' ',' ',' ',' ',' ',' ',' ','LIDAR F!');
// -----------------------------------------------------------------------------
WoDay : Array[0..6] of string[3]=('So.','Mo.','Di.','Mi.','Do.','Fr.','Sa.');
{----------------------------------------------------------------------}
{EEPROM}
var
{ Var Declarations }
{$IDATA}
LED [@PortD,2] : bit; // LED
Sig [@PortD,3] : bit; // Signal
// RES [@PortB,1] : bit; // CO2 HW-Reset (D09)
// WD [@PortD,3] : bit; // Watchdog Timer STWD100 (Puls 1usH>L>H)
// Mute [@PortB,2] : bit; // LS TE EIN/AUS (D10)
// Radar [@PinD, 5] : bit; // Radar Sensor Anlagenschutz (D05)
// Busy_F [@PinB, 3] : bit; // Busy Flag = 1 (Mp3 Player)) (D11)
// ----------------------------------------------------------------------------
var
n,count,stat,LB,C1,C2,C3,C4,C5,C6,C7,C8,C,ss,anz,I2 : byte;
schleife,CO2temp,status_R,stat_bin,Temp,I_,fr,x_,p_adr : byte;
hour,minute,secound,day,month,year,weekday,com,bef,pu_f : byte;
lbx,hbx,power_x,xe,power_b,n_,counter,play,h,l : byte;
STd_,Min_,ME_,Tag_,Jahr_,d_std,d_min,vol,ewa : byte;
WO,WC,NTC,LHL,LUX,CO2_,VOC_,LM35,ts,pw,pxc,u_akku,ima : word;
pxm,poff,Udiff;CT_,Udiff,Udig,LM35d,count_w,dt_,power : word;
ADC1,ADC2,ADC3,ADC4,ADC5,ADC6 : word;
HDC_T, HDC_H,step : float;
LW : LongWord;
br,hd : char;
lByArr[@LW] : Array[1..2] of byte;
CO2Array : Array[1..8] of byte;
I2Cbuff : Array[1..30]of byte;
BF : String[2];
clk : String[2];
DFP_ : string[8];
player : String[3];
ea : String[3];
ed : String[3];
{---------------------- Datenrichtungs Register -------------------------}
{ functions }
procedure Init_Ports; // 1 = Output
begin
// DDRB := %00000111; //
// DDRA := %00000000; //
DDRC := %00000000; //
DDRD := %00001100; // LED - Signal
//DDRF := %
// DDRF := %00000000;
end;
// -------------------------------------------------------------------------
procedure CR;
begin
write(serout,char(13)+char(10)); // CR & LF
end;
// -------------------------------- Timer --------------------------------------
procedure t_(s_t:word); // n x 1ms Timer!!
begin
repeat
dec(s_t);mdelay(1);
until s_t=0;
end;
// -----------------------------------Text -------------------------------------
procedure TXT(nr:byte); // Text Ausgabe
begin
Write(Serout,TEXT[nr]); // Ausgabe Text(nr)
end;
// -----------------------------------------------------------------------------
procedure LED_(ts:word);
begin
excl(LED); t_(ts); incl(LED);
end;
//-----------------------------------Signal ------------------------------------
procedure Sig_(ts:word); // Signal
begin
excl(Sig);t_(ts);incl(Sig); // H- > L
end;
// -------------------------------- PCA INIT -----------------------------------
procedure Init_PCA_; // OE = GND!
begin
if TWIstat(PCA) = true then // PCA9685 ok ?
write(serout,'PCA9685 ok! ADR: $41');CR;
fr:= 3; // 1600Hz
TWIOut(PCA,$00,$30); // Mode1 Adr. 00 sleep (wichtig!)
TWIOut(PCA,$FE,fr); // Prescale Reg. setze Freq (3 ~1700Hz)
TWIOut(PCA,$00,$20); // Mode1 Adr. 00 sleep A(wichti!g)
TWIOut(PCA,$01,$10); // NMOS($04) ohne Invert. ($10))!!
t_(25); // ein must! min 250ms
else // wait 200ms !
write(serout,'PCA9685 F!');CR;
endif;
end;
// ----------------------------------- INIT ------------------------------------ q
procedure PCA_Init_; // ohne invertierung!!
begin
TWIOut(PCA,$00,$30);TWIOut(PCA,$FE,fr);TWIOut(PCA,$01,$10);TWIOut(PCA,$00,0);
t_(10);
end;
// -------------------------------- PCA Reset ----------------------------------
procedure PCAR; // PCA Reset (Clear)
begin
TWIOut(PCA,$FA,$00);TWIOut(PCA,$FB,$00);TWIOut(PCA,$FC,$00);TWIOut(PCA,$FD,$00); // PCA clear
end;
// ----------------------------------------------
procedure PCA_R_;
begin
TWIOut(PCA,$00,$80);t_(5); // Restart 1000 0000
end;
// ---------------------------- PCA Adressen ------------------------------
procedure PCA_(adr:byte); // PWM Ports 1600Hz-0..4095(12bit)
begin
p_adr:= 4*adr+6; // 0 = 1 Adresse(6) M1!
lbx:=lo(pw);hbx:=hi(pw); // LB & HB
TWIOut(PCA,p_adr,lbx);TWIOut(PCA,p_adr+1,hbx); // ab Adresse 7!(0-15)!
end;
// -----------------------------------------------------------------------------
procedure PCA_up_down; // PWM 0-15 ON
begin
write(serout,'Count 0-15 UP');CR;
pw:=1;
for i_:=1 to 4 do
for n:=0 to 15 do
PCA_(n);t_(25);PCAR;t_(25); // R-G-B-Y-M C-W Check
endfor;
PCAR;
write(serout,'Count 15-0 down');CR;
for n:=15 downto 0 do // Time (ms)
PCA_(n);t_(25);PCAR;t_(25); // R-G-B-Y-M C-W Check
endfor;
endfor;
PCAR;
end;
// -----------------------------------------------------------------------------
procedure PCA_ud;
begin
PCAR;
write(serout,'UP/DOWN');CR;
for n:=1 to 6 do
for pw:=0 to 4095 do // up
lbx:=lo(pw);hbx:=hi(pw);
TWIOut(PCA,$FC,lbx);TWIOut(PCA,$FD,hbx);sdelay(3);
endfor;
t_(10);
for pw:=4095 downto 0 do
lbx:=lo(pw);hbx:=hi(pw);
TWIOut(PCA,$FC,lbx);TWIOut(PCA,$FD,hbx);sdelay(6);
endfor;
endfor;
pw:=0;PCAR;
end;
// ------------------------------------- -------------------------------------
procedure PCA_on_off; //all LED ON/OFF
begin
write(serout,'ON/OFF');CR;
for n_ := 0 to 40 do
TWIOut(PCA,$FB,$10);t_(25);PCAR;t_(50);
endfor;
PCAR;
end;
// ------------------------------------------------------------------------
Procedure PCA_Check_;
begin
write(serout,'PCA9685 INIT ~16kHz & Out(LED or NMOS');CR;
PCA_up_down; // enzelne LED's
PCA_ud;
PCA_on_off;
PCAR;
end;
// ------------------------------------ TON E/A --------------------------------
procedure ADC_Check_; // Verstärker E(1)/A(0)
begin
step:= 4.89; // Uref (5000mV / 1023
write(serout,'Step :'+floattostr(step:1:3));CR;
ADC1:= getadc(1);
ADC2:= getadc(2);
ADC3:= getadc(3);
ADC4:= getadc(4);
// ADC5:= getadc(5);
// ADC6:= getadc(6);
Write(serout,'ADC1 : '+inttostr(ADC1)+' U : '+floattostr(float(ADC1)*step :2)+'mV');CR;
Write(serout,'ADC2 : '+inttostr(ADC2)+' U : '+floattostr(float(ADC2)*step :2)+'mV');CR;
Write(serout,'ADC3 : '+inttostr(ADC3)+' U : '+floattostr(float(ADC3)*step :2)+'mV');CR;
Write(serout,'ADC4 : '+inttostr(ADC4)+' U : '+floattostr(float(ADC4)*step :2)+'mV');CR;
// Write(serout,'ADC5 : '+inttostr(ADC5:4)+' U : '+floattostr(float(ADC5)*step :2)+'mV');CR;
// Write(serout,'ADC6 : '+inttostr(ADC6:4)+' U : '+floattostr(float(ADC6)*step :2)+'mV');CR;
end;
// -----------------------------------------------------------------------------
procedure I2C_Check_; // Ermitteln von I2C Anschlüssen
begin
write(serout,' I2C-Check ');CR;
anz:=0;
for i2:=$02 to $FE do
if TWIstat(i2)= true then
inc(anz);t_(10); // Sensor found
write(serout,bytetostr(anz)+' Adressen : $'+bytetohex(i2));CR; // ok !
endif;
endfor;
write(serout,' Adressen : '+bytetostr(anz));CR;
end;
// ----------------------------------------------------------------------------
Procedure U_Akku_;
begin
U_Akku:=GetADC(3)div 10 *234; // U_akk in mV 12V = 12000mV
Udig:=GetADC(3);
write(serout,'Spannung : '+inttostr(U_Akku div 10 :2:2)+'V Udig '+inttostr(Udig));CR;
end;
//--------------------------- LED R-G-B-Y-M-C-W -------------------------------
procedure RGB(color:byte;pxc:word;t_sw:word); // RGB Leuchtband Check
begin
pw:=pxc; // wichtig
case color of
1 : PCA_(8)| // red (2x) start Code(Service)
2 : PCA_(9)| // grn !!! Leitungen!! am LED Band
3 : PCA_(10)| // blue
4 : PCA_(8);PCA_(9)| // yellow
5 : PCA_(8);PCA_(10)| // magenta
6 : PCA_(9);PCA_(10)| // cyan
7 : PCA_(8);PCA_(9);PCA_(10)| // white
endcase; // 25ms -> xxx ms
pw:=0;PCA_(08);PCA_(09);PCA_(10); // Reset R-G-B R_G-B Aus
end;
//----------------------------------------------------------------------------------
procedure B_Reg; // App Funktionen!(Befehle)
begin
if serstat = true then // an RX Zeichen?
read(SerInp, BF);
if BF='ad' then ADC_Check_;endif; // ADC Check 1-6 // Infos
if BF='pc' then PCA_Check_;endif; // PCA Check =end! ~1600hz (4094 max) non invert
if BF='sr' then system_reset;endif; // System Reset
if BF='si' then Sig_(100);endif; // Signal Buzzer
if BF='ld' then LED_(200);endif; // LED rt 100ms
if BF='i2' then I2C_Check_;endif; // TWI Check
FlushBuffer(RxBuffer);
endif;
end;
// ***************************************************************************
begin
EnableInts; // Interrupt zulassen
init_Ports; // Set Ports I/O
write(serout,'--- PCA - ADC - LED - Sig Check ---');CR;
init_PCA_;
// PCA_Check_;
// PCA_up_down; // enzelne LED's
// PCA_ud;
// PCA_on_off; // PCA LED Check
LED_(100);Sig_(50); //
// -----------------------------------------------------------------
loop
t_(10); // 1ms!! tick-Timer ~10ms
B_Reg; // Befehls Register
endloop;
End MC4808_Check_03.