|
在主函數(shù)里調(diào)試打printf("n: %c ",USART2_RX_BUF[10]);,無論是USART2_RX_BUF[]中的哪一位都是/0。串口1用的例程并未修改,單獨(dú)使用串口2收發(fā)調(diào)試沒有問題。使用
不是波特率的問題,中斷服務(wù)程序也沒問題。是數(shù)據(jù)沒有傳到USART2_RX_BUF嗎?
main.c
/****/
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "usart2.h"
extern char j_buf[256];
extern int n_cnt;
extern short angles[3];
int main(void)
{
short axis_x,axis_y,axis_z;
delay_init(); //延時函數(shù)初始化
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //設(shè)置NVIC中斷分組2:2位搶占優(yōu)先級,2位響應(yīng)優(yōu)先級
uart_init(115200); //串口初始化為115200
usart2_init(9600); //串口初始化為9600
printf("It's ok");
while(1)
{
j_buf[0] = 0xAA;//數(shù)據(jù)幀頭
j_buf[1] = 0x01;//數(shù)據(jù)長度
j_buf[2] = 0x01;//讀取傳感器數(shù)據(jù)命令
j_buf[3] = 0xAC; //校驗(yàn)和
send_workdata(j_buf,4);//發(fā)送至傳感器
printf("n: %c ",USART2_RX_BUF[10]);
delay_ms(1000);
}
}
/******/
usart2.c
/*****/
#include "sys.h"
#include "delay.h"
#include "usart2.h"
#include "stdarg.h"
#include "stdio.h"
#include "string.h"
#include "usart.h"
unsigned char j_buf[256]; //緩沖
int n_cnt = 0; //計(jì)數(shù)標(biāo)準(zhǔn)位
short angles[3]={0}; //存儲三個軸的角度
//串口接收緩存區(qū)
char USART2_RX_BUF[USART2_MAX_RECV_LEN];
void usart2_init(u32 bound)
{
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); // GPIOA時鐘
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); //串口2時鐘使能
USART_DeInit(USART2); //復(fù)位串口2
//USART2_TX
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA2
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //復(fù)用推挽輸出
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA2
//USART2_RX
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空輸入
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA3
USART_InitStructure.USART_BaudRate = bound;//波特率一般設(shè)置為9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字長為8位數(shù)據(jù)格式
USART_InitStructure.USART_StopBits = USART_StopBits_1;//一個停止位
USART_InitStructure.USART_Parity = USART_Parity_No;//無奇偶校驗(yàn)位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//無硬件數(shù)據(jù)流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收發(fā)模式
USART_Init(USART2, &USART_InitStructure); //初始化串口 2
USART2->CR1=USART2->CR1&~(1<<4); //先清除,否則會一直進(jìn)入idel中斷
USART2->SR;
USART2->DR;
USART_Cmd(USART2, ENABLE); //使能串口
//使能接收中斷
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//開啟中斷
//設(shè)置中斷優(yōu)先級
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority= 2;//搶占優(yōu)先級3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子優(yōu)先級3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根據(jù)指定的參數(shù)初始化VIC寄存器
}
void USART2_IRQHandler(void) //串口2中斷服務(wù)程序
{
u8 res;
res = USART_ReceiveData(USART2);//讀取接收到的數(shù)據(jù)
j_buf[n_cnt] = res;//將讀取的數(shù)據(jù)放到j(luò)_buf里去
n_cnt++;
}
void send_workdata(u8 *data,u8 times)
{
int i;
for(i=0;i<times;i++)
{
while((USART2->SR&0X80)==0);
USART2->DR=*(data++);
}
}
|
|