#include "stm32f10x.h"
#include "stm32f10x_exti.h"
#include "stm32f10x_usart.h"
void RCC_Config(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void Delay(vu32 nCount);
void Usart1_Configration(void);
vu32 falg1;
void RCC_Configuration(void)
{
/* Enable GPIOA/GPIOB clocks */
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1 |RCC_APB2Periph_AFIO, ENABLE);
}
/**
* @brief Configures the different GPIO ports.
* @param None
* @retval None
*/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure PA.02, PA.03 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//USART3_TX =a.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure);
//USART3_RX A.10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void Usart1_Configration()
{
// 配置串口参数
/* USARTx configured as follow:
- BaudRate = 9600 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 9600;//波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长8位
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_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启中断
USART_Cmd(USART1, ENABLE); //使能串口
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//选择中断分组
/* Enable the EXTI Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//选择中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//抢断式中断优先级设置
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;//响应式中断优先级设置
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能中断
NVIC_Init(&NVIC_InitStructure);//初始化
}
void USART1_IRQHandler(void) //串口1中断服务程序
{
vu32 temp;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)//接收中断
{
temp=USART_ReceiveData(USART1);//(USART1->DR); //读取接收到的数据
// while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);//等待接收结束
if(temp==0xff){falg1=1;}
// if(temp==0xfe){flag=2;}
USART_ClearITPendingBit(USART1, USART_IT_RXNE); //清楚接收标志
}
}
void Delay(vu32 nCount)
{
for(; nCount != 0; nCount--);
}
int main()
{
vu32 nCount1=0;
RCC_Configuration();
GPIO_Configuration();
NVIC_Configuration();
Usart1_Configration();
while(1)
{
// GPIO_ResetBits(GPIOA,GPIO_Pin_2);
// Delay(0x2fffff);
// GPIO_SetBits(GPIOA,GPIO_Pin_2);
// Delay(0x2fffff);
// while(Flag==1)
// {
GPIO_ResetBits(GPIOA,GPIO_Pin_2);
Delay(0x2fffff);
GPIO_SetBits(GPIOA,GPIO_Pin_2);
Delay(0x2fffff);
// USART_SendData(USART1 ,0xfb);
while( falg1==1)
{
//falg1=0;
GPIO_ResetBits(GPIOA,GPIO_Pin_3);
Delay(0x2fffff);
GPIO_SetBits(GPIOA,GPIO_Pin_3);
Delay(0x2fffff);
USART_SendData(USART1 ,0xfa);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)== RESET);//等待发送完成
USART_ClearITPendingBit(USART1, USART_FLAG_TXE); //清楚发送标志
}
while( nCount1==2)
{
nCount1=0;
GPIO_SetBits(GPIOA,GPIO_Pin_2);
GPIO_ResetBits(GPIOA,GPIO_Pin_3);
Delay(0x2fffff);
GPIO_SetBits(GPIOA,GPIO_Pin_3);
Delay(0x2fffff);
USART_SendData(USART1 ,0xf8);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)== RESET);//等待发送完成
USART_ClearITPendingBit(USART1, USART_FLAG_TXE); //清楚发送标志
}
// USART_SendData(USART3 ,0xfa); ///发送串口地址
// while(USART_GetFlagStatus(USART3, USART_FLAG_TXE)== RESET);
//
// if(temp==0xff)
// {
// GPIO_ResetBits(GPIOA,GPIO_Pin_3);
// Delay(0x2fffff);
// GPIO_SetBits(GPIOA,GPIO_Pin_3);
// Delay(0x2fffff);
// }
//}
//等待发送完成
// USART_ClearITPendingBit(USART1, USART_FLAG_TXE); //清楚发送标志
// USART_SendData(USART1 ,0x02); ///发送寄存器地址
// while(USART_GetFlagStatus(USART2, USART_FLAG_TXE)== RESET); //等待发送完成
// USART_ClearITPendingBit(USART1, USART_FLAG_TXE); //清楚发送标志
// USART_SendData(USART1 ,0x05); ///发送测量量程
// while(USART_GetFlagStatus(USART2, USART_FLAG_TXE)== RESET); //等待发送完成
// USART_ClearITPendingBit(USART1, USART_FLAG_TXE); //清楚发送标志
}
}
|