Skip to content

Hinweise zum Beleg

Aufgabenstellung

https://bildungsportal.sachsen.de/opal/auth/RepositoryEntry/12427591700/CourseNode/94390817219110/Aufgabenstellung_SS2021.pdf

Beispiel-Programm

#include <msp430.h>
#include <stdint.h>

void i2c_write(uint8_t address, const uint8_t* data, uint8_t len)
{
    // Warten, bis Bus frei
    while (UCB0CTL1 & UCTXSTP)
    {
    }

    UCB0I2CSA = address; // Slave Adresse setzen
    UCB0CTL1 |= UCTR + UCTXSTT;      // W-Mode, Start der Übertragung

    // Senden der Datenbytes
    for (uint8_t i = 0; i < len; i++)
    {
        while (!(IFG2 & UCB0TXIFG))
        {
        }

        UCB0TXBUF = data[i];
    }

    // Senden der Stoppsequenz
    while (!(IFG2 & UCB0TXIFG))
    {
    }
    UCB0CTL1 |= UCTXSTP;
}

void i2c_read(uint8_t address, uint8_t* data, uint8_t len)
{
    // Warten, bis Bus frei
    while (UCB0CTL1 & UCTXSTP)
    {
    }

    UCB0I2CSA = address; // Slave Adresse setzen
    UCB0CTL1 &= ~UCTR;  // R-Mode
    UCB0CTL1 |= UCTXSTT; // Start der Übertragung

    // Warte, bis Startsequenz gesendet
    while (UCB0CTL1 & UCTXSTT)
    {
    }

    // Empfangen der Datenbytes
    for (uint8_t i = 0; i < len; i++)
    {
        if (i == len - 1)
        {
            // Setzen der Stop-Bits, während des Empfangen des letzten Bytes
            UCB0CTL1 |= UCTXSTP;
        }

        while (!(IFG2 & UCB0RXIFG))
        {
        }

        data[i] = UCB0RXBUF;
    }
}

void uart_print(char *str)
{
    while (*str != 0)
    {
        while (!(IFG2 & UCA0TXIFG))
        {
        }
        UCA0TXBUF = *str;
        *str++;
    }
}

void uart_hex(uint8_t number)
{
    char str[3];
    for (uint8_t i = 0; i < 2; i++)
    {
        uint8_t digit = number & 0xf;
        if (digit < 10)
        {
            str[1 - i] = '0' + digit;
        }
        else
        {
            str[1 - i] = 'a' + digit - 10;
        }
        number >>= 4;
    }

    str[2] = 0;
    uart_print(str);
}

int main(void)
{
    WDTCTL = WDTPW | WDTHOLD;    // stop watchdog timer

    BCSCTL1 = CALBC1_1MHZ;
    DCOCTL = CALDCO_1MHZ;

    // Pins
    P1SEL |= BIT2 + BIT6 + BIT7;
    P1SEL2 |= BIT2 + BIT6 + BIT7;

    // UART
    UCB0CTL1 |= UCSWRST;                // Soft-Reset des I2C-Moduls
    UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC;   // Master, I2C, Synchron
    UCB0BR0 = 20;   // Teiler: 8 MHz / 20 = 400 kHz
    UCB0BR1 = 0;
    UCB0CTL1 = UCSSEL_2; // Reset-Loslassen, SMCLK

    // I2C
    UCA0CTL1 |= UCSWRST;
    UCA0CTL0 = 0;
    UCA0CTL1 |= UCSSEL_2;
    UCA0BR0 = 104;
    UCA0BR1 = 0;
    UCA0MCTL = (1 << 1);
    UCA0CTL1 &= ~UCSWRST;

    __enable_interrupt();

    uint8_t buff[8];

    // ID Test
    uart_print("ID (0x92) = ");
    buff[0] = 0x92;
    i2c_write(0x39, buff, 1);
    i2c_read(0x39, buff, 1);
    uart_hex(buff[0]);
    uart_print("\r\n");

    // PON
    buff[0] = 0x80;
    buff[1] = BIT2 + BIT1 + BIT0;
    i2c_write(0x39, buff, 2);

    // CONTROL
    buff[0] = 0x8F;
    buff[1] = BIT7 + BIT6 + BIT3 + BIT2 + BIT1 + BIT0;
    i2c_write(0x39, buff, 2);

    while (1)
    {
        // Proximity
        uart_print("PDATA = ");
        buff[0] = 0x9C;
        i2c_write(0x39, buff, 1);
        i2c_read(0x39, buff, 1);
        uart_hex(buff[0]);

        // COLOR
        buff[0] = 0x94;
        i2c_write(0x39, buff, 1);
        i2c_read(0x39, buff, 8);

        uart_print("  CDATA = ");
        uart_hex(buff[1]);
        uart_hex(buff[0]);

        uart_print("  RDATA = ");
        uart_hex(buff[3]);
        uart_hex(buff[2]);

        uart_print("  GDATA = ");
        uart_hex(buff[5]);
        uart_hex(buff[4]);

        uart_print("  BDATA = ");
        uart_hex(buff[7]);
        uart_hex(buff[6]);

        uart_print("\r\n");

        __delay_cycles(100000L);
    }
}