Advertisement
olelek

kolprem_buzzer.c

Jun 18th, 2025
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.61 KB | None | 0 0
  1. /*
  2.  * kolprem_buzzer.c
  3.  *
  4.  * Created: 2019-02-06 10:25:48
  5.  * Author : a
  6.  
  7.  Buzzer connected to PB0:PA4
  8.  INPUTS:   
  9.  6  PA5 AIN5 - main input
  10.  7  PA6 AIN6
  11.  8  PA7 AIN7
  12.  9  PB5 AIN8
  13.  10 PB4 AIN9 (TXD)
  14.  
  15.  Workflow:
  16.  The MCU monitors the AIN5 analog input, and when it detects a rising edge defined by thershold levels low_tsh and high_tsh,
  17.  it activates buzzer for 1 second (generating ~3.3kHz square wave frequency on pins PB0 & PA4).
  18.  
  19.  
  20.  
  21.  */
  22. #define F_CPU   32000UL
  23.  
  24. #include <avr/io.h>
  25. #include <util/delay.h>
  26. #include <avr/interrupt.h>
  27. #include <avr/sleep.h>
  28. #include <avr/eeprom.h>
  29. #include <avr/lock.h>
  30.  
  31. LOCKBITS = (LB_RWLOCK_gc);
  32.  
  33.  
  34.  
  35. volatile uint8_t start_signal = 1; //1 after rising edge
  36. volatile uint8_t lastState = 0, currentState = 0;
  37. const uint8_t low_tsh=(uint8_t)256.0*0.8/5.0, high_tsh=(uint8_t)256.0*1.2/5.0;
  38. const char info[] EEMEM = "Kolprem bnuzzer REV 02. Analog readout. Rising-edge trigger above 1.2V.";
  39. extern void ccp_write_io(void *addr, uint8_t value);
  40.  
  41.  
  42. int8_t DIGITAL_GLUE_LOGIC_0_init()
  43. {
  44.  
  45.     // Enable Protected register, peripheral must be disabled (ENABLE=0, in TCD.LUT0CTRLA).
  46.  
  47.     // CCL.SEQCTRL0 = CCL_SEQSEL_DISABLE_gc /* Sequential logic disabled */;
  48.  
  49.     CCL.TRUTH0 = 1; /* Truth 0: 1 */
  50.  
  51.     // CCL.LUT0CTRLC = CCL_INSEL2_MASK_gc /* Masked input */;
  52.  
  53.     CCL.LUT0CTRLB = CCL_INSEL0_TCA0_gc /* TCA0 WO0 input source */
  54.     | CCL_INSEL1_MASK_gc /* Masked input */;
  55.  
  56.     CCL.LUT0CTRLA = 0 << CCL_CLKSRC_bp       /* Clock Source Selection: disabled */
  57.     | CCL_EDGEDET_DIS_gc     /* Edge detector is disabled */
  58.     | CCL_FILTSEL_DISABLE_gc /* Filter disabled */
  59.     | 1 << CCL_ENABLE_bp     /* LUT Enable: enabled */
  60.     | 1 << CCL_OUTEN_bp;     /* Output Enable: enabled */
  61.  
  62.     // CCL.TRUTH1 = 0; /* Truth 1: 0 */
  63.  
  64.     // CCL.LUT1CTRLC = CCL_INSEL2_MASK_gc /* Masked input */;
  65.  
  66.     // CCL.LUT1CTRLB = CCL_INSEL0_MASK_gc /* Masked input */
  67.     //       | CCL_INSEL1_MASK_gc /* Masked input */;
  68.  
  69.     CCL.LUT1CTRLA = 0 << CCL_CLKSRC_bp       /* Clock Source Selection: disabled */
  70.     | CCL_EDGEDET_DIS_gc     /* Edge detector is disabled */
  71.     | CCL_FILTSEL_DISABLE_gc /* Filter disabled */
  72.     | 0 << CCL_ENABLE_bp     /* LUT Enable: disabled */
  73.     | 1 << CCL_OUTEN_bp;     /* Output Enable: enabled */
  74.  
  75.     CCL.CTRLA = 1 << CCL_ENABLE_bp      /* Enable: enabled */
  76.     | 0 << CCL_RUNSTDBY_bp; /* Run in Standby: disabled */
  77.  
  78.     return 0;
  79. }
  80. /************************************************************************/
  81. /* CLK init CPU_CLK=32Khz                                                                     */
  82. /************************************************************************/
  83. int8_t CLKCTRL_init()
  84. {
  85.  
  86.     // ccp_write_io((void*)&(CLKCTRL.OSC32KCTRLA),0 << CLKCTRL_RUNSTDBY_bp /* Run standby: disabled */);
  87.  
  88.     // ccp_write_io((void*)&(CLKCTRL.XOSC32KCTRLA),CLKCTRL_CSUT_1K_gc /* 1k cycles */
  89.     //       | 0 << CLKCTRL_ENABLE_bp /* Enable: disabled */
  90.     //       | 0 << CLKCTRL_RUNSTDBY_bp /* Run standby: disabled */
  91.     //       | 0 << CLKCTRL_SEL_bp /* Select: disabled */);
  92.  
  93.     // ccp_write_io((void*)&(CLKCTRL.OSC20MCTRLA),0 << CLKCTRL_RUNSTDBY_bp /* Run standby: disabled */);
  94.  
  95.     ccp_write_io((void *)&(CLKCTRL.MCLKCTRLB),
  96.     CLKCTRL_PDIV_6X_gc /* 6 */
  97.     | 0 << CLKCTRL_PEN_bp /* Prescaler enable: disabled */);
  98.  
  99.     ccp_write_io((void *)&(CLKCTRL.MCLKCTRLA),
  100.     CLKCTRL_CLKSEL_OSCULP32K_gc /* 32KHz Internal Ultra Low Power Oscillator (OSCULP32K) */
  101.     | 0 << CLKCTRL_CLKOUT_bp /* System clock out: disabled */);
  102.  
  103.     /* wait for system oscillator changing to finish */
  104.     while (CLKCTRL.MCLKSTATUS & CLKCTRL_SOSC_bm) {
  105.     }
  106.  
  107.     // ccp_write_io((void*)&(CLKCTRL.MCLKLOCK),0 << CLKCTRL_LOCKEN_bp /* lock enable: disabled */);
  108.  
  109.     return 0;
  110. }
  111.  
  112. void GPIO_init(void)
  113. {
  114.     /* Buzzer PB0:PA4 */
  115.     /* INPUT PA5 AIN5 */
  116.     /* GPIO */
  117.     PORTA_OUTSET = PIN4_bm;
  118.     PORTB_DIRSET = PIN0_bm;
  119.    
  120.     //PORTA_PIN5CTRL = PORT_PULLUPEN_bm;
  121.     DIGITAL_GLUE_LOGIC_0_init();
  122. }
  123.  
  124. void TCA0_init(void){
  125.     /* Timer A configuration - audio freq. output */
  126.     TCA0_SINGLE_CTRLA = TCA_SINGLE_ENABLE_bm;
  127.     TCA0_SINGLE_CTRLB = TCA_SINGLE_WGMODE_FRQ_gc|TCA_SINGLE_CMP0_bm;
  128.     TCA0_SINGLE_CMP0 = 4; // 550 = 3Khz @ CPU_CLK = 3.3MHz; 4 = 3.3KHz @ CPU_CLK=32Khz
  129.    
  130.     TCA0_SINGLE_CNT = 0;
  131.     TCA0_SINGLE_CTRLA = 0;
  132.     /* Timer B configuration - pulse freq. output */
  133.     TCB0_CTRLA = TCB_ENABLE_bm;
  134.     TCB0_CTRLB = 0;
  135.    
  136.    
  137. }
  138.  
  139. void ADC_init(void){
  140.     ADC0_CTRLA = ADC_RUNSTBY_bm|ADC_RESSEL_bm|ADC_ENABLE_bm;
  141.     ADC0_CTRLB = ADC_SAMPNUM_ACC4_gc;
  142.     ADC0_CTRLC = ADC_REFSEL_VDDREF_gc|ADC_PRESC_DIV2_gc;
  143.     ADC0_MUXPOS = ADC_MUXPOS_AIN5_gc;
  144.     ADC0_INTCTRL = ADC_RESRDY_bm;
  145.     ADC0_COMMAND = ADC_STCONV_bm;
  146.    
  147. }
  148.  
  149. void signal_on(void){
  150.     TCA0_SINGLE_CTRLA = TCA_SINGLE_ENABLE_bm;
  151. }
  152.  
  153. void signal_off(void){
  154.     TCA0_SINGLE_CTRLA = 0;
  155. }
  156.  
  157. int main(void)
  158. {
  159.     sei();
  160.     GPIO_init();
  161.     TCA0_init();
  162.     signal_off();
  163.     CLKCTRL_init();
  164.     ADC_init();
  165.    
  166.     while (1)
  167.     {
  168.        
  169.         if(start_signal){
  170.             signal_on();
  171.             _delay_ms(1000);
  172.             signal_off();
  173.             _delay_ms(1000);
  174.             start_signal = 0;
  175.             //go to idle sleep
  176.             set_sleep_mode(SLEEP_MODE_STANDBY);
  177.             sleep_enable();
  178.             sleep_cpu();
  179.         }
  180.     }//while(1)
  181. }
  182.  
  183.  
  184. /************************************************************************/
  185. /* ADC ISR                                                              */
  186. /************************************************************************/
  187. ISR(ADC0_RESRDY_vect){
  188.    
  189.     lastState = currentState;
  190.     //calculate current state
  191.     if (ADC0_RES > high_tsh)
  192.     {
  193.         currentState = 1;
  194.     }
  195.     if (ADC0_RES < low_tsh)
  196.     {
  197.         currentState = 0;
  198.     }
  199.    
  200.     //rising edge detection
  201.     if ((currentState ==1) && (lastState == 0))
  202.     {
  203.         start_signal = 1;
  204.     }
  205.     //start next aquisition
  206.     ADC0_INTFLAGS = ADC_RESRDY_bm;
  207.     ADC0_COMMAND = ADC_STCONV_bm;
  208. }
  209.  
  210.  
  211.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement