hendriawan

ProjectsoundV1

May 7th, 2020
2,530
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <avr/io.h>
  2. #include <avr/interrupt.h>
  3. #include <util/delay.h>
  4. #include <avr/sleep.h>
  5. #include <avr/pgmspace.h>
  6. #include "sound.h"
  7.  
  8.  
  9. // create prototype function
  10. void TimerInit();
  11. void PWMInit();
  12.  
  13. // global variable here
  14. volatile uint16_t cnt_snd=0;
  15.  
  16. /*
  17.  pada mode ctc interupsi yang berpengaruh adalah compare match
  18.  karena tcnt tidak pernah mencapai level max(0xFF) tetapi
  19.  mencapai level TOP yaitu sama dengan nilai dari output compare
  20.  */
  21.  
  22. /*
  23.  prescaller 8 , Xtal 16MHz
  24.  tick = 8/16Mhz= 0.5 us
  25.  delay=counter*tick
  26.  delay=counter*0.5us
  27.  untuk sampling rate 8 khz maka Tsampling= 1/8Khz= 125us
  28.  counter = 125/0.5us = 250
  29.  */
  30.  
  31. ISR (TIMER1_COMPA_vect)
  32. {
  33.     PORTB ^= (1 << PB7); // pin 13 Arduino
  34.     OCR3A=~pgm_read_byte(&salam_raw[cnt_snd]);
  35.     if (cnt_snd==salam_raw_len-1) {
  36.         cnt_snd=0;
  37.         _delay_ms(200);
  38.     } else {
  39.         cnt_snd++;
  40.     }
  41. }
  42.  
  43. int main(void) {
  44.     DDRB |=_BV(DDB7); //LED output
  45.     TimerInit(); /// schedule 10ms
  46.     PWMInit();
  47.     sei();
  48.     /// enable global interupsi
  49.  
  50.     while (1) {
  51.         sleep_mode(); // sleep
  52.     }
  53.  
  54. }
  55.  
  56. void TimerInit() {
  57.     TCCR1B |= _BV(WGM12);  // CTC
  58.     TCCR1B |= _BV(CS11); //prescaller 8
  59.     OCR1A = 250 ; // delayms
  60.     TIMSK1 |= (1 << OCIE1A); // enable compare match interrupt
  61. }
  62.  
  63. void PWMInit(){
  64.     DDRE= _BV(PE3); //activate PWM output
  65.     TCCR3B = _BV(CS30); //prescaller 1  
  66.     TCCR3B |= _BV(WGM32); //Fast PWM 8 Bit on Timer3
  67.     TCCR3A |= _BV(WGM30);
  68.     TCCR3A |= _BV(COM3A1); //non inverting
  69.     OCR3A=128; // test PWM
  70. }
Advertisement
Add Comment
Please, Sign In to add comment