Sure, here is an example for 20kHz PWM on an ATtiny25. Double check data sheet for your particular ATmega chip, change the define to your liking, or just plug in the value for PWM_LED for your duty cycle value.
From a quick code review point of view.... there is only one set of code for PWM. If the AVR has the same config as a PC then it could work. Tyically, we have options in the code for PIC and AVR - I see the default only which means both chip types gets the same treatment.
I normally hand crank PWM but I have no experience with AVR.
Anboium
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2014-04-07
Thanks. I can see that GCB is generating a lot of PWM assembler code for the AVR, but I'm still missing something. Here's a sample which runs on the PIC16F88. But it also compiles for the AVR, just by changing the #CHIP type.
;A program to fade a single LED on and off using hardware PWM.
;----- Configuration
#chip mega328p, 16
#config ccp1=rb3 ;CCP module connected to B.3
;----- Constants
#define LED PortB.3
;----- Variables
dim width as byte ;current pulse width
;----- Program
dir LED out ;make this an output
do
for width = 1 to 255 ;this spans 0% to 100%
hpwm 1, 1, width ;CCP1, frequency (kHz), duty
wait 5 ms ;pause a bit
next
loop ;repeat in perpetuity
Now, I think where I'm getting hung up is the line:
#config ccp1=rb3 ;CCP module connected to B.3
On the Pic, this indicates which pin should connect to the PWM module. Does anyone know how this is accomplished with the AVR?
Anyway, it sure seems like the compiled code is almost there, but just needs a simple indication of how to assign pin. Or am I being stupid?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
@Thomas
Ditto what Anobium says, manual hardware PWM setup for the AVR.
The frequency is set with the clock fuse selected by the programmer, and the prescale value established in the TCCR1 register of the ATtiny 25. In the ATtiny13A it is TCCR0B. In any case it should be in the Timer Counter Control register.
Without further adjustments you will get PWM frequencies of 33Khz (ATtiny25), or 9.6Khz (ATtiny13A and 9.6Mhz clk?). Frequency can be further adjusted with prescale value in the TCCR, so 1/2 gets you 16.5Khz and 4.8Khz, and so on.
To get custom frequency, then preloading a Timer Counter Register (OCR1C for Attiny25) or (TCNT0 for ATtiny13A) will increase the frequency. In the above example; 20Khz was obtained by setting the TCCR1, along with a base 16.5Khz frequency.
The duty cycle is established by setting the Output Compare Register, say OCR1B for Attiny25, or OCR0B for the ATtiny13A. In the above example; a #define PWM_LED OCR1C is used, so that PWM_LED can adjust the duty cycle.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2014-04-07
Hey, this is great information. Many thanks. One final question. I see on the chip I'm using that there are six pins labeled as being PWM capable. What's the secret to assigning the module to a particular pin?
Given that the manual approach is straightforward as you describe, I wondering what all this code is that GC Basic is generating for the AVR.
Thanks again for your clear explanation of the manual approach.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The PWM Module being used is defined by which Timer Counter Control Register is enabled. For the ATtiny25 OCR1B probably corresponds to the OC1B pin, don't have that data sheet in front of me. The ATtiny13, OCR0B equates to the OC0B pin.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2014-04-07
Again, many thanks. I don't have it all worked out, but do have some pulses on the scope now. I can take it away from here, thanks to your help Kent.
I spent some time sorting through the assembler code generated by the HPWM command for AVR, and it's clear that it's trying to do something, but not setting the registers appropriately.
For this reason, I believe the Help file should state that the HPWM command is not supported on the AVR series. The alternative, at this time, is to use the soft PWM.
Either that, or maybe we should try to make the code work.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
How could you note the support of the Atmel chip is still quite "immature," but I think he's improving a lot :-).
The method that I use to set the records, and to make sure everything is correct, is to use a very effective tool to initialize the registers, it is called "Algorithm builder" and it is free. was born to program in assembler, but I find it very effective, simple and easy to understand and use. ;-)
Good work, Marco
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
It seems PWM doesn't work with AVR . Is't possible to complete PWM.H for AVR's ?
Is there somebody who knows how to implement PWM with Atmega ?
Sure, here is an example for 20kHz PWM on an ATtiny25. Double check data sheet for your particular ATmega chip, change the define to your liking, or just plug in the value for PWM_LED for your duty cycle value.
Thanks Kent . I'll try this with an atmega8 . Regards
Can one of you experts offer some comments on what's going on here? I'm brand new to the AVR's. Which of these would be the case?
In any event, from the example above, I'm not clear on what is setting the frequency and what is setting the duty cycle.
Thanks for any advice!
From a quick code review point of view.... there is only one set of code for PWM. If the AVR has the same config as a PC then it could work. Tyically, we have options in the code for PIC and AVR - I see the default only which means both chip types gets the same treatment.
I normally hand crank PWM but I have no experience with AVR.
Anboium
Thanks. I can see that GCB is generating a lot of PWM assembler code for the AVR, but I'm still missing something. Here's a sample which runs on the PIC16F88. But it also compiles for the AVR, just by changing the #CHIP type.
Now, I think where I'm getting hung up is the line:
On the Pic, this indicates which pin should connect to the PWM module. Does anyone know how this is accomplished with the AVR?
Anyway, it sure seems like the compiled code is almost there, but just needs a simple indication of how to assign pin. Or am I being stupid?
@Thomas
Ditto what Anobium says, manual hardware PWM setup for the AVR.
The frequency is set with the clock fuse selected by the programmer, and the prescale value established in the TCCR1 register of the ATtiny 25. In the ATtiny13A it is TCCR0B. In any case it should be in the Timer Counter Control register.
Without further adjustments you will get PWM frequencies of 33Khz (ATtiny25), or 9.6Khz (ATtiny13A and 9.6Mhz clk?). Frequency can be further adjusted with prescale value in the TCCR, so 1/2 gets you 16.5Khz and 4.8Khz, and so on.
To get custom frequency, then preloading a Timer Counter Register (OCR1C for Attiny25) or (TCNT0 for ATtiny13A) will increase the frequency. In the above example; 20Khz was obtained by setting the TCCR1, along with a base 16.5Khz frequency.
The duty cycle is established by setting the Output Compare Register, say OCR1B for Attiny25, or OCR0B for the ATtiny13A. In the above example; a #define PWM_LED OCR1C is used, so that PWM_LED can adjust the duty cycle.
Hey, this is great information. Many thanks. One final question. I see on the chip I'm using that there are six pins labeled as being PWM capable. What's the secret to assigning the module to a particular pin?
Given that the manual approach is straightforward as you describe, I wondering what all this code is that GC Basic is generating for the AVR.
Thanks again for your clear explanation of the manual approach.
The PWM Module being used is defined by which Timer Counter Control Register is enabled. For the ATtiny25 OCR1B probably corresponds to the OC1B pin, don't have that data sheet in front of me. The ATtiny13, OCR0B equates to the OC0B pin.
Again, many thanks. I don't have it all worked out, but do have some pulses on the scope now. I can take it away from here, thanks to your help Kent.
I spent some time sorting through the assembler code generated by the HPWM command for AVR, and it's clear that it's trying to do something, but not setting the registers appropriately.
For this reason, I believe the Help file should state that the HPWM command is not supported on the AVR series. The alternative, at this time, is to use the soft PWM.
Either that, or maybe we should try to make the code work.
How could you note the support of the Atmel chip is still quite "immature," but I think he's improving a lot :-).
The method that I use to set the records, and to make sure everything is correct, is to use a very effective tool to initialize the registers, it is called "Algorithm builder" and it is free. was born to program in assembler, but I find it very effective, simple and easy to understand and use. ;-)
Good work, Marco