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

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;
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