学前班
最后登录1970-1-1
在线时间 小时
注册时间2017-11-21
|
#include"stm32f10x.h"
#include"string.h"
//#include"stm32f10x_it.h"
void RCC_Config();
void GPIO_Config();
void USART_Config();
void NVIC_Config();
//void Tx_string(u8*str);
//u8 buffer[]="nihao";
u8 Txdata[100]={0xaa, 0x55 ,0x77 ,0x9a ,0x76 ,0x79 ,0x88};
u8 length;
u8 i;
int main(void)
{
RCC_Config();
GPIO_Config();
NVIC_Config();
USART_Config();
//Tx_string(buffer);
length=strlen(Txdata);
while(length--){
if (USART_GetITStatus(USART2, USART_IT_TXE) == SET)
{
USART_ClearITPendingBit(USART2,USART_IT_TXE);
USART_SendData(USART2,Txdata[i++]);
while(USART_GetFlagStatus(USART2,USART_FLAG_TC )==1);
}
}
while (1); //μè′yÖD¶Ï
}
/* void Tx_string(u8*str)
{
u8 i;
u8 long;
long=strlen(buffer);
for(i=0;i<long;i++)
{
USART_SendData(USART2,*str++);
//while((USART2->SR&0X40)==0);//Ñ-»··¢Ëí,Ö±μ½·¢Ëííê±Ï
//USART2->DR = *str++;
GPIO_SetBits(GPIOB,GPIO_Pin_9);
}
}*/
void RCC_Config()
{
//RCC_TypeDef RCC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE); //ê1ÄüLED4/5
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_USART1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
}
void GPIO_Config()
{
GPIO_InitTypeDef GPIO_InitStructure;
//ÅäÖÃLED->D4/D5
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9|GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; //ÅäÖÃíÆíìêä3ö
GPIO_Init(GPIOB,&GPIO_InitStructure);
//ÅäÖÃTX
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //ÅäÖÃíÆíìêä3ö
GPIO_Init(GPIOB,&GPIO_InitStructure);
//ÅäÖÃRX
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //ÅäÖø¡¿Õêäèë
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
void NVIC_Config()
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
//°′¼ü1ÖD¶ÏÅäÖÃKEY1->PA0
NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USART_Config()
{
USART_InitTypeDef USART_InitStructure;
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_Mode=USART_Mode_Tx|USART_Mode_Rx;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_Init(USART1,&USART_InitStructure);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
USART_Cmd(USART1,ENABLE); //Ïèê1Äü′®¿ú»1êÇÏè′ò¿aÖD¶Ï£¿
}
void USART_IRQHandle()
{
u8 Rxdata[100];
u8 data,flag;
if(USART_GetITStatus(USART2,USART_IT_RXNE)!=RESET)
{
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
Rxdata[data++]=USART_ReceiveData(USART2);
GPIO_SetBits(GPIOB,GPIO_Pin_9);
if(flag==1) //μú¶t′νóêüêy¾YμÆáá
{
if(USART_GetITStatus(USART2,USART_IT_RXNE)!=RESET)
{
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
Rxdata[data++]=USART_ReceiveData(USART2);
GPIO_ResetBits(GPIOB,GPIO_Pin_9);
}
}
flag++;
}
}
|
|