728x90

아두이노의 Serial.write() 함수는  Serial 포트에 하나의 바이트 또는 그 이상의 바이트를 전송하는데 사용합니다. 주로 데이터 통신을 구현할 때 사용되며 Serial.print() 함수와 구분되어 사용합니다.

Serial.write() 함수는 숫자를 전송할 때 숫자 있는 그대로 전송합니다. 이는 숫자를 ASCII 코드로 변환하여 전송하는 Serial.print() 함수와 구분되며 일부 매개변수 입력에도 차이가 있습니다.

 

Serial.write()

 

Serial.write(val);
Serial.write(str);
Serial.write(buf, len);

매개변수(Parameters)
val : 1 바이트 데이터(0x00 ~ 0xFF)
str : 문자열(ex ‘A’, “Hello”)
buf : byte형 배열에 저장된 데이터, len 만큼 바이트 전송

반환값(Return)
size_t: write() : 전송한 byte 수

 

 

byte buff[10] = { 64, 'A', 0x42, 67, 68, 69, 70, 71, 72, 73};
 
void setup() {
  Serial.begin(115200);
}
 
void loop() {
  Serial.write(64);
  Serial.write("abc");
  Serial.write(buff, 5);
  Serial.println(" ");
  delay(1000);
}

위 예제를 아두이노 보드에 업로드하고 시리얼 모니터로 실행 결과를 보면 “@abc$ABCD” 이 반복적으로 출력될 것입니다.

하나씩 살펴보면

시리얼 모니터에서 수신된 <64>를 ASCII 값으로 판단하고 이에 해당하는 문자 ‘@’을 출력합니다. 문자열은 그대로 출력하여 “abc”가 이어서 출력되고 byte형 배열 중 5개의 데이터는 숫자는 ASCII 문자로, 문자는 문자로 출력합니다.

<64> → ‘@’
“abc” → “abc”
<36> → ‘$’
‘A’ → ‘A’
<0x42> → ‘B’
<67> → ‘C’
<68> → ‘D’

 

참고

아두이노 시리얼 모니터는 수신된 데이터를 ASCII 코드의 문자로 변화하여 출력합니다.  raw 데이터를 확인하기 위해서는 헥사(16진수) 값 등을 확인할 수 있는 별도의 시리얼 모니터 프로그램을 사용하셔야 합니다.

'홍익인간 프로젝트 > Arduino C' 카테고리의 다른 글

LiquidCrystal and Adafruit SSD1306(SSH1102) Collison  (0) 2021.03.19
delayMicroseconds()  (0) 2021.03.11
pulseIn() Function  (0) 2021.01.22
pinMode() Function  (0) 2021.01.22
What's means F() function ?  (0) 2021.01.14
728x90

[Advanced I/O]

Description

Reads a pulse (either HIGH or LOW) on a pin. For example, if value is HIGH, pulseIn() waits for the pin to go from LOW to HIGH, starts timing, then waits for the pin to go LOW and stops timing. Returns the length of the pulse in microseconds or gives up and returns 0 if no complete pulse was received within the timeout.

The timing of this function has been determined empirically and will probably show errors in longer pulses. Works on pulses from 10 microseconds to 3 minutes in length.

Syntax

pulseIn(pin, value)
pulseIn(pin, value, timeout)

Parameters

pin: the number of the Arduino pin on which you want to read the pulse. Allowed data types: int.
value: type of pulse to read: either HIGH or LOW. Allowed data types: int.
timeout (optional): the number of microseconds to wait for the pulse to start; default is one second. Allowed data types: unsigned long.

Returns

The length of the pulse (in microseconds) or 0 if no pulse started before the timeout. Data type: unsigned long.

Example Code

The example prints the time duration of a pulse on pin 7.

int pin = 7;
unsigned long duration;

void setup() {
  Serial.begin(9600);
  pinMode(pin, INPUT);
}

void loop() {
  duration = pulseIn(pin, HIGH);
  Serial.println(duration);
}

'홍익인간 프로젝트 > Arduino C' 카테고리의 다른 글

delayMicroseconds()  (0) 2021.03.11
[아두이노 레퍼런스] Serial.write() 함수  (0) 2021.01.31
pinMode() Function  (0) 2021.01.22
What's means F() function ?  (0) 2021.01.14
u8g2setupcpp  (0) 2020.12.25
728x90

[Digital I/O]

Description

Configures the specified pin to behave either as an input or an output. See the Digital Pins page for details on the functionality of the pins.

As of Arduino 1.0.1, it is possible to enable the internal pullup resistors with the mode INPUT_PULLUP. Additionally, the INPUT mode explicitly disables the internal pullups.

Syntax

pinMode(pin, mode)

Parameters

pin: the Arduino pin number to set the mode of.
mode: INPUT, OUTPUT, or INPUT_PULLUP. See the Digital Pins page for a more complete description of the functionality.

Returns

Nothing

Example Code

void setup() {
  pinMode(13, OUTPUT);    // sets the digital pin 13 as output
}

void loop() {
  digitalWrite(13, HIGH); // sets the digital pin 13 on
  delay(1000);            // waits for a second
  digitalWrite(13, LOW);  // sets the digital pin 13 off
  delay(1000);            // waits for a second
}

Notes and Warnings

The analog input pins can be used as digital pins, referred to as A0, A1, etc.

See also

728x90

Serial.print(F(“Hello World”));

Oct 06, 2016, 03:38 pm

I found this comment on Serial.print:

You can pass flash-memory based strings to Serial.print() by wrapping them with F(). For example :

Serial.print(F("Hello World"))

What does it mean to pass flash-memory based strings?

 

 

Re: Serial.print(F(“Hello World”));

#1

Oct 06, 2016, 03:39 pm

It means the string isn't using up RAM.

 

 

 

Re: Serial.print(F(“Hello World”));

#2

Oct 06, 2016, 03:59 pm

When you compile your program it says how much program memory (stored in flash) you are using and how much dynamic ram you are using.

If you use F() you can move constant strings to the program memory instead of the ram. This will take up space that will decrease the amount of other code you can write. But it will free up dynamic ram.

My chip has 32KB of Flash memory (2Kb taken up by bootloader) and 2KB or RAM. Sometimes I need to store lots of data in RAM so I move the constant strings to Flash memory. Sometimes my program has a lot of steps and I have a shortage of Flash memory so I leave the strings in RAM.

mrburnette

 

 

 

 

#3

Oct 06, 2016, 07:52 pm Last Edit: Oct 06, 2016, 07:55 pm by mrburnette

Quote

If you use F() you can move constant strings to the program memory instead of the ram.

Actually what happens in a Harvard architecture uC is that the compiled string stays in flash and does not get copied to SRAM during the C++ initialization that happens before your sketch receives run control.

Since the string is not moved to SRAM, it has the PROGMEM property and runs from flash.

 

 

Re: Serial.print(F(“Hello World”));

#4

Oct 06, 2016, 08:08 pm

Quote from: CaverAdam on Oct 06, 2016, 03:59 pm

Sometimes my program has a lot of steps and I have a shortage of Flash memory so I leave the strings in RAM.

That's not how it works.

Your string constants are ALWAYS in flash memory. They have to be, because when you take power away from RAM everything's gone. When you put power back on again, they have to be reloaded from somewhere, and that somewhere is the flash memory. Leaving off PROGMEM will do nothing to save you significant amounts of flash memory.

It's just that a normal part of initializing a C++ program is to load those arrays into SRAM before setup(). PROGMEM is an AVR-specific attribute that can be applied to variables that tells the compiler to not do that. Because flash is in a different memory space than RAM, special functions are needed to access from flash than from RAM.

Hackaday: https://hackaday.io/MarkRD
Advanced C++ Techniques: https://forum.arduino.cc/index.php?topic=493075.0

'홍익인간 프로젝트 > Arduino C' 카테고리의 다른 글

pulseIn() Function  (0) 2021.01.22
pinMode() Function  (0) 2021.01.22
u8g2setupcpp  (0) 2020.12.25
PCF8574 I2C 확장보드로 CLCD, Text(텍스트) LCD 구동 (아두이노)  (0) 2020.12.25
LiquidCrystal I2C - 3 Arguments Type  (0) 2020.12.25

+ Recent posts