USART2接收的数据
代码
如上图所示,USART2接收的数据,全部代码如下:
/****************************************Copyright (c)****************************************************
**
**
**
**--------------File Info---------------------------------------------------------------------------------
** File name: uctsk_Blink.c
** Descriptions: The uctsk_Blink application function
**
**--------------------------------------------------------------------------------------------------------
** Created by: AVRman
** Created date: 2016-2-6
** Version: v1.0
** Descriptions: The original version
**
**--------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
*********************************************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include <includes.h>
#include "ip.h"
#include "lwip/api.h"
#include "lwip/tcpip.h"
#include "lwip/memp.h"
#include "lwip/stats.h"
#include "netif/loopif.h"
#include "search.h"
#include "sockets.h"
#include "lwIP.h"
#include "netif.h"
/* Private variables ---------------------------------------------------------*/
static OS_STK App_TaskExhaustkStk[APP_TASK_EXHAUST_STK_SIZE];
static OS_STK App_TaskLWIPReceiveStk[APP_TASK_LWIPRECEIVE_STK_SIZE];
static void USART_START(INT32U baud_rate);
static void USART_SendStr(int *pucStr,int ulNum);//·¢ËíÖ¸áîoˉêy
static void USART_ReceiveStr(int *recv,int len);
static void USART_RecvData();
static void USART2_IRQHandler2() ;
static void GPIO_Configuration(void);
static void GPIO_LedOn(void);
static void GPIO_LedOn3(void);
static void NVIC_Configure(void);
/* Private function prototypes -----------------------------------------------*/
static void uctsk_ExhaustAna ();
static void uctsk_LWIPReceive (void);
int socket;
int Uart2_Rx;
int Uart2_Tx;
int Uart2_Len;
int Uart2_Sta=0;
int Uart2_Buffer[140];//êy¾Y3¤¶è
int Uart2_Buffer1[140];
int Status;//×′ì¬
int Res;
int TOPLC[10];
//static void usartReceive(int*Uart2_Rx,int Uart2_Len);
void App_ExhaustAnaTaskCreate (void)
{
CPU_INT08U os_err;
os_err = os_err; /* prevent warning... */
os_err = OSTaskCreate((void (*)(void *)) uctsk_ExhaustAna,
(void * ) 0,
(OS_STK * )&App_TaskExhaustkStk[APP_TASK_LWIPRECEIVE_STK_SIZE - 1],
(INT8U ) APP_TASK_EXHAUST_PRIO );
#if OS_TASK_NAME_EN > 0
OSTaskNameSet(APP_TASK_EXHAUST_PRIO, " Task Exhaust Analyze", &os_err);
#endif
}
void App_LWIPReceiveTaskCreate (void)
{
CPU_INT08U os_err;
os_err = os_err; /* prevent warning... */
os_err = OSTaskCreate((void (*)(void *)) uctsk_LWIPReceive,
(void * ) 0,
(OS_STK * )&App_TaskLWIPReceiveStk[APP_TASK_EXHAUST_STK_SIZE - 1],
(INT8U ) APP_TASK_LWIPRECEIVE_PRIO );
#if OS_TASK_NAME_EN > 0
OSTaskNameSet(APP_TASK_LWIPRECEIVE_PRIO, " Task LWIPReceive ", &os_err);
#endif
}
static void uctsk_LWIPReceive (void)
{
int err;
// int newsocetTemp;
struct ip_addr ipaddr;
char recvbuff[100];
u32_t len;
struct sockaddr_in sin;
struct sockaddr_in adressClient;
struct sockaddr sockaddr_name;
uint8_t dataH=0,dataL=0;
uint16_t enginespeed=0,pedal=0;
uint16_t count=0;
Ethernet_Initialize();
Init_lwIP();
lwip_socket_init();
// SpeedTansducer_Init();
IP4_ADDR(&ipaddr, 192, 168, 0, 1 );
socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
sin.sin_len = sizeof(sin);
sin.sin_family = AF_INET;
sin.sin_port = htons(2600);
sin.sin_addr.s_addr = ipaddr.addr;
sockaddr_name= *((const struct sockaddr*)&sin);
// bind(socket,(const struct sockaddr*)&sin,sizeof( struct sockaddr));
len=sizeof(struct sockaddr);
// listen(socket,5);
err=connect(socket,(const struct sockaddr*)&sin,sizeof( struct sockaddr));
// if(USART_GetITStatus(USART2,USART_IT_RXNE)!=RESET)
}
static void uctsk_ExhaustAna ()
{
int increase=0;
int receive[120];
int measure[6]={0xAA,0x03,0x43,0x16,0x01,0xA3};
int stop[6]={0xAA,0x03,0x43,0x16,0x00,0xA4};
int check[7]={0xAA, 0x04 ,0x43 ,0x11, 0xFF, 0x01 ,0xA8};
USART_START(9600);
USART_SendStr(measure,6);
USART_ITConfig(USART2, USART_IT_TXE, DISABLE);
//USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
OSTimeDlyHMSM(0, 0, 3, 0);
while(1)
{
USART_SendStr(check,7);
OSTimeDlyHMSM(0, 0,5, 0);
USART_ITConfig(USART2, USART_IT_TXE, DISABLE);
//USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART2_IRQHandler2();
OSTimeDlyHMSM(0, 0,1, 0);
}
}
/*·¢ËíÖ¸áî*/
static void USART_SendStr(int *pucStr,int ulNum) //·¢Ëíò»′®Ö¸áî
{
int i;
for(i=0;i<ulNum;i++)
{
//while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET);
while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==RESET);
USART_SendData(USART2,*pucStr++);
OSTimeDlyHMSM(0, 0,0, 10);
}
}
/*USARTÅäÖÃÇé¿ö*/
static void USART_START(INT32U baud_rate)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/*RCC Configuration*/
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO ,ENABLE);
RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART2 ,ENABLE);
/*GPIO Configuration*/
GPIO_PinRemapConfig(GPIO_Remap_USART2,ENABLE);
/*
* USART2_TX -> PD5 , USART2_RX -> PD6
*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/*NVIC Configuration*/
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/*USART Configuration*/
USART_InitStructure.USART_BaudRate = baud_rate;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
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(USART2, &USART_InitStructure);
USART_ClearFlag(USART2,USART_FLAG_TC);
USART_Cmd(USART2, ENABLE);
}
static void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOD , ENABLE);
/**
* LED1 -> PD2 , LED2 -> PD3 , LED3 -> PD4 , LED4 -> PD7
*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
}
/************************************************************************/
/******************************ÖD¶Ï½óêÕêy¾Y*************************/
void USART2_IRQHandler2(void)
{
if(USART_GetFlagStatus(USART2,USART_IT_RXNE)==SET) //½óêÕÖD¶ÏóDD§
{
USART_ClearITPendingBit(USART2,USART_IT_RXNE);//Çå3yÖD¶Ï±êÖ¾
for(Uart2_Rx=0;Uart2_Rx<140;Uart2_Rx++)
{
Uart2_Buffer[Uart2_Rx] = USART_ReceiveData(USART2);//·μ»ØUSART2½óêÕμÄ×î½üêy¾Y
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
//Uart2_Rx = (Uart2_Rx + 1) % 140;
}
}
}
/*********************************************************************************************************
END FILE
*********************************************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include <includes.h>
#include "ip.h"
#include "lwip/api.h"
#include "lwip/tcpip.h"
#include "lwip/memp.h"
#include "lwip/stats.h"
#include "netif/loopif.h"
#include "search.h"
#include "sockets.h"
#include "lwIP.h"
#include "netif.h"
/* Private variables ---------------------------------------------------------*/
static OS_STK App_TaskExhaustkStk[APP_TASK_EXHAUST_STK_SIZE];
static OS_STK App_TaskLWIPReceiveStk[APP_TASK_LWIPRECEIVE_STK_SIZE];
static void USART_START(INT32U baud_rate);
static void USART_SendStr(int *pucStr,int ulNum);//·¢ËíÖ¸áîoˉêy
static void USART_ReceiveStr(int *recv,int len);
static void USART_RecvData();
static void NVIC_Configuration();
//static void USART2_IRQHandler2() ;
static void GPIO_Configuration(void);
static void GPIO_LedOn(void);
static void GPIO_LedOn3(void);
/* Private function prototypes -----------------------------------------------*/
static void uctsk_ExhaustAna ();
static void uctsk_LWIPReceive (void);
int socket;
//extern int Uart2_Rx;
int Uart2_Tx;
int Uart2_Len;
int Uart2_Sta=0;
//extern int Uart2_Buffer[140];//êy¾Y3¤¶è
int Status;//×′ì¬
int Res;
int TOPLC[10];
//static void usartReceive(int*Uart2_Rx,int Uart2_Len);
void App_ExhaustAnaTaskCreate (void)
{
CPU_INT08U os_err;
os_err = os_err; /* prevent warning... */
os_err = OSTaskCreate((void (*)(void *)) uctsk_ExhaustAna,
(void * ) 0,
(OS_STK * )&App_TaskExhaustkStk[APP_TASK_LWIPRECEIVE_STK_SIZE - 1],
(INT8U ) APP_TASK_EXHAUST_PRIO );
#if OS_TASK_NAME_EN > 0
OSTaskNameSet(APP_TASK_EXHAUST_PRIO, " Task Exhaust Analyze", &os_err);
#endif
}
void App_LWIPReceiveTaskCreate (void)
{
CPU_INT08U os_err;
os_err = os_err; /* prevent warning... */
os_err = OSTaskCreate((void (*)(void *)) uctsk_LWIPReceive,
(void * ) 0,
(OS_STK * )&App_TaskLWIPReceiveStk[APP_TASK_EXHAUST_STK_SIZE - 1],
(INT8U ) APP_TASK_LWIPRECEIVE_PRIO );
#if OS_TASK_NAME_EN > 0
OSTaskNameSet(APP_TASK_LWIPRECEIVE_PRIO, " Task LWIPReceive ", &os_err);
#endif
}
static void uctsk_LWIPReceive (void)
{
int err;
// int newsocetTemp;
struct ip_addr ipaddr;
char recvbuff[100];
u32_t len;
struct sockaddr_in sin;
struct sockaddr_in adressClient;
struct sockaddr sockaddr_name;
uint8_t dataH=0,dataL=0;
uint16_t enginespeed=0,pedal=0;
uint16_t count=0;
Ethernet_Initialize();
Init_lwIP();
lwip_socket_init();
// SpeedTansducer_Init();
IP4_ADDR(&ipaddr, 192, 168, 0, 1 );
socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
sin.sin_len = sizeof(sin);
sin.sin_family = AF_INET;
sin.sin_port = htons(2600);
sin.sin_addr.s_addr = ipaddr.addr;
sockaddr_name= *((const struct sockaddr*)&sin);
// bind(socket,(const struct sockaddr*)&sin,sizeof( struct sockaddr));
len=sizeof(struct sockaddr);
// listen(socket,5);
err=connect(socket,(const struct sockaddr*)&sin,sizeof( struct sockaddr));
// if(USART_GetITStatus(USART2,USART_IT_RXNE)!=RESET)
}
static void uctsk_ExhaustAna ()
{
int increase=0;
int receive[120];
int measure[6]={0xAA,0x03,0x43,0x16,0x01,0xA3};
int stop[6]={0xAA,0x03,0x43,0x16,0x00,0xA4};
int check[7]={0xAA, 0x04 ,0x43 ,0x11, 0xFF, 0x01 ,0xA8};
USART_START(9600);
USART_SendStr(measure,6);
OSTimeDlyHMSM(0, 0, 3, 0);
while(1)
{
USART_SendStr(check,7);
//OSTimeDlyHMSM(0, 0,5, 0);
OSTimeDlyHMSM(0, 0,1, 0);
}
NVIC_Configuration();
// printf("%d%d ",receive[13],receive[12]);
// printf("%d%d ",receive[21],receive[20]);
//
// TOPLC[0]=receive[13]*256 +receive[12];//CO
// TOPLC[1]=receive[15]*256 +receive[14];//HC
// TOPLC[2]=receive[19]*256 +receive[18];//CO2
// TOPLC[3]=receive[21]*256 +receive[20];//O2
// TOPLC[4]=receive[23]*256 +receive[22];//NO
// TOPLC[5]=receive[27]*256 +receive[26];//labuna
}
/*·¢ËíÖ¸áî*/
static void USART_SendStr(int *pucStr,int ulNum) //·¢Ëíò»′®Ö¸áî
{
int i;
for(i=0;i<ulNum;i++)
{
//while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET);
while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==RESET);
USART_SendData(USART2,*pucStr++);
OSTimeDlyHMSM(0, 0,0, 10);
}
}
static void NVIC_Configuration()
{
NVIC_InitTypeDef NVIC_InitStructure;
/*NVIC Configuration*/
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/*USARTÅäÖÃÇé¿ö*/
static void USART_START(INT32U baud_rate)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
/*RCC Configuration*/
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO ,ENABLE);
RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART2 ,ENABLE);
/*GPIO Configuration*/
GPIO_PinRemapConfig(GPIO_Remap_USART2,ENABLE);
/*
* USART2_TX -> PD5 , USART2_RX -> PD6
*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/*USART Configuration*/
USART_InitStructure.USART_BaudRate = baud_rate;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
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(USART2, &USART_InitStructure);
USART_ClearFlag(USART2,USART_FLAG_TC);
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART2, ENABLE);
}
static void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOD , ENABLE);
/**
* LED1 -> PD2 , LED2 -> PD3 , LED3 -> PD4 , LED4 -> PD7
*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
}
/*********************************************************************************************************
END FILE
*********************************************************************************************************/
以上是发送命令的代码。
void USART2_IRQHandler(void)
{
int myname;
#if ( OS_VIEW_MODULE == DEF_ENABLED )
OSView_RxTxISRHandler();
#endif
myname=10;
if(USART_GetFlagStatus(USART2,USART_IT_RXNE)!=RESET) //½óêÕÖD¶ÏóDD§
{
myname=12;
Uart2_Buffer[Uart2_Rx++] = USART_ReceiveData(USART2);//·μ»ØUSART2½óêÕμÄ×î½üêy¾Y
//USART_ClearITPendingBit(USART2,USART_IT_RXNE);//Çå3yÖD¶Ï±êÖ¾
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
//Uart2_Rx = (Uart2_Rx + 1) % 140;
}
}
接收不了数据了目前。
如果你用串口调试能正确接收,而单片机不能正常接收显然是程序问题,串口中断接收数组网上例程很多可以拿来参考。
一周热门 更多>