[STM32 / MBED] MBED 통신

ARMmbedLogo2-1170x316.png


Serial

한 줄 요약 : 시리얼을 통하여 기본은 비동기로 하나의 디바이스와 통신한다.

예제 :

#include "mbed.h"
 
Serial pc(USBTX, USBRX); // 기본 USB를 통한 TX와 RX 통신 설정 (컴퓨터와 통신하여 확인)
 
int main() {
    pc.printf("Hello World!\n");
    while(1) {
        pc.putc(pc.getc() + 1);
    }
}

참고 주소 : https://os.mbed.com/handbook/Serial

 

SPI

한 줄 요약 : SPI를 통하여 여러 디바이스와 통신한다.

Master

예제 :

#include "mbed.h"
 
SPI spi(p5, p6, p7); // mosi, miso, sclk DigitalOut cs(p8);
 
int main() {
    // Chip must be deselected     cs = 1;
 
    // Setup the spi for 8 bit data, high steady state clock,
    // second edge capture, with a 1MHz clock rate 
    spi.format(8,3);
    spi.frequency(1000000);
 
    // Select the device by seting chip select low
    cs = 0;
 
    // Send 0x8f, the command to read the WHOAMI register
    spi.write(0x8F);
 
    // Send a dummy byte to receive the contents of the WHOAMI register
    int whoami = spi.write(0x00);
    printf("WHOAMI register = 0x%X\n", whoami);
 
    // Deselect the device
    cs = 1;
}

참고 주소 : https://os.mbed.com/handbook/SPI

Slave

예제 :

// Reply to a SPI master as slave
 
#include "mbed.h"
 
SPIslave device(p5, p6, p7, p8); // mosi, miso, sclk, ssel
 
int main() {
    device.reply(0x00);              // Prime SPI with first reply
    while(1) {
        if(device.receive()) {

            int v = device.read();   // Read byte from master
            v = (v + 1) % 0x100;     // Add one to it, modulo 256             device.reply(v);         // Make this the next reply
        }
    }
}

참고 주소 : https://mbed.org/users/mbed_official/code/mbed/docs/tip/classmbed_1_1SPISlave.html

 

I2C

한 줄 요약 : I2C을 통하여 여러 디바이스와 통신한다.

Master

예제 :

#include "mbed.h"
 
// Read temperature from LM75BD
 
I2C i2c(p28, p27);
 
const int addr = 0x90;
 
int main() {
    char cmd[2];
    while (1) {
        cmd[0] = 0x01;
        cmd[1] = 0x00;
        i2c.write(addr, cmd, 2);
 
        wait(0.5);
 
        cmd[0] = 0x00;
        i2c.write(addr, cmd, 1);
        i2c.read(addr, cmd, 2);
 
        float tmp = (float((cmd[0]<<8)|cmd[1]) / 256.0);
        printf("Temp = %.2f\n", tmp);
    }
}

참고 주소 : https://mbed.org/handbook/I2C

Slave

예제 :

// Simple I2C responder
#include "mbed.h"

I2CSlave slave(p9, p10);

int main() {
    char buf[10];
    char msg[] = "Slave!";

    slave.address(0xA0);
    while (1) {
        int i = slave.receive();
        switch (i) {
            case I2CSlave::ReadAddressed:
                slave.write(msg, strlen(msg) + 1); // Includes null char                 break;
            case I2CSlave::WriteGeneral:
                slave.read(buf, 10);
                printf("Read G: %s\n", buf);
                break;
            case I2CSlave::WriteAddressed:
                slave.read(buf, 10);
                printf("Read A: %s\n", buf);
                break;
        }
        for(int i = 0; i < 10; i++) buf[i] = 0;    // Clear buffer     }
}

참고 주소 : https://mbed.org/users/mbed_official/code/mbed/docs/tip/classmbed_1_1I2CSlave.html

 

CAN

한 줄 요약 : CAN을 통하여 여러 디바이스와 통신한다.

예제 :

#include "mbed.h"
 
Ticker ticker;
DigitalOut led1(LED1);
DigitalOut led2(LED2);
CAN can1(p9, p10);
CAN can2(p30, p29);
char counter = 0;
 
void send() {
    printf("send()\n");
    if(can1.write(CANMessage(1337, &counter, 1))) {
        printf("wloop()\n");
        counter++;
        printf("Message sent: %d\n", counter);
    } 
    led1 = !led1;
}
 
int main() {
    printf("main()\n");
    ticker.attach(&send, 1);
    CANMessage msg;
    while(1) {
        printf("loop()\n");
        if(can2.read(msg)) {
            printf("Message received: %d\n", msg.data[0]);
            led2 = !led2;
        } 
        wait(0.2);
    }
}

참고 주소 : https://mbed.org/handbook/CAN

 

Ethernet

한 줄 요약 : Ethernet을 이용하여 통신한다.

예제 :

#include "mbed.h"
 
Ethernet eth;
 
int main() {
    char buf[0x600];
 
    while(1) {
        int size = eth.receive();
        if(size > 0) {
            eth.read(buf, size);
            printf("Destination:  %02X:%02X:%02X:%02X:%02X:%02X\n",
                    buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
            printf("Source: %02X:%02X:%02X:%02X:%02X:%02X\n",
                    buf[6], buf[7], buf[8], buf[9], buf[10], buf[11]);
        }
 
        wait(1);
    }
}

참고 주소 : https://mbed.org/handbook/Ethernet

0
0
이 글을 페이스북으로 퍼가기 이 글을 트위터로 퍼가기 이 글을 카카오스토리로 퍼가기 이 글을 밴드로 퍼가기

임베디드 보드

번호 제목 글쓴이 날짜 조회수
8 STM32 / MBED [MED] Switch debouncing icon HellMaker 12-30 11,063
7 STM32 / MBED 19년 Mbed OS 동영상 강좌 List icon HelloMaker 03-11 7,868
6 STM32 / MBED MBED 수학 icon HelloMaker 03-11 7,656
5 STM32 / MBED MBED RTOS icon HelloMaker 03-11 7,320
4 STM32 / MBED MBED 파일 시스템 icon HelloMaker 03-11 7,357
3 STM32 / MBED MBED 통신 icon HelloMaker 03-10 7,851
2 STM32 / MBED MBED Timer icon HelloMaker 03-10 9,227
1 STM32 / MBED MBED 기본 구조 icon HelloMaker 03-10 6,671