1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
| #include "stm32f10x.h" #include <stdio.h> #include <stdarg.h>
uint8_t Serial_RxData; uint8_t Serial_RxFlag;
void Serial_Init(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); USART_InitTypeDef USART_InitStructure; USART_InitStructure.USART_BaudRate = 9600; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_Init(USART1,&USART_InitStructure); USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_Init(&NVIC_InitStructure);
USART_Cmd(USART1,ENABLE); }
void Serial_SendByte(uint8_t Byte) { USART_SendData(USART1, Byte); while( USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET ); }
void Serial_SendArray(uint8_t *Array, uint16_t Length) { uint16_t i; for (i=0;i<Length;i++) { Serial_SendByte(Array[i]); } }
void Serial_SendString(char *String) { uint8_t i; for ( i = 0; String[i] != '\0'; i++ ) { Serial_SendByte(String[i]); } }
uint32_t Serial_Pow(uint32_t X,uint32_t Y) { uint32_t Result = 1; while (Y--) { Result *= X; } return Result; }
void Serial_SendNumber(uint32_t Number, uint8_t Length) { uint8_t i; for ( i = 0; i<Length; i++ ) { Serial_SendByte(Number / Serial_Pow(10,Length - i - 1) % 10 + '0'); } }
int fputc(int ch, FILE *f) { Serial_SendByte(ch); return ch; }
void Serial_Printf(char *format, ... ) { char String[100]; va_list arg; va_start(arg,format); vsprintf(String, format, arg); va_end(arg); Serial_SendString(String); }
uint8_t Serial_GetRxFlag(void) { if(Serial_RxFlag == 1) { Serial_RxFlag = 0; return 1; } return 0; }
uint8_t Serial_GetRxData(void) { return Serial_RxData; }
void USART1_IRQHandler(void) { if(USART_GetFlagStatus(USART1,USART_IT_RXNE) == SET) { Serial_RxData = USART_ReceiveData(USART1); Serial_RxFlag = 1; USART_ClearITPendingBit(USART1,USART_IT_RXNE); } }
|