Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * I2C_MASTER.c
- *
- * Created: 4/2/2018 7:38:20 PM
- * Author : suwas
- */
- #define F_CPU 16000000UL /* Define CPU clock Frequency e.g. here its 16MHz */
- #include <avr/io.h> /* Include AVR std. library file */
- #include <util/delay.h> /* Include inbuilt defined Delay header file */
- #include <stdio.h> /* Include standard I/O header file */
- #include <string.h> /* Include string header file */
- #include <avr/lcd4bits.h> /* Include I2C header file */
- #include "I2C_Master_H_file.h" /* Include LCD header file */
- #include <avr/dht11.h>
- #define Slave_Write_Address 0x20
- #define Slave_Read_Address 0x21
- unsigned int I_RH=0,D_RH=0,I_Temp=0,D_Temp=0,CheckSum=0;
- char data[5];
- unsigned int count=0;
- int main()
- {
- DDRB = 0XFF;
- DDRD &=~(1<<PIND6); //input for sensors
- PIND &=~(1<<PIND6);
- lcd_init(); /* Initialize LCD */
- I2C_Init(); /* Initialize I2C */
- LCDWriteStringXY(0,0, "Master Device");
- while (1)
- {
- I2C_Start_Wait(Slave_Write_Address);
- _delay_ms(20);
- Request();
- _delay_us(40); /* send start pulse */
- Response(); /* receive response */
- I_RH=Receive_data(); /* store first eight bit in I_RH */
- D_RH=Receive_data(); /* store next eight bit in D_RH */
- I_Temp=Receive_data(); /* store next eight bit in I_Temp */
- D_Temp=Receive_data(); /* store next eight bit in D_Temp */
- CheckSum=Receive_data(); /* store next eight bit in CheckSum */
- if ((I_RH + D_RH + I_Temp + D_Temp) != CheckSum)
- {
- lcd_gotoLineOne();
- dis_string("Error");
- }
- lcd_gotoLineOne();
- dis_string(" ROSA FARMING ");
- lcd_gotoLineTwo();
- itoa(I_RH,data,10);
- dis_string("RH: ");
- dis_string(data);
- dis_string("%");
- itoa(I_Temp,data,10);
- dis_string("T: ");
- dis_string(data);
- dis_data(0xDF);
- dis_string("C ");
- dis_string(" ");
- I2C_Write(I_RH);
- I2C_Write(I_Temp);
- _delay_ms(5);
- I2C_Repeated_Start(Slave_Read_Address); /* Repeated Start I2C communication with SLA+R */
- _delay_ms(5);
- I2C_Stop();
- }
- }
Add Comment
Please, Sign In to add comment