PWM PD3 ATMEL START CONFIG

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

How can I config PWM on pin PD3 in ATMEGA328P using Atmel START?

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

What makes you want to do that?...setting up the PWM is easy in C or asm & rather straightforward. just set a few timer values and out comes the PWM.   Do you see any advantage of using Start to make your PWM?---it has already generated your first bit of confusion.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

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

 

You cant use PD3 for ADC, because its not available on that pin. the only possibility to use ADC are on PC0/PC1/PC2/PC3/PC4/PC5, Please check the datasheet page 3:

 

http://ww1.microchip.com/downloa...

 

now if you want to use that in atmel start (Although I dont suggest it):

1- add software component:

 

 

2- Choose the pin that you want for ADC function:

 

 

3- click on view file...

 

Atmel start in my opinion is good when you would like to take a look how things might be setup, although its full of bugs. but i dont recommend it for developing application.

 

Regards,

Moe

 

EDIT:

 

I just checked the atdf file just to make sure that there isnt ADC in PD3:

 

        <module name="ADC">
          <instance name="ADC" caption="Analog-to-Digital Converter">
            <register-group name="ADC" name-in-module="ADC" offset="0x00" address-space="data" caption="Analog-to-Digital Converter"/>
            <signals>
              <signal group="ADC" index="0" function="default" pad="PC0"/>
              <signal group="ADC" index="1" function="default" pad="PC1"/>
              <signal group="ADC" index="2" function="default" pad="PC2"/>
              <signal group="ADC" index="3" function="default" pad="PC3"/>
              <signal group="ADC" index="4" function="default" pad="PC4"/>
              <signal group="ADC" index="5" function="default" pad="PC5"/>
            </signals>
          </instance>
        </module>

 

and its in line with the datasheet as it suggest only PC0/1/2/3/4/5 have ADC.

 

 

Last Edited: Thu. Aug 1, 2019 - 06:12 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The OP wrote "PWM", not "ADC".

 

PWM is available on pin PD3 as OC2B output. Thus, it uses TIMER2 which is an 8-bit timer.

 

There should be  something in START for this but it is relatively simple to do it with basic programming. START is, I think, considered only barely useful for devices like Mega328P.

 

Jim

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

Last Edited: Thu. Aug 1, 2019 - 03:18 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

tiago.possato wrote:

How can I config PWM on pin PD3 in ATMEGA328P using Atmel START?

 

As others have said...WHY?  START is not really a very good interface for 8 bit AVR's as many others have already found out.

 

For me, I use Codevisions Code Wizard to set things up, then copy/paste the output to my Studio project and off I go!

 

 

East Coast Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

 

As others have said you are over-complicating this by trying to use "Start". It's for big/complex processors. For simple ones like a 328 it's much easier to read the datasheet and program the registers direct.

 

Now you haven't really given enough info in your specification like how fast the 328 will run or what frequency you want your PWM to operate at but to give you an idea...

 

So first, as others have said, the PWM signal on PD3 is called OC2B so that immediately tells you that you need to use channel B on timer 2.

 

Now one of the first decisions you need to make is exactly what kind of PWM you want. When you look up timer 2 for 328 then you find that timer 2 has Waveform Generation Mode (WGM) bits in registers TCCR2A and TCCR2B. The table that shows your options is within the description of TCCR2A:

 

 

That table has 4 possible modes of PWM. Two of the options are "fast" and two are "phase correct". In fast you can get twice the frequency (as the timer just keeps counting up over and over rather than alternating up and down) but when you change PWM duty from one value to another there can be a "jump". For some things (like controlling motors that are slow to react) this may not matter but in other uses (maybe playing audio?) it could be significant. On the whole you'd probably pick "Normal" unless you have a strong reason for phase correct. So now you have modes 3 and 7 to choose from. They differ in what "TOP" is. In mode 7 TOP (ie PWM frequency) can be varied by adjusting OCR(2)A) while in mode 3 TOP=0xFF which means the timer always cycles from 0 right up to 255 so the frequency is fixed.

 

As you have chosen to use B as the output you don't need A so the OCR2A register can be dedicated to frequency adjustment so I'd pick mode 7.

 

Finally you need to decide how you actually want the OC2B pin to behave. An often used choice is to have the pin go high when the timer resets (wraps to 0) then have the signal go low when it reaches the compare value in the OCR2B register. You control this pin behaviour using the  COM2B bits in the TCCR2A register:

 

 

So the mode where the output goes high (SET) at BOTTOM (0) and goes low (CLEAR) at compare is the 10 combination.

 

Finally you need to decide on a frequency for the timer and also to set an initial (and perhaps vary later?) the compare point. 

 

Putting that all together:

// just guessing the CPU speed
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>

void time_init(void) {
    // set at bottom, clear on match, Mode 7
    TCCR2A = (1 << COM2B1) | (1 << WGM21) | (1 << WGM20);
    // Mode7 (pt 2) and start timer at aaritrary /8
    TCCR2B = (1 << WGM22) | (1 << CS21);
    // some arbitrary frequency
    OCR2A = 185;
}

int main(void) {
    int8_t pwm_increment = 1;
    timer_init();
    whhile(1) {
        OCR2B += pwm_increment;
        if ((OCR2B == 184) || (OCR2B == 0)) {
            pwm_increment = -pwm_increment;
        }
        _delay_ms(100);
    }
}

The majority of that is simply about ramping the PWM duty up and down over time. Actually getting the timer started to do PWM is just the few lines in timer_init(). The timer actually starts when the CS bits are set to something other than 0.

 

So I guessed some numbers. I assume a 16MHz 328P (may on Arduinos are!). If I start the timer with CS bits set to /8 then the timer ticks at 2MHz. I have told it to count from 0 to 185 then reset to 0. That is 186 steps so it will have a PWM frequency of 2MHz/186 = 10.753kHz

 

It then just varies the duty up from 0 (OCR2B default) to 184/185 and then down to 0/185 and then back up and so on.

 

So an LED on the pin should gradually increase in brightness, then dim, then brighten, then dim and so on.

 

I threw in a _delay_ms(200) - which may be "too slow" - so the change could be seen over time - otherwise it may ramp up and down too quickly to really see the effect.

 

If nothing appears to happen for a while reduce the delay.

 

I just typed this without even compiling it, let alone running so caveat emptor !

 

Most of all forget "Start" for a 328 !

 

Last Edited: Thu. Aug 1, 2019 - 04:58 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
Most of all forget "Start" for a 328 !

 

In other words...STOP using START!

 

 

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

Does Atmel have trouble coming up with product names that aren't confusing or hard to do unique searches on?

 

Start   ???   

ICE     ???

Studio ??

A while back I was trying to find what the current ATMEL ICE product model name was ...finally saw that it was ICE

 

I'm surprised they didn't call the their IDE  IDE 

 

it can be aggravating...what brand of jeans do you wear?  Jeans

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

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

Thank you all for the information.
I am now leaving the Arduino IDE, I have many questions and never programmed directly with registers.
I will try to apply what you said.

 

Tiago

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

tiago.possato wrote:
I am now leaving the Arduino IDE,

 

HUH?

 

Your thread says Atmel START, now you say Arduino?  What is it you are using?

 

Start over and explain EVERYTHING you are trying to do.

 

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

Start over and explain EVERYTHING you are trying to do.

Some things are best left unsaid  surprise

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

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

I'm migrating my Arduino project to Atmel Studio.

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

tiago.possato wrote:

I'm migrating my Arduino project to Atmel Studio.

So where does START come into this then?

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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


jgmdesign wrote:
So where does START come into this then?
+1

 

Perhaps you have not spotted this but AS7 has a "create project from Arduino sketch" function.

 

 

It will turn any Arduino code you have into an AS7 project. (it's still exactly the same code - only now it will be built within AS7)