본문 바로가기
아두이노/보드

아두이노 통신 번외편 -#4

by 오징어땅콩2 2021. 11. 20.
반응형

3번까지 이해 했다면 거의 모두 이해 했다.

여기서 의문이 들어야 한다. 

정의하지 않는다고 했다.  소프트웨어 시리얼, 하드웨어 시리얼.. 두개를 정의 하지 않고 

사용자가 그냥 호출하는것만으로 결정되어 진다.

 

정의하지 않는다고 했지, 하드웨어 시리얼, 소프트웨어 시리얼 통신이라고 하지는 않았다.

그렇다. 여기서는 아무것도 정의하지 않았다. 

쉽게 말해서 모든 통신을 넣을수가 있다는것이다.

 

지금 이야기가 정확히 이해가 안 될수도 있지만, 

앞으로 I2C와 SPI를 통해서 코드를 보면서 이해 하도록 하자.

 

이전내용에 대한 전체 코드다.

 

#include <SoftwareSerial.h>
#include <LiquidCrystal_I2C.h>

SoftwareSerial sSerial= SoftwareSerial(2, 3);
//HardwareSerial&  sSerial = Serial2;
#define version 0x01
#define BAUD 9600 

typedef struct _Info
{ 
  char ver;
  char lcd_1[20+1];
  _Info()
  {
    ver = version;
    memset(lcd_1, 0x00, sizeof(char)*21);
  }
  
}Info;


//LiquidCrystal_I2C lcd(0x27,20,2);  
LiquidCrystal_I2C lcd(0x3F,20,2);  
volatile unsigned long previousMillis;

void print_lcd(Info& _info);
void read_serial(Info& _info);

void setup()
{
  Serial.begin(BAUD);
  sSerial.begin(BAUD);     
  previousMillis = millis();
   
  LCD_setup();
}

void LCD_setup()
{
  lcd.init();                      // initialize the lcd 
  // Print a message to the LCD.
  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.print("ARDUINO Start");
}

void loop() 
{          
  Info adcinfo;
  read_serial(adcinfo, sSerial);
  print_lcd(adcinfo);

  static int time=0;
  unsigned long currentMillis = millis();
  if ((currentMillis-previousMillis)> 1000*1)
  {
      time++;
      char buf2[50];
      sprintf(buf2, "time:%4d sec ", time);         
      
      lcd.setCursor(0,1);
      lcd.print(buf2);   
      previousMillis = currentMillis;
      if(time > 1000) time = 0;
  }
  
  delay(10);
}

template <class T> void read_serial(Info& _info, T& ss)
{   
    memset(&_info, 0x00, sizeof(Info)); 
    if (ss.available())
    { 
          char  c; 
          char *p;
          
          p = (char*)&_info;
          while( (p -  ((char*)&_info)) < sizeof(Info) )
          {
              if(ss.available()) 
              {
                c = ss.read();
                //*p = c; p++; 
                *(p++) = c;
                *p = '\0';                
                if (c == '\r' || c == '\n') break;
              }
          }
          _info.ver = version;
    }
    
    return ;
}

void print_lcd(Info& _info)
{
    if(_info.ver==version)
    {
        char buf2[50];
        sprintf(buf2, "receive  ====> %dbyte \n", sizeof(Info));         
        Serial.write(buf2);
        
        lcd.setCursor(0,0);
        lcd.print("                  "); 
        lcd.setCursor(0,0);
        lcd.print(_info.lcd_1);   
    }
    
   return ; 
}

'아두이노 > 보드' 카테고리의 다른 글

아두이노 통신 UNO TO UNO -#6  (0) 2021.12.04
아두이노 통신 번외편 -#3  (0) 2021.11.20
아두이노 통신 번외편 -#2  (0) 2021.11.20
아두이노 통신 번외편 -#1  (0) 2021.11.20
아두이노 통신 UNO TO UNO -#5  (0) 2021.11.16

댓글