2014-05-23 2 views
0

안녕하세요, 저는 Arduino를 처음 사용하고 있습니다. 1 초 지연을 만들려고하는데, delay_ms 함수를 완성하는 코드를 어떻게 생성 할 수 있는지 알지 못합니다. 그것을 해결하기 위해 시도했지만 더 나은 당신이 그냥 줄 가 {1 초 지연을 만드는 방법 Arduino

word millisecs; 
Serial.begin(9600); 
} 
void setup() asm volatile(
"   ldi r16,0x3F ; r16 = 00111111\n" 
"   out 4,r16  ; set pins 8-13 as outputs in DDRB\n" 
::: "r16"); 
millisecs = 1000;   // 1s blink delay 
Serial.begin(9600); 
} 
void loop() 
{ 
long starttime = millis();  // make a note of the start time 

asm volatile(
// jump to "blink" - ie jump around the delay_ms subroutine 
"   rjmp blink%=  ; relative jump to 'blink' \n" 
    "   ldi r16,0x3F ; r16 = 00111111\n" 
"   out 4,r16  ; set pins 8-13 as outputs in DDRB\n" 
::: "r16"); 

registers used: 
r31 - millisecond count (lo byte) 
r30 - millisecond count (hi byte) 
r17 - 100 microsecond count 
r16 - 1 microsecond count 
Overall delay (ms) = r30:r31 * r17 * r16 
---------------------------------------------------------------------*/ 
"delay_ms%=: nop     ; code to replace nop \n" 
"delay_100us%=: nop     ; code to replace nop \n" 
"delay_1us%=: nop     ; code to replace nop \n" 
"    sbiw r30,1   ; decrement ms count (r31:r30)\n" 
"    brne delay_ms%=  ; loop to delay_ms while > 0 \n" 
"    ret     ; return from subroutine  \n" 

답변

1

가 내장이

http://arduino.cc/en/Reference/delay 

을 할 수 있습니다 당신에게 무효 설치() 감사 얻을하지 않는 것 지연하는 ms 수 잠깐만.

delay(1000) 
+0

네,하지만 내장을 사용하지 않고 그것을 완성하고 싶습니다. 어떻게 delay_ms % delay_100us %와 delay_1us % 함수를 완료 할 지 알아야합니다. 고맙습니다. – user3669884

0

너무 단순한 작업을하기에는 너무 열심히 노력하고 있습니다. BlinkWithoutDelay (아래 코드)을 참조하십시오. 이 코드는 Arduino IDE를 설치할 때 설치되는 Arduino 예제의 일부이기도합니다. 좋은 학습 사례가 많이 있습니다. (오 - 코드는 VAR previousMillis에 대한 long를 사용하는 오류 (IMO)이다는 것보다 unsigned long를 사용하는...)

또한 (Timer interrupts를 사용하지만, 간단한 초 지연에 대한 옵션이 있습니다 esp. 당신이 많은 경험이 없다면), 나는 위의 방법을 제안하고 그것을 이해하기 쉬운 모든 코드로 다룰 것이다.

/* Blink without Delay 

Turns on and off a light emitting diode(LED) connected to a digital 
pin, without using the delay() function. This means that other code 
can run at the same time without being interrupted by the LED code. 

The circuit: 
* LED attached from pin 13 to ground. 
* Note: on most Arduinos, there is already an LED on the board 
that's attached to pin 13, so no hardware is needed for this example. 


created 2005 
by David A. Mellis 
modified 8 Feb 2010 
by Paul Stoffregen 

This example code is in the public domain. 


http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay 
*/ 

// constants won't change. Used here to 
// set pin numbers: 
const int ledPin = 13;  // the number of the LED pin 

// Variables will change: 
int ledState = LOW;    // ledState used to set the LED 
long previousMillis = 0;  // will store last time LED was updated 

// the follow variables is a long because the time, measured in miliseconds, 
// will quickly become a bigger number than can be stored in an int. 
long interval = 1000;   // interval at which to blink (milliseconds) 

void setup() { 
    // set the digital pin as output: 
    pinMode(ledPin, OUTPUT);  
} 

void loop() 
{ 
    // here is where you'd put code that needs to be running all the time. 

    // check to see if it's time to blink the LED; that is, if the 
    // difference between the current time and last time you blinked 
    // the LED is bigger than the interval at which you want to 
    // blink the LED. 
    unsigned long currentMillis = millis(); 

    if(currentMillis - previousMillis > interval) { 
    // save the last time you blinked the LED 
    previousMillis = currentMillis; 

    // if the LED is off turn it on and vice-versa: 
    if (ledState == LOW) 
     ledState = HIGH; 
    else 
     ledState = LOW; 

    // set the LED with the ledState of the variable: 
    digitalWrite(ledPin, ledState); 
    } 
} 
+0

네,하지만 어셈블리 언어로 깜박임 함수를 쓰고 싶습니다. 타이머를 사용하는 방법을 이미 알고 있습니다. – user3669884

+0

@ user3669884 - 그렇다면 왜 충분한 정보를 제공하지 않고 시간을 낭비하지 말 것인가? –

관련 문제