diff --git a/Gyrosensor.uvprojx b/Gyrosensor.uvprojx index bf6929e..40f28ef 100644 --- a/Gyrosensor.uvprojx +++ b/Gyrosensor.uvprojx @@ -389,10 +389,32 @@ 1 .\main.c + + gyrosens.c + 1 + .\gyrosens.c + + + I2C.c + 1 + .\I2C.c + lib + + + gyrosens.h + 5 + .\gyrosens.h + + + I2C.h + 5 + .\I2C.h + + ::CMSIS @@ -416,12 +438,6 @@ - - - - - - diff --git a/I2C.c b/I2C.c new file mode 100644 index 0000000..1959bea --- /dev/null +++ b/I2C.c @@ -0,0 +1,67 @@ +#include "stm32f10x.h" +#include "I2C.h" +#include +#include + +#define I2C_TIMEOUT 100000 + +static uint8_t I2C_WaitFlag(volatile uint16_t *reg, uint16_t flag) +{ + uint32_t timeout = I2C_TIMEOUT; + + while(!(*reg & flag)) + { + if(--timeout == 0) + return 0; + } + return 1; +} + +void I2C1_Init(void) +{ + /* 1. Clock aktivieren */ + RCC->APB2ENR |= RCC_APB2ENR_IOPBEN; // GPIOB + RCC->APB1ENR |= RCC_APB1ENR_I2C1EN; // I2C1 + + /* 2. GPIO konfigurieren: PB6=SCL, PB7=SDA */ + GPIOB->CRL &= ~((0xF << 24) | (0xF << 28)); + GPIOB->CRL |= ((0xB << 24) | (0xB << 28)); + // 0xB = Alternate Open Drain, 50MHz + + /* 3. I2C Reset */ + I2C1->CR1 |= I2C_CR1_SWRST; + I2C1->CR1 &= ~I2C_CR1_SWRST; + + /* 4. Peripherietakt einstellen */ + I2C1->CR2 = 36; // APB1 = 36 MHz + I2C1->CCR = 180; // 100kHz + I2C1->TRISE = 37; // laut Datenblatt + + /* 5. Enable */ + I2C1->CR1 |= I2C_CR1_PE; +} + +void startSeq(void) +{ + I2C1->CR1 |= I2C_CR1_START; // START setzen + while(!(I2C1->SR1 & I2C_SR1_SB)); // Warten bis START gesendet +} + +void stopSeq(void) +{ + I2C1->CR1 |= I2C_CR1_STOP; +} + +uint8_t I2C_SendAddress_Write(uint8_t addr) +{ + I2C1->DR = addr << 1; // R/W = 0 + + if(!I2C_WaitFlag(&I2C1->SR1, I2C_SR1_ADDR)) + return 0; + + (void)I2C1->SR1; // ADDR löschen + (void)I2C1->SR2; + + return 1; +} + diff --git a/I2C.h b/I2C.h new file mode 100644 index 0000000..97d7e6f --- /dev/null +++ b/I2C.h @@ -0,0 +1,14 @@ +#ifndef I2C_DRIVER_H +#define I2C_DRIVER_H + +#include + +void I2C1_Init(void); +uint8_t I2C1_write(uint8_t addr, uint8_t reg, uint8_t data); +uint8_t I2C1_read(uint8_t addr, uint8_t reg, uint8_t *data); +uint8_t I2C_WriteReg(uint8_t addr, uint8_t reg, uint8_t data); +void startSeq(void); +void stopSeq(void); +static uint8_t I2C_WaitFlag(volatile uint16_t *reg, uint16_t flag); + +#endif \ No newline at end of file diff --git a/gyrosens.c b/gyrosens.c new file mode 100644 index 0000000..e69de29 diff --git a/gyrosens.h b/gyrosens.h new file mode 100644 index 0000000..e69de29 diff --git a/main.c b/main.c index 6083ebb..43e4534 100644 --- a/main.c +++ b/main.c @@ -1,102 +1,18 @@ #include #include +#include "I2C.h" -void portConfig() -{ - RCC->APB2ENR |= RCC_APB2ENR_IOPAEN; // Takt für Port A - RCC->APB2ENR |= RCC_APB2ENR_IOPBEN; // Takt für Port B - - GPIOA->CRL &= 0x00000000; - GPIOA->CRL |= 0x22222222; // PA0 - PA7 Output - - GPIOB->CRL &= 0x00000000; - GPIOB->CRL |= 0x00000022; // PB0 - PB1 Output -} - -void set7Seg(uint8_t zahl) -{ - const uint8_t segmente[10] = - { - 0b00111111, // 0 - 0b00000110, // 1 - 0b01011011, // 2 - 0b01001111, // 3 - 0b01100110, // 4 - 0b01101101, // 5 - 0b01111101, // 6 - 0b00000111, // 7 - 0b01111111, // 8 - 0b01101111 // 9 - }; - - if (zahl < 10) - { - GPIOA->ODR &= 0xFFFFFF00; - GPIOA->ODR |= segmente[zahl]; - } -} - -void segEnable(uint8_t zahl) -{ - if(zahl == 1) - { - GPIOB->ODR &= 0b00000000; - GPIOB->ODR |= 0b00000001; - } - else if(zahl == 2) - { - GPIOB->ODR &= 0b00000000; - GPIOB->ODR |= 0b00000010; - } -} - -uint8_t varTo7Seg(uint8_t zahl) -{ - uint8_t einer = 0; - uint8_t zehner = 0; - - int i = 0; - - if(zahl > 99) - { - set7Seg(0); - } - else - { - einer = zahl % 10; - zehner = zahl / 10; - } - - for(i = 0; i < 51; i++) - { - segEnable(1); - set7Seg(zehner); - wait_ms(5); - segEnable(2); - set7Seg(einer); - wait_ms(5); - } - - return zahl; -} - int main(void) { - portConfig(); + I2C1_Init(); + wait_ms(100); - uint8_t zahl = 99; - uint16_t tick = 0; - - int i = 0; - - while(1) - { - for(i = 0; i < 100; i++) - { - varTo7Seg(i); - } - - wait_ms(2000); - i = 0; - } + while(1) + { + startSeq(); + I2C_SendAddress_Write(0x68); + //stopSeq(); + + wait_ms(100); + } } \ No newline at end of file