28x2 Pwm

ccdubs

Member
I need to generate 3 seperate frequencies all with the same duty cycle (50%), I have been playing with a 28X2 but haven't been able to acheive this (it didn't help that I was sent an old 28X2 5V version), so i have been reading the forums and manuals and was hoping someone could clarify my understanding while I wait for the latest 28X2 to arrive.

The 28X2 has 4 PWM pins. C1 and C2 share the same frequency but can have different duty cycles. The same is true for B0 and B5. So this is the solution to getting two of my three frequencies.

Can hpwm then be used to generate the third frequency which will have the same duty as the first two but completely different frequency? I understand that C2 then becomes unavailable when using hpwm but hopefully C1 can have a unique frequency.

Also, if I use hpwm in single mode using only 1 pin, are the other 3 pins available for IN/OUT/ADC, etc?

If not, is there another way to generate the third tone (say 1kHz).

The plan is to combine these 3 frequencies into a speaker to generate a tone.

Many thanks.
 

ccdubs

Member
I finally got a chip to test this on and I have hit an interesting issue.

It appears from the tests I have been doing that the pwmdiv4 and pwmdiv16 don't work when using all PWMs and it depends which pins they are assigned to. Ultimately, what I want is to generate a 830Hz, 1000Hz and 1200Hz tone, my first program was simple:

pwmout pwmdiv16, C.1, 74, 151 '830 Hz
pwmout pwmdiv4, B.0, 249, 500 '1000 Hz
pwmout pwmdiv4, B.5, 207, 417 '1200 Hz

This resulted in C.1 = 0 Hz, B.0 = 4000 Hz and B.5 = 1200 Hz. Next I tried

pwmout pwmdiv4, C.1, 249, 500 '1000 Hz
pwmout pwmdiv16, B.0, 74, 151 '830 Hz
pwmout pwmdiv4, B.5, 207, 417 '1200 Hz

This resulted in C.1 = 0 Hz, B.0 = 13300 Hz and B.5 = 1200 Hz. Next I tried

pwmout pwmdiv4, C.1, 249, 500 '1000 Hz
pwmout pwmdiv4, B.0, 207, 417 '1200 Hz
pwmout pwmdiv16, B.5, 74, 151 '830 Hz

This resulted in C.1 = 0 Hz, B.0 = 4810 Hz and B.5 = 830 Hz.

The trend seems to be that B.5 works while the pwmdiv for the other pins doesn't. If I use C.2 instead of C.1, the result for C.2 is not 0 Hz but rather the pwm value as if I hadn't entered the pwmdiv command.

Any ideas?
 

ccdubs

Member
I sorted this out in the end by doing the below. It appears that the compiler issue mentioned in the following thread hasn't been resolved:

http://www.picaxeforum.co.uk/showthread.php?t=18070&highlight=pokesfr
Code:
	pwmout B.0, 249, 500 '1000 Hz
	pwmout B.5, 207, 417 '1200 Hz
	pwmout pwmdiv16, C.2, 74, 151	'830 Hz
		
	'bit0=0 => 1/1, bit0=1 => 1/4, bit1=1 => 1/16 
	'Set divisor for B.0
	peeksfr $4A,b0
	bit0=1
	bit1=0
	pokesfr $4A,b0
	
	'Set divisor for B.5
	peeksfr $51,b0
	bit0=1	
	bit1=0
	pokesfr $51,b0
 
Last edited:
Top