Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Includes ------------------------------------------------------------------*/
- #include "stm32f4_discovery.h"
- #include "stm32f4xx_usart.h"
- volatile uint16_t calc = (uint16_t)1;
- void DELAY(){
- uint32_t d = 0x01000000;
- while(d--);
- }
- void EXTI0_IRQHandler(){
- if (EXTI_GetITStatus(EXTI_Line0)){
- while(!USART_GetFlagStatus(USART1, USART_FLAG_TXE));
- USART_SendData(USART1, calc);
- EXTI_ClearITPendingBit(EXTI_Line0);
- }
- calc++;
- if(calc>153) calc = 0;
- }
- volatile uint16_t data = (uint16_t)0x0;
- void USART1_IQRHandler(){
- if(USART_GetITStatus(USART1, USART_FLAG_RXNE)){
- data=USART_ReceiveData(USART1);
- if(data){
- GPIO_SetBits(GPIOD,0xF000);
- DELAY();
- GPIO_ResetBits(GPIOD, 0xF000);
- DELAY();
- }
- data = (uint16_t)0x0;
- }
- USART_ClearFlag(USART1, USART_FLAG_RXNE);
- }
- int main(void){
- //Start the device(= pins) clock
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
- //Init the Device Structure
- GPIO_InitTypeDef u1;
- u1.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
- u1.GPIO_Mode = GPIO_Mode_AF;
- u1.GPIO_Speed = GPIO_Speed_2MHz;
- u1.GPIO_OType = GPIO_OType_PP;
- u1.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_Init(GPIOB, &u1);
- //Alternative function pins setup
- GPIO_PinAFConfig(GPIOB, GPIO_PinSource6,GPIO_AF_USART1);
- GPIO_PinAFConfig(GPIOB, GPIO_PinSource7,GPIO_AF_USART1);
- //Usart Setup
- //Start the device(=usart1)
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
- USART_InitTypeDef USART_InitStruct;
- USART_InitStruct.USART_BaudRate = 9600;
- USART_InitStruct.USART_WordLength = USART_WordLength_8b;
- USART_InitStruct.USART_StopBits = USART_StopBits_1;
- USART_InitStruct.USART_Parity = USART_Parity_No;
- USART_InitStruct.USART_HardwareFlowControl =
- USART_HardwareFlowControl_None;
- USART_InitStruct.USART_Mode = USART_Mode_Tx |
- USART_Mode_Rx;
- USART_Init(USART1, &USART_InitStruct);
- USART_Cmd(USART1, ENABLE);
- //Start the device(=leds) clock
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
- //Init the Device Structure
- GPIO_InitTypeDef leds;
- leds.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
- leds.GPIO_Mode = GPIO_Mode_OUT;
- leds.GPIO_Speed = GPIO_Speed_2MHz;
- leds.GPIO_OType = GPIO_OType_PP;
- leds.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_Init(GPIOD, &leds);
- //Start the device(= button) clock
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
- //Init the device(=button) structure
- GPIO_InitTypeDef button;
- button.GPIO_Pin = GPIO_Pin_0;
- button.GPIO_Mode = GPIO_Mode_IN;
- button.GPIO_Speed = GPIO_Speed_2MHz;
- button.GPIO_OType = GPIO_OType_PP;
- button.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_Init(GPIOD, &button);
- //Interupt for button
- NVIC_InitTypeDef NVIC_InitStructure;
- NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- //Exception handler for button
- EXTI_InitTypeDef EXTI_InitStructure;
- EXTI_InitStructure.EXTI_Line = EXTI_Line0;
- EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
- EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
- EXTI_InitStructure.EXTI_LineCmd = ENABLE;
- EXTI_Init(&EXTI_InitStructure);
- //Interrupt for USART
- NVIC_InitTypeDef NVIC_InitStructure2;
- NVIC_InitStructure2.NVIC_IRQChannel = USART1_IRQn;
- NVIC_InitStructure2.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure2.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure2.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure2);
- USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
- //Start
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
- SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);
- while (1){
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement