这是我的usart.c文件#include "sys.h"
#include "usart.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 fputc(int ch, FILE *f)
{
while((USART1->SR&0X40)==0);//Ñ-»··¢Ëí,Ö±μ½·¢Ëííê±Ï
USART1->DR = (u8) ch;
return ch;
}
#endif
#if EN_USART1_RX //èç1ûê1Äüá˽óêÕ
//′®¿ú1ÖD¶Ï·tÎñ3ìDò
//×¢òa,¶áè¡USARTx->SRÄü±üÃaÄaÃûÆäÃîμÄ′íÎó
u8 USART_RX_BUF[USART_REC_LEN]; //½óêÕ»o3å,×î′óUSART_REC_LEN¸ö×Ö½ú.
//½óêÕ×′ì¬
//bit15£¬ ½óêÕíê3é±êÖ¾
//bit14£¬ ½óêÕμ½0x0d
//bit13~0£¬ ½óêÕμ½μÄóDD§×Ö½úêyÄ¿
u16 USART_RX_STA=0; //½óêÕ×′쬱ê¼Ç
void uart1_init(u32 bound)
{
//GPIO¶Ë¿úéèÖÃ
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //ê1ÄüUSART1£¬GPIOAê±Öó
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸′óÃíÆíìêä3ö
GPIO_Init(GPIOA, &GPIO_InitStructure);//3õê¼»ˉGPIOA.9
//USART1_RX GPIOA.103õê¼»ˉ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//¸¡¿Õêäèë
GPIO_Init(GPIOA, &GPIO_InitStructure);//3õê¼»ˉGPIOA.10
//Usart1 NVIC ÅäÖÃ
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
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¼Ä′æÆ÷
//USART 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(USART1, &USART_InitStructure); //3õê¼»ˉ′®¿ú1
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//¿aÆô′®¿ú½óêüÖD¶Ï
USART_Cmd(USART1, ENABLE); //ê1Äü′®¿ú1
}
void USART1_IRQHandler(void) //′®¿ú1ÖD¶Ï·tÎñ3ìDò
{
u8 Res;
static u8 start;
#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); //¶á衽óêÕμ½μÄêy¾Y
USART_RX_BUF[USART_RX_STA]=Res; //′æ′¢½óêÕμ½μÄêy¾Y
if(USART_RX_BUF[0]==0x5A)
start=1;
else USART_RX_STA=0,start=0;
if(start==1)
{
USART_RX_STA++;
start=0;
if(USART_RX_STA>=24)
{
USART_RX_STA=0;
}
}
}
#if SYSTEM_SUPPORT_OS //èç1ûSYSTEM_SUPPORT_OSÎaÕ棬ÔòDèòaÖ§3ÖOS.
OSIntExit();
#endif
}
#endif
主程序
#include "lcd.h"
#include "delay.h"
#include "usart.h"
#include "sys.h"
#include "led.h"
int main(void)
{
u8 i,j,n;
u8 str[]={0x5A,0xA5,0x20,0xF0,0x01,0x03,0x00};
unsigned char temp[24];
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
delay_init();
LED_Init();
uart1_init(115200);
LCD_Init();
LCD_Clear(WHITE);
while(1)
{
for(i=0;i<sizeof(str);i++)
{
USART_SendData(USART1,str[i]);
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);
}
delay_ms(10);
LED0=!LED0;
POINT_COLOR=BLACK;
for(j=0;j<24;j++)
{
sprintf(temp,"a=%x",USART_RX_BUF[j]);
if(j<12) LCD_ShowString(50,(40+j*20),200,16,16,temp);
else LCD_ShowString(100,(40+(j-12)*20),200,16,16,temp);
}
}
}
正常为发送七个字节的命令字节,会收到传感器的24个数据字节,但是这样调试出来收到的数据不正常
求大神帮我看看代码
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>