programming AVR ATMEGA328P with arduino IDE

hi! i am very new to Microcontrollers. i haven't had my hands on the arduino yet. so i had this question if it is possible to build a program in the arduino IDE and upload it to a ATMEGA328P with a usbasp?

actually there are a few libraries that i'd like to test and work with which have been written for arduino only.

i know the vice versa is possible . do i need the arduino bootloader to be installed on the atmega

With a USBasp you should "Burn Bootloader" first. That sets the programming fuses to enable the external 16 MHz crystal clock. Then you can either use the bootloader to upload or use Upload Using Programmer to upload a sketch over the bootloader.

johnwasser:
Then you can either use the bootloader to upload ...

USBasp connects via SPI, I'm not sure it can use the bootloader. Usually after I use the USBasp it prevents normal serial uploads, my guess being that it overwrites the bootloader.

Ps991:
USBasp connects via SPI, I'm not sure it can use the bootloader.

To use the bootloader you need a TTL (5V) Serial connection to the UART pins. You would not use the USBasp for that.

Ps991:
Usually after I use the USBasp it prevents normal serial uploads, my guess being that it overwrites the bootloader.

Yes. If you use the USBasp to upload a sketch you will have to burn a fresh bootloader if you want to upload a sketch using TTL Serial.

do i need the arduino bootloader to be installed on the atmega

Arduinos come with a bootloader. Are you asking about Arduinos or the Atmega328 chip itself?

hi! thanks for replying so quick.
so i finally resolved to buying a Atmega328P with bootloader preloaded onto it and make my own arduino on a perfboard with two 22pF capacitors and a 16mhz crystal oscillator. i would still want to program the arduino with a usbasp as i already have it. that should work right?
thanks!!

I think you have to make up your mind. Do you want to use the usbasp or the bootloader?

I suppose he can do both. I have an UNO of which the USB (16u2) is defect. Then I just use the USBASP to program it. After all the UNO has its 6-pin connector on it as well

nicoverduin:
I suppose he can do both. I have an UNO of which the USB (16u2) is defect. Then I just use the USBASP to program it. After all the UNO has its 6-pin connector on it as well

He CANNOT do both. Once he uploads a sketch with USBasp, the bootloader is GONE (Got that OP, GONE). You CANNOT upload using a USB port or FTDI cable / device. If you wanted to upload via USB, then you will need to burn the bootloader again using the USBasp, then you will need to upload you sketch via an FTDI device. FTDI devices are tricky because you need to add a capacitor in series between the DTR on the FTDI and RESET on the ATmega328P.

nicoverduin:
I suppose he can do both. I have an UNO of which the USB (16u2) is defect. Then I just use the USBASP to program it. After all the UNO has its 6-pin connector on it as well

Nope, he can't. He must choose one. Anything uploaded with an USBasp (or any AVR ISP) will overwrite the bootloader.

Then again, the bootloader is only required if he wants to upload using the chip's UART, and that is exactly what the bootloader is there for.

Ps991:
FTDI devices are tricky because you need to add a capacitor in series between the DTR on the FTDI and RESET on the ATmega328P.

I hardly call that tricky. A 100nF ceramic cap is pretty common and cheap, and must be placed between the USB-to-UART bridge (doesn't have to be a FTDI) DTR pin and the AVR's RST pin.

ok so arduino bootloader is not necessary for uploading arduino sketches. so if i just use a atmega328 without the bootloader . i should be able to upload sketches to the atmega328 by modifying the preferences.txt file as mentioned http://arduino.cc/en/Hacking/Programmer here right?
please note i do not have a arduino . i am building a minimal arduino uno with the crystal oscillator and 22pF capacitors.

thanks

No need to change preferences, just change the PROGRAMMER option to USBasp and upload all of your sketches using CTRL+SHIFT+U or just select the proper upload option in the drop down menu.

shubh9835:
i am building a minimal arduino uno with the crystal oscillator and 22pF capacitors.

You need a little more than that:

  • 100nF ceramic cap on VCC and GND
  • 10 kOhm resistor between RST and VCC

thanks all for solving my issue

Bump:

Could u use these with the arduino IDE?

Or do i have to get an alternate/broader IDE, such as Atmel studio or Some arduino plugin ?

https://www.fasttech.com/p/6208100

i should study a pin library, these seem to have more pins !

Yes, you should be able to use that with the IDE.

You may have to chase down a driver for the USB/Serial chip next to the USB-B connector. I can't tell what it is. It doesn't look like an Atmega16U2 (I don't believe it is available in that package) or an FTDI chip (FT232 doesn't need a crystal), for which the IDE would have driver support.

If you're unlucky, the bootloader will be screwed up, and you'll have to re-bootload it before you can use the Serial port to download code.

It will not have any more pins, just the same pins arranged differently on a 10-pin header to connect a Programmer, same function as the 6-pin ICSP header (just more grounds).

yeah just now i realized the thing has 2 clock crystals ?

Thanks for share