'***** pwm in out test 20081113 'ATmega168 'RC 8M ver auto changer test '******** syokika settei ************** $Clock=8 'RC 8MHz $Device=m168 $Stack = 32 $Source= On $Lcd = PORTC.0, RS=PORTD.0, EN=PORTD.1 , 16, 2 $LeadChar=" ",Format (3,0) '***** dim value ****** Dim A As Word Dim Av As Word Dim Avf As Byte Dim Avf2 As Byte Dim Avf3 As Byte Dim Auto As Byte Dim Diag As Bit Dim Tq As Word Dim Tq00 As Word Dim Tqx As Word Dim Tqmax As Byte Dim Tqoff As Word Dim Tqont As Word Dim Tqofft As Byte Dim Tqon As Bit Dim Tqtlim As Byte Dim Tqeg As Bit Dim Tudisp As Bit Dim T As Word Dim T2 As Word Dim Tt2 As Word Dim Tc As Byte Dim Tc1 As Byte Dim Tc2 As Byte Dim Mq As Word Dim Mqx As Word Dim Mqmax As Byte Dim Mqcal As Bit Dim V As Word Dim Q As Word Dim C1 As Byte Dim C2 As Byte ' Dim C3 As Byte Dim C4 As Byte Dim C6 As Byte Dim I As Byte Dim N As Byte Dim L As Byte Dim Kdc As Byte ' Dim kpt As Byte ' Dim kpt2 As Byte Dim Sc As Bit Dim Sh As Byte Declare Sub Cap2() Declare Sub Bardisp() Declare Sub Tud() Declare Interrupt Icp1() Declare Sub Avfcal() '**** port ***************** 'Config Portc = Input 'Config Portb = Input 'Config PORTD = Output 'mod_plus_output OC2B PD3(5pin) 'plus input Icp1 PB0(14pin) '***** SW input port ****** ' SW1 PB1(15pin) active high ' SW2 PB2(16pin) active high ' SW3 PB3(17pin) active high '***ADC*** 'Config Adc = Single , Prescaler = 8 'Start Adc ADMUX=5 ' ADCSRA=128+64+3 ADCSRA=195 '=195 'aden[bit7]=128 'adps1[bit1]=2 'adps0[bit0]=1 ADCSRB=0 '*** VR analog voltage input port 'PC5[ADC5]=28pin 0-5V EXT Vol 'PC4[ADC4]=27pin 0-5V diag osc volume 'Config PINC.4 = Input 'Config PINC.5 = Input '***** LED output ***** 'PC0(23p) active high 'DDRC.0=1 '**** LCD ***** '***for LCD pin set output port *** ' 2 = 0V / 1 = 5V ' 4 = RS PD0 / 3 = Vo[0-5V_VR] ' 6 = E PD1 / 5 = R/W [to GND] ' 8 = D1 nc / 7 =D0 nc '10 = D3 nc / 9 = D2 nc '12 = D5 PC1 / 11 = D4 PC0 '14 = D7 PD3 / 13 = D6 PC2 'Config Lcd = 16 * 2 'Config Lcdmode = Port 'Config Lcdpin = Pin , Db4 = PORTC.0 , Db5 = PORTC.1 , Db6 = PORTC.2 , Db7 = PORTC.3 , E = PORTD.1 , RS = PORTD.0 InitLcd() Cursor Off DefLcdChar 1 , 32 , 32 , 32 , 32 , 32 , 32 , 31 , 31 DefLcdChar 2 , 32 , 32 , 32 , 31 , 31 , 32 , 32 , 32 DefLcdChar 3 , 32 , 32 , 32 , 31 , 31 , 32 , 31 , 31 '**** debug(Diag) flag set *** Diag = 0 '0=normal[pwm0b nothing] '1=pwm0b output If PINB.3 = 1 Then Diag = 1 '*** PWM [timer2] setting PD3(5pin)*** TCCR2B = 10 TCCR2A = 161 PORTD.3 = 0 DDRD.3 = 1 '*** PWM [timer0] diag osc setting *** '** normal=off ** TCCR0A = 0 TCCR0B = 0 PORTD.5 = 0 DDRD.5 = 0 If Diag = 1 Then 'when diag is pwmb0=on TCCR0B = 10 TCCR0A = 161 PORTD.5 = 0 DDRD.5 = 1 End If '*** capture1 [timer1 TQ input width count] setting *** TCCR1A = 0 'wgm11=1(bit1)=0 wgm10=0 com1a1=1(bit7)=0 com1a0=0(bit6) 'com1b1=1(bit5)=0 com1b0=0(bit4) 'Tccr1b .1 = 1 'Tccr1b .0 = 0 TCCR1B = 0 'Fall = 2 '130 'Rise = 66 '194 'wgm13=0(bit4=16) wgm12=0 cs11(bit1=2) 'ices1(bit6=64)(input Capture1 Edge Select) rise=1 fall=0 'icnc1(bit7=128)(Input Capture1 Noise Canceler) on=1 'Timsk1 = 'icie1(bit5=32)(Timer/Counter1 Input Capture Interrupt Enable '********* Pwm output width set ********** If Diag=1 Then '128+32 OCR0A = 64 '=64[RC 8MHz] OCR0B = 7 '=4[RC 8MHz] 'for diag input PWM OSC freq width=125us End If OCR2A=64 '=64[RC 8MHz] 'for output PWM OSC freq width=125us OCR2B=4 '=4[RC 8MHz] 'output PWM Duty width If Diag=1 Then Cls Lcd "D" ' Else ' Lcd "Normal" End If Wait 1 '***** warikomi set ***** Enable Interrupts '*** TQ init value setup *** Tqmax = 126 '126[8M] 'TQ input Cap2() 'TQ=8-126[8M:TCCR1B=66] 'TQ=67-1010[8M:TCCR1B=65] 'Tq00 is offset about 7(pwm=12us) Tq00 = Tq 'Tq00=13 Q=Tq Mqmax = Tqmax - Tq00 '6000=(64-4)*100 T2=6000/Mqmax T = Tqmax - Tq00 T=T*10 T = T / 16 Locate 2,1 Lcd "TQ=" ; Tq00 Wait 1 Cls '** mode init value** ' c1=40 ' C2=40 ' C3=100 ' C4=10 Tqtlim=120 Tqont=0 Tqon=0 L=0 Q=0 Tc=0 Tqx=0 Tc1=0 Tc2=0 ' V2=4 Avf=0 Kdc=0 Tqofft=180 Tqoff=0 Sc=0 ' kpt=0 ' kpt2=0 ' kpt3=0 '***** main loop start ***** Main: If L mod 5=0 Then '*** TQ sensor input *** Cap2() 'TQ=8-126[8M:TCCR1B=66] If Tq > Tqmax Then Tq = Tqmax '*** Tq cal setting *** If Tq > Tq00 Then Tq = Tq - Tq00 Else Tq = 0 End If 'If Tq2+1063 Then V=63 OCR0B = V 'V= 4-64[8M] ' Diag Duty size 10-100% End If '*** EXT VOLUME control *** '[Real amp gain cont] ADMUX=5 ADCSRA=195 '&B11000011 V = Adc(5) 'ch5=PC5[28p] ADCSRA=0 V = V / 16 '**** PB1(normal mode x1.0 ) SW input set LED on/off**** ' If PINB.1=0 Then ' V=20 ' PORTC.0=0 'LED off ' Else ' If c6>3 Then ' PORTC.0=0 'LED on ' Else ' PORTC.0=1 ' End If ' End If '********0=normal 1=strong ****************** If V>30 Then v=31 '*** auto mode set and reset*** Auto=0 ' auto=1 If V<30 Then Auto=1 If V=0 Then Auto=2 If Auto<>1 Then Avf=0 ' V=15 'V=0 turbo 'V=1-29 auto 'V=30 normal 'V=31 off C1=20+v 'auto asist change start level C2=10+v 'C2=asist cal start offset level C4 = V 'asist level '*** KPT counter *** ' If kpt<50 Then kpt=kpt+1 End If '*** If L mod 10 =0 start *** If L mod 10 = 0 Then Tq=Tqx/N Tqx = Tq*10 / T 'Tqx=0-16 '**tq start up timing load** If Tqont>Tqtlim Then Tqont=0 Tqon=0 Kdc=0 Sc=0 End If If Tq>5 Then If Tqoff=0 Then Tqon=1 Tqont=0 End If Tqoff=Tqofft End If '**tq Peak to peak sample hold ** '**rising edge** If Tqeg=0 Then If Tqon=1 Then If Kdc>0 Or Sc=1 Then Tqont=Tqont+1 End If If Tq>Tc Then Tc=Tq If Tq+210 Then ' kpt2=kpt2+kpt ' kpt2=kpt2/2 ' kpt=0 ' End If If Av>30 Then Sh=40 Else Sh=Av*2 Sh=100-Sh End If If Tqon=1 Then Sh=20 Tt2=Tq*Sh/100 ' If L=0 Then kpt=0 Tudisp=1 Tc2=Tc Tqeg=1 Avf2=0 ' tud() '*** High kpt asist up *** ' If kpt2<30 Then ' If kpt2<10 Then kpt3=10 ' kpt3=30-kpt2 ' kpt3=kpt3/2 ' Else ' kpt3=0 ' End If If Tqon=1 Then Kdc=Kdc+1 End If If Tq < Tc Then Tq=Tc End If '**Falling edge** If Tqeg=1 Then If TqTc+2 Then Tqeg=0 ' If kpt4=1 And L=0 Then kpt4=0 Tc1=Tc End If End If If Tqeg=1 Then If Tq > Tt2 And Auto=1 Then Tq=Tc2 If Avf>0 And Avf2=0 Then avf=avf-1 If Avf>0 Then Avf=Avf-1 Avf2=1 Avf3=0 End If End If '*** Mq cal setting *** '**** auto mode cal **** If Auto=1 And Tqeg=0 Then If Tq>C1 And Avf3=0 Then 'c1=25+V(1-19) normal=44-26 'Av=40-C4+Avf avfcal() If Av<43 Then Avf=5+Avf Avf3=1 End If End If End If ' Av=30-C4+Avf+kpt3 ' Av=40-C4+Avf avfcal() ' If av>50 Then AV=50 '**** auto timer ************* If Tqoff>0 Then Tqoff=Tqoff-1 If Tqoff=0 Then ' kpt2=50 ' kpt=50 Avf=0 Avf2=0 Avf3=0 Tc2=0 Tud() Tqon=0 End If Mqcal=0 Mq = Tq A=V/10+1 '** tq start power save ** If Auto=1 Then If Tq >= A Or Tqon=1 Then '** tq start power save ** If Tqon=1 Then AV=AV*Tqont/Tqtlim If Av<10 Then AV=10 A=0 If Sc=0 And Tq>C2 Then Sc=1 'avf=C4+10 If tq>c1 Then avf=c4+15 'avfcal() 'If av>35 Then av=35 Tqont=120 End If End If Mqcal=1 End If End If If Mqcal=1 Then Mq = Tq - A Mq = Mq * Av Mq = Mq / 10 Mq = Mq + A End If 'max limit set If Mq > Mqmax Then Mq = Mqmax '** Mq disp cal *** If C4=31 Then q=0 Mqx = Q*10/ T If Auto=2 Then Mqx=16 '*** LCD Display *** Bardisp() '** If L mod 10 =0 End** End If '*** auto decay *** ' If auto=2 Or Mq>c3 Then ' uplim=1 ' Else ' uplim=0 ' End If If Auto=2 Or Mq>100 Then Mq=Mqmax-L+15 If C6=9 Then Mq=Tq00 Tq=Tq00 Mqx=0 End If If C6=5 Then Locate 1,1 Lcd " " End If Else C6=0 End If If Mq<100 Then If Q>Mq+1 Then Q=Q-1 If Q63 Then A=63 If C4=31 Then A=4 OCR2B=A '*** L= sw scan interrupt timer *** L = L + 1 If L = 31 Then L = 0 If Auto=2 Or Mq>100 Then C6=C6+1 If C6=10 Then C6=0 End If End If GoTo Main '***** main end ***** '*** av keisan *** Sub avfcal() Av=40-C4+Avf End Sub '*** [warikomi sub] enable cap1-sub control *** Sub Cap2() Cap3: ICR1H=0 ICR1L=0 TIMSK1 = 32 WaitUs 2 TCCR1B = 66 If ICR1L=0 Then Tq=0 WaitUs 64 TIMSK1 = 0 WaitUs 2 If Tq<>Tcnt1l Then Tq=Tcnt1l GoTo Cap3 End If End Sub '*** [warikomi sub] capture1 *** Interrupt Icp1(),Save 3 If TCCR1B = 66 Then TCCR1B = 2 TCNT1H=0 TCNT1L=0 Else TCCR1B=0 End If End Interrupt '*** sub bar disp *** Sub Bardisp() '**** multi LCD disp **** For N = 1 To 16 Locate 2 , N I = 0 If Tqx >= N Then I = I + 2 If Mqx >= N Then I = I + 1 If I > 0 Then Lcd Chr(I) Else Lcd " " End If Next N N=0 Tqx=0 Format(2,0) ' Locate 1,6 ' Lcd tqoff/15 ' Locate 1 , 9 ' Lcd q ' Locate 2 , 15 ' Lcd V2 If Tudisp=1 Then Tud() Locate 1 , 1 Format(1,1) If Auto=2 Then Lcd "-MAX-" Else If C4<31 Then If Avf>0 Then Lcd "A";Av;" " Else If Tqon=0 Then Lcd "x";Av;" " Else Lcd "S";Av;" " End If End If Else Lcd "=OFF=" End If End If End Sub Sub Tud() Format(2,0) ' Locate 1,9 ' Lcd kpt2 Tudisp=0 ' Format(2,0) Locate 1,15 Lcd Tc2 ' Locate 2 , 15 ' Lcd V2 End Sub End