c programm

hello everyone

i have heard that its possible to write directly in c code if you are using arduino IDE i tested it and this is my code below, the ATMEGA 2560 datasheet shows me that Pin13 is same as PORTH1 (PH1) so why is at line 3 and 6 (DDRH, PORTH and PORTH1) an error? why i cant use direct c code

i speak german too, so feel free to answer in one of the offered languages :slight_smile:

int main(void) {
char x = 1000;
DDRH &= (1<<DDH1);

while(1){
PORTH &= (1<<PORTH1);
_delay_ms(x);
PORTH &= ~(1<<PORTH1);
_delay_ms(x);
}
return 0;
}

void loop() {
// put your main code here, to run repeatedly:

}

When I try to compile your code, using 1.0.5, I get:

sketch_aug30a.ino: In function 'int main()':
sketch_aug30a.ino:5: warning: overflow in implicit constant conversion
sketch_aug30a:11: error: '_delay_ms' was not declared in this scope

How do you expect 1000 to fit in a char?

In what scope do you think _delay_ms() is declared?

hey Paul
ok, here is my improvement
_delay_ms(x) is declared in the scope of x → scope of signed short
but this is not solving my problems, right?

int main(void) {
short x = 1000;
char y = 1;
DDRH &= (1<<DDH1);

while(y){
PORTH &= (1<<PORTH1);
_delay_ms(x);
PORTH &= ~(1<<PORTH1);
_delay_ms(x);
}
return 0;
}

void loop() {
// put your main code here, to run repeatedly:

}

_delay_ms(x) is declared in the scope of x

No. It is CALLED in that scope. Where is it implemented? You can't call undefined functions.

ok here is the complete code
_delay_ms() is implemented in the delay library in the second line
it`s compiling and uploading but not working

#include <avr/io.h>
#include <util/delay.h>

int main(void) {
short x = 1000;
char y = 1;
DDRH &= (1<<DDH1);

while(y){
PORTH |= (1<<PORTH1);
_delay_ms(x);
PORTH &= ~(1<<PORTH1);
_delay_ms(x);
}
return 0;
}

void loop() {
// put your main code here, to run repeatedly:

}

One of your &= should be a |=

i improved that in the code and tried that before too, but it is still not working.
i don't know why...
it`s curious

pin13 is PH1 --> PORTH1 right?

Do you mean chip pin or board pin?
Chip pin 13 is board pin 16 (TX2).
Board pin 13 is chip pin 26 (PORTB7).

OP: Please read the two posts by Nick Gammon at the top of this Forum for guidelines on posting to the Forum, especially the use of code tags when posting source code.

oqibidipo:
Do you mean chip pin or board pin?
Chip pin 13 is board pin 16 (TX2).
Board pin 13 is chip pin 26 (PORTB7).

Maybe this could be solving my problem.
I am using pin13 on board, because there is already an integrated resistance for the LED.
if pin 13 on board is actually pin 26 or PB7 on chip then in the code i am initialising the wrong port
i will try that later if i am at home again

oqibidipo, could you tell me too where did you get the wiring diagram (board pins with chip pins)
thank you for your help

http://pighixxx.com/megapdf.pdf

buell25:
i have heard that it`s possible to write directly in c code if you are using arduino IDE

This seems to me a complete waste of time. It won't produce code that is any better than using the normal Arduino system.

...R

ok it has worked, it’s PortB7
But there is now another question.
If i start the program the LED is not blinking with its highest brightness, but if I use Arduino code it has its full power.

why?

(with my code i will break the rules again, i know)

#include <avr/io.h>
#include <util/delay.h>

int main(void) {
  short x = 1000;
  char z = 7;
  char y = 1;
  DDRB &= (1<<(DDB0+z));

  while(y){
    PORTB |= (1<<(PORTB0+z));
    _delay_ms(x);
    PORTB &=  ~(1<<(PORTB0+z));
    _delay_ms(x);
  }
   return 0;
}

void loop() {
  // put your main code here, to run repeatedly:

}

with my code i will break the rules again, i know

Then why not do it right ?

buell25:
If i start the program the LED is not blinking with its highest brightness, but if I use Arduino code it has its full power. why?

Reply #5

thank you coding badly :wink:

it has worked

here is my code:

#include <avr/io.h>
#include <util/delay.h>

int main(void) {
  const short x = 200;
  char z = 6;
  char y = 1;
  DDRB |= (1<<(DDB0+z));

  while(y){
    PORTB |= (1<<(PORTB0+z));
    _delay_ms(x);
    PORTB &=  ~(1<<(PORTB0+z));
    _delay_ms(x);
  }
   return 0;
}

void loop() {
  // put your main code here, to run repeatedly:

}

You are welcome.

Bear in mind this about _delay_ms…

In order for these functions to work as intended, compiler optimizations must be enabled, and the delay time must be an expression that is a known constant at compile-time. If these requirements are not met, the resulting delay will be much longer (and basically unpredictable), and applications that otherwise do not use floating-point calculations will experience severe code bloat by the floating-point library routines linked into the application.

This should eliminate any potential problem…

...
  const short x = 200;
...

What is the point of this? Your code now works on one of the dozen Arduino models. Using pinMode() and digitalWrite() it would work on ALL Arduinos, AND changing the pin number would be trivial. Try changing yours to pin 3, instead. How much effort is that?

I'd have the LED blinking on pin 3 before you got the datasheet opened.

PaulS:
I'd have the LED blinking on pin 3 before you got the datasheet opened.

hahaha :wink:

You are right, but i only wanted to test. :slight_smile:
it worked
i mean i know what you want to say, but if i would write own functions for example like initLED(char x) or setLED(char x) i would maybe as fast as you using Arduino functions.
but it wouldn't make any sense writing functions in arduino, i know i know.. :stuck_out_tongue_winking_eye:

while(1){
    PORTH &= (1<<PORTH1);
    _delay_ms(x);
    PORTH &=  ~(1<<PORTH1);
    _delay_ms(x);
  }

In line 2, above, don’t you mean:

while(1){
    PORTH |= (1<<PORTH1);
    _delay_ms(x);
    PORTH &=  ~(1<<PORTH1);
    _delay_ms(x);
  }