DIC Stunde 25.03.2026

This commit is contained in:
Fabian Schieder 2026-02-25 15:03:11 +01:00
parent aea4404d33
commit a043ddb276
6 changed files with 114 additions and 101 deletions

View File

@ -389,10 +389,32 @@
<FileType>1</FileType>
<FilePath>.\main.c</FilePath>
</File>
<File>
<FileName>gyrosens.c</FileName>
<FileType>1</FileType>
<FilePath>.\gyrosens.c</FilePath>
</File>
<File>
<FileName>I2C.c</FileName>
<FileType>1</FileType>
<FilePath>.\I2C.c</FilePath>
</File>
</Files>
</Group>
<Group>
<GroupName>lib</GroupName>
<Files>
<File>
<FileName>gyrosens.h</FileName>
<FileType>5</FileType>
<FilePath>.\gyrosens.h</FilePath>
</File>
<File>
<FileName>I2C.h</FileName>
<FileType>5</FileType>
<FilePath>.\I2C.h</FilePath>
</File>
</Files>
</Group>
<Group>
<GroupName>::CMSIS</GroupName>
@ -416,12 +438,6 @@
<targetInfo name="MDDS"/>
</targetInfos>
</component>
<component Cclass="HTL Hollabrunn" Cgroup="Libraries" Csub="I2CLib" Cvendor="HTL Hollabrunn" Cversion="1.0.0">
<package name="STD_Pack" schemaVersion="1.4" url="" vendor="HTL Hollabrunn" version="3.3.1"/>
<targetInfos>
<targetInfo name="MDDS"/>
</targetInfos>
</component>
<component Cclass="HTL Hollabrunn" Cgroup="Libraries" Csub="NextionLib" Cvendor="HTL Hollabrunn" Cversion="1.1.0" condition="STDLib">
<package name="STD_Pack" schemaVersion="1.4" url="" vendor="HTL Hollabrunn" version="3.3.1"/>
<targetInfos>

67
I2C.c Normal file
View File

@ -0,0 +1,67 @@
#include "stm32f10x.h"
#include "I2C.h"
#include <armv30_std.h>
#include <stm32f10x.h>
#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;
}

14
I2C.h Normal file
View File

@ -0,0 +1,14 @@
#ifndef I2C_DRIVER_H
#define I2C_DRIVER_H
#include <stdint.h>
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

0
gyrosens.c Normal file
View File

0
gyrosens.h Normal file
View File

106
main.c
View File

@ -1,102 +1,18 @@
#include <armv30_std.h>
#include <stm32f10x.h>
#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);
}
}