江湖规矩先贴代码:
void System_Init(void)
{
RCC_Configuration(); //开启外设时钟 //SYSCLK=8MHz
GPIO_Configuration();
USART_Initial();
NVIC_Configuration();
}
void USART_Initial(void)
{
USART_DeInit(USART1);
USART_StructInit(&USART_InitStructure);
USART_Init(USART1,&USART_InitStructure);
USART1->CR1|=0x206c; //;USART模块使能,允许接收发送,使能USART中断
USART1->BRR=0x1A0A;//BaudRate;//; //19133 bits/s;
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/*USART 配置*/
/* Configure USART1 Tx (PA.09) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
/*对于发送PIN必须配置成复用输出上拉否则不能正常发送*/
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART1 Rx (PA.10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
/*对于接收PIN必须配置成浮点输入否则不能正常接收*/
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure I2C2 pins: SCL and SDA ----------------------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void RCC_Configuration(void)
{
#define GPIO_Remap_JTAG_Disable 0x04000000
/* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG and AFIO clocks */
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1|
RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC|
RCC_APB2Periph_GPIOD,ENABLE);
//启动IO口的复用功能(AFIO), 用于外部中断
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
/*使能I2C2, SPI2 时钟*/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2|RCC_APB1Periph_TIM2, ENABLE );
//不使用外部晶振影射PD0,PD1到OSC_IN,OSC_OUT.
GPIO_PinRemapConfig(GPIO_Remap_PD01, ENABLE );
// #if !DebugModel
// AFIO->MAPR|=GPIO_Remap_JTAG_Disable; // 使能与JTAG复用的GPIO 使GPIO可用。
// #endif
//使能FSMC时钟
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
}
这是准备部分,只贴一部分 大家都懂哈!
void USART1_IRQHandler(void)
{
#define LCD485_Address 0x10
BYTE RecTemp;
if(USART1->SR&0x40) //发送
{
USART1->SR&=~0x40;
if(SCIStartSent)
{
if(SendCounter<SendBuffer[0])
USART1->DR=SendBuffer[SendCounter++];
else
{
if(!SCIRece)sentIntervalDelay = 3; // 3ms 完成一循通信稍做延时以便SCI有空闲做下次通信,此种情况专属sentkey
//SCIErrorCN=0;
SCISentDis;
SendCounter=0;
SCIStartSent=0;
}
}
}
if(USART1->SR&0x20) //接收
{
RecTemp=USART1->DR;
if(AddrEqual==0)
{
if(RecTemp==LCD485_Address)
{
AddrEqual=1; //被正确询址
}
}
else
{
if(SendCounter==0)
ReceBuffer[SendCounter++]=RecTemp; //接收到长度
else if(SendCounter<ReceBuffer[0])
ReceBuffer[SendCounter++]=RecTemp;
if(SendCounter==ReceBuffer[0])
{
SendCounter=0;
if(SCIRece)
{
SCIRece=0;
//SCIErrorCN=0;
sentIntervalDelay = 3; // 3ms 完成一循通信稍做延时以便SCI有空闲做下次通信
//scisentpnter = Null;
}
FinishedRece=1; //完成一帧数据接收
}
}
}
}
串口中断部分
问题如下:
1,如何设置为同步通讯?
2,中断中如何实现同步收发数据?
2,如果用串口猎人高级发码能和MCU同步通讯不?
备注:现在的设置是异步通信 通讯正常 |