Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.04 KB | None | 0 0
  1. /*
  2.  * debounce_sw.c
  3.  *
  4.  * Created: 1/20/2018 12:34:11 AM
  5.  *  Author: Islam
  6.  */
  7. #define F_CPU 1000000UL
  8.  
  9. #include <stdint.h>
  10.  
  11. #include <avr/io.h>
  12. #include <avr/interrupt.h>
  13.  
  14. #define SAMPLES_NUM 13
  15. #define UNUSED_BITS ((uint16_t)(0xFFFF<<SAMPLES_NUM))
  16. typedef enum { PRESSED , RELEASED , DE_BOUNCING }SW_ST;
  17.  
  18. typedef struct
  19. {
  20.     uint16_t samples_mem ;
  21.     SW_ST status ;
  22.     uint8_t samples_count ;
  23.     uint8_t pin_num ;
  24. }SWITCH;
  25.  
  26. volatile SWITCH sw = {.status = DE_BOUNCING , .pin_num = 3} ;
  27.    
  28. #define read_pin(PORT,PIN) (PORT & (1<< (PIN)))
  29.  
  30. #define PULL_UP   1
  31. #define PULL_DOWN 2
  32. #define RESISTOR  PULL_DOWN
  33.  
  34. void timer_init() ;
  35.  
  36. int main(void)
  37. {
  38.     DDRA = 0 ;
  39.     DDRC = 0xFF ;
  40.     //PORTA = 0xFF ;
  41.     DDRB = 0xFF ;
  42.     uint16_t x = 0 ;   
  43.     timer_init() ;
  44.  
  45.     uint8_t press_flag = 0 ;
  46.    
  47.     while(1)
  48.     {
  49.         if(sw.status == PRESSED && !press_flag)
  50.         {
  51.             press_flag = 1 ;
  52.             PORTC++;
  53.         }
  54.        
  55.         if(sw.status == RELEASED  && press_flag)
  56.         {
  57.             press_flag = 0 ;
  58.             //PORTC = 0 ;
  59.         }
  60.        
  61.     // PORTB++ ;
  62.     }
  63. }
  64.  
  65. void timer_init()
  66. {
  67.     OCR0 = 69 ;
  68.     TCNT0 = 0 ;
  69.     /* Pre-scale  = 64 , CTC-mode */
  70.     TIMSK |= (1<<OCIE0) ; // enable timer 0 CTC interrupt
  71.  
  72.     TCCR0 |= ( 1<<CS00 ) | ( 1<<CS01 ) | ( 0<<CS02 ) | ( 0<<WGM00  ) | ( 1<<WGM01 ) ;
  73.    
  74.     sei() ;
  75. }
  76.  
  77. ISR(TIMER0_COMP_vect)
  78. {
  79.     /* 13 samples * 4.4ms = 57.2ms */
  80.     sw.samples_mem = (sw.samples_mem <<1) | (read_pin(PINA,sw.pin_num)>=1) |UNUSED_BITS;
  81.    
  82.     /*Switch is pressed */
  83.     if(sw.samples_mem == UNUSED_BITS)
  84.     {
  85.         if(++sw.samples_count  == SAMPLES_NUM )
  86.         {
  87.             sw.samples_count =  0 ;
  88.             sw.status =
  89.                       #if RESISTOR == PULL_UP
  90.                         PRESSED ;
  91.                       #else
  92.                         RELEASED ;
  93.                       #endif       
  94.         }
  95.     }
  96.    
  97.     /*Note that status will be 0xFFFF only when switch is released and with 13 identical samples (all 13 samples are high) */
  98.     else if (sw.samples_mem  == 0xFFFF)
  99.     {
  100.        sw.status =
  101.                    #if RESISTOR == PULL_UP
  102.                         RELEASED ;
  103.                       #else
  104.                         PRESSED ;
  105.                       #endif
  106.     }
  107.    
  108.     else
  109.     {
  110.         sw.status =DE_BOUNCING ;
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement