stm32f4-discoveryの作法 その12

#概要

stm32f4-discoveryの作法、調べてみた。
AMラジオを鳴らしてみた。

#写真

![CIMG2850.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/773707ec-d6e5-e904-43ab-64bfb953ab1a.jpeg)


#サンプルコード

```
#include "stm32f4_discovery.h"

volatile uint64_t ksystick;
uint64_t micros(void)
{
	return ksystick;
}
void SysTick_Handler(void)
{
	ksystick++;
}
void delaym(__IO uint32_t n)
{
	uint64_t p;
	p = micros() + n;
	while (p > micros())
	{
	}
}
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
#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 END		0x00
#define OFF		0x01
#define T03		3
#define T06		6
#define T09		9
#define NOTE		32
int radioPin = 11;
const unsigned char musicData[] = {
	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,
	OFF, 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,
	OFF,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,
	OFF, 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,
	END,T03
};
void playTone(uint8_t tone, uint8_t tempo)
{
	int i,
		hz,
		itone;
	switch (tone)
	{
	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 ((tone == OFF) || (tone == END))
		{
			delaym(1000000 / NOTE);
		}
		else
		{
			for (i = 0; i < hz; i++)
			{
				TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable;
				TIM_OC1Init(TIM3, &TIM_OCInitStructure);
				delaym(itone);
				TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
				TIM_OC1Init(TIM3, &TIM_OCInitStructure);
				delaym(itone);
			}
		}
	}
	delaym(50000);
}
void loop()
{
	unsigned int i;
	uint8_t tone,
		tempo;
	i = 0;
	tone = OFF;
	while (tone != END)
	{
		tone = musicData[i];
		i++;
		tempo = musicData[i];
		i++;
		playTone(tone, tempo);
	}
}
int main(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_Init(GPIOC, &GPIO_InitStructure); 
	GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM3);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
	TIM_TimeBaseStructure.TIM_Period = 12 - 1;
	TIM_TimeBaseStructure.TIM_Prescaler = 5 - 1;
	TIM_TimeBaseStructure.TIM_ClockDivision = 0;
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
	TIM_OCInitStructure.TIM_Pulse = 1;
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
	TIM_OC1Init(TIM3, &TIM_OCInitStructure);
	TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Disable);
	TIM_Cmd(TIM3, ENABLE);
	RCC_ClocksTypeDef RCC_Clocks;
	RCC_GetClocksFreq(&RCC_Clocks);
	SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000000);
	loop();
}


```


以上。