Math in C in AVR

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

I am new in C but have been working with 8 bit processors in assembly for years. This is my first real project in C.

The project is an ac motor controller and display for my foundry furnace. The motor controller is a AT2313 controlling the timing to a triac from an zero crossing interrupt. It talks, TWI to a ATmega168  controlling display, keypad, temperature sensors etc.

So the motor controller has a 16 meg x-tal, with timer 0  prescaler at 1024 I should get a count of 130 at 60hz.(2 pulses per cycle) I worked out the following formula in Java and it displays ok between 55.5 hz and 70.0 hz. 

 

uint8_t pulseCount2hz(uint8_t pulseCount){
    return (((130-pulseCount)* .5) +60);
}

I plugged it into AS7 and to my surprise it runs fine. I thought I would have to do the subtraction, then shift right then add 60. Is that what it is doing? And how do I get a listing of the asm decoding? 

Whats a good source of information for AS7.

Thanks in advance for any of comments.

 

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

The output file with the lss extension is disassembled.

 

return (((130 - pulseCount) * .5) + 60);

This is slow because it is a floating point operation.
It can be improved by shifting to the right of 1 bit.

 

return (((130 - pulseCount) > 1) + 60);

 

Last Edited: Fri. Oct 16, 2020 - 05:49 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Studio7 has help built-in, have you tried that  it also leads you to these videos:

 

https://onlinedocs.microchip.com...

 

Why do you need the 2313 at all??...you can easily control the triac with your mega168.  Or is this some far away triac from the rest of the system?  In that case you need to include isolation grn loop prevention, etc. 

 

Of course if it is AC line power, be careful & also use isolation (like opto).    Also in addition & above all, make sure your PC--usb is isloated (use an ADMxxxx isolator module)  so when you drop a paper clip into your pcb you don't blow 110 into your PC &  kapoof goes the whole kaboodle.

 

I thought I would have to do the subtraction, then shift right then add 60.

What exactly are you trying to figure out?  Remember 60 Hz is just 120 zero crossings every second. 

 

if you are trying to convert timer counts to freq you just divide 156250 by your count  and add a decimal point.  You do NOT use floating point

 

For example, if you get 130 counts, you have a calc of 156250/130===>1201  (or 120.1 Hz)   if you look at the remainder (which is 120, which is greater than 130/2), you can round up to 1202 (120.2 Hz)

So you just need a 24 bit / 1byte capable divide.  You need to divide this by 2 to convert from pulse freq to line freq.

 

Taking this into account makes it even easier, since you can simply take 78125 and divide by the counts to get freq:

Say your count is 136, you just integer calc 78125/136, giving 574 remainder 61. Since the (remainder*2  <136 ),  you don't round up & stay at 574 (57.4 Hz) 

Say your count is 129, you just integer calc 78125/129, giving 605 remainder 80. Since the (remainder*2  >=129 ), you do round up & settle on 606 (60.6 Hz) 

 

Note that 1024 gives you poor counting results (130 is pretty low, 130 vs 131 is almost 1%) , use a lower prescaler division than 1024 so your counts become larger numbers. 

With a prescaler of 1024 you can only get these freqs (when rounded to 0.1 Hz):

 ....62.5 62.0, 61.5, 61.0, 60.6, 60.1, 59.6, 59.2, 58.7, 58.3, 57.9, 57.4.....    

without rounding (less preferred, but you do show 60.0):

 ....62.5 62.0, 61.5, 61.0, 60.5, 60.0, 59.6, 59.1, 58.7, 58.3, 57.8, 57.4.....    

       some people might feel better seeing "60.0", even if it skews the error from not rounding.  Seeing 60.1 might cause great angst.

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

Last Edited: Fri. Oct 16, 2020 - 07:06 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ShorelineJohn wrote:
130 at 60hz.(2 pulses per cycle)
Last time I looked twice 60 was 120 not 130.

Ross McKenzie ValuSoft Melbourne Australia

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

Last time I looked twice 60 was 120 not 130.

 

...he switch to talking about his timebase instead, without mentioning it

 

16e6/1024==> 15625 counts/sec. ....in 120th sec  ===>130.1 counts  

 

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

Last Edited: Fri. Oct 16, 2020 - 06:23 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'd use the input capture of the 16bit timer and measure every half-period.

 

The use of I2C/TWI should be entertaining.  Lockup is a valid state.

 

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

Thanks for the quick reply's.

I did not know Studio7 even had floating point, its because of my assembly background I'm sure. Like I said I am new at C. Shifting once is the way to go.

I am using the 2313 to try to use the brownout detection to trip what I use to call a COP circuit.(I forget what it stands for) A simple timing circuit that will cut power if the processor stop running and trigger a reset. I maybe over thinking this, but its a project.

The controller I am using now I did in assembly on a AT8051 and it still works fine, I just trying to make things more complicated. Once I get this to work I might try replacing the 168 with an ARM cortex-m board and blue tooth link it to a phone. The foundry makes for good microcontroller projects.

Last Edited: Fri. Oct 16, 2020 - 07:01 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I did not know Studio7 even had floating point

Well studio itself doesn't have "it" per se, it's really part of the language deployment.

Generally you should have no need to use floating point & burn up memory and processor speed. Stick with integer, unless you get into a very complex or dire situation.

If you can use hammer instead of a pile driver, use a hammer.  

 

Once I get this to work I might try replacing the 168 with an ARM cortex-m board and blue tooth link it to a phone.

The game of complexity never ends.  Who wins?  

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

Last Edited: Fri. Oct 16, 2020 - 07:19 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am sorry I did not explain it well, but the easiest way I see was to keep the count low,  then subtract, shift and add which is quick and simple.

It should have a time out function for the TWI, I have been looking at that too.

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

but the easiest way I see was to keep the count low

You should easily be able to do 16 or 32 bit integer divides with little overhead....give it a whirl. 

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: 2
return (((130 - pulseCount) > 1) + 60);

 

">" should be ">>"

 

The compiler is smart enough to optimize

return (((130 - pulseCount) / 2) + 60);

to the same output code, and it's a bit less "obscure."

 

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

Ah! sorry!
">>" is correct.
 

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

Notice as described previously, you need to divide by pulse count, whereas you seem to be multiplying.  

If you get triple the pulse count that means it took a lot longer time, hence the freq is cut by 3x...you get that by dividing by pulse count. 

 

However, you are forming 65 - 0.5 * Pcount...you are needlessly fitting a line segment approximation over a small part of the reciprocal curve 

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 curious as to how you arrived at this aproximation:

 

ShorelineJohn wrote:
uint8_t pulseCount2hz(uint8_t pulseCount){
    return (((130-pulseCount)* .5) +60);
}

 

Since frequency and count are inversely proportional (count is related to period). So the real equation is:

 

f = (130 * 60) / count

 

This is an hyperbolic curve. Of course you can approximate it piecewise with straight lines and your approximation is ok in a close neighborhood of count=130

 

 

Looks like a tangent. Is that the method you used?

 

edit: on reading posts more carefully, I see #3 is related to this.

 

edit2: I'll calculate the actual tangent equation, because why not? The only difference is the slope, it's not 0.5 (actually, -0.5) but instead can be calculated from the derivative of the frequency equation

f = (130 * 60) / count

 

which is

df/dcount = - (130 * 60) / count^2

 

so at point count = 130,

df/dcount = -60/130 = -0.46

 

Therefore, the actual tangent equation at point count=130 is

f = (130-count)* .46) +60)

I edited the graph above to include the tangent for comparison.

 

Last Edited: Fri. Oct 16, 2020 - 10:43 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ShorelineJohn wrote:
The project is an ac motor controller and display for my foundry furnace.
Cool!

ShorelineJohn wrote:
Whats a good source of information for AS7.

  • Some of the Atmel Studio 7 creators post and support; so, browse and inquire to go along with the well experienced operators.
  • Guide

Atmel Studio (AVR-related) | AVR Freaks

CommunityKnowledge (try an initial search on 'Studio')

Atmel Studio 7.0 (release note)

Atmel Studio 7

 

"Dare to be naïve." - Buckminster Fuller

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

ShorelineJohn wrote:

foundry furnace.

gchapman wrote:
Cool

Hopefully, far from it?!

 

cheeky

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 2

gchapman wrote:

Cool!

No, HOT!

 

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

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

ShorelineJohn wrote:
Whats a good source of information for AS7.

Since it's based on Microsoft VisualStudio, a lot of stuff about VS is applicable.

 

At the bottom of the AS7 page, there's a load of introductory videos:

 

https://www.microchip.com/mplab/...

 

Also:

 

https://onlinedocs.microchip.com...

 

https://onlinedocs.microchip.com...

 

The Toolchain used by Atmel Studio is AVR-GCC:

 

https://gcc.gnu.org/wiki/avr-gcc

 

The runtime library is avr-libc:

 

https://www.nongnu.org/avr-libc/...

 

For some learning & reference materials on the 'C' language itself, see:

 

https://blog.antronics.co.uk/2011/08/08/so-youre-thinking-of-starting-with-c/

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avrcandies wrote:
... make sure your PC--usb is isloated (use an ADMxxxx isolator module) ...
The Linear Technology side of Analog Devices also has USB isolators; Maxim Integrated is one of at least several competitors.

USB has a place in industrial though Ethernet will have greater range and is very typically isolated.

avrcandies wrote:
... so when you drop a paper clip into your pcb you don't blow 110 into your PC &  kapoof goes the whole kaboodle.
A foundry operator does deal with flying metal (small fragments) that, in this case, is likely to enter via the keypad opening in the enclosure.

Microchip has a method for key sensing through thin aluminum sheet (edit2 : and stainless steel); the display can be sealed behind polycarbonate sheet.

 


USB-ISO USB 2.0 Full Speed insulator passed all EMC tests in certified laboratory and now can bear CE mark! | olimex

USB Isolators | Analog Devices

UART Fun | AVR Freaks

<sense thru aluminum> When I overcome my CRS, insert the URL indecision

edit : Metal over Cap (MoC)

Implementing Metal Over Capacitive Touch Sensors and T9 Xplained Pro Extension Kit

due to AVR128DA28 - 8-bit AVR Microcontrollers

 

"Dare to be naïve." - Buckminster Fuller

Last Edited: Tue. Oct 20, 2020 - 09:24 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ShorelineJohn wrote:
I did not know Studio7 even had floating point, ...
and fixed-point.

ShorelineJohn wrote:
... its because of my assembly background I'm sure.
That will serve you well.

ShorelineJohn wrote:
I am using the 2313 to try to use the brownout detection to trip what I use to call a COP circuit.(I forget what it stands for)
COP -  Computer Operating Properly

ShorelineJohn wrote:
I maybe over thinking this, ...
You're being prudent given molten metal.

ShorelineJohn wrote:
... but its a project.
and don't overthink that as such has been reviewed, verified, validated, and built into ASIC (as you've found)

There are external watchdogs that will either reset the MCU or stop the process, in this case a motor driver, ideally with a required action from the process's operator (process enters fail-safe, operator confirms safe, operator re-arms, operator re-starts; if fail then the operator has a procedure for the failure)

Latch-up is possible though not probable in this case; so, external watchdog cycles the LDO or AC/DC or DC/DC instead of resetting the MCU.

ShorelineJohn wrote:
... and blue tooth link it to a phone.
Typical Bluetooth is limited range (spec a range in the context of safety) and can be interfered with by Wi-Fi and possibly damaged by ESD, EFT, and nearby lightning (add redundancy to the data link)

A smartphone likely has Wi-Fi; might consider a WebSocket server on the MCU.

An industrial process controller is more likely to have Ethernet instead of a wireless link.

ShorelineJohn wrote:
The foundry makes for good microcontroller projects.
Indeed and very small or micro foundries may have an interest from blacksmiths.

 


Fixed-Point Support - avr-gcc - GCC Wiki

Where Do Programming Languages Go to Die? – EEJournal

by Jim Turley

September 28, 2020

Assembly Programming has Become a Lost Art

[last paragraph]

Instead, I think all programmers, regardless of product focus or chosen language(s) should be trained in the assembly language of at least one processor.

...

MC68HC705P6A Advance Information - Data Sheet

...

M68HC05 Microcontrollers

...

[page 13, first paragraph]

...

...  and a computer operating properly (COP) watchdog timer.

...

Efficient web server technology for resource-constrained microcontrollers - Embedded.com (WebSocket)

 

"Dare to be naïve." - Buckminster Fuller

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

ShorelineJohn wrote:

I did not know Studio7 even had floating point, ...

gchapman wrote:
and fixed-point.

Actually, that's not Atmel Studio as such - that's the compiler (and its libraries).

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As mentioned in #13 & fully detailed in #14 is that you can use an approximation...the only big reason to do so is to avoid a division operation.  But since you can use integer division, it is no issue (high speed or otherwise), so no need to use the mult approx, which as you can see will start to deviate greatly as you move away from the matchup point.

 

Also, to avoid confusion, note 130.208333... is truly needed, not 130....hence the other numbers presented in #3 for accuracy, since they approx 130 more exactly.

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

Last Edited: Fri. Oct 16, 2020 - 04:58 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you Everyone for Your responses.

To relieve anyone's fears I been testing this using a pulse generator built out of another controller, so no chance at this point of blowing stuff up too much. On the 8051 I did not use in circuit programming so I just haven't thought about it but that would be bad.

In real life I probably don't need to compensate for frequencies shifts , the 8051 just assumes 60hz and works fine, counting the time of the last half cycle and using it could easily cause glitches, maybe a running average but that is more math for the 2313.

 

I have found the high prescaler setting causes jitter in the output so I will go back to using timer1 and a 16 bit count. Not sure why this happens.  

The big thing with this motor is on high it has to run at just over 50% or it will blow the flame out. When the furnace is cold it has to run so slow the motor won't start by itself, I have a vent on the air input and block the air until it get up to temperature. Thats what I should be working on.

I have been taking C classes on Udemy for ARM cortex boards and just not sure how it all relates to 8 bit processors, I really didn't think I could put 0.5 into AS7. I have been doing this stuff really old school and need to update my skills.

 

I really have gone through a lot of the videos and I know I should't ask but can someone tell me how to get the disassembled code out of AS7. The only examples I have found are using the xplained boards.

Thanks Again for all of you great responses.

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

 I really didn't think I could put 0.5 into AS7.

 

Well you can't...it will burp loudly....you can put it in your program!

...But  again try to avoid floating point, or just experiment with it.

It's like walking on nails & taking pain pills...then there is no problem, right?

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

ShorelineJohn wrote:
On the 8051 I did not use in circuit programming so I just haven't thought about it but that would be bad.
USB isolator on the ICSP though the COP might make that a no-go.

ShorelineJohn wrote:
... but can someone tell me how to get the disassembled code out of AS7.
per #2

In-System Programming and Kit Connection | Atmel Studio 7

[near bottom]

Settings Verification

...

2.When Atmel Studio 7 builds the project (automatically done when pressing Start Without Debugging), several generated output files will show up in the Solution Explorer window. The following output files are generated:

...

2.4.LSS file: Disassembled ELF file.

...

 

"Dare to be naïve." - Buckminster Fuller

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

gchapman,

Thank you! I know it a real simple thing but I could not find that file. It will help. I have found Studio 7 information hard to find sometimes. I think my brain kept looking for a .LST file.

 

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

You're welcome and thanks to kabasan for the answer.

 

"Dare to be naïve." - Buckminster Fuller

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

Oh I did miss that. Thank you kabasan!

I keep telling myself I am not to old to learn this, but somedays I just don't know.