Guest User

Untitled

a guest
Apr 22nd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. /*
  2. * I2C_MASTER.c
  3. *
  4. * Created: 4/2/2018 7:38:20 PM
  5. * Author : suwas
  6. */
  7. #define F_CPU 16000000UL /* Define CPU clock Frequency e.g. here its 16MHz */
  8. #include <avr/io.h> /* Include AVR std. library file */
  9. #include <util/delay.h> /* Include inbuilt defined Delay header file */
  10. #include <stdio.h> /* Include standard I/O header file */
  11. #include <string.h> /* Include string header file */
  12. #include <avr/lcd4bits.h> /* Include I2C header file */
  13. #include "I2C_Master_H_file.h" /* Include LCD header file */
  14. #include <avr/dht11.h>
  15. #define Slave_Write_Address 0x20
  16. #define Slave_Read_Address 0x21
  17. unsigned int I_RH=0,D_RH=0,I_Temp=0,D_Temp=0,CheckSum=0;
  18.  
  19. char data[5];
  20.  
  21. unsigned int count=0;
  22. int main()
  23. {
  24. DDRB = 0XFF;
  25. DDRD &=~(1<<PIND6); //input for sensors
  26. PIND &=~(1<<PIND6);
  27. lcd_init(); /* Initialize LCD */
  28. I2C_Init(); /* Initialize I2C */
  29. LCDWriteStringXY(0,0, "Master Device");
  30.  
  31. while (1)
  32. {
  33. I2C_Start_Wait(Slave_Write_Address);
  34. _delay_ms(20);
  35. Request();
  36. _delay_us(40); /* send start pulse */
  37. Response(); /* receive response */
  38. I_RH=Receive_data(); /* store first eight bit in I_RH */
  39. D_RH=Receive_data(); /* store next eight bit in D_RH */
  40. I_Temp=Receive_data(); /* store next eight bit in I_Temp */
  41. D_Temp=Receive_data(); /* store next eight bit in D_Temp */
  42. CheckSum=Receive_data(); /* store next eight bit in CheckSum */
  43. if ((I_RH + D_RH + I_Temp + D_Temp) != CheckSum)
  44. {
  45. lcd_gotoLineOne();
  46. dis_string("Error");
  47. }
  48. lcd_gotoLineOne();
  49. dis_string(" ROSA FARMING ");
  50. lcd_gotoLineTwo();
  51. itoa(I_RH,data,10);
  52. dis_string("RH: ");
  53. dis_string(data);
  54. dis_string("%");
  55. itoa(I_Temp,data,10);
  56. dis_string("T: ");
  57. dis_string(data);
  58. dis_data(0xDF);
  59. dis_string("C ");
  60. dis_string(" ");
  61. I2C_Write(I_RH);
  62. I2C_Write(I_Temp);
  63. _delay_ms(5);
  64. I2C_Repeated_Start(Slave_Read_Address); /* Repeated Start I2C communication with SLA+R */
  65. _delay_ms(5);
  66. I2C_Stop();
  67. }
  68. }
Add Comment
Please, Sign In to add comment