Gast
#4340405
Hallo Leute, ich habe eine Frage an euch ob mir jemand helfen kann. Ich bin noch Schüler und wir müssen in diesem Jahr ein Projekt in der Schule machen. Wir haben ein Beispielprogramm vom Lehrer bekommen. Unsere Aufgabe ist es funktionierende Kommunikation mit dem AS3911B Chip von AMS herzustellen. Wir verwenden den STM32F4 und das Programm dazu Coocox Coide. Ich würde gerne wissen wie ich eine Reihe von Bytes senden kann. Es sollen bis zu 4 Bytes auf einmal gesendet werden. Die Kommunikation funktioniert über SPI. Ich würde mich sehr freuen wenn ihr mir weiter helfen könnt. #include "SmartcontrolOS.h" #include "CMSIS.h" static uint8_t AccSens_SendByte(uint8_t byte){ while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); /* Loop while DR register in not emplty */ SPI_I2S_SendData(SPI1, byte); while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); /* Wait to receive a Byte */ return (uint8_t)SPI_I2S_ReceiveData(SPI1); } /* Acceleration Sensor Beschleunigungssensor * ------------------- --------------------- * @brief This application uses the 3-axis acceleration sensor of the STM32F4 Discovery board to Dieses Programm benutzt den 3-Achs-Beschleunigungssensor des STM32F4 Discovery boards, um die * measure and display the acceleration raw-vaues in meters per second squared. Beschleunigungs-Rohwerte zu messen und in Meter Pro Sekunde zum Quadrat anzuzeigen. * @param n - is the number of the application and is displayed on the LCD during application startup. n - ist die Nummer des Programms, die beim Programmstart auf dem LCD angezeigt wird. */ void micro_App__CMSIS_SPI(ui32 n){ Resources__ExBoard_init(_, n, "CMSIS-SPI"); RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); GPIO_InitTypeDef GPIO_InitStructure = {0}; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7; GPIO_Init(GPIOA, &GPIO_InitStructure); DO__write(_, PE3, 1); /* Deselect : Chip Select high */ GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1); /* SPI SCK pin Configuration */ GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1); /* SPI MISO pin Configuration */ GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1); /* SPI MOSI pin Configuration */ SPI_I2S_DeInit(SPI1); SPI_InitTypeDef SPI_InitStructure = {0}; SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_Init(SPI1, &SPI_InitStructure); SPI_Cmd(SPI1, ENABLE); DO__write(_, PE3, 0); /* Set chip select Low at the start of the transmission */ AccSens_SendByte(0x20); /* Address: CTRL_REG1_ADDR */ AccSens_SendByte(0x40 | 0x07); /* Value: LOWPOWERMODE_ACTIVE | XYZ_ENABLE */ DO__write(_, PE3, 1); /* Set chip select High at the end of the transmission */ while(1){ Scheduler__wait_T_optimal(_, 500*ms); // input DO__write(_, PE3, 0); /* Set chip select Low at the start of the transmission */ AccSens_SendByte(0x29 | 0x80 | 0x40); /* Address: OUT_X_ADDR | READWRITE_CMD | MULTIPLEBYTE_CMD */ uint8_t buf[6]; int i; for(i = 0; i < 5; i++){ /* Receive the data that will be read from the device (MSB First) */ buf[i] = AccSens_SendByte(0x00); /* Send dummy byte (0x00) to generate the SPI clock to AccSens (Slave device) */ } DO__write(_, PE3, 1); /* Set chip select High at the end of the transmission */ char input = buf[0]; DO__write(_, PB0, input & 1); DO__write(_, PB1, input & 2); DO__write(_, PB2, input & 4); DO__write(_, PB3, input & 8); DO__write(_, PB4, input & 16); DO__write(_, PB5, input & 32); DO__write(_, PB6, input & 64); DO__write(_, PB7, input & 128); // output while(!LCD__write(_, "ax=%5.0fay=%5.0f\naz=%6.0f m/s2", wf((i8)buf[0]), wf((i8)buf[2]), wf((i8)buf[4]))); } }