Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
62
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 = 576;
  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. if (buttonpressed) {
  68. buttonpressed = 0;
  69. PORTG ^= _BV(PORT0);
  70. }
  71. }
  72.  
  73.  
  74. return 0;
  75. }
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84. /*
  85. #include <avr/io.h>
  86. #include <util/delay.h>
  87. #include "blik.h"
  88. #include <avr/pgmspace.h>
  89. #include <avr/interrupt.h>
  90.  
  91.  
  92.  
  93.  
  94.  
  95. ISR(INT6_vect)
  96. {
  97. // PORTG = PORTG ^ _BV(PG1); //XOR - preklopeni, 11 je 0, 01 je 1
  98.  
  99. }
  100.  
  101. ISR(TIMER1_COMPA_vect)
  102. {
  103. PORTG = PORTG ^ _BV(PG1); //XOR - preklopeni, 11 je 0, 01 je 1
  104. #define S_ALL (_BV(PE2
  105. static uint8_t old_state = PINE & S_ALL;
  106. uint8_t
  107. }
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114. int main(void)
  115.  
  116.  
  117. {
  118. // 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};
  119.  
  120. DDRG |= _BV(PG1);
  121. DDRG |= _BV(PG2);
  122. PORTE |= _BV(PE2) | _BV(PE3) | _BV(PE4) | _BV(PE5) | _BV(PE6) ; //interrupty: 17.1.1
  123. EICRB |= _BV(ISC61) ; //int 6 aktivace na falling edge
  124. EICRB &= ~(_BV(ISC60)); //vynulujeme 60
  125. EIMSK |= _BV(6);
  126. sei();
  127.  
  128. // timer setup
  129.  
  130. TCCR1A&= ~(_BV(WGM10) | _BV(WGM11)); //vynuluju tyhle bity. TOP je OCR1A (mode4 )
  131. TCCR1B&= ~(_BV(WGM13));
  132. TCCR1B&= _BV(WGM12);
  133.  
  134. OCR1A=4608; // 40ms...25Hz je kmitocet preklapeni , 7.37M, 64x prescaler OCR1A = 7.3728M/64/25
  135. TCCR1B&= ~_BV(CS12);
  136. TCCR1B&= _BV(CS11) | _BV(CS10);
  137.  
  138. TIMSK=_BV(OCIE1A);
  139. //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
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151. while (1) {
  152.  
  153.  
  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. PORTG |= _BV(pin);
  217. _delay_ms(1000);
  218. PORTG &= ~_BV(pin); //led 1 is turned off. PORTG = PORTG & 0b11111101 - this is called mask.
  219. _delay_ms(1000);
  220.  
  221.  
  222. for (int i=0;i<32;i++) {
  223.  
  224. blik(1,morse[i]);
  225. }
  226.  
  227. //PORTG = PORTG & 0b11111101
  228. for (uint8_t i = 0; i < 4; i++) {
  229. uint8_t pomocna = 0;
  230. pomocna = pgm_read_byte(&pole[i]);
  231. for (uint8_t j = 0; j < 8; j++) {
  232. uint8_t pomocna2 = 0;
  233. pomocna2 = pomocna & 1; //vezmu bit
  234. blik(1, pomocna2);
  235. pomocna = pomocna >> 1;
  236. }
  237.  
  238. }
  239.  
  240. }
  241.  
  242. return 0;
  243.  
  244. }
  245. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement