send.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "io.h"
  5. #define PIN_DATA 0
  6. #define PIN_CLK 1
  7. #define PIN_ACK 2
  8. #define PIN_IN 3
  9. #define PIN_LED 4
  10. #include "bus.h"
  11. void init_interrupt()
  12. {
  13. MCUSR = 0;
  14. wdt_disable();
  15. DDRB &= ~(1<<PB3); /* Set PB0 as input */
  16. PORTB |= (1<<PB3); /* Activate PULL UP resistor */
  17. PCMSK |= (1 << PCINT3); /* Enable PCINT0 */
  18. GIMSK |= (1 << PCIE); /* Activate interrupt on enabled PCINT7-0 */
  19. sei ();
  20. }
  21. int main()
  22. {
  23. int wait=0, state=0;
  24. unsigned long last=0;
  25. bus_t bus;
  26. init();
  27. init_write_pin(PIN_DATA,PIN_CLK,PIN_ACK);
  28. //init_interrupt();
  29. pinMode(PIN_IN, INPUT);
  30. pinMode(PIN_LED, OUTPUT);
  31. bus_init_send(&bus, PIN_DATA, PIN_CLK, PIN_ACK);
  32. while(1)
  33. {
  34. if(!wait && read_pin(PIN_IN) )
  35. {
  36. unsigned long val;
  37. val=micros()-last;
  38. if(val<5000) continue;
  39. wait=1;
  40. last=micros();
  41. //bus_write_sync(&bus, &val, 4);
  42. state=(!state)?1:0;
  43. digitalWrite(PIN_LED, state);
  44. }
  45. if(!read_pin(PIN_IN)) wait=0;
  46. }
  47. }
  48. /*
  49. * #include <stdio.h>
  50. #include <stdlib.h>
  51. #include <string.h>
  52. #include "io.h"
  53. #define PIN_DATA 0
  54. #define PIN_CLK 1
  55. #define PIN_ACK 2
  56. #define PIN_IN 3
  57. #define PIN_LED 4
  58. #include "bus.h"
  59. void init_interrupt()
  60. {
  61. MCUSR = 0;
  62. wdt_disable();
  63. DDRB &= ~(1<<PB3);
  64. PORTB |= (1<<PB3);
  65. PCMSK |= (1 << PCINT3);
  66. GIMSK |= (1 << PCIE);
  67. sei ();
  68. }
  69. bus_t bus;
  70. int main()
  71. {
  72. init();
  73. init_write_pin(PIN_DATA,PIN_CLK,PIN_ACK);
  74. init_interrupt();
  75. pinMode(PIN_IN, INPUT);
  76. pinMode(PIN_LED, OUTPUT);
  77. bus_init_send(&bus, PIN_DATA, PIN_CLK, PIN_ACK);
  78. while(1)
  79. {
  80. //bus_send_thread(&bus);
  81. }
  82. }
  83. unsigned long last=0;
  84. uint8_t ledstate=0;
  85. ISR (PCINT0_vect){
  86. uint8_t x = (PORTB&(1<<PB3))>>PB3;
  87. if(x)
  88. {
  89. uint32_t val=micros()-last;
  90. last=micros();
  91. bus_write(&bus, &val, 4);
  92. ledstate=(ledstate)?0:1;
  93. digitalWrite(PIN_LED, ledstate);
  94. }
  95. }
  96. */