0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ArduinoAdvent Calendar 2021

Day 16

arduinoでAMラジオを鳴らす その2

Posted at

#概要

arduinoでAMラジオを鳴らしてみた。
wemos d1で、やってみた。

#配線

D9に、アンテナ用に、リード線をつなぐ。

#サンプルコード

resetが、かかる。

volatile int v;

#define CLOCK	   (1000000/8)
#define UA		  	1
#define SILO		2
#define DO		  	3
#define DOSH		4
#define RE		  	5
#define RESH		6
#define MI		  	7
#define FA		  	8
#define FASH		9
#define SO		  	10
#define SOSH		11
#define LA		  	12
#define SIFL		13
#define SI		  	14
#define DOHI		15
#define DOHS		16
#define REHI		17
#define REHS		18
#define MIHI		19
#define FAHI		20
#define FAHS		21
#define END0		0x30
#define OFF0		0x31
#define T03		 	3
#define T06		 	6
#define T09		 	9
#define NOTE		32

byte musicData[200] = {
	DO, T03,
	FA, T03,
	FA, T09,
	FA, T03,
	MI, T03,
	RE, T03,
	DO, T03,
	RE, T09,
	DO, T03,
	DO, T03,
	UA, T03,
	DO, T03,
	RE, T06,
	RE, T03,
	RE, T03,
	RE, T03,
	FA, T03,
	RE, T03,
	MI, T06,
	UA, T03,
	OFF0, T03,
	UA, T03,
	DO, T03,
	RE, T06,
	RE, T03,
	FA, T03,
	SO, T06,
	FA, T03,
	MI, T03,
	FA, T06,
	MI, T03,
	RE, T03,
	RE, T06,
	DO, T03,
	RE, T06,
	RE, T03,
	RE, T03,
	LA, T03,
	FA, T03,
	LA, T03,
	SO, T09,
	OFF0, T03,
	DO, T03,
	FA, T09,
	FA, T03,
	FA, T03,
	MI, T03,
	RE, T03,
	DO, T03,
	RE, T09,
	DO, T03,
	DO, T03,
	UA, T06,
	DO, T03,
	RE, T03,
	RE, T06,
	RE, T03,
	RE, T03,
	FA, T03,
	RE, T03,
	MI, T03,
	UA, T06,
	OFF0, T03,
	UA, T03,
	DO, T03,
	RE, T06,
	RE, T03,
	FA, T03,
	SO, T06,
	FA, T03,
	MI, T03,
	FA, T06,
	MI, T03,
	RE, T03,
	RE, T06,
	DO, T03,
	RE, T06,
	RE, T03,
	RE, T03,
	LA, T03,
	FA, T03,
	LA, T03,
	SO, T09,
	END0, T03,
	END0, T03
};
void playTone(byte tone0, byte tempo) {
	int i,
		hz,
		itone;
	switch (tone0)
	{
	case UA:
		hz = 466 / NOTE;
		itone = ((int) (CLOCK / 466)) << 3;
	break;
	case SILO:
		hz = 493 / NOTE;
		itone = ((int) (CLOCK / 493)) << 3;
	break;
	case DO:
		hz = 523 / NOTE;
		itone = ((int) (CLOCK / 523)) << 3;
	break;
	case DOSH:
		hz = 554 / NOTE;
		itone = ((int) (CLOCK / 554)) << 3;
	break;
	case RE:
		hz = 587 / NOTE;
		itone = ((int) (CLOCK / 587)) << 3;
	break;
	case RESH:
		hz = 622 / NOTE;
		itone = ((int) (CLOCK / 622)) << 3;
	break;
	case MI:
		hz = 659 / NOTE;
		itone = ((int) (CLOCK / 659)) << 3;
	break;
	case FA:
		hz = 698 / NOTE;
		itone = ((int) (CLOCK / 698)) << 3;
	break;
	case FASH:
		hz = 739 / NOTE;
		itone = ((int) (CLOCK / 739)) << 3;
	break;
	case SO:
		hz = 783 / NOTE;
		itone = ((int) (CLOCK / 783)) << 3;
	break;
	case SOSH:
		hz = 830 / NOTE;
		itone = ((int) (CLOCK / 830)) << 3;
	break;
	case LA:
		hz = 880 / NOTE;
		itone = ((int) (CLOCK / 880)) << 3;
	break;
	case SIFL:
		hz = 932 / NOTE;
		itone = ((int) (CLOCK / 932)) << 3;
	break;
	case SI:
		hz = 987 / NOTE;
		itone = ((int) (CLOCK / 987)) << 3;
	break;
	case DOHI:
		hz = 1046 / NOTE;
		itone = ((int) (CLOCK / 1046)) << 3;
	break;
	case DOHS:
		hz = 1108 / NOTE;
		itone = ((int) (CLOCK / 1108)) << 3;
	break;
	case REHI:
		hz = 1174 / NOTE;
		itone = ((int) (CLOCK / 1174)) << 3;
	break;
	case REHS:
		hz = 1244 / NOTE;
		itone = ((int) (CLOCK / 1244)) << 3;
	break;
	case MIHI:
		hz = 1318 / NOTE;
		itone = ((int) (CLOCK / 1318)) << 3;
	break;
	case FAHI:
		hz = 1396 / NOTE;
		itone = ((int) (CLOCK / 1396)) << 3;
	break;
	case FAHS:
		hz = 1480 / NOTE;
		itone = ((int) (CLOCK / 1480)) << 3;
	break;
	default:
		hz = 880 / NOTE;
		itone = ((int) (CLOCK / 880)) << 3;
	break;
	}
	for ( ; tempo > 0; tempo--)
	{
		if ((tone0 == OFF0) || (tone0 == END0))
		{
			delayMicroseconds(NOTE * 1000);
		}
		else
		{
			for (i = 0; i < hz; i++)
			{
				pinMode(LED_BUILTIN, INPUT);
				delayMicroseconds(itone);
				pinMode(LED_BUILTIN, OUTPUT);
				delayMicroseconds(itone);
			}
		}
	}
	delayMicroseconds(50000);
}
void isr0(void) {
	if (v == 0)
	{
		GPOC = 1 << 2;
		v = 1;
	}
	else
	{
		GPOS = 1 << 2;
		v = 0;
	}
	timer0_write(ESP.getCycleCount() + 1000L);
}

void setup() {
	pinMode(LED_BUILTIN, OUTPUT);
	digitalWrite(LED_BUILTIN, HIGH);
	Serial.begin(9600);
	while (!Serial)
	{
		;
	}
	Serial.println("start");
	noInterrupts();
	timer0_isr_init();
	timer0_attachInterrupt(isr0);
	timer0_write(ESP.getCycleCount() + 4000L);
	interrupts();
	ESP.wdtDisable();
	Serial.println("ok0");
}
void loop() {
	int i;
	byte tone0,
		tempo;
	for (i = 0; i < 80; i++)
	{
		tone0 = musicData[i * 2];
		tempo = musicData[i * 2 + 1];
		playTone(tone0, tempo);
	}
  	//yield();
}





以上。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?