//usart2.c//
#include "sys.h"
#include "usart2.h"
//////////////////////////////////////////////////////////////////////////////////
//èç1ûê1óÃucos,Ôò°üà¨ÏÂÃæμÄí·Îļt¼′¿é.
#if SYSTEM_SUPPORT_OS
#include "includes.h" //ucos ê1óÃ
#endif
#if 1
#pragma import(__use_no_semihosting)
//±ê×¼¿aDèòaμÄÖ§3Öoˉêy
struct __FILE
{
int handle;
};
//FILE __stdout;
////¶¨òå_sys_exit()òÔ±üÃaê1óðëÖ÷»úÄ£ê½
//_sys_exit(int x)
//{
// x = x;
//}
////Öض¨òåfputcoˉêy
//int fputc2(int ch, FILE *f)
//{
// while((USART2->SR&0X40)==0);//Ñ-»··¢Ëí,Ö±μ½·¢Ëííê±Ï
// USART2->DR = (u8) ch;
// return ch;
//}
#endif
#if EN_USART2_RX //èç1ûê1Äüá˽óêÕ
u8 USART2_RX_BUF[USART2_REC_LEN]; //½óêÕ»o3å,×î′óUSART_REC_LEN¸ö×Ö½ú.
u16 USART2_RX_STA=0; //½óêÕ×′쬱ê¼Ç
void uart2_init(u32 bound){
//GPIO¶Ë¿úéèÖÃ
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //ê1ÄüGPIOAê±Öó
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);//ê1ÄüUSART1ê±Öó
//′®¿ú1¶Ôó|òy½Å¸′óÃó3éä
GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2); //GPIOA9¸′óÃÎaUSART1
GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2); //GPIOA10¸′óÃÎaUSART1
//USART1¶Ë¿úÅäÖÃ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; //GPIOA9óëGPIOA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//¸′óÃ1|Äü
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //Ëù¶è50MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //íÆíì¸′óÃêä3ö
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //éÏà-
GPIO_Init(GPIOA,&GPIO_InitStructure); //3õê¼»ˉPA9£¬
A10
//USART1 3õê¼»ˉéèÖÃ
USART_InitStructure.USART_BaudRate = bound;//2¨ìØÂêéèÖÃ
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×Ö3¤Îa8λêy¾Y¸ñê½
USART_InitStructure.USART_StopBits = USART_StopBits_1;//ò»¸öí£Ö1λ
USART_InitStructure.USART_Parity = USART_Parity_No;//ÎTÆæżD£Ñéλ
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//ÎTó2¼têy¾Yá÷¿ØÖÆ
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //êÕ·¢Ä£ê½
USART_Init(USART2, &USART_InitStructure); //3õê¼»ˉ′®¿ú1
USART_Cmd(USART2, ENABLE); //ê1Äü′®¿ú1
USART_ClearFlag(USART2, USART_FLAG_TC);
#if EN_USART2_RX
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//¿aÆôÏà1ØÖD¶Ï
//Usart1 NVIC ÅäÖÃ
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;//′®¿ú1ÖD¶Ïí¨μà
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//ÇàÕ¼óÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =3; //×óóÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μàê1Äü
NVIC_Init(&NVIC_InitStructure); //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉVIC¼Ä′æÆ÷¡¢
#endif
}
void USART2_IRQHandler(void) //′®¿ú1ÖD¶Ï·tÎñ3ìDò
{
u8 Res;
#if SYSTEM_SUPPORT_OS //èç1ûSYSTEM_SUPPORT_OSÎaÕ棬ÔòDèòaÖ§3ÖOS.
OSIntEnter();
#endif
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //½óêÕÖD¶Ï(½óêÕμ½μÄêy¾Y±ØDëêÇ0x0d 0x0a½áÎ2)
{
Res =USART_ReceiveData(USART2);//(USART1->DR);
if((USART2_RX_STA&0x8000)==0)//½óêÕÎ′íê3é
{
if(USART2_RX_STA&0x4000)//½óêÕμ½áË0x0d
{
if(Res!=0x0a)USART2_RX_STA=0;//½óêÕ′íÎó,ÖØD¿aê¼
else USART2_RX_STA|=0x8000; //½óêÕíê3éáË
}
else //»1ûêÕμ½0X0D
{
if(Res==0x0d)USART2_RX_STA|=0x4000;
else
{
USART2_RX_BUF[USART2_RX_STA&0X3FFF]=Res ;
USART2_RX_STA++;
if(USART2_RX_STA>(USART2_REC_LEN-1))USART2_RX_STA=0;
}
}
}
}
#if SYSTEM_SUPPORT_OS
OSIntExit();
#endif
}
#endif
//main.c//
#include "sys.h"
#include "delay.h"
#include "usart2.h"
//#include "usart.h"
#include "led.h"
#include "beep.h"
#include "key.h"
int main(void)
{
u8 t;
u8 len;
u16 times=0;
//NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//éèÖÃÏμí3ÖD¶ÏóÅÏ輶·Ö×é2
delay_init(168); //Ñóê±3õê¼»ˉ
//uart_init(115200); //′®¿ú3õê¼»ˉ2¨ìØÂêÎa115200
uart2_init(115200); //′®¿ú3õê¼»ˉ2¨ìØÂêÎa115200
LED_Init(); //3õê¼»ˉóëLEDᬽóμÄó2¼t½ó¿ú
while(1)
{
if(USART2_RX_STA&0x8000)
{
len=USART2_RX_STA&0x3fff;//μÃμ½′Ë′νóêÕμ½μÄêy¾Y3¤¶è
printf("
Äú·¢ËíμÄÏûÏ¢Îa:
");
for(t=0;t<len;t++)
{
USART_SendData(USART2, USART2_RX_BUF[t]); //Ïò′®¿ú1·¢Ëíêy¾Y
while(USART_GetFlagStatus(USART2,USART_FLAG_TC)!=SET);//μè′y·¢Ëí½áêø
}
printf("
");//2åèë»»DD
USART2_RX_STA=0;
}else
{
times++;
if(times%5000==0)
{
printf("
ALIENTEK ì½Ë÷ÕßSTM32F407¿a·¢°å ′®¿úêμÑé
");
printf("ÕyμãÔ-×ó@ALIENTEK
");
}
if(times%200==0)printf("Çëêäèëêy¾Y,òÔ»Ø3μ¼ü½áêø
");
if(times%30==0)LED0=!LED0;//éá˸LED,ìáê¾Ïμí3ÕyÔúÔËDD.
delay_ms(10);
}
}
}
你看看你串口跳线帽那边是不是接对了(PA2,PA3要接com2而不是485)
程序没看出啥问题,要结合你板子实验看的(我也是新手,估计有问题也没看出来,哈哈哈哈)
一周热门 更多>