LoginSignup
0
0

More than 1 year has passed since last update.

STM32G031で時間関数と繰り返しタイマー割込みで遊ぶ(1秒)

Posted at

x OBでPA2をGPIOがわかる人むけ

目的
簡易な時間づくり

leafonyさんのページより引用

o_con362.jpg

o_con585.jpg

参考

STM32 MCU の Arduino IDE 設定

SER_time_test2_031_1




#include <Arduino.h>
#include <time.h>

#define LOOP_INTERVAL 1000000
#define BOARD_LED_PIN PA11

HardwareTimer *timer2 = new HardwareTimer (TIM2);

//time_t seconds = 0 + (60 * 60 * 9); // JST
time_t seconds = (60*60*24*365) - (60 * 60 * 9) ; // JST

//二桁の変換
#define DIV10(n) ((n*205)>>11)
char data1[16];
char *itoa99(int n) {


  data1[0] = '0' + DIV10(n);                        // n / 10
  data1[1] = '0' + (  n - ((data1[0] - '0') * 10) ); // n % 10
  data1[2] =  0;

  return (data1);

}//itoa99

void setup() {

  delay(3000); //not delete

  //シリアルの初期化
  Serial.setTx(PA2_ALT1);
  Serial.setHalfDuplex();
  Serial.begin(9600);

  pinMode(BOARD_LED_PIN, OUTPUT);

  timer2->setOverflow(LOOP_INTERVAL, MICROSEC_FORMAT);       // 125ms
  timer2->attachInterrupt(intTimer);
  timer2->resume();

}

struct tm *t;
struct tm tm;

void loop() {

  //t = localtime(&seconds);

  int tj = seconds;

  tm.tm_hour = ( (tj / 3600 + 9) % 24);
  tm.tm_min =  (tj / 60 % 60);
  tm.tm_sec =  (tj % 60);

  t = &tm;

  //    Serial.print(         t->tm_year + 1900 );
  //    Serial.print( itoa99( t->tm_mon + 1 ) );
  //    Serial.print( itoa99( t->tm_mday    ) );
  //    Serial.print( ' ' );
  Serial.print( itoa99( t->tm_hour    ) ); Serial.print( ':' );
  Serial.print( itoa99( t->tm_min     ) ); Serial.print( ':' );
  Serial.print( itoa99( t->tm_sec     ) );

  Serial.printf("\r\n");

  delay(1000);

}

void intTimer(void) {

  static int LEDstate = 0;
  digitalWrite( BOARD_LED_PIN, LEDstate);
  if ( 0 < LEDstate )
  {
    LEDstate = 0;
  } else {
    LEDstate = 1;
  }

  seconds++;

}



0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0