
	;Transposeur PWM 2KHz -> PWM 31.25KHz pour carte Sirocco
	;input PWM automate 512uS max (automate = 570uS soit 1.75kHz)
	;version avec Tiny45
	;version 1.0 du 25-07-2010 par H. Le Duigou 


;	constantes 

.include "..\tn45def.inc"

.def  Null		=R2
.def  Aux1		=R16
.Def  Data_PWM	=R17



;		Registres affectés en adressage Bit

	;Syntaxe pour gestion des bits en registre
;Exemple	sbr	r16,(1<<PB6)+(1<<PB5)	;set PB6 and PB5 (use masks, not bit#)


;	Attribution des ports

.Equ  OutPWM		=0	;PB.0 : Out PWM (+ input MOS Status)
.Equ  Chk_LED		=1	;PB.1 : Port libre -> Check Led
.Equ  InPWM		=2	;PB.2 : input PWM automate 512uS max (automate = 570uS)
.Equ  Status		=3	;PB.3 : sortie status pour info automate
;ADC2		=PB.4 : retour FCEM moteur



;***** Code

.CSEG
.ORG 0

RESET:
	RJmp	StartProg



StartProg:
	;init Stack Pointer = RamEnd par le processeur à l'init

	Clr	Null

	LdI	Aux1,0b10000000	;Set ClkPCE
	Out	ClkPR,Aux1
	LdI	Aux1,0b00000000	;Set Clk/1 -> 8MHz
	Out	ClkPR,Aux1

	SBi	DDRB,OutPWM	;Out : sortie du PWM
	CBi	PortB,OutPWM	;PWM off

	SBi	DDRB,Status	;Out : sortie Status pour automate
	CBI	PortB,Status	;Init Status Bad pour automate

	SBi	DDRB,Chk_LED	;PB.x = Out Chk LED
	CBi	PortB,Chk_LED	;Clear Chk LED : sera OFF

;******	Initialisation Timer 0 = Fast PWM 8 bits canal A

	LdI	Aux1,0b10000011	;com0A1 com0A0 com0B1 com0B0 - - wgm01 wgm00 
	out	tccr0A,Aux1	;out A : non invert fast PWM

	LdI	Aux1,0b00000001	;foc0A foc0B - - wgm02 cs02 cs01 cs00 
	out	tccr0B,Aux1	;Clock timer 0

	LdI	Aux1,0	;tcnt0[7:0] 
	out	tcnt0,Aux1



;******  Initialisation convertisseur Analog/Digital
	LdI	Aux1,0b10000100	;aden adsc adate adif adie adps2 adps1 adps0
	Out	adcsrA,Aux1	;config AD

;******	Initialisation Timer 1 = 


Loop_Main:

	CBi	PortB,Chk_LED	;Clear Chk LED : sera ON durant 1 cycle

	In	Aux1,tifr	;- ocf1A ocf1B ocf0A ocf0B tov1 tov0 -
	Out	tifr,Aux1	;clear timer over flow 1

	LdI	Aux1,0b00000110	;ctc1 pwm1a com1a1 com1a0 cs13 cs12 cs11 cs10 
	out	tccr1,Aux1	;Timer, Clk /32
	out	tcnt1,Null
LoopPWM_In_C:
	In	Aux1,tifr	;- ocf1A ocf1B ocf0A ocf0B tov1 tov0 -
	SBRC 	Aux1,tov1
	RJmp	ov_PWM_in_C	;overflow PWM input Clear après 1024 uS

	SBIS	PinB,InPWM
	RJmp	LoopPWM_In_C	;poursuite de la phase repos du PWM input

	SBi	PortB,Chk_LED	;Chk LED off

	In	Aux1,tifr	;- ocf1A ocf1B ocf0A ocf0B tov1 tov0 -
	Out	tifr,Aux1	;clear timer over flow 1

	LdI	Aux1,0b00000101	;ctc1 pwm1a com1a1 com1a0 cs13 cs12 cs11 cs10 
	out	tccr1,Aux1	;Timer, Clk /16
	out	tcnt1,Null
LoopPWM_In_S:
	In	Aux1,tifr	;- ocf1A ocf1B ocf0A ocf0B tov1 tov0 -
	SBRC 	Aux1,tov1
	RJmp	ov1_PWM_in_S	;overflow PWM input Set


	SBIC	PinB,InPWM
	RJmp	LoopPWM_In_S	;poursuite de la phase active du PWM input
	In	Data_PWM,tcnt1

SetMaxPWM:
	LdI	Aux1,0b10000011	;com0A1 com0A0 com0B1 com0B0 - - wgm01 wgm00 
	out	tccr0A,Aux1	;out A : non invert fast PWM : réactivation PWM
	out	ocr0A,Data_PWM
	SBI	PortB,Status	;Status OK pour automate

	RJmp	Loop_Main	;boucle de traitement normal

ov_PWM_in_C:
	out	tccr0A,Null	;Stop PWM
	CBI	PortB,Status	;Status Bad pour automate
Wait_End_PWM_In_C:
	SBIS	PinB,InPWM
	RJmp	Wait_End_PWM_In_C	;attente fin anormale phase repos du PWM input

	RJmp	Loop_Main	;reprise boucle de traitement normal


ov1_PWM_in_S:			;overflow toléré -> set Max PWM
	In	Aux1,tcnt1
	CPI	Aux1,64
	BrCC	ov2_PWM_in_S	;overflow PWM input Set

	SBIC	PinB,InPWM
	RJmp	ov1_PWM_in_S	;poursuite de la phase active du PWM input
	LdI	Data_PWM,255
	RJmp	SetMaxPWM



ov2_PWM_in_S:
	out	tccr0A,Null	;Stop PWM
	CBI	PortB,Status	;Status Bad pour automate
Wait_End_PWM_In_S:
	SBIC	PinB,InPWM
	RJmp	Wait_End_PWM_In_S	;attente fin anormale phase active du PWM input

	RJmp	Loop_Main	;reprise boucle de traitement normal








