Minimizing microcontroller code size

Reducing microcontroller code size isn’t ease task, but what if you want resulting code to fit your available memory? Minimizing microcontroller code size can be done in two ways: Firs is using compilers optimization feature by code size. This optimization is very dangerous. Your optimized code may not work as supposed to because compiler may eliminate some code like empty loops or adding zeros. For example compilers like to remove for(int i=0;i by leaving one or several iterations. So don’t think that your optimized code will work as un-optimized. There are other things you can do to minimize microcontroller code size. One of them is avoiding usage of standard libraries routines. Because these libraries are general and handles all standard possible execution cases. By including them in your design you include a bunch of code you dont need. If it is possible it is better to write your own routines or modify existing ones to fit your needs. Also try to use needed variable sizes. If you need unsigned integers use uint type if you need byte size avoid using int which usually is more than 8 bits (16 or 32). This may significantly reduce your code as there are fewer…

Continue reading

Increase microcontroller code efficiency

C compilers are getting more and more advanced, but there is always a trade off made between speed and code size. Compiled code can be faster or smaller but not both. So you have to choose which part is more important speed or code size. The Increase of microcontroller code efficiency can be done in many ways. Don’t trust compiler optimization features, as they might not bee as effective as you expect. It is better to grab some profiler and inspect what parts of your code takes the most time and size. It is better to follow some techniques that may reduce code execution time and increase microcontroller code efficiency:

Continue reading

Interfacing LCD to Atmega using two wires

This is not new Idea of interfacing LCD using two wires, but it can help in many situations when there is not enough of microcontroller pins. This example is based on Hitachi 44780 Alphanumerical LCD. This circuit I provide is only to represent an idea but I think it should work also if soldered. We know, that to make LCD working you need at least 6 (in 4 bit mode) wires to control. But what if you need as many pins as possible from your avr and still want to see results on LCD. Then you need to use serial LCD or make one. In this example you just need to convert serial data coming to LCD using shift register. I suggest using 74HC164. You need only two wires to push data to shift register and then give them to LCD using “E” strobe signal. So how this operates? Atmega’s PC1 pin clocks shift register and PC0 is data line. Before you write to shift register clean it by sending “0” in eight clock cycles. You can erase register with additional wire controlling register reset, but there would be 3 lines used. After register is cleared, send eight bits to…

Continue reading

Power sources for AVR

Power sources for AVR are very important part in projects. Avery circuit has to be powered from some source like battery or from AC adapter 110V/220V. Using batteries is more convenient way to power the microcontroller projects as the circuits are simpler and constructed devices become portable. There are many types of batteries in shapes and sizes or capacities. So when choosing battery you should consider many factors: Capacity – this is very important parameter measured in mA/h. This parameter defines how long your microcontroller project will be working before recharging or replacing batteries. Rule is simple – as bigger battery capacity as longer your circuit will be working, but in other hand your project may become more expensive or even heavier because of bigger batteries. Second parameter is battery Voltage. If Voltage of battery is to small for tou circuit, youll have to connect several batteries in series. Other is Expirity date. You don’t want your battery energy leakage or become obsolete because of this. Working temperature. If your project will be working in more extreme temperatures – heat or cold, then you should consider this. And the last parameter would be chape, size and weight. If your project…

Continue reading

Atmega EEPROM memory writing

All atmega family microcontrollers have internal EEPROM memory. They can have from 512bytes to 4kBytes. EEPROM memory has its own memory space and is linearly organized. In order to access EEPROM mempry in atmega there are three registers used: Address register, Data register and Control register. Address register EEAR (EEPROM Address Register) is made of two 8 bit registers EEARH:EEARL. In this register the EEPROM cell address has to be loaded. Data register EEDR (EEPROM Data Register). When writing to EEPROM the data is loaded to this register. When getting data from EEPROM – you read data from this register. Control register EECR (EEPROM Control Register) is used to control access to EEPROM memory. EERIE – EEPROM Ready Interrupt Enable. This bit generates interrupt after write cycle is finished. If bit is set to ‘1’ and also I bit SREG, then EEPROM Ready interrupt is enabled. If bit is set to ‘0’ – then interrupt is disabled. EEMWE – EEPROM Master Write Enable. The EEMWE bit determines whether setting EEWE to one cause the EEPROM to be written. When EEMWE is set, setting EEWE within four clock cycles will write data to the EEPROM at the selected address If EEMWE…

Continue reading

Simplest 128 atmega programmer

Atmega 128 is like other AVR microcontrollers. They are ISP – is in-system programmable. Earlier I wrote an article about AVR ISP programmer where 74HC244 buffer is used. Using buffer is safer for your AVR. But what if you need 128 atmega programmer without any parts, then you can connect your microcontroller directly to LPT port or use protection resistors (220R) just in case. of course circuit works without resistors, but you put your LPT port at risk. Just connect GND, SCK, MISO, MOSI and RESET to adequate LPT pins and you can program atmega’s flash memory without removing it from socket. Programming software can be PonyProg

Continue reading

Integrated Processor RISC AVR rules!

AVR family is quite new in integrated processor RISC family. These microcontrollers can solve many embedded problems. They differ from other integrated processor RISC families by high speed performance, and by big deficiency. Because of this AVR RISC processors can be used instead some other 16 bit processors. In other hand AVR processors are easy to program. Let’s see why AVR from Atmel are becoming so popular: Very fast Harvard architecture. Most instructions are held in one clock cycle. AVR can be clocked in up to 16MHz so this means about 16MIPS; Not a specific feature, but AVR have internal Flash which can be reprogrammed about 1000 times without failing. According to this processors can be programmed directly in circuit without removing them off. This speeds up development of embedded applications. Integrated Processor RISC AVR command system from beginning was developed to be effectively compiled using C language. This is why compiled code for AVR is very effective than in other microcontrollers. In this case you get better performance and smaller code size. There are 32 registers in AVR. And they all directly work with processing unit. This also decreases code size and increases program performance. In other microcontrollers there…

Continue reading