浏览代码

picadae/tiny/i2c_timer_pwm.c : Added 'hold_state' to cleanly set the enable/disable PCM.

master
kevin.larke 4 年前
父节点
当前提交
3851812b8e
共有 1 个文件被更改,包括 22 次插入10 次删除
  1. 22
    10
      control/tiny/i2c_timer_pwm.c

+ 22
- 10
control/tiny/i2c_timer_pwm.c 查看文件

@@ -6,8 +6,8 @@
6 6
                                     AT TINY 85
7 7
                                      +--\/--+
8 8
                               RESET _| 1  8 |_ +5V
9
-             ~OC1B       HOLD  DDB3 _| 2  7 |_ SCL         yellow 
10
-              OC1B      ONSET  DDB4 _| 3  6 |_ DDB1 LED
9
+             ~OC1B       HOLD PINB3 _| 2  7 |_ SCL         yellow 
10
+              OC1B      ONSET PINB4 _| 3  6 |_ PINB1 LED
11 11
                                 GND _| 4  5 |_ SDA         orange
12 12
                                      +------+
13 13
         * = Serial and/or programming pins on Arduino as ISP
@@ -201,13 +201,18 @@ volatile uint8_t tmr0_coarse_cur   = 0;
201 201
 #define set_attack()    do { ctl_regs[kState_idx] |= kState_Attk_Fl;  PORTB |= _BV(ATTK_PIN);            } while(0)
202 202
 #define clear_attack()  do { PORTB &= ~_BV(ATTK_PIN);           ctl_regs[kState_idx] &= ~kState_Attk_Fl; } while(0)
203 203
 
204
+volatile uint8_t hold_state = 0;  // state=0 hold should not be set, state=1 hold can be set
205
+#define clear_hold() PORTB &= ~(_BV(HOLD_PIN))
206
+#define set_hold()   PORTB |= _BV(HOLD_PIN)
204 207
 
205 208
 // Use the current tmr0 ctl_reg[] values to set the timer to the starting state.
206 209
 void tmr0_reset()
207 210
 {
208 211
   tmr0_coarse_cur       = 0;               // clear the coarse time counter
209 212
   ctl_regs[kState_idx] |= kState_Attk_Fl;  // set the attack state
210
-  PORTB                |= _BV(ATTK_PIN);   // set the attack pin 
213
+  PORTB                |= _BV(ATTK_PIN);   // set the attack pin
214
+  clear_hold();                            // clear the hold pin
215
+  hold_state = 0;
211 216
   
212 217
   // if a coarse count exists then go into coarse mode 
213 218
   if( ctl_regs[kTmr_Coarse_idx] > 0 )
@@ -249,7 +254,8 @@ ISR(TIMER0_COMPA_vect)
249 254
 
250 255
       clear_attack();
251 256
 
252
-      TCNT1   = 0;
257
+      TCNT1      = 0;   // reset the PWM counter to 0
258
+      hold_state = 1;   // enable the hold output
253 259
       TIMSK  |= _BV(OCIE1B) + _BV(TOIE1);  // PWM interupt Enable interrupts          
254 260
       TIMSK &= ~_BV(OCIE0A);               // clear timer interrupt
255 261
       
@@ -288,13 +294,14 @@ void pwm1_update()
288 294
 // At this point TCNT1 is reset to 0, new OCR1B values are latched from temp. loctaion to OCR1B
289 295
 ISR(TIMER1_OVF_vect)
290 296
 {
291
-  PORTB &= ~(_BV(HOLD_PIN)); // clear PWM pin
297
+  clear_hold();
292 298
 }
293 299
 
294
-// Called when TCN1 == OCR1B
300
+// Called when TCNT1 == OCR1B
295 301
 ISR(TIMER1_COMPB_vect)
296 302
 {
297
-  PORTB |= _BV(HOLD_PIN);  // set PWM pin
303
+  if(hold_state)
304
+    set_hold();
298 305
 }
299 306
 
300 307
 
@@ -309,6 +316,7 @@ void pwm1_init()
309 316
   GTCCR  |= _BV(PSR1);     // Set the pre-scaler to the selected value
310 317
 
311 318
   pwm1_update();
319
+
312 320
 }
313 321
 
314 322
 //------------------------------------------------------------------------------
@@ -453,10 +461,12 @@ void on_receive( uint8_t byteN )
453 461
 
454 462
       // if the PWM prescaler was changed
455 463
       if( i == 3 )
464
+      {
456 465
         cli();
457 466
         pwm1_init();
458 467
         sei();
459
-          
468
+      }
469
+      
460 470
       pwm1_update();
461 471
       break;
462 472
 
@@ -473,17 +483,19 @@ void on_receive( uint8_t byteN )
473 483
       }   
474 484
       // if a prescaler was included then the timer needs to be re-initialized
475 485
       if( i == 3 )
486
+      {
476 487
         cli();
477 488
         tmr0_init();
478 489
         sei();
490
+      }
479 491
       
480 492
       tmr0_reset();
481 493
       break;
482 494
 
483 495
     case kNoteOff_Op:
484 496
       TIMSK  &= ~_BV(OCIE0A);                // clear timer interrupt (shouldn't be necessary)
485
-      TIMSK  &= ~(_BV(OCIE1B) + _BV(TOIE1)); // PWM interupt disable interrupts          
486
-      PORTB  &= ~_BV(HOLD_PIN);              // clear the HOLD pin
497
+      //TIMSK  &= ~(_BV(OCIE1B) + _BV(TOIE1)); // PWM interupt disable interrupts
498
+      hold_state = 0;
487 499
       break;
488 500
 
489 501
     case kSetReadAddr_Op:

正在加载...
取消
保存