Skip to content

Instantly share code, notes, and snippets.

@SzShow
Created November 8, 2019 09:29
Show Gist options
  • Save SzShow/d24a1bd7c7f269a94882d9316d92a3fa to your computer and use it in GitHub Desktop.
Save SzShow/d24a1bd7c7f269a94882d9316d92a3fa to your computer and use it in GitHub Desktop.
LED点滅生成用のコード
#include <avr/io.h>
#include <stdio.h>
#include <avr/sleep.h>
#include <avr/interrupt.h>
#include <math.h>
#define F_CPU 20000000UL
//#define INTERVAL_TIME 1 //ms
#define CENTRAL_FREQ 8 //Hz
#define MODULATION_FREQ 1//Hz
#define MODULATION_DEPTH 1
unsigned int _segment=0;
float _modArray[1000] = {}; //追加したfloat配列(4Byte*1000 = 4KB)
ISR(TIMER0_COMPA_vect){
_segment++;
float t = _segment*0.001;
float modulationWave=MODULATION_DEPTH*sin(2*M_PI*MODULATION_FREQ*t);
float val=cos((2*M_PI*CENTRAL_FREQ*t)+modulationWave);
if(val>0.95){
PORTB=0xff;
PORTD=0x01;
}else{
PORTB=0x00;
PORTD=0x00;
}
if(_segment>=1000){
_segment=0;
}
}
int main(void)
{
_modArray[1] = 0; // 配列の使用が確定した時点でコンパイラがメモリ・オーバーフローを吐く
TCCR0A= 0b00000010;
TCCR0B = 0b00000100;
OCR0A = 77;
TIMSK0 = 0b00000010;
DDRB=0xff;//点滅コマンド
DDRD=0xff;//トリガ
PORTB=0xff;
PORTD=0x00;
sei();
/* Replace with your application code */
while (1)
{
;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment