Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "stm32f30x_usart.h"
- #include "stm32f30x_rcc.h"
- /** @addtogroup STM32F3_Discovery_Peripheral_Examples
- * @{
- */
- /** @addtogroup GPIO_IOToggle
- * @{
- */
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- #define BSRR_VAL 0xC000
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- static __IO uint32_t TimingDelay;
- volatile uint16_t usart_buffer = 0;
- volatile char usartMessage[] = "message";
- /* Private function prototypes -----------------------------------------------*/
- void Delay(__IO uint32_t nTime);
- void USART1_IRQHandler(void);
- /* Private functions ---------------------------------------------------------*/
- void USART_print (USART_TypeDef* USARTx, volatile char *buffer)
- {
- /* transmit till NULL character is encountered */
- while(*buffer)
- {
- USART_SendData(USARTx, *buffer++);
- while (USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET);
- }
- }
- /**
- * @brief Main program.
- * @param None
- * @retval None
- */
- int main(void)
- {
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_7);
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_7);
- /* Configure USART1 pins: --------------------------------------*/
- RCC_APB1PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
- USART_DeInit(USART1);
- USART_InitStructure.USART_BaudRate = 9600;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_Parity = USART_Parity_No;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
- USART_Init(USART1,&USART_InitStructure);
- USART_Cmd(USART1, ENABLE);
- USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // enable the USART1 receive interrupt
- NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; // we want to configure the USART1 interrupts
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; // this sets the priority group of the USART1 interrupts
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // this sets the subpriority inside the group
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // the USART2 interrupts are globally enabled
- NVIC_Init(&NVIC_InitStructure); // the properties are passed to the NVIC_Init function which takes care of the low level stuff
- // finally this enables the complete USART1 peripheral
- USART_Cmd(USART1, ENABLE);
- if (SysTick_Config(SystemCoreClock / 1000))
- {
- /* Capture error */
- while (1);
- }
- STM_EVAL_LEDInit(LED5);
- while (1)
- {
- int i = USART_ReceiveData(USART1);
- if(i == '1'){
- USART_print(USART1, "messsage");
- }
- /*for(i=0; usartMessage[i] != 0; i++){
- USART_print(USART1, &usartMessage[i]);
- }*/
- Delay(200);
- }
- }
- void USART1_IRQHandler(void){
- Delay(100);
- USART_print(USART1, "message INTERRUPT!");
- if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET){
- usart_buffer = USART_ReceiveData(USART1);
- }
- }
- void Delay(__IO uint32_t nTime)
- {
- TimingDelay = nTime;
- while(TimingDelay != 0);
- }
- /**
- * @brief Decrements the TimingDelay variable.
- * @param None
- * @retval None
- */
- void TimingDelay_Decrement(void)
- {
- if (TimingDelay != 0x00)
- {
- TimingDelay--;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement