ATmega328p ADC to PWM

Go To Last Post
63 posts / 0 new

Pages

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

OK, the lower value for 0.1V is 20.48 and ofcource I understand that it can't be represented.

I have used the calculation in the source but does not provide any improvement for the maximum duty cycle.

 

 

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

Ok, the value for 0.1V is 20.48 and ofcourse I understand that the figures behind the dot can't be represented.

I used the calculations in the source but it didn't provide a larger dutycycle towards 99%

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

Cliff has explained it perfectly well.  And both Jim and I have even pointed out where you can find the source code which will completely solve your problem.  Jim even actually posted it here!  Why are you still working on this?

 

IF you expect the ADC to return a value no lower than 20, and

IF you expect the ADC to return a value no higher than 410, and

IF you want a minimum OCR1A value of 6 (2.73%), and

IF you want a maximum OCR1A value of 250 (98.05%), and

WERE you to use the Arduino map() function, then it would look like this:

OCR1A = map(ADC, 20, 410, 6, 250);

Since you're not using the Arduino environment, you can simply implement the same function in your own code.  Have you even >>looked<< at the source?

 

You will need to handle cases where the ADC value is outside the range you expect so that your device doesn't explode.  What happens if the sensor fails?  What happens if there is a short somewhere?

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

I did make a recalculation and used it in the source but it gave no improvement for the maximum dutycycle e.g. 99.9%

Attachment(s): 

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

I will not open a random .docx file.  Post the text of it here instead.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

Have you even >>looked<< at the source?

 

I searched a lot for the Arduino map() source but could not find it.

Could you please tell me where finding it?

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

I will post it in txt format.

Attachment(s): 

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

I searched a lot for the Arduino map() source but could not find it.

Could you please tell me where finding it?

You have got to be kidding me.

 

It's in >>this<< thread!

 

Scroll to the top of the page and find post #11.

 

He first mentioned it in #8.

 

Also find it where I pointed you to a google search result it in post # 47, and mentioned it again in #54.

 

You are clearly not paying attention, or making much of an effort.

 

I'm out.  Good luck with your project.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

Be_logic wrote:
but it gave no improvement for the maximum dutycycle e.g. 99.9%

???  Does everyone else know what that means?  Do you mean that you want a duty cycle greater than 99.9% ?  What duty cycle >>do<< you want?  For which input value?  What duty cycle are you getting now?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

Yesterday I made reply"s but can't see this here so I send it again.

 

The ADC value for 0.1V is 20.48, see the recalculations and ofcource I can use the figures after the dot.

At which part in the source I must place ORC1A = map(ADC, 20, 410, 6, 250);

Because I managed using the map function in Atmel Studio 7.

 

Attachment(s): 

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

Dutycycle must be variable between 1 and 99.9%, now it's ~67.5 max

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

There must be some kind of a language barrier (or stubbornness, or laziness) at work here.  I don't care which.  I am tiring of it all, and just want it to end!  So here's a crack at it:

#include <avr/io.h>

#define ADC_MIN 21
#define ADC_MAX 409
#define PWM_MIN 6
#define PWM_MAX 250

static uint32_t map(const uint32_t x,
                    const uint32_t in_min, const uint32_t in_max,
                    const uint32_t out_min, const uint32_t out_max) {
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

int main(void) {

  uint16_t adc;

  // Disable input buffer on ADC5.
  DIDR0 = (1 << ADC5D);
  // AVcc, ADC5.
  ADMUX = (1 << REFS0) | (5 << MUX0);
  // div128, free running, start.
  ADCSRA = (1 << ADEN) | (1 << ADSC) | (1 << ADATE) | (7 << ADPS0);

  // OC1A output for PWM.
  DDRB = (1 << PORTB1);
  // Mode 5 (8-bit Fast PWM), /1, non-inverted output on OC1A.
  TCCR1A = (2 << COM1A0) | (1 << WGM10);
  TCCR1B = (1 << WGM12) | (1 << CS10);

  // Forever.
  while(1) {
    adc = ADC;
    // Constrain.
    if (adc < ADC_MIN) {
      adc = ADC_MIN;
    }
    else if (adc > ADC_MAX) {
      adc = ADC_MAX;
    }
    // Scale to PWM output.
    OCR1A = map(adc, ADC_MIN, ADC_MAX, PWM_MIN, PWM_MAX);
  }

}

Ask if you don't understand something.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

Last Edited: Thu. Mar 15, 2018 - 03:26 AM

Pages