send.c 2.2 KB

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