void MPU_write_adress(unsigned char mpuAdress, unsigned char registerAdress, unsigned char value){ TWI_start(); TWI_check_ack(mpuAdress); TWI_write_data(registerAdress); TWI_write_data(value); TWI_stop(); }
void TWI_ScrollFrame(void) { uint8_t segindex=0,i,j; uint8_t str_Ptr[8]; uint8_t activeStatus = 1; while((segindex<8) && (NewMsgReceived==0)) { activeStatus = 1; NumberOfSegments = 1; for(i=0; (activeStatus) && (NewMsgReceived==0);i++) { for(j=0;j<MaxColPerSegment;j++) { str_Ptr[j] = FrameBuffer[(i*8)+j+segindex]; } if(i==0) { for(j=0;j<MaxColPerSegment;j++) { pattern[j] = str_Ptr[j]; } } else { activeStatus = TWI_start(); // Function to send start condition if(activeStatus) { activeStatus = TWI_write_address(i*2+0x30); // Function to write address and data direction bit(write) on SDA if(activeStatus) { NumberOfSegments++; for(j=0;j<8;j++) { TWI_write_data(str_Ptr[j]); // Function to write data in slave } } } TWI_stop(); // Function to send stop condition } } for(i=NumberOfSegments;i<MAX_DEVICES;i++) DELAY_ms(SCROLL_DELAY_MS); segindex++; } }
int main(void) { _delay_ms(2000); DDRB=0xff; TWI_init_master(); // Function to initialize TWI while(1) { if(write_data==0x00) write_data=1; TWI_start(); // Function to send start condition TWI_write_address(address+write); // Function to write address and data direction bit(write) on SDA TWI_write_data(255); // Function to write data in slave TWI_stop(); // Function to send stop condition _delay_ms(100); // Delay of 10 mili second PORTB=0xff; TWI_start(); TWI_read_address(address+read); // Function to write address and data direction bit(read) on SDA TWI_read_data(); // Function to read data from slave TWI_stop(); _delay_ms(2000); } }
void DisplayString(char *ptr_msgPointer) { int i,j; uint8_t activeStatus = 1; NumberOfSegments = 1; BuildFrame(ptr_msgPointer); for(i=0;activeStatus;i++) { if(i==0) { for(j=0;j<8;j++) pattern[j] = FrameBuffer[j]; } else { TWI_start(); // Function to send start condition activeStatus = TWI_write_address(i*2+0x30); // Function to write address and data direction bit(write) on SDA if(activeStatus) { NumberOfSegments++; for(j=0;j<8;j++) { TWI_write_data(FrameBuffer[i*8+j]); // Function to write data in slave // UART_TxChar(' '); // UART_TxHexNumber(i,2); // Function to write data in slave } } TWI_stop(); // Function to send stop condition } } }
void TWI_wait(char slave) { //send slave address until a slave ACKs it. Good for checking if the EEPROM //has finished a write operation. Use carefully! If the wrong slave address //is being waited for, this function will end in an infinite loop. do { TWI_start(); } while(TWI_write_data(slave) != 0x18); TWI_stop(); }
/////////////////Functions///////////////////////////////// void TWI_init_master(void) // Function to initialize master { TWBR=3; // Bit rate TWSR=(0<<TWPS1)|(0<<TWPS0); // Setting prescalar bits PORTD|=(1<<0)|(1<<1); // SCL freq= F_CPU/(16+2(TWBR).4^TWPS) TCCR2=0b00001011; TIMSK|=1<<OCIE2; OCR2=200; TWI_start(); TWI_write_address(0xD2); TWI_write_data(0x20); TWI_write_data(0b11101111); TWI_stop(); TWI_start(); TWI_write_address(0xD2); TWI_write_data(0x23); TWI_write_data(0b00010000); TWI_stop(); }
signed short int getdata(unsigned char add) { signed short int DATA; TWI_start(); TWI_write_address(0xD2); TWI_write_data(add); TWI_repeated_start(); TWI_read_address(0xD3); DATA=TWI_read_data(); TWI_stop(); return DATA; }
char EE_write_byte(const int address, const char data) { char dummy; //we need this for the first if() dummy = TWI_start(); //if the start was successful, continue, otherwise return 1 if((dummy != 0x08) && (dummy != 0x10)) return TWSR; //now send the EEPROM slave address together with the address bits 8..10 for page select //address format: //|bit7 |bit6 |bit5 |bit4 |bit3 |bit2 |bit1 |bit0 | //| EE_ADDR | page select | R/W | if(TWI_write_data(EE_ADDR|((address>>7) & 0x000E)) != 0x18) return TWSR; //now send the word address byte if(TWI_write_data((char)(address)) != 0x28) return TWSR; //now send the data byte if(TWI_write_data(data) != 0x28) return TWSR; //if everything was OK, return zero. return 0; }
void MPU_read_adress(unsigned char mpuAdress, unsigned char registerAdress, unsigned char* variable){ TWI_start(); TWI_check_ack(mpuAdress); TWI_write_data(registerAdress); TWI_repeated_start(); TWI_read_address(mpuAdress|0x01); *variable = TWI_read_data(); TWI_stop(); }
void MPU_self_test(unsigned char mpuAdress){ char buf[17]=" "; TWI_start(); TWI_check_ack(mpuAdress); TWI_write_data(0x75); TWI_repeated_start(); TWI_read_address(mpuAdress|0x01); char response = TWI_read_data(); sprintf(buf,"Response: %x",response); LCD_WriteText(buf); TWI_stop(); }
char EE_read_byte(const int address) { char dummy; //we need this for the first if() dummy = TWI_start(); //as in EE_write_byte, first send the page address and the word address if((dummy != 0x08) && (dummy != 0x10)) return TWSR; if(TWI_write_data(EE_ADDR|((address>>7) & 0x000E)) != 0x18) return TWSR; if(TWI_write_data((char)(address)) != 0x28) return TWSR; //send a repeated start for entering master receiver mode if(TWI_start() != 0x10) return TWSR; //send slave address, now with the read bit set if(TWI_write_data((EE_ADDR|1)|((address>>7) & 0x000E)) != 0x40) return TWSR; //now get the data from the EEPROM, don't return ACK if(TWI_read_data(0) != 0x58) return TWSR; //if everything was OK, return zero return 0; }