Hot answers tagged

21

Assuming that you want to optimize for speed: Unless you really find out how many cycles it takes if you use mul, you can't compare. So let’s try: If you use the mul instruction, you need two of them (the operand is 16 bit). So that already costs 4 cycles. You have to load constant 10 into one register: 1 cycle. Then you have to add both 16 bit results. ...


19

There have been repeated attempts to create various different auto-generating code for the last 20-30 years or so. Each time, it's marketed as something revolutionary, but it never becomes a success. This isn't something new at all. Siemens/Infineon had such tools way back in the late 1990s. Motorola/Freescale also attempted something similar in the early ...


14

People have been inventing ways to generate code without having to write any code for decades now - and they all hit the same limitations. CubeMX does not really do anything very clever (complicated yes, clever no) and it's not really writing original code the way a programmer would - it's creating a configuration with blocks of pre-written code, filling in ...


9

Don't use the sizeof operator. char[10], char[40], char* are all slightly different data types and return different sizes. Use strlen to find the length of a string if your micro controller library provides it. If it doesn't, write one yourself by counting till you find the string terminating character, usually '\0'. See a sample code below. int i; // store ...


8

It all depends on who you are and what do you use it for. If you are a hobbyist that wants to start a project, you can use CubeMX to have all the peripherals you need configured and a LED blinking under a real-time OS in 15 minutes so you can start building your application. If you are a medical device manufacturer, you may not even be able to use CubeMX due ...


7

This is by no means a unique feature: Renesas code generator and Infineon Toolbox come to mind. Those tools all have the same flaw: they force you to use a certain framework which is not flexible enough in practice. The moment you edit the auto-generated code to adapt it to your needs, you lose the option to use the code generator again: it will typically ...


6

If the LED is drawing its power through the 7805, the regulator will require a large heatsink. If the bench supply has an adjustable current limit restricting the output to a max of 1 A, this might be why the regulator survives bench operation. A lithium battery has no such limiting. BTW, the 7805 is not a low-dropout regulator. It is a second-generation ...


5

The code works as written, it just makes no sense. The code uses sizeof to take the size of a pointer to a string and pointers on AVR are 2 bytes long. Subtract three and that's -1, and your loop variable I must be unsigned as -1 is largest positive number that fits to variable i. What you really want is to take the length of the string given as a parameter ...


4

It would be preferable for reliability to use a hardware WDT rather than another black box processor with a huge number of possible internal states. Saves the programming process as well, but it might cost a bit more.


4

I think the problem is the itoa() function. Its parameter is a normal signed integer, so your unsigned int is being coerced to a signed int.


4

This is a generic method. The ubrr is an unsigned int which makes it 16 bits wide (2 bytes). To break this into discrete bytes like in the example, you need to shift the bits right by 8 to get the high byte. In your specific example it doesn't matter because the high byte is empty anyway, but what if you need a ubrr of 3,096? Also, you might be confusing ...


4

Branching to the interrupt, saving registers, restoring registers, and then returning from the interrupt all take clock cycles. If the only thing you are doing is transmitting this serial data, then not having the interrupt is going to use less clock cycles overall. Some other comments about the code. Blocking while you send multiple characters within an ...


4

Code generation as you speak of it is mostly a way to setup and abstract away the direct hardware setup register interface. That has been a trend for quite some time from several (all?) chip providers, and is known as a HAL (Hardware Abstraction Layer). Auto-generating that code from a GUI has been done by at least Atmel and ST and probably others already. ...


4

I can't tell you why GCC is doing the multiply this way, but I can tell you that it is not faster than using mul. I used the the datasheet (section 33) to count the number of cycles the following routine takes (AVR-GCC 9.2.0, compiler explorer): #include <stdint.h> uint16_t times_ten(uint16_t word) { return word * 10; } For -O1 through -O3 (...


4

I currently have Attiny85 , attiny45 , atmega328 , 8051 MCU. I want to program them in a chipless method ( which doesn't use any intermediate chips... Sorry, that isn't possible. You need a USB interface, and this will need at least one chip. I have searched about Serial and parallel avr programming and most said that because of voltage levels , direct ...


4

There seems to be a common problem with the Atmel ICE being very slow to program using avrdude. It's to do with the programmer seemingly defaulting to a low clock rate for programming. The problem can be resolved by instructing avrdude to run the programmer at a higher speed by adding -B1 to the command line arguments, for example: avrdude -c atmelice_isp -...


3

The code is tagged as ATMega328, so I assume you are coming from an Arduino plaform. For an AVR, pins are controlled by hardware registers, each of which controls a bank of up to eight pins, known as a "port". For example you will find the pins are labelled as for example: PA0 (Port A, bit 0), PB3 (Port B, bit 3) and so on. Each bank of eight pins ...


3

Yes, you need a delay for two reasons. First of all, the internal pull-up is weak, so it can take a while before the voltage has risen enough to read as logic 1. How many clock cycles it takes depends on the stray capacitances. Second reason is the input pin synchronizer, explained on datasheet page 70. The synchronization adds a delay, so even if you did ...


3

In general this is not a "you must" requirement but a reminder of the fact that the processor can only update one register at a time. On the ATmega series, the input vs. output and the output value or pull-up are controlled by distinct registers with a bit per pin, and the processor can only modify one register at a time, so for any change which ...


3

I've found the problem: there is a glitch on the code that converts the floating point number representing the temperature. The leading zero is removed. For example the number xx.09 is erroneously converted to xx.9. To solve the problem it is sufficient to change the sprintf call in this way: sprintf(buffer, "%i.%02u %i %i\r", temp_, dec, vcc, ...


3

Get a piece of copper foil, or single_layer PCB material, and use those as GROUND PLANE. Drill holes in the foil, or the copper_clad single_Layer, and solder all the GROUND points to this new Ground Plane, with 1/8" or shorter wires.


3

As noted in other answer, __memx might help. Check it's description: This is a 24-bit address space that linearizes flash and RAM: If the high bit of the address is set, data is read from RAM using the lower two bytes as RAM address. If the high bit of the address is clear, data is read from flash with RAMPZ set according to the high byte of the address. ...


3

Your method 1 does not work, because in a single interrupt, you are writing all characters of the string to the Uart Data Register. When an UDRE interrupt happens, it is ready to accept one (1) character until another UDRE interrupt happens. Which is the reason why your method 2 works, because it expicitly waits for the Uart Data Register to be empty before ...


3

RS485 needs a 120ohm terminating resistor at the end of the bus, and it is missing in your schematics. (Notice my artistic talents)


3

Obviously there are many answers here which basically forbid the use of "generated" software. I'll take a stand for the other side: Use what's there. I am what you would call a (more or less) professional software developer (at least that's where I get my income from). I have been working with larger teams as well as alone. Unless there are very ...


3

In industry, do developers generally try to simplify things ... even if it's an overkill in the sense of potentially adding more uC capability ... Why do you care if the microprocessor is "too capable"? You care that it's capable enough, then you care about the lifetime cost of the product (to oversimplify: engineering time / number of boards + ...


3

There is only 100nF capacitor at the output of the LDO, so perhaps it is oscillating. This should not result in its destruction, but who knows. The datasheet isn't very helpful as it contains no advice on capacitor selection, which is a red flag, but all the examples use a 1µF cap on the output, so perhaps replacing the 100nF cap with 1µF could help.


3

You have an oscilloscope as mentioned in the comments. Check the battery voltage - most likely higher than 14V by a good bit. Hook up your AL8871Q with the LED to the battery, but with the AP7370 and the processor removed. Check the battery voltage with the oscilloscope while the LED is lit. I think you will find that there are peaks on the battery voltage ...


3

The reason for the GND pins is crosstalk. This is not much of a problem with short cables but becomes an issue with longer ones. The crosstalk is caused by the capacitance between the wires in the cable. With the AVR, the original 'standard' was 10 pin by Kanda. Only later did the 6 pin emerge. All my AVR based boards used the 10 pin. A 6 pin connector can ...


3

The timer interrupt-flag is reset incorrectly. An interrupt flag is reset by writing a 1 to it, and not a 0. TIFR0 &=~ (1 << OCF0A); ^ Incorrect It should be TIFR0 |= (1 << OCF0A); See Section 11.9.7 of the ATtiny24A/44A/84A datasheet (2020 version - Revision A) listed here ...OCF0A is cleared by hardware when executing the cor- ...


Only top voted, non community-wiki answers of a minimum length are eligible