Bare 328: DDRC=_BV(PC0) works, pinMode(PC) doesn

Hi all,

This is my first post, and my first experience with MCU’s and electronics ever. I am an experienced C# programmer though.
In other words, I am a complete newbie regarding AVR’s arduino, etc, so Please be gentile :slight_smile:

I have a ATMEGA328 on a breadboard. And a hello world app that blinks a led.
First, I ran without the arduino bootloader. My hello world app worked great.
Then, I figured how nice it would be to have the arduino bootloader.
So, I took the ATmegaBOOT_168_atmega328_pro_8MHz.hex and did some research on the correct fuse and lockbits settings. Finally I flashed the 328 with the bootloader, without any errors.
I use an USBTinyISP btw.

Next, I tried my helloworld app again, and it still worked great. Nice steady flashing led. Now I figured that the loading the bootloader really succeeded, since my nice little program still works.

The next step I took was to rewrite some code.
The original code looks like this (main.cpp)

#define F_CPU 8000000UL // 8MHz Clock

#include "wiring.h"
#include <util/delay.h>
#include "WProgram.h"
#include <SparkSoftLCD.h>

int main(void) {

      /* Must call init for arduino to work properly */
      init();

      /****************************/
      /* Add your setup code here */
      /****************************/
      DDRC = _BV(PC0); // PC0 digital output

      /****************************/
    /*** write main loop here ***/
    /****************************/

      for (;;) {
            PORTC &= ~_BV(PC0);
            _delay_ms(500);
            PORTC |= _BV(PC0);
            _delay_ms(500);

      } // end for
      return -1;
} // end main

I modified it to use the pinMode and digitalWrite functions:

#define F_CPU 8000000UL // 8MHz Clock

#include "wiring.h"
#include <util/delay.h>
#include "WProgram.h"
#include <SparkSoftLCD.h>

int main(void) {

      /* Must call init for arduino to work properly */
      init();

      /****************************/
      /* Add your setup code here */
      /****************************/
      pinMode(PC0, OUTPUT);

      /****************************/
    /*** write main loop here ***/
    /****************************/

      for (;;) {
            digitalWrite(PC0,HIGH);
            _delay_ms(500);
            digitalWrite(PC0,LOW);
            _delay_ms(500);

      } // end f

      return -1;
} // end main

Unfortunately, the led no longer flashes. I am in the dark right now. Please help!

Some additional info:

  • I am using AVR Eclipse as IDE
  • I compiled the arduino code into a library, which I reference from the program
  • I use the internal 8Mhz osc

[update 1]
read some more about the bootloader… not sure anymore if I need the bootloader in order to be able to execute the arduino library code! I guess not, since I am alsway overwriting the flash mem with my program…

In other words, I am a complete newbie regarding AVR's arduino, etc,

Why are you not starting with the Arduino IDE?

Please be gentile

You'd like us to be Christian?

I don't want to start a discussion about IDE's, but I like Eclipse more as a general development environment.

Christian? LOL any religion is fine as long as you don't kill me for asking noob questions :slight_smile:

Apart from the above, do you have anything else constructive to add to this thread? :stuck_out_tongue: :wink: Just kidding :slight_smile:

I don't want to start a discussion about IDE's, but I like Eclipse more as a general development environment.

You've proven that the hardware works. You've proven that the tool-chain works. The only thing left is this...

I compiled the arduino code into a library, which I reference from the program

Someone else will have to help. This is not something I've done.

Christian? LOL

:slight_smile:

any religion is fine as long as you don't kill me for asking noob questions

As far as I'm concerned, you're welcome here.

      pinMode(PC0, OUTPUT);
      for (;;) {
            digitalWrite(PC0,HIGH);
            _delay_ms(500);
            digitalWrite(PC0,LOW);

When you convert the code to use pinMode and digitalWrite, you'll also need to convert the arguments to the appropriate ARDUINO PIN NUMBER values, rather than leaving them as bitmasks off of portC. PC0 is going to have a value of 0 from the avr include files, but maps to "Pin 14" in the arduino world, so you want to have:

   pinMode(14, OUTPUT);
   digitalWrite(14, HIGH);
and etc...

I used the schematic from pins_arduino.c, and there it shows PC0 as pin 23 (same as in the atmel datasheet):

// ATMEL ATMEGA8 & 168 / ARDUINO
//
//                  +-\/-+
//            PC6  1|    |28  PC5 (AI 5)
//      (D 0) PD0  2|    |27  PC4 (AI 4)
//      (D 1) PD1  3|    |26  PC3 (AI 3)
//      (D 2) PD2  4|    |25  PC2 (AI 2)
// PWM+ (D 3) PD3  5|    |24  PC1 (AI 1)
//      (D 4) PD4  6|    |23  PC0 (AI 0)
//            VCC  7|    |22  GND
//            GND  8|    |21  AREF
//            PB6  9|    |20  AVCC
//            PB7 10|    |19  PB5 (D 13)
// PWM+ (D 5) PD5 11|    |18  PB4 (D 12)
// PWM+ (D 6) PD6 12|    |17  PB3 (D 11) PWM
//      (D 7) PD7 13|    |16  PB2 (D 10) PWM
//      (D 8) PB0 14|    |15  PB1 (D 9) PWM
//                  +----+
//

I have no time to test pin 14 right now, will let you know if it works.
For now, thanks a lot for the replies!!

PC0 == AVR Chip pin 23 == Arduino pin A0 == Arduino Digital Pin 14

It works!!
So, the schematic in the pins_arduino.c is should not be used for pin number. Lesson learned :slight_smile:

I found a relatively nice schematic on the arduino website:

Funny thing is, that it doesn't show pin 14. I guess I just need to continue counting so analog input 0 ==14, analog 1==15, etc.

Thanks for all the help!