main.c
//---------------------------------------------------------------------------------------------------
typedef unsigned char u8;
typedef unsigned short u16;
#include "stm32f10x.h"
void RS232_Send_Data(unsigned char *send_buff, unsigned int length);
void USART_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
/**********************************************/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
/*
* USART2_TX -> PA2 , USART2_RX -> PA3
*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 19200;
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(USART2, &USART_InitStructure);
USART_ITConfig(USART2, USART_IT_TXE , ENABLE);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_Cmd(USART2, ENABLE);
USART_ClearITPendingBit(USART2, USART_IT_TC);
}
void RS232_Send_Data(unsigned char *send_buff,unsigned int length)
{
unsigned int i = 0;
for(i = 0;i < length;i ++)
{
USART2->DR = send_buff[i];
while((USART2->SR&0X40)==0);
}
}
//
#define RS232_REC_BUFF_SIZE 100
extern unsigned char RS232_REC_Flag;
extern unsigned char RS232_buff[RS232_REC_BUFF_SIZE];
extern unsigned int RS232_rec_counter;
unsigned char a[12] = "abcdefghij ";
int main(void)
{
SystemInit();
USART_Configuration();
while (1)
{
if(RS232_REC_Flag == 1)//
{
RS232_REC_Flag = 0;
RS232_Send_Data(RS232_buff,RS232_rec_counter); //
RS232_rec_counter = 0; //
}
}
}
|