Code conversion C to Assembly.

Thread Starter

MaxHeadRoom

Joined Jul 18, 2013
28,619
Would it be possible to convert this to assembly, I am not familiar with C at all very much.
I am attempting an 255 value exponential curve in 16 steps

C:
const int outputPin = 9;

void setup() {
  // Set the pin as a digital output
  pinMode(outputPin, OUTPUT);
}

void loop() {
  // Fade the brightness from 0 (min) to 255 (max) with 40ms delay per step
  for (int brightness = 0; brightness <= 255; brightness++) {
    analogWrite(outputPin, brightness);
    delay(40);
  }
}
//Setup the output PWM pin
const int outputPin = 9;

// The number of Steps between the output being on and off
const int pwmIntervals = 100;

// The R value in the graph equation
float R;

void setup() {
  // set the pin connected to the LED as an output
  pinMode(outputPin, OUTPUT);

  // Calculate the R variable (only needs to be done once at setup)
  R = (pwmIntervals * log10(2))/(log10(255));

}

void loop() {
  int brightness = 0;

  for (int interval = 0; interval <= pwmIntervals; interval++) {
      // Calculate the required PWM value for this interval step
      brightness = pow (2, (interval / R)) - 1;
      // Set the LED output to the calculated brightness
      analogWrite(outputPin, brightness);
      delay(40);
  }
 

WBahn

Joined Mar 31, 2012
29,979
Sure. Both C and assembly are Turing complete.

But I have a feeling that's not really what you are trying to get at. Could you flesh out the context of your question some more?
 

joeyd999

Joined Jun 6, 2011
5,237
Would it be possible to convert this to assembly, I am not familiar with C at all very much.
I am attempting an 255 value exponential curve in 16 steps

C:
const int outputPin = 9;

void setup() {
  // Set the pin as a digital output
  pinMode(outputPin, OUTPUT);
}

void loop() {
  // Fade the brightness from 0 (min) to 255 (max) with 40ms delay per step
  for (int brightness = 0; brightness <= 255; brightness++) {
    analogWrite(outputPin, brightness);
    delay(40);
  }
}
//Setup the output PWM pin
const int outputPin = 9;

// The number of Steps between the output being on and off
const int pwmIntervals = 100;

// The R value in the graph equation
float R;

void setup() {
  // set the pin connected to the LED as an output
  pinMode(outputPin, OUTPUT);

  // Calculate the R variable (only needs to be done once at setup)
  R = (pwmIntervals * log10(2))/(log10(255));

}

void loop() {
  int brightness = 0;

  for (int interval = 0; interval <= pwmIntervals; interval++) {
      // Calculate the required PWM value for this interval step
      brightness = pow (2, (interval / R)) - 1;
      // Set the LED output to the calculated brightness
      analogWrite(outputPin, brightness);
      delay(40);
  }
What processor?
 

cmartinez

Joined Jan 17, 2007
8,220
Would it be possible to convert this to assembly, I am not familiar with C at all very much.
I am attempting an 255 value exponential curve in 16 steps

C:
const int outputPin = 9;

void setup() {
  // Set the pin as a digital output
  pinMode(outputPin, OUTPUT);
}

void loop() {
  // Fade the brightness from 0 (min) to 255 (max) with 40ms delay per step
  for (int brightness = 0; brightness <= 255; brightness++) {
    analogWrite(outputPin, brightness);
    delay(40);
  }
}
//Setup the output PWM pin
const int outputPin = 9;

// The number of Steps between the output being on and off
const int pwmIntervals = 100;

// The R value in the graph equation
float R;

void setup() {
  // set the pin connected to the LED as an output
  pinMode(outputPin, OUTPUT);

  // Calculate the R variable (only needs to be done once at setup)
  R = (pwmIntervals * log10(2))/(log10(255));

}

void loop() {
  int brightness = 0;

  for (int interval = 0; interval <= pwmIntervals; interval++) {
      // Calculate the required PWM value for this interval step
      brightness = pow (2, (interval / R)) - 1;
      // Set the LED output to the calculated brightness
      analogWrite(outputPin, brightness);
      delay(40);
  }
Looks straight enough to me ... but Bahn is right, more nuanced info might be needed.
 

joeyd999

Joined Jun 6, 2011
5,237
Why not just a lookup table?
Code:
Interval    Brightness
0    0
1    1
2    1
3    1
4    1
5    1
6    1
7    1
8    1
9    1
10    1
11    1
12    1
13    2
14    2
15    2
16    2
17    2
18    2
19    2
20    3
21    3
22    3
23    3
24    3
25    3
26    4
27    4
28    4
29    4
30    5
31    5
32    5
33    6
34    6
35    6
36    7
37    7
38    8
39    8
40    9
41    9
42    10
43    10
44    11
45    12
46    12
47    13
48    14
49    15
50    15
51    16
52    17
53    18
54    19
55    21
56    22
57    23
58    24
59    26
60    27
61    29
62    31
63    32
64    34
65    36
66    38
67    40
68    43
69    45
70    48
71    51
72    54
73    57
74    60
75    63
76    67
77    71
78    75
79    79
80    84
81    88
82    94
83    99
84    105
85    111
86    117
87    124
88    131
89    138
90    146
91    154
92    163
93    173
94    182
95    193
96    204
97    215
98    228
99    241
100    254
 

Thread Starter

MaxHeadRoom

Joined Jul 18, 2013
28,619
Pic16f18131, the program I intend is to use a 16 word index table to fade LED's in and out using PWM, exponentially.
Also, Unfortunately I am having to use MPLABX v5.35 due to the Pic not referenced in MPLAB IDE and so far have not been able to find a way of using a View-by-step as is possible in MPLAB.
I am using a look-up table, just need the exponential values.
 

joeyd999

Joined Jun 6, 2011
5,237
Ahh! Ok, you have no reason other than personal stubbornness - which you are totally entitled to. It’s like arguing with my 99 yo mother. Those arguments usually end with. “Because that’s the way I do it!”
That crappy Windows product runs most of the country, unlike OfficeLibre, so it can’t be too bad.
You're all just being stubborn.
 

Ian0

Joined Aug 7, 2020
9,675
Is there any instructional material for implementing/creating the various features such lookup table values etc ?
There's a British/European standard. It says that the brightness should vary over 3 decades, i.e. from 0.1% to 100%, with 1% at 33% slider travel, and 10% at 67% slider travel (imagine an slider on a lighting desk).
Unfortunately, that doesn't work too well with 8-bit PWM. Not too bad with 12-bit PWM.
I might have a copy of the standard obtained in the days when it was permissible to download them.
 

joeyd999

Joined Jun 6, 2011
5,237
Is there any instructional material for implementing/creating the various features such lookup table values etc ?
I just noticed your original request was for 16 steps, not 256.

Here is the 16 step table:

Code:
pwmIntervals    16
R    1.875
    
Interval    Brightness
0    0
1    0
2    1
3    2
4    3
5    5
6    8
7    12
8    18
9    26
10    39
11    57
12    83
13    121
14    175
15    255
And I attached the LibreOffice .ods file for you to play with.
 

Attachments

Thread Starter

MaxHeadRoom

Joined Jul 18, 2013
28,619
Thanks, I am looking at the least size look-up table that will work, LED fade in and out time is around 4-5sec for each.
 

Ian0

Joined Aug 7, 2020
9,675
I have some touch-operated dimmers that fade up and down taking 15 seconds, I used 12-bit PCM on a PIC and they are very noticeably steppy at low light levels. I'm going for 14-bit PCM when I next upgrade them. I wonder if some dither could be introduced to improve the resolution without it becoming flickery.
 

joeyd999

Joined Jun 6, 2011
5,237
I have some touch-operated dimmers that fade up and down taking 15 seconds, I used 12-bit PCM on a PIC and they are very noticeably steppy at low light levels. I'm going for 14-bit PCM when I next upgrade them. I wonder if some dither could be introduced to improve the resolution without it becoming flickery.
The right way to do this is to "animate" the changing of the brightness with a succession of "small" steps from one "big" step to the next.
 
Top