Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. #include <stdint.h>
  2. #include <avr/interrupt.h>
  3.  
  4. #define SLAVE_ADDRESS 0x40
  5.  
  6. #define TIMER_OVERFLOW_COUNT 279
  7. volatile uint8_t i2cstage = 0;
  8. void reset_i2c()
  9. {
  10. USISR = 0xF0;
  11. USICR = (1 << USISIE | 1 << USIWM1) | 1 << USICS1;
  12. PORTB |= 1 << PORTB0;
  13. PORTB |= 1 << PORTB2;
  14. DDRB |= 1 << DDB2;
  15. DDRB &= ~(1 << DDB0);
  16. i2cstage = 0;
  17. }
  18. #define transmit_i2c_data() {DDRB |= 1 << DDB0;USISR = 0xF0;}
  19. #define transmit_i2c_ACK() {DDRB |= 1 << DDB0;USIDR = 0x00;USISR = 0xFE;}
  20. #define receive_i2c_ACKNACK() {DDRB &= ~(1 << DDB0);USISR = 0xFE;}
  21. ISR(USI_START_vect)
  22. {
  23. USISR |= 1 << USISIF;
  24. USICR |= 1 << USIOIE;
  25. i2cstage = 1;
  26. }
  27. volatile uint8_t i2c_0 = 0;
  28. ISR(USI_OVF_vect)
  29. {
  30. uint8_t data = USIDR;
  31. switch ( i2cstage)
  32. {
  33. case 1:
  34. if( data == (SLAVE_ADDRESS << 1) + 1)
  35. {
  36. i2cstage = 2;
  37. transmit_i2c_ACK();
  38. }
  39. else
  40. {
  41. reset_i2c();
  42. }
  43. break;
  44.  
  45. case 2:
  46. i2cstage = 3;
  47. USIDR = i2c_0;
  48. transmit_i2c_data();
  49. break;
  50.  
  51. /*case 3:
  52. i2cstage = 4;
  53. receive_i2c_ACKNACK();
  54. break;*/
  55.  
  56. default:
  57. reset_i2c();
  58. break;
  59. }
  60. }
  61. volatile uint16_t timerCount = TIMER_OVERFLOW_COUNT;
  62. ISR(TIMER1_OVF_vect)
  63. {
  64. timerCount++;
  65. }
  66. int main(void)
  67. {
  68. reset_i2c();
  69. TIMSK |= 1 << TOIE1;
  70. TCCR1 |= 1 << PWM1A;
  71. OCR1C = 27;
  72. sei();
  73. TCCR1 |= (1 << CS13) | (1 << CS11) | (1 << CS10);
  74. while( 1)
  75. {
  76. if( timerCount >= TIMER_OVERFLOW_COUNT)
  77. {
  78. timerCount = 0;
  79. if(++i2c_0 > 5)
  80. i2c_0 = 0;
  81. }
  82. if( (USISR & (1 << USIPF)) == (1 << USIPF))
  83. {
  84. USISR = 1 << USIPF;
  85. reset_i2c();
  86. }
  87. }
  88. return 0;
  89. }
  90.  
  91. #include <stdint.h>
  92. #include <avr/interrupt.h>
  93. #include <stdbool.h>
  94. #include <util/delay.h>
  95.  
  96. #define SLAVE_ADDRESS 0x40
  97.  
  98. volatile bool i2cCompletedQ = true;
  99. void wait_for_i2c()
  100. {
  101. while( !(TWCR & 1 << TWINT))
  102. {
  103. }
  104. if( TWCR & 1 << TWINT)
  105. {
  106. i2cCompletedQ = true;
  107. }
  108. else
  109. {
  110. i2cCompletedQ = false;
  111. }
  112. }
  113. void transmit_i2c_start()
  114. {
  115. if( !i2cCompletedQ)
  116. {
  117. return;
  118. }
  119. TWCR = 1 << TWINT | 1 << TWEN | 1 << TWSTA;
  120. wait_for_i2c();
  121. }
  122. void transmit_i2c_stop()
  123. {
  124. if( !i2cCompletedQ)
  125. {
  126. return;
  127. }
  128. TWCR = 1 << TWINT | 1 << TWEN | 1 << TWSTO;
  129. }
  130. void transmit_i2c_data(uint8_t data)
  131. {
  132. if( !i2cCompletedQ)
  133. {
  134. return;
  135. }
  136. TWDR = data;
  137. TWCR = 1 << TWINT | 1 << TWEN;
  138. wait_for_i2c();
  139. }
  140. void receive_i2c_data(uint8_t *odata, bool ack)
  141. {
  142. if( !i2cCompletedQ)
  143. {
  144. return ;
  145. }
  146. if( ack)
  147. {
  148. TWCR = (1 << TWINT | 1 << TWEN) | 1 << TWEA;
  149. }
  150. else
  151. {
  152. TWCR = 1 << TWINT | 1 << TWEN;
  153. }
  154. wait_for_i2c();
  155. *odata = TWDR;
  156. }
  157. void update_i2c_Input(uint8_t *val)
  158. {
  159. transmit_i2c_start();
  160. transmit_i2c_data((SLAVE_ADDRESS << 1) + 1);
  161. receive_i2c_data(val, true);//should be false?
  162. transmit_i2c_stop();
  163. }
  164. int main(void)
  165. {
  166. TWBR = 100;
  167. TWSR &= ~(1 << TWPS1 | 1 << TWPS0);
  168.  
  169. DDRD |= 1 << DDD6;
  170. TCCR0A |= 1 << COM0A1;
  171. TCCR0A |= 1 << WGM00;
  172. sei();
  173. TCCR0B |= (1 << CS02) | (1 << CS00);
  174. uint8_t val = 0;
  175. while( 1)
  176. {
  177. i2cCompletedQ = true;
  178. update_i2c_Input(&val);
  179. OCR0A = (val * 255) / 5;
  180. _delay_ms(1000);
  181. }
  182. return 0;
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement