新手入门
- 积分
- 9
- 金钱
- 9
- 注册时间
- 2020-6-8
- 在线时间
- 14 小时
|
3金钱
本帖最后由 albertsjc 于 2016-7-9 19:38 编辑
最近在用一款dynamixel舵机,需要用到单线半双工,但是配置时一直出现问题,不知道哪位朋友之前用过usart的单线半双工模式,能指点我一下。
主函数代码
[mw_shl_code=c,true]#include "stm32f10x.h"
#include "lcd.h"
#include "tongxun.h"
#include "delay.h"
#include "key.h"
#include "led.h"
int main(void)
{
u8 send_data[6]={0,1,2,3,4,5};
u8 receive_data[5],len=0;
u8 key_val=0,i=0;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
LED_Init();
LED0=0;
delay_init();
KEY_Init();
usart2_init(115200);
LCD_Init();
LCD_Clear(WHITE);
POINT_COLOR=RED;
LCD_DrawRectangle(0,0,240,320);
LCD_DrawLine(0,160,240,160);
LCD_ShowString(30,10,200,16,16,"KEY2:Start Transmition");
LCD_ShowString(30,40,200,24,24,"Send_Data:");
LCD_ShowString(30,200,200,24,24,"Receive_Data:");
USART2->CR2&=~(1<<14);
USART2->CR2&=~(1<<11);
USART2->CR3&=~(1<<5);
USART2->CR3&=~(1<<1);
PEout(5)=0;
while(1)
{
key_val=KEY_Scan(0);
/********发送数字**********/
if(KEY2_PRES==key_val)
{
for(i=0;i<6;i++)
LCD_ShowxNum(30+32*i,80,send_data,3,16,0x80);
USART2_Send_Data(send_data,6);
LED1=0;
LED0=~LED0;
}
USART2_Receive_Data(receive_data,&len);
if(len) //判断是否接受到发送过来的数据
{
for(i=0;i<len;i++)
{
LCD_ShowxNum(30+32*i,240,receive_data,3,16,0x80);
}
PEout(5)=~PEout(5);
LCD_ShowxNum(30,270,len,3,16,0x80);
}
}
}[/mw_shl_code]
下面是收发函数代码
[mw_shl_code=c,true]#include "tongxun.h"
#include "delay.h"
u8 Receive_CNT=0; //用于计算接受到的数据
u16 Receive_Data[64]; //用于储存接收到的数据
void usart2_init(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //打开GPIO时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); //打开USART2时钟
//全双工模式的配置
//配置TX为复用推挽输出 (push-pull)PA2
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_2;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStruct);
//配置RX为浮空输入(floating) PA3
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_3;
GPIO_Init(GPIOA,&GPIO_InitStruct);
//配置USART
USART_InitStruct.USART_Mode=USART_Mode_Rx|USART_Mode_Tx; //使能发送接收
USART_InitStruct.USART_Parity=USART_Parity_No; //无奇偶校验
USART_InitStruct.USART_StopBits=USART_StopBits_1; //一位停止位
USART_InitStruct.USART_WordLength=USART_WordLength_8b; //8位字长
USART_InitStruct.USART_BaudRate=bound; //设置波特率
USART_InitStruct.USART_HardwareFlowControl=USART_HardwareFlowControl_None; //无硬件数据流控制
USART_Init(USART2,&USART_InitStruct);
//NVIC初始化
NVIC_InitStruct.NVIC_IRQChannel=USART2_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=2;
NVIC_InitStruct.NVIC_IRQChannelSubPriority=2;
NVIC_Init(&NVIC_InitStruct);
USART_HalfDuplexCmd(USART2,ENABLE);//使能USART2单线半双工模式
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE); //开启接受中断
USART_Cmd(USART2,ENABLE);
}
void USART2_IRQHandler(void) //接收的中断
{
u16 res;
if(USART_GetITStatus(USART2,USART_IT_RXNE))
{
res = USART_ReceiveData(USART2);
if(Receive_CNT<64) //到达数组最大长度后,计数清零
{
Receive_Data[Receive_CNT++]=res;
}
}
USART_ClearITPendingBit(USART2,USART_IT_RXNE);
}
void USART2_Send_Data(u8 a[],u8 n) //发送函数
{
u8 i;
USART_ClearFlag(USART2,USART_FLAG_TC);
for(i=0;i<n;i++)
{
USART_SendData(USART2,a);
while(!USART_GetFlagStatus(USART2,USART_FLAG_TC));
}
Receive_CNT=0;
}
void USART2_Receive_Data(u8 a[],u8* n) //接收函数
{
u8 i;
delay_ms(30); //等待10ms,连续超过10ms没有接收到一个数据,则认为接收结束
*n=0;
if(Receive_CNT)
{
for(i=0;i<Receive_CNT;i++)
{
a=Receive_Data;
}
*n=Receive_CNT;
Receive_CNT=0;
}
}
[/mw_shl_code]
|
|