Advertisement
Guest User

Untitled

a guest
Jun 1st, 2014
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.07 KB | None | 0 0
  1. //////////////////////////////////////////////
  2.  
  3. // Program for Master Mode
  4.  
  5. #include
  6. #include
  7. #include
  8.  
  9. void TWI_start(void);
  10. void TWI_repeated_start(void);
  11. void TWI_init_master(void);
  12. void TWI_write_address(unsigned char);
  13. void TWI_read_address(unsigned char);
  14. void TWI_write_data(unsigned char);
  15. void TWI_read_data(void);
  16. void TWI_stop(void);
  17.  
  18. unsigned char address1=0×20,address2=0×08, read=1, write=0,address=0×20;unsigned char write_data=0xf0, recv_data;
  19. int c=0;int dt=0,i=0;
  20. int main(void)
  21. {
  22. _delay_ms(2000);
  23.  
  24. DDRA=0xff;
  25.  
  26. if(write_data==0×00)
  27. write_data=0xf0;
  28.  
  29. TWI_init_master();
  30. TWI_start();
  31. TWI_write_address(address+write);
  32. TWI_write_data(write_data);dt=1;
  33. TWI_write_data(0x1f);dt=0;
  34. PORTA=0xff;
  35.  
  36. address=0×08;
  37. c=1;
  38.  
  39. TWI_repeated_start();
  40. TWI_write_address(address+write);
  41. TWI_write_data(write_data);dt=1;
  42. TWI_write_data(0x1f);dt=0;
  43. TWI_stop();
  44.  
  45. while(1)
  46. {
  47.  
  48. }
  49.  
  50. }
  51.  
  52. void TWI_init_master(void) // Function to initialize master
  53. {
  54.  
  55. TWBR=0×12; // Bit rate
  56. TWSR=(0<<TWPS1)|(1<<TWPS0); // Setting prescalar bits
  57. // SCL freq= F_CPU/(16+2(TWBR).4^TWPS)
  58. }
  59.  
  60. void TWI_start(void)
  61. {
  62. // Clear TWI interrupt flag, Put start condition on SDA, //Enable TWI
  63. TWCR= (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
  64.  
  65. // Wait till start condition is transmitted
  66. while(!(TWCR & (1<<TWINT)));
  67.  
  68. // Check for the acknowledgement
  69. while((TWSR & 0xF8)!= 0×08);
  70. }
  71.  
  72. void TWI_repeated_start(void)
  73. {
  74. //Clear TWI interrupt flag, Put start condition on SDA, //Enable TWI
  75. TWCR= (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
  76.  
  77. // wait till restart condition is transmitted
  78. while(!(TWCR & (1<<TWINT)));
  79.  
  80. // Check for the acknoledgement
  81. while((TWSR & 0xF8)!= 0×10);
  82. }
  83.  
  84. void TWI_write_address(unsigned char data)
  85. {
  86. TWDR=data; // Address and write instruction
  87. // Clear TWI interrupt flag,Enable TWI
  88. TWCR=(1<<TWINT)|(1<<TWEN);
  89.  
  90. // Wait till complete TWDR byte transmitted
  91. while (!(TWCR & (1<<TWINT)));
  92.  
  93. // Check for the acknoledgement
  94. while((TWSR & 0xF8)!= 0×18);
  95. }
  96.  
  97. void TWI_read_address(unsigned char data)
  98. {
  99. TWDR=data; // Address and read instruction
  100. // Clear TWI interrupt flag,Enable TWI
  101. TWCR=(1<<TWINT)|(1<<TWEN);
  102.  
  103. // Wait till complete TWDR byte received
  104. while (!(TWCR & (1<<TWINT)));
  105.  
  106. // Check for the acknoledgement
  107. while((TWSR & 0xF8)!= 0×40);
  108. }
  109.  
  110. void TWI_write_data(unsigned char data)
  111. {
  112. TWDR=data; // put data in TWDR
  113. // Clear TWI interrupt flag,Enable TWI
  114. TWCR=(1<<TWINT)|(1<<TWEN);
  115. // Wait till complete TWDR byte transmitted
  116. while (!(TWCR & (1<<TWINT)));
  117.  
  118. // Check for the acknoledgement
  119. while((TWSR & 0xF8) != 0×28);
  120. }
  121.  
  122. void TWI_read_data(void)
  123. {
  124. // Clear TWI interrupt flag,Enable TWI
  125. TWCR=(1<<TWINT)|(1<<TWEN);
  126.  
  127. // Wait till complete TWDR byte transmitted
  128. while (!(TWCR & (1<<TWINT)));
  129.  
  130. // Check for the acknoledgement
  131. while((TWSR & 0xF8) != 0×58);
  132.  
  133. recv_data=TWDR;
  134. PORTA=recv_data;
  135. }
  136.  
  137. void TWI_stop(void)
  138. {
  139. // Clear TWI interrupt flag, Put stop condition on SDA, //Enable TWI
  140. TWCR= (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
  141. // Wait till stop condition is transmitted
  142. while(!(TWCR & (1<<TWSTO)));
  143.  
  144. }
  145.  
  146. ////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement