I am writing my fiorst AVR program for the STK500. It kind of works. What I'm trying to do is generate two PWM outputs(along with the inverted outputs---that is, using all four PB0-PB3 outputs.)
I do not want to use any interrupts--are any needed?
I want to use the high freq PLL clock to generate a 20khz pwm with a 7 bit approx duty cycle adjustment resolution. This should give about 0.5% adjustment per step
The chip is programming properly (I added the two extra jumper wires for the tiny 26)!!! I have left all of the STK500 jumpers in place (default).
The program SEEMS to work, but not quite. It reads the switches & does adj the freq & duty cycle. BUT the duty cyle jumps in about 5% steps & is very course. this is strange. If I HARD code the inital duty cycle value as $69 I get 52.5% duty, $6A give 53%, $6B gives 53.5% etc, which is just fine (note the small duty % steps).
I'm assuming my switch reading is inc/dec the duty value by one count...but doing it this way is giving 5-10% duty steps. Is there a trick to updating the duty cycle (timer) registers? This code just dumps the lastest duty value into OCR1A & OCR1B. Does something intermediate, like shutting off timers, need done? Like polling or setting an interrupt? At this point I really don't want to use any interrrupts (unless I must).
I am almost positive the switches are reading properly, and this is not a switch bounce problem.
Also, my coding skill is rather new. Please look at where I am reading the PLL lock bit. Why can't I use an SBIS instruction here???!!. My code methods seems a bit kludgy. Can't the bit be read/tested in one instruction?
Also in setting all these register I load a value into temp, then do an "OUT Reg, temp"
Is there a more direct way to set these?
Is anything special needed to update the time PWM values? Also, I seem to get the same duty cycle jumping no matter what clock option I burn in on the STK500 programming menu (pll or internal RC).
;**** tiny 26 simple PWM test
.def temp=r16 ;set up some registers
out ddra, temp ;set port a to inputs
ser Temp ;set all to ones
out ddrb,temp ;set port b to outputs
ldi duty, $69 ; set intial duty
ldi freq, $c7 ;set initial freq
ldi temp, $53
out tccr1a, temp ;set tccr1a ---IS there a more direct way to set these?
ldi temp, $85 ; set ctc1 & set tp to pck/16
out tccr1b, temp
out pllcsr, temp ;enable pcke bit
waitpll: in temp, pllcsr ;this seems a kludgy way to check the bit---any ideas?
sbrs temp,$00 ;can the bit be checked with one instruction?
rjmp waitpll ;wait for plllock
out pllcsr, temp ; enable async mode
sbis pina,$07 ;change freq & duty based on switches
inc freq ;COUNT THEM UP/DOWN
sbis pina, $04
delay: dec delay ;PUT IN A LONG WAIT
;update PWM REGISTERS
out ocr1a, duty ;change the pwm timer settings
out ocr1b, duty
out ocr1c, freq
rjmp main ;GO BACK & REREAD SWITCHES