Цитата:
Сообщение от AVK74
gnomon, а чисто поприколу, выложи исходник, а?
И нафиг кварц? Лучше от внутреннего RC затактироваться.
|
От исходника толка будет мало, т.к. он на написан на редком варианте Паскаля для AVR
Хотя... забирай исходник
program pwr1;
{$NOSHADOW}
{ $WG} {global Warnings off}
{$OPTIMISE}
Device = mega8, VCC=5;
{ $BOOTRST $01C00} {Reset Jump to $01C00}
Import SysTick, WatchDog, SoftPWM, FreqCount;
{ From System Import LongInt; }
Define
ProcClock = 16000000; {Hertz}
SysTick = 2; {msec}
WatchDog = msec2000; {presc = 7}
StackSize = $0032, iData;
FrameSize = $0032, iData;
FreqTimer = Timer1; {use Timer1}
SoftPWMport = PortD; {use PortB for PWM output}
SoftPWMtimer = Timer2, 10; {use timerx, PWM cycle time msec}
SoftPWMres = 100; {PWM resolution is 100 points}
SoftPWMchans = 1, 0; {x channels, bity is the first bit}
Implementation
{$IDATA}
{--------------------------------------------------------------}
{ Type Declarations }
type
{--------------------------------------------------------------}
{ Const Declarations }
{--------------------------------------------------------------}
{ Var Declarations }
{$IDATA}
var
TimeSpd : word;
StopCount: word;
const
SpeedOn : byte = 6;
SpeedMin : byte = 70;
SpeedMax : byte = 95;
TimeOn : word = (60000 div SpeedOn);
TimeMin : word = (60000 div SpeedMin);
TimeMax : word = (60000 div SpeedMax);
PWMOff : word = 0;
PWMStop: word = 30;
PWMMid : word = 85;
PWMMax : word = 95;
PWMStd : word = 100;
PWMDelta : word = ((PWMMax - PWMMin)*100) div (TimeMin - TimeMax);
StopCycles : byte = 22;
{ Main Program }
{$IDATA}
begin
InitPorts;
StopCount := 0;
SoftPWM1 := byte(PWMOff);
EnableInts;
loop
WatchDogStart;
if PinB.1 = false then
if SoftPWM1 <> byte(PWMStd) then
SetPWM(byte(PWMStd), 100);
else
mDelay(500);
endif;
else
SetFreqCountMode(TTimeBase100ms);
mDelay(1000);
if SemaStat(FreqCountSema) <> 0 then
TimeSpd := GetTimeCounter;
if GetFreqCountOvrFlow <> 0 then
if (PinB.1 = true) and (SoftPWM1 > byte(PWMStop)) then
inc(StopCount);
if StopCount >= StopCycles then
SetPWM(byte(PWMStop), 100);
StopCount := 0;
endif;
endif;
else
StopCount := 0;
if (TimeSpd <= TimeMax) and (SoftPWM1 <> byte(PWMMax)) then
SetPWM(byte(PWMMax), 50);
endif;
if (TimeSpd > TimeMax) and (TimeSpd < TimeMin) then
//SetPWM(byte((PWMMin * 100 + ((TimeMin - TimeSpd) * PWMDelta)) div 100), 20);
SetPWM(byte(PWMMid), 50);
endif;
if (TimeSpd <= TimeOn) and (TimeSpd >= TimeMin) and (SoftPWM1 <> byte(PWMMin)) then
SetPWM(byte(PWMMin), 50);
endif;
endif;
else
if (PinB.1 = true) and (SoftPWM1 > byte(PWMStop)) then
inc(StopCount);
if StopCount >= StopCycles then
SetPWM(byte(PWMStop), 100);
StopCount := 0;
endif;
endif;
endif;
endif;
WatchDogTrig;
endloop;
end pwr1.
Кварц нужен для увеличения производительности и точности, т.к. используется прожорливый программный ШИМ и прожорливая прецизионная процедура точного замера периода импульсов с датчика скорости с усреднением по нескольким замерам. С внутренним генератором точность измерения будет очень низкой +-10...15%.