main.c:
main.h:
/** ****************************************************************************** * @file main.h * @author Lukas Berger * @version V1.0.0 * @date 18-Oktober-2012 * @brief Header for main.c module ****************************************************************************** */ /* Define to prevent recursive inclusion -------------------------------------*/ #ifndef __STM32F4_DISCOVERY_DEMO_H #define __STM32F4_DISCOVERY_DEMO_H /* Includes ------------------------------------------------------------------*/ #include #include "stm32f4xx_conf.h" #include "stm32f4_discovery.h" #include "stm32f4_discovery_lis302dl.h" #include "usbd_cdc_core.h" #include "usbd_usr.h" #include "usbd_desc.h" #include "usbd_cdc_vcp.h" /* Exported types ------------------------------------------------------------*/ /* Exported constants --------------------------------------------------------*/ /* Exported macro ------------------------------------------------------------*/ #define ABS(x) (x < 0) ? (-x) : x #define MAX(a,b) (a < b) ? (b) : a /* Exported functions ------------------------------------------------------- */ void TimingDelay_Decrement(void); void Delay(__IO uint32_t nTime); void Fail_Handler(void); /* Private typedef -----------------------------------------------------------*/ #pragma pack(push) /* push current alignment to stack */ #pragma pack(1) /* set alignment to 1 byte boundary */ #pragma pack(pop) /* set default */ /* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ #ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED #if defined ( __ICCARM__ ) /*!< IAR Compiler */ #pragma data_alignment = 4 #endif #endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */ __ALIGN_BEGIN USB_OTG_CORE_HANDLE USB_OTG_dev __ALIGN_END; __IO uint32_t TimingDelay; void initUSART(void); uint8_t inttohex(int32_t czahl, uint8_t chex[]); #endif /* __STM32F4_DISCOVERY_DEMO_H */ /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
i2c.c:
/* File: i2c.c * Date: 18.04.13 * Author: Boris Wasserstrom * Description: This file contains functions for I2C1-Bus of STM32F4xx */ #ifndef _I2C_C #define _I2C_C #include // Initialisierungsstruktur für IO-Ports anlegen GPIO_InitTypeDef GPIO_InitStruct; // Initialisierungsstruktur für I2C1 anlegen I2C_InitTypeDef I2C_InitStruct; uint8_t data; void I2CBus_Init (void) { // Clock für die notwendigen Pins einschalten RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); // Pins für I2C1 konfigurieren // PB11 = SDA // PB10 = SCL GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_OType = GPIO_OType_OD; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOB, &GPIO_InitStruct); // Aktivierte Pins für I2C1 (AF) freischalten GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_I2C2); GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_I2C2); // Clock für I2C1 einschalten RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE); // I2C konfigurieren I2C_InitStruct.I2C_ClockSpeed = 100000; I2C_InitStruct.I2C_Mode = I2C_Mode_I2C; I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStruct.I2C_OwnAddress1 = 0x00; I2C_InitStruct.I2C_Ack = I2C_Ack_Disable; // Im Lesemodus ACK deaktivieren I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_Init(I2C2, &I2C_InitStruct); // I2C aktivieren I2C_Cmd(I2C2, ENABLE); } /**********************************************************************************/ void I2C_start(I2C_TypeDef* I2Cx, uint8_t addr, uint8_t direction) { // warten bis die I2C-Leitung frei wird while(I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY)); // START-Bedingung I2C_GenerateSTART(I2Cx, ENABLE); // Warten bis Slave mit ACK bestätigt hat while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT)); // Adresse vom Slave senden I2C_Send7bitAddress(I2Cx, addr, direction); // Warten bis Slave den Master als Empfänger bzw. Sender bestätigt hat if(direction == I2C_Direction_Transmitter){ while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); } else if(direction == I2C_Direction_Receiver){ while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)); } } /**********************************************************************************/ // Sendet 1 Byte an den Slave und wartet auf die Bestätigung void I2C_write(I2C_TypeDef* I2Cx, uint8_t data) { I2C_SendData(I2Cx, data); while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); } /**********************************************************************************/ // Diese Funktion empfängt 1 Byte vom Slave und bestätigt den Empfang mit ACK // (notwendig, falls weitere Bytes mit Daten folgen) uint8_t I2C_read_ack(I2C_TypeDef* I2Cx) { // ACK aktivieren I2C_AcknowledgeConfig(I2Cx, ENABLE); // warten bis 1 Byte empfangen wurde while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED) ); // Daten ausm Register lesen data = I2C_ReceiveData(I2Cx); return data; } /**********************************************************************************/ // 1 Byte empfangen und Mit NACK bestätigen // (zum Empfangen vom letzten Byte) uint8_t I2C_read_nack(I2C_TypeDef* I2Cx) { // ACK deaktivieren I2C_AcknowledgeConfig(I2Cx, DISABLE); // warten bis 1 Byte empfangen wurde while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED) ); // Daten ausm Register lesen data = I2C_ReceiveData(I2Cx); return data; } /**********************************************************************************/ // STOP-Bedingung, gibt den Bus frei void I2C_stop(I2C_TypeDef* I2Cx) { I2C_GenerateSTOP(I2Cx, ENABLE); } /**********************************************************************************/ #endif /*_I2C_C*/
i2c.h:
/* File: i2c.h * Date: 18.04.13 * Author: Boris Wasserstrom * Description: This file contains prototypes for i2c.c */ #ifndef _I2C_H #define _I2C_H #include #include #include #include void I2CBus_Init(void); void I2C_start(I2C_TypeDef*, uint8_t, uint8_t); void I2C_write(I2C_TypeDef*, uint8_t); uint8_t I2C_read_ack(I2C_TypeDef*); uint8_t I2C_read_nack(I2C_TypeDef*); void I2C_stop(I2C_TypeDef*); #endif /*_I2C_H*/
Datenblatt ADC