void main (void) { //init LCD LCD_init(); //set TWBR = 32 for 100kHz SCL @ 8MHz TWI_init(32, 0); //write 0x55 @ 513 and print return value on LCD LCD_puthex(EE_write_byte(513, 0x55)); //send stop TWI_stop(); LCD_wait(); LCD_putchar(' '); //wait for the EEPROM to finish the write operation TWI_wait(EE_ADDR); //read the write location again and print return code on LCD LCD_puthex(EE_read_byte(513)); LCD_wait(); LCD_putchar(' '); //print the value read from the EEPROM on the LCD LCD_puthex(TWDR); TWI_stop(); //LCD should now show "0x00 0x00 0x55_" //where the _ is the blinking cursor. }
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_write_int( uint8_t SLA, uint8_t data) { TWI_start(); TWI_write(SLA); TWI_write(data); TWI_stop(); }
int main(void) { unsigned char write_data=0x75, recv_data; int testn=0; USART_init(); print_UART(" This is the test for TWI "); _delay_ms(1000); PORTD |= 0x80; ///turn led on _delay_ms(100); TWI_init_master(); sei(); print_UART(" Sending Start condition "); TWI_start(); print_UART(" address= %x ", ((address<<1)|write)); TWI_write_address((address<<1)|write); print_UART(" writing register = %x ", write_data); TWI_write_byte(write_data); //write register print_UART(" Repeat Start "); TWI_start(); //repeated start print_UART(" address read= %x ", ((address<<1)|read)); TWI_write_address((address<<1)|read); print_UART(" read Data "); recv_data=TWI_read_byte(1); ///read byte and nack TWI_stop(); print_UART(" This is the value of the register= %x ", recv_data); PORTD &= 0x7F; return 0; }
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++; } }
// funkcja do odczytu z EXPANDERA PCF8574 ********************* uint8_t PCF8574_read( uint8_t SLA ) { uint8_t res=0; TWI_start(); TWI_write(SLA + 1); res = TWI_read( NACK ); TWI_stop(); return res; }
void TWI_write_buf( uint8_t SLA, uint8_t adr, uint8_t len, uint8_t *buf) { TWI_start(); TWI_write(SLA); TWI_write(adr); while (len--) TWI_write(*buf++); TWI_stop(); }
void sfp_read_vendor_name(uint8_t *buf) { uint8_t len=15; TWI_start(); TWI_write(0xa0); TWI_write(0x14); TWI_start(); TWI_write(0xa1); while (len--) *buf++ = TWI_read( len ? ACK : NACK ); TWI_stop(); }
void sfp_read_to_buf(uint8_t SLA, uint8_t len, uint8_t *buf) { TWI_start(); TWI_write(SLA); TWI_write(0xa0); TWI_start(); TWI_write(SLA + 1); while (len--) *buf++ = TWI_read( len ? ACK : NACK ); TWI_stop(); }
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(); }
void TWI_read_buf(uint8_t SLA, uint8_t adr, uint8_t len, uint8_t *buf) { TWI_start(); TWI_write(SLA); TWI_write(adr); TWI_start(); TWI_write(SLA + 1); while (len--) *buf++ = TWI_read( len ? ACK : NACK ); TWI_stop(); }
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(); }
uint8_t TWI_read_int(uint8_t SLA) { uint8_t data; TWI_start(); TWI_write(SLA); TWI_start(); TWI_write(SLA + 1); data = TWI_read(NACK); TWI_stop(); return data; }
/////////////////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; }
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 EI2C_read_buf(uint8_t device, uint16_t subAddr, uint16_t len, uint8_t *buf) { while (len--) { TWI_start(); TWI_write(device | ((subAddr>>8)<<1) ); TWI_write(subAddr); TWI_start(); TWI_write(device + 1); *buf++ = TWI_read( NACK ); TWI_stop(); subAddr++; } }
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(); }