Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////////////////////////////////////////
- // Program for Master Mode
- #include
- #include
- #include
- void TWI_start(void);
- void TWI_repeated_start(void);
- void TWI_init_master(void);
- void TWI_write_address(unsigned char);
- void TWI_read_address(unsigned char);
- void TWI_write_data(unsigned char);
- void TWI_read_data(void);
- void TWI_stop(void);
- unsigned char address1=0×20,address2=0×08, read=1, write=0,address=0×20;unsigned char write_data=0xf0, recv_data;
- int c=0;int dt=0,i=0;
- int main(void)
- {
- _delay_ms(2000);
- DDRA=0xff;
- if(write_data==0×00)
- write_data=0xf0;
- TWI_init_master();
- TWI_start();
- TWI_write_address(address+write);
- TWI_write_data(write_data);dt=1;
- TWI_write_data(0x1f);dt=0;
- PORTA=0xff;
- address=0×08;
- c=1;
- TWI_repeated_start();
- TWI_write_address(address+write);
- TWI_write_data(write_data);dt=1;
- TWI_write_data(0x1f);dt=0;
- TWI_stop();
- while(1)
- {
- }
- }
- void TWI_init_master(void) // Function to initialize master
- {
- TWBR=0×12; // Bit rate
- TWSR=(0<<TWPS1)|(1<<TWPS0); // Setting prescalar bits
- // SCL freq= F_CPU/(16+2(TWBR).4^TWPS)
- }
- void TWI_start(void)
- {
- // Clear TWI interrupt flag, Put start condition on SDA, //Enable TWI
- TWCR= (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
- // Wait till start condition is transmitted
- while(!(TWCR & (1<<TWINT)));
- // Check for the acknowledgement
- while((TWSR & 0xF8)!= 0×08);
- }
- void TWI_repeated_start(void)
- {
- //Clear TWI interrupt flag, Put start condition on SDA, //Enable TWI
- TWCR= (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
- // wait till restart condition is transmitted
- while(!(TWCR & (1<<TWINT)));
- // Check for the acknoledgement
- while((TWSR & 0xF8)!= 0×10);
- }
- void TWI_write_address(unsigned char data)
- {
- TWDR=data; // Address and write instruction
- // Clear TWI interrupt flag,Enable TWI
- TWCR=(1<<TWINT)|(1<<TWEN);
- // Wait till complete TWDR byte transmitted
- while (!(TWCR & (1<<TWINT)));
- // Check for the acknoledgement
- while((TWSR & 0xF8)!= 0×18);
- }
- void TWI_read_address(unsigned char data)
- {
- TWDR=data; // Address and read instruction
- // Clear TWI interrupt flag,Enable TWI
- TWCR=(1<<TWINT)|(1<<TWEN);
- // Wait till complete TWDR byte received
- while (!(TWCR & (1<<TWINT)));
- // Check for the acknoledgement
- while((TWSR & 0xF8)!= 0×40);
- }
- void TWI_write_data(unsigned char data)
- {
- TWDR=data; // put data in TWDR
- // Clear TWI interrupt flag,Enable TWI
- TWCR=(1<<TWINT)|(1<<TWEN);
- // Wait till complete TWDR byte transmitted
- while (!(TWCR & (1<<TWINT)));
- // Check for the acknoledgement
- while((TWSR & 0xF8) != 0×28);
- }
- void TWI_read_data(void)
- {
- // Clear TWI interrupt flag,Enable TWI
- TWCR=(1<<TWINT)|(1<<TWEN);
- // Wait till complete TWDR byte transmitted
- while (!(TWCR & (1<<TWINT)));
- // Check for the acknoledgement
- while((TWSR & 0xF8) != 0×58);
- recv_data=TWDR;
- PORTA=recv_data;
- }
- void TWI_stop(void)
- {
- // Clear TWI interrupt flag, Put stop condition on SDA, //Enable TWI
- TWCR= (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
- // Wait till stop condition is transmitted
- while(!(TWCR & (1<<TWSTO)));
- }
- ////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement