Week8

Embedded Programming


Group Assignment Link

  • Compare the performance and development workflows for different microcontroller families
  • Document your work (in a group or individually)

  • Assignment

  • Read the datasheet for the microcontroller you are programming
  • Program the board you have made to do something, with as many different programming languages
  • languages and programming environments as possible


  • I will programming the Attiny1614 board that i made

    [Window OS]+[Arduino IDE]+[c/c++]+[UPDI]+[Attiny1614]
    I need to check the Datasheet and select the appropriate MCU for my development board
    Datasheet allows you to select the MCU for your board

    [Attiny1614 Datasheet Link]



    I checked the drawing package on the data sheet.
    I chose attiny1614 because it is easy to soldering because of the wide space between launches



    Absolute Maximum Ratings
    I've confirmed that we can't give you more than 6v of voltage,
    and we can only allow up to 40 mA of each pin



     PORT Function Multiplexing
    We can confirm that PA0 is UPDI.
    You can check which TX,RX pin can be used instead of PA1,PA2

    Attiny 1614 pinout

    All I/O pins can be configured with internal pullup resistance
    UPDI is used for the next generation Attiny MCU and is convenient because it can be coded with fewer pins than isp
    It takes a while to load the program, but with just one pin and GND, you can program it
    pin11 is UPDI
    pin4, 5 > FTDI
    pin7, 6 > Dust sensor
    pin8, 9 > Humidifier
    pin 0,1,2,3 is left for for LED

    The Beginning of Arduino

    1.pre-processor
    A sentence that is pre-processed before compilation
    Used to make source programs easy to change and compile
    #include, #define, #error, #if, etc
    2.setup()
    The part where data is initialized
    In the setup() function, the statement runs only once at first start,
    so it usually writes commands such as setting up Arduino hardware,
    resetting sketch variables, and declaring variables
    3.loop()
    Function that executes a statement over and over again
    The command statement within the loop() function is a function that runs from beginning to end unless it is stopped or de-energized,
    so the code that controls the actual behavior of the aduino is written in this loop() function

    pinMode (pin, INPUT or OUTPUT);
    Pin mode is a function that informs the compiler whether to use each pin of the input-output pin (0-13) as input or output

    digitalWrite (pin, HIGH or LOW);
    It is a function that outputs a status value on a set pin when the input-output pin (0-13) is defined as an output

    delay (time);
    a function that delays time by a certain amount of time

    Serial.begin (communication speed);
    Announce the beginning of serial communication
    The parameter sets the transmission speed for serial communication at what bps (usually 9600)

    Serial.println (output value);
    Outputs data for serial communications

    Reference Arduino example code




    Use UPDI
    In order to use UPDI, you need to turn the Arduino Uno Board into an UPDI programmer
    First download this file [UPDi - jtag2updi.ino File]


    After connecting Arduino Uno to the computer
    Open the file above and upload it to the Uno board


    Search and install mega tiny core in the board manager


    Seting Board Attiny 1614
    Chip attiny 1614
    programmer jtag2updi
    I connect 6 pin of Arduino Uno and the updi on my board and program my board
    Burn the bootroder before that
    The Arduino code below was used in my final project
    There are three example codes for each device
    and Aduino example code control > ifstatement code is applied together
    The file is at the bottom
    Week 9 Dust sensor code
    Week 11 Humidifier code
    Arduino-ples-05.Control-IfStatementConditional/

    //FA20 hyunho
    
    #include <Adafruit_NeoPixel.h>
    #ifdef __AVR__
    #include <avr/power.h>      // Required for 16 MHz Adafruit Trinket
    #endif
    
    
    #define NUM_LEDS 1
    #define DATA_PIN 0
    #define HUMIDIFIER 9
    
    
    Adafruit_NeoPixel pixels(NUM_LEDS, DATA_PIN, NEO_GRB + NEO_KHZ800);
    
    #define DELAYVAL 500      // Time (in milliseconds) to pause between pixels
    
    #include <Arduino.h>
    #include <SoftwareSerial.h>
    //#include <FastLED.h>
    #define LENG 31           //0x42 + 31 bytes equal to 32 bytes
    unsigned char buf[LENG];
    
    int PM01Value=0;          //define PM1.0 value of the air detector module
    int PM2_5Value=0;         //define PM2.5 value of the air detector module
    int PM10Value=0;         //define PM10 value of the air detector module
    
    int neor=255;
    int neog=255;
    int neob=255;
    
    static int maxdust = 50;
    static int mindust = 0;
    
    SoftwareSerial PMSerial(6, 7); // RX, TX
    
    void setup()
    {
      pinMode(HUMIDIFIER, OUTPUT);
      pixels.begin();       // INITIALIZE NeoPixel strip object (REQUIRED)
    
    
    
      
      PMSerial.begin(9600);
      PMSerial.setTimeout(1500);
      pixels.setPixelColor(0, pixels.Color(255, 255, 255));
      pixels.show();
      digitalWrite(HUMIDIFIER, HIGH);
      delay(3000);
      pixels.setPixelColor(0, pixels.Color(0, 0, 0));
      pixels.show();
      digitalWrite(HUMIDIFIER, LOW);
    }
    
    void loop()
    {
    
    //Serial.print("loop:");
    //unsigned int uptime = millis();
    //Serial.println(uptime);
      measuredust();
      int firstr=255;
      int firstg=120;
      int firstb=0;
      //ledgo();
    
      if (PM2_5Value < mindust)
      {PM2_5Value = mindust;}
      if (PM2_5Value > maxdust)
      {PM2_5Value = maxdust;}
      firstr= map(PM2_5Value, mindust, maxdust, 255, 250);
      firstg= map(PM2_5Value, mindust, maxdust, 255, 120);
      firstb= map(PM2_5Value, mindust, maxdust, 255, 0);
    
      if (PM2_5Value = maxdust)
      { digitalWrite(HUMIDIFIER, HIGH); }
      else { digitalWrite(HUMIDIFIER, LOW); }             
                         
        if(firstr < neor){
          neor = neor - 1;
      } else {  
          neor = neor + 1;
    
          
      }
      
       if(firstg < neog){
          neog = neog - 1;
      } else {  
          neog = neog + 1;
      }
    
       if(firstb < neob){
          neob = neob - 1;
      } else {  
          neob = neob + 1;
      }
    
      pixels.setPixelColor(0, pixels.Color(neor, neog, neob));
    
      pixels.show();
      delay(10);
    }
    
    
    void measuredust(){
      
    
    
      static unsigned long OledTimer=millis();
        if (millis() - OledTimer >=10000)
        {
          OledTimer=millis();
      if(PMSerial.find(0x42)){
        PMSerial.readBytes(buf,LENG);
    
        if(buf[0] == 0x4d){
          if(checkValue(buf,LENG)){
            PM01Value=transmitPM01(buf); //count PM1.0 value of the air detector module
            PM2_5Value=transmitPM2_5(buf);//count PM2.5 value of the air detector module
            PM10Value=transmitPM10(buf); //count PM10 value of the air detector module
           }
         }
       }
     }
    
    }
    char checkValue(unsigned char *thebuf, char leng)
    {
      char receiveflag=0;
      int receiveSum=0;
    
      for(int i=0; i<(leng-2); i++){
      receiveSum=receiveSum+thebuf[i];
      }
      receiveSum=receiveSum + 0x42;
    
      if(receiveSum == ((thebuf[leng-2]<<8)+thebuf[leng-1]))  //check the serial data
      {
        receiveSum = 0;
        receiveflag = 1;
      }
      return receiveflag;
    }
    
    int transmitPM01(unsigned char *thebuf)
    {
      int PM01Val;
      PM01Val=((thebuf[3]<<8) + thebuf[4]); //count PM1.0 value of the air detector module
      return PM01Val;
    }
    
    //transmit PM Value to PC
    int transmitPM2_5(unsigned char *thebuf)
    {
      int PM2_5Val;
      PM2_5Val=((thebuf[5]<<8) + thebuf[6]);//count PM2.5 value of the air detector module
      return PM2_5Val;
      }
    
    //transmit PM Value to PC
    int transmitPM10(unsigned char *thebuf)
    {
      int PM10Val;
      PM10Val=((thebuf[7]<<8) + thebuf[8]); //count PM10 value of the air detector module
      return PM10Val;
    }
    



    Credit for this code
    Dust sensor - Zuyang @ HUST
    LED - NeoPixle Example code
    Humidifier - xiaohe

    The above board and code were used for my final project!



    File
    finalcode.ino