Pwm and sound

Go To Last Post
22 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have a problem :D!

I made a metronome. It should emit a beep but it doesn't. I used pwm in ctc mode. The frequency of the signal is about 1KHz. This is the code:

pwm_2:

clr temp
out tcnt2, temp
ldi temp, 0b00001101	;PWM in ctc mode & FCLK/1024
out tccr2, temp

ldi temp,1
out ocr2, temp

pwm_3:


clr temp2
clr temp
out tcnt0, temp
ldi temp, 0b00000101	;prescaler FCLK/1024
out TCCR0, temp

aaz:
in temp, tcnt0		;wait 0,005s
cpi temp, 20
brne stepkz	


clr temp			;stop timer counter
out tcnt2, temp
out tccr2, temp
out tcnt0, temp

rjmp contatore

And then it start count 1sec, 1/2 sec etc...

The output circuit is just a rc filter with r=4.7k and C=0.33 uf and then a jack

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It shall be OK using "raw" output signal with some generic transducer - piezo buzzer or dynamic speaker.
RC constant for a filter (LP I would guess) is a way big for the chosen output frequency methinks.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So, should i use a buzzer?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

A piezo without any filter will prove You have an audible output.
Edit: naturally bare piezo speaker, without any circuitry.

Last Edited: Sat. Jul 9, 2011 - 10:44 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The RC filter is used when you are trying to make audio from many samples that come from a PWM. You can also make a tone by just toggling a pin and feed that directly to a sound maker (speaker, for example).

A third way is to use a "buzzer" that makes a tone as long as power is applied. With this, you have no control over the pitch, only how long on.

A narrow pulse will make a "tic" sound in the speaker. Several pulses, equal on and off time, will make a rough "beep". Beep frequency is set by the total time of one on plus one off time.

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

@Jim: I read your paper on PWM and Filters. Very good. Thanks.

--greg
Still learning, don't shout at me, educate me.
Starting the fire is easy; the hardest part is learning how to keep the flame!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ka7ehk wrote:
The RC filter is used when you are trying to make audio from many samples that come from a PWM. You can also make a tone by just toggling a pin and feed that directly to a sound maker (speaker, for example).

A third way is to use a "buzzer" that makes a tone as long as power is applied. With this, you have no control over the pitch, only how long on.

A narrow pulse will make a "tic" sound in the speaker. Several pulses, equal on and off time, will make a rough "beep". Beep frequency is set by the total time of one on plus one off time.

Jim

I used the same filter with a sampled beep stored in eeprom but I had no sound too. So I decided to use something easier. If i would make sound by toggling a pin, how the frequency should be? Can I use the pwm to give power to the buzzer?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

PWM does not make much sense with a buzzer. A buzzer makes sound when it has power. If you made the PWM slow enough, you would get buz-pause-buz-pause-...

If you want a 1KHz tone from a PWM, then your period must be 1/1000Hz = 1ms. Since it must be high about half the time and low the remainder, I would make the PWM pereiod = 1ms and the on-time about 500us.

When you say that you tried to make audio from a sampled beep in EEPROM, what did you do after the filter? You cannot just hook a loud speaker there. You need some kind of audio (power) amplifier.

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

With just 5v it's hard to find something to amplify the sound. I didn't do anything efter the filter. I'm not sure the frequency was right anyway. I have those value

    136 143
    129
    112
    97
    71
    189
    255
    196
    72
    75
    109
    184
    223
    167
    0
    105
    224
    223
    122
    68
    130
    166
    169
    129
    56
    89
    156
    163
    103
    70
    85
    117
    153
    162
    75
    90
    139
    177
    180
    169
    141
    142
    156
    165
    166
    144
    132

So what is the total frequency with the 1024 prescaler? I'm really in trouble with this project. What do you suggest to do?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have a question. The speaker must be connected to OCRx and GND or OCRx and VCC ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The port pin will NOT drive a speaker, unless it is a very high impedance one (a lot more than 32 ohms). That said, if you connect it OCRx to ground, you will drive the signal with the PWM, if you connect it Vcc or OCRx, you just drive it with the complement which effectively inverts the waveform.

In your list of values, it appears that one tone period is about 6 PWM cycles. We assume 8 bit timer (since all of your values are limited to 255 or less) . But, we cannot tell your tone frequency because you do not tell us the CPU clock frequency. That is the source of the signal the prescaler divides from, so help stops here. The crystal ball has suddenly gone blank.

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

The port pin will NOT drive a speaker, unless it is a very high impedance one (a lot more than 32 ohms).

You'd be surprised - I've direct driven a 2" 8ohm from a mega16 - probably best not to think about he current involved in that though! ;-) Put an empty plastic cup over the speaker and it makes a pretty good resonant chamber.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I basically agree. My concern is that it will exceed the 20ma (or so) of pin current. Not very different from a short circuit "load".

But, it will absolutely not work with a speaker as "load" to an RC smoothing filter. With a series R of 4.7K, you will see about 32/(4700 + 32) of the port pin amplitude, or less than 5mv, peak-peak, if lucky. You MIGHT hear something from that, but not much.

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ka7ehk wrote:
The port pin will NOT drive a speaker, unless it is a very high impedance one (a lot more than 32 ohms). That said, if you connect it OCRx to ground, you will drive the signal with the PWM, if you connect it Vcc or OCRx, you just drive it with the complement which effectively inverts the waveform.

In your list of values, it appears that one tone period is about 6 PWM cycles. We assume 8 bit timer (since all of your values are limited to 255 or less) . But, we cannot tell your tone frequency because you do not tell us the CPU clock frequency. That is the source of the signal the prescaler divides from, so help stops here. The crystal ball has suddenly gone blank.

Jim

Frequency is 4MHz. I make a little amplifier with 2bjt after the filter but no signal arrives there. What do you suggest to do in order to make a little speaker execute the sampled tone? Should I post the code?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Probably one npn bjt shall do in emitter follower configuration. For an experiment this shall provide high input impedance and sufficient current for a speaker. Maybe Darlington for bigger speaker, triple Darlington for macho speaker.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OK, lets run the numbers:

4MHz CPU clock
1024 prescale value
Thus, clock count rate is (approximately) 4KHz.
With an 8 bit counter, you get an PWM period of 4KHz/256 = 16Hz (approx)

This is WAAAAAAY too slow! You will get a noisy "buzz" at best. I suggest that you run the prescale at 1X. Now, the counter will count at 4MHz. It will have a period of 4MHz/256 = 15.625KHz. This is the rate at which new samples will be generated. At (about) 6 samples per cycle, your "tone" will be 15.625KHz/6 = 2.6KHz. That is much closer to your target.

Now. hook your speaker directly to the port pin, as Cliff suggested. It needs to be a small speaker (about 5cm diameter, or so. Put a plastic cup over the speaker. You should hear your tone.

It may sound pretty "ratty" because the sample rate is so low and you can probably hear the sample rate, itself, unless you are a moldy-oldie or a "young-un" whose hearing is shot due to playing stuff too loud on your iPod.

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No sound. There is no signal from the OCR2 pin. I think the problem is the code.

;USCITA SUONO

uscita_pwm:


ldi romreg2, 0x00				

ldi temp, 0xff	

out ocr2, temp


ldi temp, 0x80					

out timsk, temp

pwm_2:

;LETTURA EEPROM

clr temp
out EEARL, romreg2
sbi EECR,EERE

in cn,EEDR

inc romreg2

clr temp
out tcnt2, temp
ldi temp, 0b0111010

;fastpwm mode & timer counter 2 4mhz/8 clear ocr2 on ;compare

out tccr2, temp

out ocr2, cn

pwm_3:

sbis PIND, 3 ;switch control
rjmp tasto


sbis pind,0	;switch control
rjmp up

sbis pind,1	;switch control
rjmp down


cpi cn, 132
brne pwm_3

clr romreg2

;reset eeprom position

clr temp
out tccr2, temp					;stop timer counter
out tcnt2, temp					;reset timer counter




rjmp contatore


skip_pwm_3:
rjmp pre_main


;*************CONTATORE


contatore:

clr temp2          ;it counts to  1/target sec
clr temp
out tcnt0, temp
ldi temp, 0b00000101
out TCCR0, temp

step1:
in temp, tcnt0
cpi temp, k
brne stepk					;controlla se temp =k

cp temp2, target

breq uscita_pwm
					
inc temp2

ldi temp, 0
out tcnt0, temp
rjmp step1

stepk:
sbis PIND, 3   	;switch control
rjmp tasto
sbis pind,0	;switch control
rjmp up
sbis pind,1     ;switch control
rjmp down



rjmp step1


jump_pre_main:
rjmp pre_main

CP_MATCH:
clr temp
out EEARL, romreg2
sbi EECR,EERE

in cn,EEDR
inc romreg2
out ocr2, cn
reti
;***
;***


This is just the metronome, i think the guitar tuner is not important. It works and doesn't interfere with the metronome.

EDIT:

I verified with an oscilloscope. No signal from the pwm so the problem is the code

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Nobody knows what's going on?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Perhaps start by saying which model of AVR this is?

Also try to avoid using:

ldi temp, 0b0111010

without knowing which AVR and having a copy of the datasheet open it's impossible to know what that magic number is achieving.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sorry, you're right. It's an atmega8535. 4MHz clk frequency. I don't know other way to put a number in to a register. Anyway there's the comment below that explain what it means

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I tried building your code to try it in a simulator but there's tons of undefined names. How about posting the WHOLE program?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I've found the problem. The pwm keep running also when it sould be no sound. So, how do I stop it? From the speaker comes an horrible bzzzzzzzzz. Its frequency changes with the metronome speed. It seems that the timer counter it's too fast and there's no pause.

The whole code:

;********************************
; written by: Giovambattista Astorino
; date:			06/05/11
; version:		1.6
; file saved as:	guitar_tuner.asm
; for AVR:			AtMega8535
; Clock frequency	4MHz
;********************************

;program function: Guitar tuner
;_________________________________

.device atmega8535
.nolist
.include "m8535def.inc"
.list



;FUNZIONANTE QUASI DEL TUTTO


;============
;Declarations

;Numeri
.equ zero =0b10001000
.equ one = 0b10111110
.equ two = 0b11000100
.equ three = 0b10010100
.equ four = 0b10110010
.equ five = 0b10010001
.equ six = 0b10000001
.equ seven = 0b10111100
.equ eight = 0b10000000
.equ nine = 0b10010000
.equ zerod =0b00001000
.equ oned = 0b00111110
.equ twod = 0b01000100

;Lettere
.equ LA=  0b10100000
.equ LAx= 0b00100000
.equ SI=  0b10000011
.equ DO=  0b11001001
.equ DOx= 0b01001001
.equ RE=  0b10000110
.equ REx= 0b00000110
.equ MI= 0b11000001
.equ FA=  0b11100001
.equ FAx= 0b01100001
.equ SOL=  0b10001001
.equ SOLx= 0b00001001

;Altro

.equ nn= 0b11110111
.equ su= 0b11111101
.equ giu= 0b11011111

.equ k=65
 

;Interrupt vector table ---------------------------------------

.cseg
.org 0x00



rjmp INIT				;RESET
rjmp gt_in				;External Interrupt0 Vector Address
reti					;External Interrupt1 Vector Address
rjmp CP_MATCH				;Timer2 compare match Vector Address
reti					;Timer2 overflow Vector Address
reti					;Timer1 Input Capture Vector Address
reti					;Timer1 Output Compare A Interrupt Vector Address
reti					;Timer1 Output Compare B Interrupt Vector Address
reti					;Overflow1 Interrupt Vector Address
reti					;Overflow0 Interrupt Vector Address
reti					;SPI Interrupt Vector Address
reti					;UART Receive Complete Interrupt Vector Address
reti					;UART Data Register Empty Interrupt Vector Address
reti					;UART Transmit Complete Interrupt Vector Address
reti					;ADC Conversion Complete Interrupt Vector Address
reti					;EEPROM Write Complete Interrupt Vector Address
reti					;Analog Comparator Interrupt Vector Address

;Subrutines --------------------------------------------------------


;=============
;Start Program


INIT:


;SRAM va da $0060 a $025F

.def temp=r16
.def stato=r17		;PIN 0=main 1=metronomo 2=accordatore
.def nota=r18
.def periodo=r19
.def cn=r20
.def target=r21
.def temp2 = r22
.def romreg1=r23
.def romreg2=r24
.def controllo_pind=r25



ldi		temp,low(ramend)		;Inizializzazione stack pointer
out		SPL,temp
ldi		temp,high(ramend)
out		SPH,temp



clr controllo_pind
clr temp2
clr temp
clr stato
clr nota
clr periodo
clr cn
clr target
clr romreg1
clr romreg2
clr r26



		ldi temp,0b10000000
		out pind, temp
		out	DDRD, temp	;PortD is input
		out	PortD, temp	;pull-up ON PortD pins
		
		ser temp
		out	DDRA, temp	;PortA is output
		out	PortA, temp ;turn off all LEDs
		
		ser temp
		out	DDRC, temp	;PortC is output
		out	PortC, temp ;turn off all LEDs

;Alla porta D son connessi 3 switch: 
		;PD3 Metronomo
		;PD5 Tasto +
		;PD6 Tasto -
;gli altri pin non sono usati


ldi target, 40  ;carica in target il valore minimo



ldi temp, 0b00000011		;rising edge interrupt PD2 (guitar in)
out MCUCR, temp				;low level interrupt PD3 (lo switch)

ldi stato, 0b00000000		;modalità main


ldi temp, 0b01000000		;attiva l'interrupt request
out GICR, temp


sei	;Sets the Global Interrupt flag


pre_main:
ldi temp, nn				;********Forse va eliminato ******
out porta, temp				;********					******
out portc, temp				;********					******


;****MAIN*****

main:

sbis PIND, 3
rjmp tasto

sbrc stato, 1	;controlla se siamo in modalità metronomo
rjmp uscita_led

sbrc stato, 2	;controlla se siamo in modalità accordatore
rjmp Calcolo_nota


rjmp main




;****INTERRUPT
gt_in:
ldi stato, 0b00000100
reti

CP_MATCH:
clr temp
out EEARL, romreg2
sbi EECR,EERE			;abilita la lettura dell'allocazione posta all'indirizzo specificato
in cn,EEDR			;informazione salvata in cn
inc romreg2				;leggiamo la eeprom
out ocr2, cn
reti
;***
;***










;****accordatore

Calcolo_nota:

sbrc nota, 0					;controlla se nota è uguale a 0b00000000
rjmp calcolo_periodo			;se son diversi vai a calcolo_periodo

ldi temp, 0
out tcnt0, temp
ldi temp, 0b00000100			;timer counter' attivo CLK/256
out TCCR0, temp
ldi nota,0b00000001
clr stato
rjmp pre_main


Calcolo_periodo:


ldi nota, 0
ldi temp, 0b00000000			;stop timer counter
out TCCR0, temp

in periodo, TCNT0
in temp, TCNT0


lsr temp
lsr temp
lsr temp
lsr temp			;dividiamo per 16

st X, temp
inc r26



;Abbiamo il periodo posto nel registro periodo

cpi r26, 16				;media di 16 valori
breq calcolo
clr stato
clr nota
out TCNT0, nota
rjmp pre_main



calcolo:
clr periodo
clr r26
clr temp

calcolo_pt2:
ld temp, X
add periodo, temp
inc r26

cpi r26, 16
brne calcolo_pt2
clr r26




ldi cn, 0b00000000
ldi romreg1, 0x3c		;impostiamo il puntatore della eeprom alla prima allocazione


nota2:

out eearl, romreg1
sbi EECR,EERE			;abilita la lettura
in temp, eedr			;carichiamo in temp i valori della eeprom
						;nell'allocazione indicata da romreg1
cp periodo, temp		;si comparano il registro temp e periodo
brsh pre_led				;se temp è minore di periodo rjmp led
inc cn					;se temp è maggiore si incrementano il puntatore e cn
inc romreg1
rjmp nota2

;in controllo_pind è salvato il valore teorico del periodo
;mentre nel registro periodo il periodo effettivo del nostro segnale


;*******
pre_led:
dec cn
dec romreg1
out eearl, romreg1
sbi EECR,EERE			;abilita la lettura
in temp2, eedr			;carichiamo in temp i valori della eeprom
sub temp2, periodo
sub periodo, temp


cp temp2, periodo
brlo led
inc cn

cp temp2, periodo
breq controllo_pt2
clr periodo
rjmp led_1
controllo_pt2:
ldi periodo, 0xaa
rjmp led_1

;********

led:
ser periodo
led_1:

in controllo_pind, eedr	;copiamo il valore
ldi romreg1, 0x78	;romreg1 punta all'allocazione 0x78 della eeprom

led_2:

cpi cn, 12			;compara cn con 12
brsh led_cycle		;se maggiore salta a led_cycle
add romreg1, cn		;se è minore o uguale di 12

out eearl, romreg1
sbi EECR,EERE			;abilita la lettura

in temp,eedr
out porta, temp



cpi periodo, 0
brne diversi
ldi temp, su			;se il valore misurato è minore di quello
out portc, temp			;teorico il led punta su
rjmp skip_nn


diversi:

cpi periodo, 0xff
brne label_nn

ldi temp, giu			;se il valore misurato è maggiore di quello
out portc, temp			;teorico il led punta giu
rjmp skip_nn


label_nn:
ldi temp, nn
out portc, temp			;non  mostra niente sul secondo led

skip_nn:

ldi temp,0b00000000
out TCNT0, temp						;altrimenti non punta a niente
rjmp contatore2


led_cycle:

subi cn, 12			;se è maggiore di 12
rjmp led_2


contatore2:

ldi target, 15					;aspettiamo 0,25 sec
clr temp2
clr temp
out tcnt0, temp
ldi temp, 0b00000101		;timer counter' attivo CLK/1024
out TCCR0, temp

step1_1:
sbis PIND, 3

clr temp2
clr temp
out TCCR0, temp
out tcnt0, temp



rjmp tasto


in temp, tcnt0
cpi temp, k
brne step1_1				;controlla se temp =k

cp temp2, target			;se sono uguali controlla se temp2=target
breq return_c2
inc temp2					;**se non lo sono incrementa temp 2 e resetta tctn0
ldi temp, 0
out tcnt0, temp
rjmp step1_1

return_c2:
ldi temp, 0					;disattiva timer counter
out TCCR0, temp
out tcnt0, temp				;reset timer counter
ldi stato, 0

rjmp main
;***
;***



;****TASTO
tasto:
ldi temp, 0xff
out porta, temp
out portc, temp		;spegne i led

clr temp			;stoppa il timer counter
out tccr0, temp
out tcnt0, temp




tasto2:
sbis PIND, 3
rjmp tasto2



sbrc nota,0
rjmp tasto3

ldi temp, 0b00000000		;disattiva l'interrupt di pd2
out GICR, temp
ldi target, 40
ldi stato, 0b00000010
ldi nota,0b00000001
rjmp pre_main


tasto3:
ldi stato, 0				;se stato è 1 lo porta a 0 (accordatore)

ldi temp, 0b01000000		;attiva l'interrupt di pd2
out GICR, temp


ldi temp, 0					;stoppa il timer counter2
out tccr2, temp

ldi nota,0
rjmp pre_main
;***
;***



;****METRONOMO


uscita_led:				;controlla terget e manda in uscita i led

cpi target, 40
brne skip40
	ldi temp, four
	out porta, temp
	ldi temp, zero
	out portc, temp
	rjmp uscita_pwm

skip40:


cpi target, 50
brne skip50
	ldi temp, five
	out porta, temp
	ldi temp, zero
	out portc, temp
	rjmp uscita_pwm

skip50:


cpi target, 60
brne skip60
	ldi temp, six
	out porta, temp
	ldi temp, zero
	out portc, temp
	rjmp uscita_pwm

skip60:


cpi target, 70
brne skip70
	ldi temp, seven
	out porta, temp
	ldi temp, zero
	out portc, temp
	rjmp uscita_pwm

skip70:


cpi target, 80
brne skip80
	ldi temp, eight
	out porta, temp
	ldi temp, zero
	out portc, temp
	rjmp uscita_pwm

skip80:


cpi target, 90
brne skip90
	ldi temp, nine
	out porta, temp
	ldi temp, zero
	out portc, temp
	rjmp uscita_pwm

skip90:


cpi target, 100
brne skip100
	ldi temp, oned
	out porta, temp
	ldi temp, zero
	out portc, temp
	rjmp uscita_pwm

skip100:


cpi target, 110
brne skip110
	ldi temp, oned
	out porta, temp
	ldi temp, one
	out portc, temp
	rjmp uscita_pwm

skip110:


cpi target, 120
brne skip120
	ldi temp, oned
	out porta, temp
	ldi temp, two
	out portc, temp
	rjmp uscita_pwm

skip120:


cpi target, 130
brne skip130
	ldi temp, oned
	out porta, temp
	ldi temp, three
	out portc, temp
	rjmp uscita_pwm

skip130:


cpi target, 140
brne skip140
	ldi temp, oned
	out porta, temp
	ldi temp, four
	out portc, temp
	rjmp uscita_pwm

skip140:


cpi target, 150
brne skip150
	ldi temp, oned
	out porta, temp
	ldi temp, five
	out portc, temp
	rjmp uscita_pwm

skip150:


cpi target, 160
brne skip160
	ldi temp, oned
	out porta, temp
	ldi temp, six
	out portc, temp
	rjmp uscita_pwm

skip160:


cpi target, 170
brne skip170
	ldi temp, oned
	out porta, temp
	ldi temp, seven
	out portc, temp
	rjmp uscita_pwm

skip170:


cpi target, 180
brne skip180
	ldi temp, oned
	out porta, temp
	ldi temp, eight
	out portc, temp
	rjmp uscita_pwm

skip180:


cpi target, 190
brne skip190
	ldi temp, oned
	out porta, temp
	ldi temp, nine
	out portc, temp
	rjmp uscita_pwm

skip190:


cpi target, 200
brne skip200
	ldi temp, twod
	out porta, temp
	ldi temp, zero
	out portc, temp
	rjmp uscita_pwm

skip200:


cpi target, 210
brne skip210
	ldi temp, twod
	out porta, temp
	ldi temp, one
	out portc, temp
	rjmp uscita_pwm

skip210:
rjmp uscita_led





;USCITA SUONO

uscita_pwm:





ldi romreg2, 0x00				;reset posizione eeprom

ldi temp, 0x80					;abilitiamo l'interupt per il compare match
out timsk, temp

pwm_2:

;LETTURA EEPROM

clr temp
out EEARL, romreg2
sbi EECR,EERE			;abilita la lettura dell'allocazione posta all'indirizzo specificato
in cn,EEDR			;informazione salvata in cn
inc romreg2				;leggiamo la eeprom

clr temp
out tcnt0, temp
ldi temp, 0b01101010			;PWM in fastpwm mode & timer counter 2 4mhz/8 cleart ocr2 on compare
out tccr0, temp

out ocr0, cn					;Il valore del duty cycle dipende dal valore di cn


pwm_3:

sbis PIND, 3
rjmp tasto


sbis pind,0	;controlla se è stato premuto il tasto +
rjmp up

sbis pind,1	;controlla se è stato premuto il tasto -
rjmp down


cpi cn, 132
brne pwm_3

clr romreg2						;reset posizione eeprom

clr temp
out tccr0, temp					;stop timer counter
out tcnt0, temp					;reset timer counter

rjmp contatore



;*************CONTATORE


contatore:

clr temp2
clr temp
out tcnt0, temp
ldi temp, 0b00000101			;timer counter' attivo CLK/1024
out TCCR0, temp

step1:
in temp, tcnt0
cpi temp, k
brne stepk					;controlla se temp =k

cp temp2, target			;*se sono uguali controlla se temp2=target
breq uscita_pwm_temp		;**se lo sono è passato il giusto temp e ritorna								
inc temp2					;**se non lo sono incrementa temp 2 e resetta tctn0
ldi temp, 0
out tcnt0, temp
rjmp step1

stepk:
sbis PIND, 3
rjmp tasto


sbis pind,0	;controlla se è stato premuto il tasto +
rjmp up

sbis pind,1	;controlla se è stato premuto il tasto -
rjmp down

rjmp step1

uscita_pwm_temp:
clr temp
out TCCR0, temp
out TCnt0, temp
rjmp uscita_pwm


jump_pre_main:
rjmp pre_main


;**************controllo_tasti

up:
clr temp			;stoppa il timer counter
out tccr0, temp

out tcnt0, temp

up2:
sbis pind,0	;controlla se è stato lasciato il tasto +
rjmp up2

cpi target, 210		;controlla se sono arrivato a max
breq jump_uscita_led			;se ci sono salta ad uscita
ldi temp, 0xa		; se non ci sono +10 e salta ad uscita
add target, temp
rjmp uscita_led


down:
clr temp			;stoppa il timer counter
out tccr0, temp
out tcnt0, temp

down2:
sbis pind,1	;controlla se è stato lasciato il tasto +
rjmp down2

cpi target, 40		;controlla se sono al minimo
breq jump_uscita_led	;se ci sono salta ad uscita
subi target, 0xa		; se non ci sono -10 e salta ad uscita
rjmp uscita_led



jump_uscita_led:
rjmp uscita_led


PortaA and PortC are connected to 2 7segments displays. The comments are in Italian. There's is also the guitar tuner part