Lab 7 Report EEL4742 (1)

pdf

School

University of Central Florida *

*We aren’t endorsed by this school

Course

4742

Subject

Electrical Engineering

Date

Apr 3, 2024

Type

pdf

Pages

21

Uploaded by BaronOxideSandpiper36

Report
1 Lab 7 Report Davi Dantas Da646168@ucf.edu EEL4742C - 419: Embedded Systems
2 Introduction For this lab, I improved my understanding of I2C. How to implement I2C and use itto have communication between the microcontroller MSP430FR6989 and PC. This lab also required me to use the family guide and gain more familiarity with the manuals as well as how to configure the light sensor in order to have the readings as required. Part 7.1 For the first part of the lab, I implemented communication so the Master could read the Device and Manufacture ID in a continuous loop. #include <msp430fr6989.h> #include <string.h> #include <stdlib.h> #define FLAGS UCA1IFG // Contains the transmit & receive flags #define RXFLAG UCRXIFG // Receive flag #define TXFLAG UCTXIFG // Transmit flag #define TXBUFFER UCA1TXBUF // Transmit buffer #define RXBUFFER UCA1RXBUF // Receive buffer void Initialize_UART ( void ); // Initialize UART function void uart_write_char ( unsigned char ch); // Write UART function void uart_write_uint16 ( unsigned int ); // Display digits void uart_write_string ( unsigned char str[]); void Initialize_I2C ( void ); int i2c_read_word ( unsigned char i2c_address, unsigned char i2c_reg, unsigned int *data); int i2c_write_word ( unsigned char i2c_address, unsigned char i2c_reg, unsigned int data); int i2c_read_word ( unsigned char , unsigned char , unsigned int *); int i2c_write_word ( unsigned char , unsigned char , unsigned int ); void main () { WDTCTL = WDTPW | WDTHOLD; PM5CTL0 &= ~LOCKLPM5; volatile unsigned int i; unsigned int data = 0xABCD; Initialize_UART(); Initialize_I2C(); while (1) { i2c_read_word(0x22, 0x7E, &data); // Read the manufacturer ID uart_write_string( "\r\nThe manufacturer ID is: " ); uart_write_uint16(data); // Write value to serial port i2c_read_word(0x22, 0x7F, &data); // Read the Device ID uart_write_string( "\r\nThe Device ID is: " ); uart_write_uint16(data); // Write value to serial port for (i = 0; i < 100000; i++) {} } }
3 // Configure UART to the popular configuration // 9600 baud, 8-bit data, LSB first, no parity bits, 1 stop bit // no flow control, oversampling reception // Clock: SMCLK @ 1 MHz (1,000,000 Hz) void Initialize_UART ( void ) { // Configure pins to UART functionality P3SEL1 &= ~(BIT4 | BIT5); P3SEL0 |= (BIT4 | BIT5); // Main configuration register UCA1CTLW0 = UCSWRST; // Engage reset; change all the fields to zero // Most fields in this register, when set to zero, correspond to the // popular configuration UCA1CTLW0 |= UCSSEL_2; // Set clock to SMCLK // Configure the clock dividers and modulators (and enable oversampling) UCA1BRW = 6; // divider // Modulators: UCBRF = 8 = 1000 --> UCBRF3 (bit #3) // UCBRS = 0x20 = 0010 0000 = UCBRS5 (bit #5) UCA1MCTLW = UCBRF3 | UCBRS5 | UCOS16; // Exit the reset state UCA1CTLW0 &= ~UCSWRST; } // The function returns the byte; if none received, returns null character unsigned char uart_read_char ( void ) { unsigned char temp; // Return null character (ASCII=0) if no byte was received if ((FLAGS & RXFLAG) == 0) return 0; // Otherwise, copy the received byte (this clears the flag) and return it temp = RXBUFFER; return temp; } void uart_write_char ( unsigned char ch) { // Wait for any ongoing transmission to complete while ((FLAGS & TXFLAG) == 0) {} // Copy the byte to the transmit buffer TXBUFFER = ch; // Tx flag goes to 0 and Tx begins! } void Initialize_I2C ( void ) { // Configure the MCU in Master mode // Configure pins to I2C functionality // (UCB1SDA same as P4.0) (UCB1SCL same as P4.1) // (P4SEL1=11, P4SEL0=00) (P4DIR=xx) P4SEL1 |= (BIT1 | BIT0); P4SEL0 &= ~(BIT1 | BIT0); // Enter reset state and set all fields in this register to zero UCB1CTLW0 = UCSWRST; // Fields that should be nonzero are changed below // (Master Mode: UCMST) (I2C mode: UCMODE_3) (Synchronous mode: UCSYNC) // (UCSSEL 1:ACLK, 2,3:SMCLK) UCB1CTLW0 |= UCMST | UCMODE_3 | UCSYNC | UCSSEL_3;
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
4 // Clock frequency: SMCLK/8 = 1 MHz/8 = 125 KHz UCB1BRW = 8; // Chip Data Sheet p. 53 (Should be 400 KHz max) // Exit the reset mode at the end of the configuration UCB1CTLW0 &= ~UCSWRST; } void uart_write_uint16 ( unsigned int n) { int digit; // Variable to hold each digit of the number // Extract and transmit each digit of the number // Check if the number is greater than or equal to 10000 if (n >= 10000) { digit = (n / 10000) % 10; // Extract the ten-thousands digit uart_write_char( '0' + digit); // Transmit the ten-thousands digit } else if (n >= 1000) { // Check if the number is greater than or equal to 1000 digit = (n / 1000) % 10; // Extract the thousands digit uart_write_char( '0' + digit); // Transmit the thousands digit } else if (n >= 100) { // Check if the number is greater than or equal to 100 digit = (n / 100) % 10; // Extract the hundreds digit uart_write_char( '0' + digit); // Transmit the hundreds digit } else if (n >= 10) { // Check if the number is greater than or equal to 10 digit = (n / 10) % 10; // Extract the tens digit uart_write_char( '0' + digit); // Transmit the tens digit } else { // For numbers less than 10 digit = n % 10; // Extract the ones digit uart_write_char( '0' + digit); // Transmit the ones digit } } // Read a word (2 bytes) from I2C (address, register) int i2c_read_word ( unsigned char i2c_address, unsigned char i2c_reg, unsigned int *data) { unsigned char byte1, byte2; // Initialize the bytes to make sure data is received every time byte1 = 111; byte2 = 111; //********** Write Frame #1 *************************** UCB1I2CSA = i2c_address; // Set I2C address UCB1IFG &= ~UCTXIFG0; UCB1CTLW0 |= UCTR; // Master writes (R/W bit = Write) UCB1CTLW0 |= UCTXSTT; // Initiate the Start Signal while ((UCB1IFG & UCTXIFG0) == 0) {} UCB1TXBUF = i2c_reg; // Byte = register address while ((UCB1CTLW0 & UCTXSTT) != 0) {} if ((UCB1IFG & UCNACKIFG) != 0) return -1; UCB1CTLW0 &= ~UCTR; // Master reads (R/W bit = Read) UCB1CTLW0 |= UCTXSTT; // Initiate a repeated Start Signal //**************************************************** //********** Read Frame #1 *************************** while ((UCB1IFG & UCRXIFG0) == 0) {} byte1 = UCB1RXBUF; //**************************************************** //********** Read Frame #2 *************************** while ((UCB1CTLW0 & UCTXSTT) != 0) {} UCB1CTLW0 |= UCTXSTP; // Setup the Stop Signal
5 while ((UCB1IFG & UCRXIFG0) == 0) {} byte2 = UCB1RXBUF; while ((UCB1CTLW0 & UCTXSTP) != 0) {} //**************************************************** // Merge the two received bytes *data = ((byte1 << 8) | (byte2 & 0xFF)); return 0; } // Write a word (2 bytes) to I2C (address, register) int i2c_write_word ( unsigned char i2c_address, unsigned char i2c_reg, unsigned int data) { unsigned char byte1, byte2; byte1 = (data >> 8) & 0xFF; // MSByte byte2 = data & 0xFF; // LSByte UCB1I2CSA = i2c_address; // Set I2C address UCB1CTLW0 |= UCTR; // Master writes (R/W bit = Write) UCB1CTLW0 |= UCTXSTT; // Initiate the Start Signal while ((UCB1IFG & UCTXIFG0) == 0) {} UCB1TXBUF = i2c_reg; // Byte = register address while ((UCB1CTLW0 & UCTXSTT) != 0) {} while ((UCB1IFG & UCTXIFG0) == 0) {} //********** Write Byte #1 *************************** UCB1TXBUF = byte1; while ((UCB1IFG & UCTXIFG0) == 0) {} //********** Write Byte #2 *************************** UCB1TXBUF = byte2; while ((UCB1IFG & UCTXIFG0) == 0) {} UCB1CTLW0 |= UCTXSTP; while ((UCB1CTLW0 & UCTXSTP) != 0) {} return 0; } void uart_write_string ( unsigned char str[]) { int index; // Reads the string until it is NULL for (index = 0; index < strlen (str) + 1; index++) { uart_write_char(str[index]); } } The address is chosen by the schematic given in binary, you can also use hex 0x44 . The Manufacture ID is 5449(hex) so it will display 21577 in decimal. The Device ID is 3001 (hex) , it will covert to 12289 in decimal.
6 Part 7.2 For this code, I needed to configure the light sensor in order to provide the readings according to the given specifications. As light got dimmer or brighter, the reading clearly change as expected. #include <msp430fr6989.h> #include <string.h> #include <stdlib.h> // UART definitions #define FLAGS UCA1IFG // Contains the transmit & receive flags #define RXFLAG UCRXIFG // Receive flag #define TXFLAG UCTXIFG // Transmit flag #define TXBUFFER UCA1TXBUF // Transmit buffer #define RXBUFFER UCA1RXBUF // Receive buffer // Function prototypes void Initialize_UART ( void ); void uart_write_char ( unsigned char ch); void uart_write_uint16 ( unsigned int ); void uart_write_string ( unsigned char str[]); void Initialize_I2C ( void ); int i2c_read_word ( unsigned char , unsigned char , unsigned int *); int i2c_write_word ( unsigned char , unsigned char , unsigned int ); // Main function void main () { WDTCTL = WDTPW | WDTHOLD; PM5CTL0 &= ~LOCKLPM5; volatile unsigned int i; int j = 1; unsigned int data; Initialize_UART(); Initialize_I2C(); while (1) { i2c_write_word(0x44, 0x01, 0x7604); uart_write_string( "\r\nLux value: " ); i2c_read_word(0x44, 0x00, &data); uart_write_uint16(data * 1.28); uart_write_string( "\r\n#" ); uart_write_uint16(j); j++; for (i = 0; i < 60000; i++) {} } } // Configure UART to the popular configuration // 9600 baud, 8-bit data, LSB first, no parity bits, 1 stop bit // no flow control, oversampling reception // Clock: SMCLK @ 1 MHz (1,000,000 Hz) void Initialize_UART ( void ) { // Configure pins to UART functionality P3SEL1 &= ~(BIT4 | BIT5); P3SEL0 |= (BIT4 | BIT5);
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
7 // Main configuration register UCA1CTLW0 = UCSWRST; // Engage reset; change all the fields to zero // Most fields in this register, when set to zero, correspond to the // popular configuration UCA1CTLW0 |= UCSSEL_2; // Set clock to SMCLK // Configure the clock dividers and modulators (and enable oversampling) UCA1BRW = 6; // divider // Modulators: UCBRF = 8 = 1000 --> UCBRF3 (bit #3) // UCBRS = 0x20 = 0010 0000 = UCBRS5 (bit #5) UCA1MCTLW = UCBRS5 | UCBRF3 | UCOS16; // Exit the reset state UCA1CTLW0 &= ~UCSWRST; } //// The function returns the byte; if none received, returns null character //unsigned char uart_read_char(void) { // unsigned char temp; // // // Return null character (ASCII=0) if no byte was received // if ((FLAGS & RXFLAG) == 0) // return 0; // // // Otherwise, copy the received byte (this clears the flag) and return it // temp = RXBUFFER; // return temp; //} void uart_write_char ( unsigned char ch) { // Wait for any ongoing transmission to complete while ((FLAGS & TXFLAG) == 0) {} // Copy the byte to the transmit buffer TXBUFFER = ch; // Tx flag goes to 0 and Tx begins! } void Initialize_I2C ( void ) { // Configure the MCU in Master mode // Configure pins to I2C functionality // (UCB1SDA same as P4.0) (UCB1SCL same as P4.1) // (P4SEL1=11, P4SEL0=00) (P4DIR=xx) P4SEL1 |= (BIT1 | BIT0); P4SEL0 &= ~(BIT1 | BIT0); // Enter reset state and set all fields in this register to zero UCB1CTLW0 = UCSWRST; // Fields that should be nonzero are changed below // (Master Mode: UCMST) (I2C mode: UCMODE_3) (Synchronous mode: UCSYNC) // (UCSSEL 1:ACLK, 2,3:SMCLK) UCB1CTLW0 |= UCMST | UCMODE_3 | UCSYNC | UCSSEL_3; // Clock frequency: SMCLK/8 = 1 MHz/8 = 125 KHz UCB1BRW = 8; // Chip Data Sheet p. 53 (Should be 400 KHz max) // Exit the reset mode at the end of the configuration UCB1CTLW0 &= ~UCSWRST; } void uart_write_uint16 ( unsigned int n) { int digit; // Variable to hold each digit of the number
8 // Extract and transmit each digit of the number // Check if the number is greater than or equal to 10000 if (n >= 10000) { digit = (n / 10000) % 10; // Extract the ten-thousands digit uart_write_char( '0' + digit); // Transmit the ten-thousands digit } if (n >= 1000) { // Check if the number is greater than or equal to 1000 digit = (n / 1000) % 10; // Extract the thousands digit uart_write_char( '0' + digit); // Transmit the thousands digit } if (n >= 100) { // Check if the number is greater than or equal to 100 digit = (n / 100) % 10; // Extract the hundreds digit uart_write_char( '0' + digit); // Transmit the hundreds digit } if (n >= 10) { // Check if the number is greater than or equal to 10 digit = (n / 10) % 10; // Extract the tens digit uart_write_char( '0' + digit); // Transmit the tens digit } //else { // For numbers less than 10 digit = n % 10; // Extract the ones digit uart_write_char( '0' + digit); // Transmit the ones digit //} } // Read a word (2 bytes) from I2C (address, register) int i2c_read_word ( unsigned char i2c_address, unsigned char i2c_reg, unsigned int *data) { unsigned char byte1, byte2; // Initialize the bytes to make sure data is received every time byte1 = 111; byte2 = 111; //********** Write Frame #1 *************************** UCB1I2CSA = i2c_address; // Set I2C address UCB1IFG &= ~UCTXIFG0; UCB1CTLW0 |= UCTR; // Master writes (R/W bit = Write) UCB1CTLW0 |= UCTXSTT; // Initiate the Start Signal while ((UCB1IFG & UCTXIFG0) == 0) {} UCB1TXBUF = i2c_reg; // Byte = register address while ((UCB1CTLW0 & UCTXSTT) != 0) {} if ((UCB1IFG & UCNACKIFG) != 0) return -1; UCB1CTLW0 &= ~UCTR; // Master reads (R/W bit = Read) UCB1CTLW0 |= UCTXSTT; // Initiate a repeated Start Signal //**************************************************** //********** Read Frame #1 *************************** while ((UCB1IFG & UCRXIFG0) == 0) {} byte1 = UCB1RXBUF; //**************************************************** //********** Read Frame #2 *************************** while ((UCB1CTLW0 & UCTXSTT) != 0) {} UCB1CTLW0 |= UCTXSTP; // Setup the Stop Signal while ((UCB1IFG & UCRXIFG0) == 0) {} byte2 = UCB1RXBUF; while ((UCB1CTLW0 & UCTXSTP) != 0) {} //**************************************************** // Merge the two received bytes *data = ((byte1 << 8) | (byte2 & 0xFF)); return 0; }
9 // Write a word (2 bytes) to I2C (address, register) int i2c_write_word ( unsigned char i2c_address, unsigned char i2c_reg, unsigned int data) { unsigned char byte1, byte2; byte1 = (data >> 8) & 0xFF; // MSByte byte2 = data & 0xFF; // LSByte UCB1I2CSA = i2c_address; // Set I2C address UCB1CTLW0 |= UCTR; // Master writes (R/W bit = Write) UCB1CTLW0 |= UCTXSTT; // Initiate the Start Signal while ((UCB1IFG & UCTXIFG0) == 0) {} UCB1TXBUF = i2c_reg; // Byte = register address while ((UCB1CTLW0 & UCTXSTT) != 0) {} while ((UCB1IFG & UCTXIFG0) == 0) {} //********** Write Byte #1 *************************** UCB1TXBUF = byte1; while ((UCB1IFG & UCTXIFG0) == 0) {} //********** Write Byte #2 *************************** UCB1TXBUF = byte2; while ((UCB1IFG & UCTXIFG0) == 0) {} UCB1CTLW0 |= UCTXSTP; while ((UCB1CTLW0 & UCTXSTP) != 0) {} return 0; } // Write a string to UART void uart_write_string ( unsigned char str[]) { int index; for (index = 0; index < strlen (str); index++) { uart_write_char(str[index]); } } The address of the configuration sensor is 0x01 I used the configuration 0x7604. This value is represented from RN=0111, CT=0, M=11, ME=1 which equals 0111 0110 0000 0100. The sensors reading seems reasonable and consistent.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
1 0 7.3 #include <msp430fr6989.h> #include <string.h> #include <stdlib.h> // UART definitions #define FLAGS UCA1IFG // Contains the transmit & receive flags #define RXFLAG UCRXIFG // Receive flag #define TXFLAG UCTXIFG // Transmit flag #define TXBUFFER UCA1TXBUF // Transmit buffer #define RXBUFFER UCA1RXBUF // Receive buffer // Function prototypes void Initialize_UART ( void ); void uart_write_char ( unsigned char ch); void uart_write_uint16 ( unsigned int ); void uart_write_string ( unsigned char str[]); void Initialize_I2C ( void ); int i2c_read_word ( unsigned char , unsigned char , unsigned int *); int i2c_write_word ( unsigned char , unsigned char , unsigned int ); // Main function void main () { WDTCTL = WDTPW | WDTHOLD; PM5CTL0 &= ~LOCKLPM5; volatile unsigned int i; int j = 1; unsigned int current_time = 0; unsigned int low_limit = 0; unsigned int high_limit = 0; unsigned int data; unsigned char limits_set = 0; // Flag to indicate if limits are set Initialize_UART(); Initialize_I2C(); // Initialize time at startup set_time(); while (1) { // Read lux value i2c_write_word(0x44, 0x01, 0x7604); i2c_read_word(0x44, 0x00, &data); // Check if lux value is out of range if (data < low_limit - 10) {
1 1 uart_write_string( "<Down>" ); low_limit = data - 10; high_limit = data + 10; } else if (data > high_limit + 10) { uart_write_string( "<Up>" ); low_limit = data - 10; high_limit = data + 10; } // Write current time and lux value to UART uart_write_string( "\r\n" ); uart_write_uint16(current_time); uart_write_string( " " ); uart_write_uint16(data); // Delay for approximately one minute delay_ms(60000); // Increment time current_time++; } } // Configure UART to the popular configuration // 9600 baud, 8-bit data, LSB first, no parity bits, 1 stop bit // no flow control, oversampling reception // Clock: SMCLK @ 1 MHz (1,000,000 Hz) void Initialize_UART ( void ) { // Configure pins to UART functionality P3SEL1 &= ~(BIT4 | BIT5); P3SEL0 |= (BIT4 | BIT5); // Main configuration register UCA1CTLW0 = UCSWRST; // Engage reset; change all the fields to zero // Most fields in this register, when set to zero, correspond to the // popular configuration UCA1CTLW0 |= UCSSEL_2; // Set clock to SMCLK // Configure the clock dividers and modulators (and enable oversampling) UCA1BRW = 6; // divider // Modulators: UCBRF = 8 = 1000 --> UCBRF3 (bit #3) // UCBRS = 0x20 = 0010 0000 = UCBRS5 (bit #5) UCA1MCTLW = UCBRS5 | UCBRF3 | UCOS16; // Exit the reset state UCA1CTLW0 &= ~UCSWRST; } //// The function returns the byte; if none received, returns null character
1 2 //unsigned char uart_read_char(void) { // unsigned char temp; // // // Return null character (ASCII=0) if no byte was received // if ((FLAGS & RXFLAG) == 0) // return 0; // // // Otherwise, copy the received byte (this clears the flag) and return it // temp = RXBUFFER; // return temp; //} void uart_write_char ( unsigned char ch) { // Wait for any ongoing transmission to complete while ((FLAGS & TXFLAG) == 0) {} // Copy the byte to the transmit buffer TXBUFFER = ch; // Tx flag goes to 0 and Tx begins! } void Initialize_I2C ( void ) { // Configure the MCU in Master mode // Configure pins to I2C functionality // (UCB1SDA same as P4.0) (UCB1SCL same as P4.1) // (P4SEL1=11, P4SEL0=00) (P4DIR=xx) P4SEL1 |= (BIT1 | BIT0); P4SEL0 &= ~(BIT1 | BIT0); // Enter reset state and set all fields in this register to zero UCB1CTLW0 = UCSWRST; // Fields that should be nonzero are changed below // (Master Mode: UCMST) (I2C mode: UCMODE_3) (Synchronous mode: UCSYNC) // (UCSSEL 1:ACLK, 2,3:SMCLK) UCB1CTLW0 |= UCMST | UCMODE_3 | UCSYNC | UCSSEL_3; // Clock frequency: SMCLK/8 = 1 MHz/8 = 125 KHz UCB1BRW = 8; // Chip Data Sheet p. 53 (Should be 400 KHz max) // Exit the reset mode at the end of the configuration UCB1CTLW0 &= ~UCSWRST; } void uart_write_uint16 ( unsigned int n) { int digit; // Variable to hold each digit of the number // Extract and transmit each digit of the number // Check if the number is greater than or equal to 10000
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
1 3 if (n >= 10000) { digit = (n / 10000) % 10; // Extract the ten-thousands digit uart_write_char( '0' + digit); // Transmit the ten-thousands digit } if (n >= 1000) { // Check if the number is greater than or equal to 1000 digit = (n / 1000) % 10; // Extract the thousands digit uart_write_char( '0' + digit); // Transmit the thousands digit } if (n >= 100) { // Check if the number is greater than or equal to 100 digit = (n / 100) % 10; // Extract the hundreds digit uart_write_char( '0' + digit); // Transmit the hundreds digit } if (n >= 10) { // Check if the number is greater than or equal to 10 digit = (n / 10) % 10; // Extract the tens digit uart_write_char( '0' + digit); // Transmit the tens digit } //else { // For numbers less than 10 digit = n % 10; // Extract the ones digit uart_write_char( '0' + digit); // Transmit the ones digit //} } // Read a word (2 bytes) from I2C (address, register) int i2c_read_word ( unsigned char i2c_address, unsigned char i2c_reg, unsigned int *data) { unsigned char byte1, byte2; // Initialize the bytes to make sure data is received every time byte1 = 111; byte2 = 111; //********** Write Frame #1 *************************** UCB1I2CSA = i2c_address; // Set I2C address UCB1IFG &= ~UCTXIFG0; UCB1CTLW0 |= UCTR; // Master writes (R/W bit = Write) UCB1CTLW0 |= UCTXSTT; // Initiate the Start Signal while ((UCB1IFG & UCTXIFG0) == 0) {} UCB1TXBUF = i2c_reg; // Byte = register address while ((UCB1CTLW0 & UCTXSTT) != 0) {} if ((UCB1IFG & UCNACKIFG) != 0) return -1; UCB1CTLW0 &= ~UCTR; // Master reads (R/W bit = Read) UCB1CTLW0 |= UCTXSTT; // Initiate a repeated Start Signal //**************************************************** //********** Read Frame #1 *************************** while ((UCB1IFG & UCRXIFG0) == 0) {} byte1 = UCB1RXBUF; //****************************************************
1 4 //********** Read Frame #2 *************************** while ((UCB1CTLW0 & UCTXSTT) != 0) {} UCB1CTLW0 |= UCTXSTP; // Setup the Stop Signal while ((UCB1IFG & UCRXIFG0) == 0) {} byte2 = UCB1RXBUF; while ((UCB1CTLW0 & UCTXSTP) != 0) {} //**************************************************** // Merge the two received bytes *data = ((byte1 << 8) | (byte2 & 0xFF)); return 0; } // Write a word (2 bytes) to I2C (address, register) int i2c_write_word ( unsigned char i2c_address, unsigned char i2c_reg, unsigned int data) { unsigned char byte1, byte2; byte1 = (data >> 8) & 0xFF; // MSByte byte2 = data & 0xFF; // LSByte UCB1I2CSA = i2c_address; // Set I2C address UCB1CTLW0 |= UCTR; // Master writes (R/W bit = Write) UCB1CTLW0 |= UCTXSTT; // Initiate the Start Signal while ((UCB1IFG & UCTXIFG0) == 0) {} UCB1TXBUF = i2c_reg; // Byte = register address while ((UCB1CTLW0 & UCTXSTT) != 0) {} while ((UCB1IFG & UCTXIFG0) == 0) {} //********** Write Byte #1 *************************** UCB1TXBUF = byte1; while ((UCB1IFG & UCTXIFG0) == 0) {} //********** Write Byte #2 *************************** UCB1TXBUF = byte2; while ((UCB1IFG & UCTXIFG0) == 0) {} UCB1CTLW0 |= UCTXSTP; while ((UCB1CTLW0 & UCTXSTP) != 0) {} return 0; } // Write a string to UART void uart_write_string ( unsigned char str[]) { int index; for (index = 0; index < strlen (str); index++) { uart_write_char(str[index]); } return ; }
1 5 // Set the time void set_time ( void ) { unsigned int hours = 0; unsigned int minutes = 0; unsigned int tens, units; // Prompt the user to enter the time uart_write_string( "Enter the time...(3 or 4 digits then hit Enter)\r\n" ); // Read hours tens = (RXBUFFER - '0' ) * 10; while ((RXBUFFER = UCA1RXBUF) != '\r' ) { units = RXBUFFER - '0' ; if (units >= 0 && units <= 9) { hours = tens + units; tens = units * 10; } } // Read minutes tens = (RXBUFFER - '0' ) * 10; while ((RXBUFFER = UCA1RXBUF) != '\r' ) { units = RXBUFFER - '0' ; if (units >= 0 && units <= 9) { minutes = tens + units; tens = units * 10; } } // Print the set time uart_write_string( "Time is set to " ); uart_write_uint16(hours); uart_write_char( ':' ); uart_write_uint16(minutes); } // Delay for milliseconds // Delay for milliseconds void delay_ms ( unsigned int ms) { unsigned int cycles_per_ms = 1000; // For 1 MHz clock frequency unsigned int cycles = ms * cycles_per_ms; while (cycles--) { __delay_cycles (1); } }
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
1 6
1 Student Q & A 1. The light sensor has an address pin that allows customizing the I2C address. How many addresses are possible? What are they and how are they configured? Look in the sensor’s data sheet a. b. c. The above the table is the information I found for the 16 BIT for the configuration Pin. There are 11 BIT that can be written, so there are 2^11 possible addresses. They are configured by setting the desired function to the right address as explained in the pictures below.
1 d.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
1 e.
1 f. 2. According to the light sensor’s data sheet, what should be the value of the pull-up resistors on the I2C wires? Did the BoosterPack use the same values? a. The typical value for the puulup resistor is 10K ohms. The pullup resistor for the booster is is typically 35K Ohms and is between 20-50K Ohms. 3. What I2C clock frequency do each of the eUSCI module and the sensor support? a. b.
1 c. d. Conclusion In conclusion, this lab was a great opportunity to practice my knowledge of I2C communication and how to configure the light sensor from the booster module. Although the application exercise was very challenging, the lab provided with the basic knowledge of I2C.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help