serial 출력
arduino
void setup() {
Serial.begin(9600); //데이터 전송속도(baud rate)설정
}
void loop() {
int i;
while(1) {
Serial.write("hello\n");
delay(5000);
}
}
avr(USART 통신 공부 필요)
#ifndef F_CPU
#define F_CPU 8000000UL
#endif
#ifndef BAUD
#define BAUD 9600
#endif //데이터 전송속도
#include <stdio.h>
#include <avr/io.h>
#include <util/delay.h>
#include <util/setbaud.h> //보오레이트 계산에 필요한 헤더파일
//UBRRH_VALUE, UBRRL_VALUE, USE_2X를 계산해서 상수로 쓸수있게 해줌
void uart_init() {
// Upper and lower bytes of the calculated prescaler value for baud.
UBRR0H = UBRRH_VALUE;
UBRR0L = UBRRL_VALUE;
// Configure data frame size to 8-bits.
UCSR0C = _BV(UCSZ01) | _BV(UCSZ00);
// Configure to enable transmitter.
UCSR0B = _BV(TXEN0); //()안의 비트를 1로 set == (1<<TXEN0)
}
void uart_putchar(char c) {
// Wait until the register to write to is free.
loop_until_bit_is_set(UCSR0A, UDRE0);
// Write the byte to the register.
UDR0 = c;
}
void uart_putstr(char *data) {
// Loop until end of string writing char by char.
while(*data){
uart_putchar(*data++);
}
}
int main() {
uart_init();
while(1) {
uart_putstr("hello\n");
_delay_ms(5000);
}
return 0;
}
스위치 누르기전 0 누르면 1출력
arduino
int btnPin=2;
int btnState=LOW;
void setup()
{
Serial.begin(115200);
pinMode(btnPin,INPUT);
}
void loop()
{
btnState = digitalRead(btnPin);
Serial.println(btnState);
delay(1000);
}
avr
#ifndef F_CPU
#define F_CPU 8000000UL
#endif
#ifndef BAUD
#define BAUD 9600
#endif //데이터 전송속도
#include <stdio.h>
#include <avr/io.h>
#include <util/delay.h>
#include <util/setbaud.h> //보오레이트 계산에 필요한 헤더파일
//UBRRH_VALUE, UBRRL_VALUE, USE_2X를 계산해서 상수로 쓸수있게 해줌
void uart_init() {
// Upper and lower bytes of the calculated prescaler value for baud.
UBRR0H = UBRRH_VALUE;
UBRR0L = UBRRL_VALUE;
// Configure data frame size to 8-bits.
UCSR0C = _BV(UCSZ01) | _BV(UCSZ00);
// Configure to enable transmitter.
UCSR0B = _BV(TXEN0); //()안의 비트를 1로 set == (1<<TXEN0)
}
void uart_putchar(char c) {
// Wait until the register to write to is free.
loop_until_bit_is_set(UCSR0A, UDRE0);
// Write the byte to the register.
UDR0 = c;
}
void uart_putstr(char *data) {
// Loop until end of string writing char by char.
while(*data){
uart_putchar(*data++);
}
}
int main() {
DDRD = 0x00; //(PD2)입력설정
uart_init();
while(1) {
if((PIND&0x04) != 0)
{
uart_putstr("1\n");
_delay_ms(1000);
}
else
{
uart_putstr("0\n");
_delay_ms(1000);
}
}
return 0;
}
'arduino,avr' 카테고리의 다른 글
Atmega 2560(아두이노 메가) pin map, 기본 포트설정 (0) | 2022.04.02 |
---|---|
adc (0) | 2022.02.24 |
주사위led 연습문제 (0) | 2022.02.23 |
아두이노 기초 : L LED점멸 (0) | 2022.02.21 |
Arduino uno (ATMEGA328P)pin map (0) | 2022.02.21 |