Created
March 24, 2015 22:12
-
-
Save nraynaud/8d0bdb7a6b0a878f40b7 to your computer and use it in GitHub Desktop.
STM32F4: sending data to 2 chained 74HCT595 shift registers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stm32f4xx.h> | |
#include "stm32f4xx_conf.h" | |
#include "stm32f4_discovery.h" | |
#include "math.h" | |
#include "arm_math.h" | |
uint16_t counter = 0; | |
void sendCounter(); | |
__attribute__ ((noreturn)) void main(void) { | |
//enable FPU | |
SCB->CPACR |= 0b000000000111100000000000000000000UL; | |
STM_EVAL_LEDInit(LED3); | |
STM_EVAL_LEDInit(LED4); | |
STM_EVAL_LEDInit(LED5); | |
STM_EVAL_LEDInit(LED6); | |
STM_EVAL_LEDOff(LED3); | |
STM_EVAL_LEDOff(LED4); | |
STM_EVAL_LEDOff(LED5); | |
STM_EVAL_LEDOff(LED6); | |
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); | |
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); | |
GPIO_PinAFConfig(GPIOB, GPIO_PinSource12, GPIO_AF_SPI2); | |
GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_SPI2); | |
GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_SPI2); | |
GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_SPI2); | |
GPIO_Init(GPIOB, &(GPIO_InitTypeDef) { | |
.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_15, | |
.GPIO_Mode = GPIO_Mode_AF, | |
.GPIO_Speed = GPIO_Speed_50MHz, | |
.GPIO_OType = GPIO_OType_PP, | |
.GPIO_PuPd = GPIO_PuPd_DOWN}); | |
GPIO_Init(GPIOB, &(GPIO_InitTypeDef) { | |
.GPIO_Pin =GPIO_Pin_14, | |
.GPIO_Mode = GPIO_Mode_AF, | |
.GPIO_Speed = GPIO_Speed_50MHz, | |
.GPIO_OType = GPIO_OType_OD}); | |
GPIO_Init(GPIOB, &(GPIO_InitTypeDef) { | |
.GPIO_Pin = GPIO_Pin_11, | |
.GPIO_Mode = GPIO_Mode_OUT, | |
.GPIO_Speed = GPIO_Speed_50MHz, | |
.GPIO_OType = GPIO_OType_PP}); | |
SPI_I2S_DeInit(SPI2); | |
SPI_Init(SPI2, &(SPI_InitTypeDef) { | |
.SPI_Mode = SPI_Mode_Master, | |
.SPI_Direction = SPI_Direction_1Line_Tx, | |
.SPI_DataSize = SPI_DataSize_8b, | |
.SPI_CPOL = SPI_CPOL_Low, | |
.SPI_CPHA = SPI_CPHA_1Edge, | |
.SPI_NSS = SPI_NSS_Hard, | |
.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_128, | |
.SPI_FirstBit = SPI_FirstBit_MSB, | |
.SPI_CRCPolynomial = 7 | |
}); | |
SPI_SSOutputCmd(SPI2, ENABLE); | |
SPI_TIModeCmd(SPI2, DISABLE); | |
SPI_Cmd(SPI2, ENABLE); | |
STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_EXTI); | |
#pragma clang diagnostic push | |
#pragma clang diagnostic ignored "-Wmissing-noreturn" | |
while (1) { | |
counter++; | |
sendCounter(); | |
for (uint32_t i = 0; i < 1000000; i++) | |
__asm__(""); | |
} | |
#pragma clang diagnostic pop | |
} | |
void EXTI0_IRQHandler(void) { | |
if (EXTI_GetITStatus(EXTI_Line0) != RESET) { | |
++counter; | |
STM_EVAL_LEDToggle(LED4); | |
sendCounter(); | |
EXTI_ClearITPendingBit(EXTI_Line0); | |
} | |
} | |
void sendCounter() { | |
GPIO_ResetBits(GPIOB, GPIO_Pin_11); | |
SPI_I2S_SendData(SPI2, counter >> 8); | |
while ((SPI2->SR & SPI_SR_TXE) == 0) | |
__asm__(""); | |
SPI_I2S_SendData(SPI2, counter); | |
while ((SPI2->SR & SPI_SR_TXE) == 0) | |
__asm__(""); | |
while (SPI2->SR & SPI_SR_BSY) | |
__asm__(""); | |
GPIO_SetBits(GPIOB, GPIO_Pin_11); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment