hendriawan icon

ProjectsoundV1

hendriawan | PRO | 05/08/20 12:45:43 AM UTC | 0 ⭐ | 2440 👁️ | Never ⏰ | []
Arduino |

1.46 KB

|

None

|

0 👍

/

0 👎

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <avr/sleep.h>
#include <avr/pgmspace.h> 
#include "sound.h"
 
 
// create prototype function
void TimerInit();
void PWMInit();
 
// global variable here
volatile uint16_t cnt_snd=0;
 
/*
 pada mode ctc interupsi yang berpengaruh adalah compare match
 karena tcnt tidak pernah mencapai level max(0xFF) tetapi
 mencapai level TOP yaitu sama dengan nilai dari output compare
 */
 
/*
 prescaller 8 , Xtal 16MHz
 tick = 8/16Mhz= 0.5 us
 delay=counter*tick
 delay=counter*0.5us
 untuk sampling rate 8 khz maka Tsampling= 1/8Khz= 125us
 counter = 125/0.5us = 250
 */
 
ISR (TIMER1_COMPA_vect)
{
    PORTB ^= (1 << PB7); // pin 13 Arduino
    OCR3A=~pgm_read_byte(&salam_raw[cnt_snd]);
    if (cnt_snd==salam_raw_len-1) {
        cnt_snd=0;
        _delay_ms(200);
    } else {
        cnt_snd++;
    }
}
 
int main(void) {
    DDRB |=_BV(DDB7); //LED output
    TimerInit(); /// schedule 10ms
    PWMInit();
    sei();
    /// enable global interupsi
 
    while (1) {
        sleep_mode(); // sleep
    }
 
}
 
void TimerInit() {
    TCCR1B |= _BV(WGM12);  // CTC
    TCCR1B |= _BV(CS11); //prescaller 8
    OCR1A = 250 ; // delayms
    TIMSK1 |= (1 << OCIE1A); // enable compare match interrupt
}
 
void PWMInit(){
    DDRE= _BV(PE3); //activate PWM output
    TCCR3B = _BV(CS30); //prescaller 1  
    TCCR3B |= _BV(WGM32); //Fast PWM 8 Bit on Timer3
    TCCR3A |= _BV(WGM30);
    TCCR3A |= _BV(COM3A1); //non inverting
    OCR3A=128; // test PWM
}

Comments