Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1.  
  2. #include <avr/io.h>
  3. #include <util/delay.h>
  4. #include <avr/interrupt.h>
  5. #define S_ALL (_BV(PE2) | _BV(PE3) |_BV(PE4)| _BV(PE5))
  6.  
  7. volatile uint8_t buttonpressed = 0;
  8. //interrupt service routine for ext. int. on PORT 6
  9. ISR(INT6_vect)
  10. {
  11. //invert LED1
  12. PORTG ^= _BV(PORT1);
  13. }
  14.  
  15. //interrupt service routine for timer1
  16. ISR(TIMER1_COMPA_vect)
  17. {
  18. static uint8_t old_state = S_ALL;
  19. uint8_t new_state = PINE & S_ALL;
  20. if (new_state < old_state) {
  21. buttonpressed = 1;
  22.  
  23. }
  24.  
  25. }
  26.  
  27.  
  28.  
  29. int main(void)
  30. {
  31.  
  32.  
  33. // insert code here
  34.  
  35. //set LED1,2 as output, set S1,...,5 as input with internal pullup
  36. DDRG |= _BV(DD0) | _BV(DD1);
  37. PORTE |= _BV(PORT2) | _BV(PORT3) | _BV(PORT4) | _BV(PORT5) | _BV(PORT6);
  38. DDRE &= ~(_BV(DD2) | _BV(DD3) | _BV(DD4) | _BV(DD5) | _BV(DD6));
  39.  
  40. //se int6 on falling edge
  41. EICRB |= _BV(ISC61);
  42. EICRB &= ~(_BV(ISC60));
  43. //enable INT6
  44. EIMSK |= _BV(INT6);
  45.  
  46. //set timer compare reg. for interrupt every 40ms~25Hz, 7.3728M, 64x prsc, OCR1A = 7.3728/64/25 = 4608
  47. OCR1A = 4608;
  48.  
  49. //set timer1 CTC
  50. TCCR1A &= ~(_BV(WGM10) | _BV(WGM11));
  51. TCCR1B &= ~(_BV(WGM13));
  52. TCCR1B |= _BV(WGM12);
  53.  
  54. //set timer1 prsc to 64
  55. TCCR1B &= ~(_BV(CS12));
  56. TCCR1B |= _BV(CS10) | _BV(CS11);
  57.  
  58. //enble tim comp1a interrupt
  59. TIMSK |= _BV(OCIE1A);
  60.  
  61. //enable global interrupts
  62. sei();
  63.  
  64.  
  65.  
  66. while(1)
  67. {
  68. if (buttonpressed)
  69. {
  70. buttonpressed=0;
  71. PORTG ^= _BV(PORT0);
  72. }
  73. }
  74.  
  75.  
  76. return 0;
  77. }
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86. /*
  87. #include <avr/io.h>
  88. #include <util/delay.h>
  89. #include "blik.h"
  90. #include <avr/pgmspace.h>
  91. #include <avr/interrupt.h>
  92.  
  93.  
  94.  
  95.  
  96.  
  97. ISR(INT6_vect)
  98. {
  99. // PORTG = PORTG ^ _BV(PG1); //XOR - preklopeni, 11 je 0, 01 je 1
  100.  
  101. }
  102.  
  103. ISR(TIMER1_COMPA_vect)
  104. {
  105. PORTG = PORTG ^ _BV(PG1); //XOR - preklopeni, 11 je 0, 01 je 1
  106. #define S_ALL (_BV(PE2
  107. static uint8_t old_state = PINE & S_ALL;
  108. uint8_t
  109. }
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116. int main(void)
  117.  
  118.  
  119. {
  120. // uint8_t morse[] = {1,0,1,0,1,0,0,1,1,1,0,1,1,1,0,1,1,1,0,0,1,0,1,0,1,0,0,0,0,0,0,0,0,0};
  121.  
  122. DDRG |= _BV(PG1);
  123. DDRG |= _BV(PG2);
  124. PORTE |= _BV(PE2) | _BV(PE3) | _BV(PE4) | _BV(PE5) | _BV(PE6) ; //interrupty: 17.1.1
  125. EICRB |= _BV(ISC61) ; //int 6 aktivace na falling edge
  126. EICRB &= ~(_BV(ISC60)); //vynulujeme 60
  127. EIMSK |= _BV(6);
  128. sei();
  129.  
  130. // timer setup
  131.  
  132. TCCR1A&= ~(_BV(WGM10) | _BV(WGM11)); //vynuluju tyhle bity. TOP je OCR1A (mode4 )
  133. TCCR1B&= ~(_BV(WGM13));
  134. TCCR1B&= _BV(WGM12);
  135.  
  136. OCR1A=4608; // 40ms...25Hz je kmitocet preklapeni , 7.37M, 64x prescaler OCR1A = 7.3728M/64/25
  137. TCCR1B&= ~_BV(CS12);
  138. TCCR1B&= _BV(CS11) | _BV(CS10);
  139.  
  140. TIMSK=_BV(OCIE1A);
  141. //str. 189, Bit 4 – OCIE1A: Timer/Counter1, Output Compare A Match Interrupt Enable pouzijeme tento, Bit 2 – TOIE1: Timer/Counter1, Overflow Interrupt Enable bychom pouzili jen pri pouhem preteceni, kdybt pocital do konce
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153. while (1) {
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218. PORTG |= _BV(pin);
  219. _delay_ms(1000);
  220. PORTG &= ~_BV(pin); //led 1 is turned off. PORTG = PORTG & 0b11111101 - this is called mask.
  221. _delay_ms(1000);
  222.  
  223.  
  224. for (int i=0;i<32;i++) {
  225.  
  226. blik(1,morse[i]);
  227. }
  228.  
  229. //PORTG = PORTG & 0b11111101
  230. for (uint8_t i = 0; i < 4; i++) {
  231. uint8_t pomocna = 0;
  232. pomocna = pgm_read_byte(&pole[i]);
  233. for (uint8_t j = 0; j < 8; j++) {
  234. uint8_t pomocna2 = 0;
  235. pomocna2 = pomocna & 1; //vezmu bit
  236. blik(1, pomocna2);
  237. pomocna = pomocna >> 1;
  238. }
  239.  
  240. }
  241.  
  242. }
  243.  
  244. return 0;
  245.  
  246. }
  247. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement