Welcome to Blogs @ Andrew Qu
Blog Index
All blogs
Search results

Arduino - Programming an MCU (Atmeg328p)

Summary

Once we have developed and tested a project, we need to copy the code onto a new MCU to be used on a breadboard separate from the Arduino Board. This exercise describes one way of downloading the program onto a new MCU.

Material List
  • 1 x Unprogrammed Atmega 328P-PU. No bootloader is necessary.
  • 1 x 10K resistor
  • 1 x 16MHz occillator to be used as an external clock.
  • 2 x 18~22 picofarad (ceramic) capacitors.

Setup Arduino Uno board as an ISP programmer
  1. In Arduino IDE, File -> Examples -> Arduino ISP
  2. Tools -> Board -> Arduino Uno
  3. Tools -> Programmer -> Arduino as ISP
  4. Note down serial port number (most likely COM3)
  5. Check settings as show in the screen short below. Then click [Upload].

The Arduino is now loaded a program that can act as an ISP programmer.

Wiring the H/W components


I am using a smaller breadboard and try to setup a more permanent configuration so that I can reuse more conveniently in the future. The component layout is slightly different, but the connections are the same.


Compile Sketch and Locate hex File

The following sketch lets an LED attached to pin 12 blink.

int i;
int ledpin=12;

void setup() {
  pinMode(ledpin, OUTPUT);
}

void loop() {
    for(i=0; i<2; i++)
    {
      digitalWrite(ledpin,HIGH);
      delay(250);
      digitalWrite(ledpin,LOW);
      delay(250);
    }
    delay(2500);
}

The output of compiling a sketch is a HEX file. The hex file contains machine instructions. When uploaded to an MCU's flash memory, it will start executing when powered on.

To located the output file, you need to turn on

File -> Peference -> Settings -> Show verbose output during compilation.

The you can locate the hex output file, it is in my case :
C:\Users\AndrewQu\AppData\Local\Temp\build7b27b474b023374a5b7498d9f754d24c.tmp\blink.ino.hex

Copy the fle to c:\temp\blink.ino.hex Using avrdude to Write the hex file to Atmeg328P-PU

First install the lates WinAVR package. You can download from the internet. Once installed, open a command prompt and type avrdude. If the program is installed correctly, it should display some program help information.

Turn on the wired Arduino board. Leave the Arduino IDE on and check the port number. Assume it is COM3. Type the following command:

C:\>avrdude -P COM3 -b 19200 -c avrisp -p m328p -n

avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.08s
avrdude: Device signature = 0x1e950f
avrdude: safemode: Fuses OK
avrdude done.  Thank you.

You are ready to proceed. Simply type the following command:

C:\>avrdude -P COM3 -b 19200 -c avrisp -p m328p -u -U flash:w:"c:\temp\blink.ino.hex":i

avrdude: verifying ...
avrdude: 1112 bytes of flash verified
avrdude done.  Thank you.

Now the Atmeg328 MCU is programmed with the led blink sketch.

Verifying the MCU has been programmed properbly

We'll verify that the above procedure worked and the programmed MCU can actually work. In a standalone project, we would required some sort of power supply. In this case, we'll simply use the Arduino's power supply.

Remove the programmed MCU and put onto a different bread board. Connect the crystal and capacitators as shown in Fig.1, except lines 10,11,12,13.

To connect the LED to the correct pin, we need the Arduino pin mapping as shown below:

From that, we see that we need to connect the LED to pin 18 on the Atmega328 MCU. The final connection diagram will look like in the picture below.

If the LED blinks, then we have completed the MCU programming correctly.

Step Motor Example

A step motor can precisely rotate the number of required steps (or angle). Using the Stepper.h library, it is very easy to control the motor. Below is a sample program to turn the motor half circle every 3s.

#include <Stepper.h>
#define MAX_MOTOR_STEPS 32
#define OUTPUT_STEPS_PER_REVOLUTION 2048

Stepper motor(MAX_MOTOR_STEPS, 8, 9, 10, 11);

void setup() {
  motor.setSpeed(600);  // Number of steps per minute
}

void loop() {
  motor.step(OUTPUT_STEPS_PER_REVOLUTION / 2);
  delay(3000);
}
MAX_MOTOR_STEPS - Is the motor's internal number of steps per revolution
OUTPUT_STEPS_PER_REVOLUTION - Is the output number of steps per revolution
motor.setSpeed(600) - Sets speed in number of steps per minute
motor.step(num_of_steps) - Ask the motor to turn the number of steps. +turn clockwise, -ccw.

Material List and connection diagram
  1. 1x28BYJ-48 Step motor
  2. 1xULN2003APG motor controller chip

The problem is that line colorings can be different from different suppliers. For example, I got 2 examples of different coloring below

In general, follow the pin number as markd marked on the motor in the above picture to connect to the Adruino pins. If it does not work, try different combinations. If you get the incorrect connection, it will not damage the motor. So you cna always try different combnations until it works.

 

Prev
Ads from Googlee
Dr Li Anchor Profi
www.anchorprofi.de
Engineering anchorage plate design system
©Andrew Qu, 2014. All rights reserved. Code snippets may be used "AS IS" without any kind of warranty. DIY tips may be followed at your own risk.