4143|3

7

帖子

0

TA的资源

一粒金砂(中级)

楼主
 

STM32 DMA发送 中断接收 的串口程序-------偶是菜鸟 [复制链接]

菜鸟,很想入门,请求热心大虾 相助
此帖出自stm32/stm8论坛

最新回复

#include "stm32f10x_lib.h" #include "USART1.h" #define USART1_DR_Base  0x40013804 const u8 USART_INIT_MESSAGE[2]="OK"; u8 Received_Data_Buffer[COM_PACKET_SIZE]; /* void DMA1_Channel5_IRQHandler(void) {         if (Received_Data_Buffer[0]==RUNCOMMAND_FIRST_CHAR) {//COM Command                         }           //DMA1->IFCR |= DMA1_FLAG_TC5;        //清除传输完成标记 }*/ void DMA_USART_Config(void) {         GPIO_InitTypeDef GPIO_InitStructure;         USART_InitTypeDef USART_InitStructure;         DMA_InitTypeDef DMA_InitStructure;         //clock     RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA , ENABLE);         RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);           /* Configure USART1 Tx (PA.09) as alternate function push-pull */         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;           GPIO_Init(GPIOA, &GPIO_InitStructure);           GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;           GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;           GPIO_Init(GPIOA, &GPIO_InitStructure);         //usart init         USART_InitStructure.USART_BaudRate = 9600;           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(USART1, &USART_InitStructure);         /***************DMA*************************/           USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);           USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE);           /* DMA1 Channel5 RX Config */           DMA_DeInit(DMA1_Channel5);           DMA_InitStructure.DMA_PeripheralBaseAddr = USART1_DR_Base;           DMA_InitStructure.DMA_MemoryBaseAddr = (u32)Received_Data_Buffer;           DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;           DMA_InitStructure.DMA_BufferSize = COM_PACKET_SIZE;         DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;           DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;           DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;           DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;           DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;//循环模式           DMA_InitStructure.DMA_Priority = DMA_Priority_Low;           DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;           DMA_Init(DMA1_Channel5, &DMA_InitStructure);         DMA_Cmd(DMA1_Channel5, ENABLE);           USART_Cmd(USART1, ENABLE);         //DMA_ITConfig(DMA1_Channel5, DMA_IT_TC, ENABLE);           /* DMA1 Channel4 TX Config */           DMA_DeInit(DMA1_Channel4);           DMA_InitStructure.DMA_PeripheralBaseAddr = USART1_DR_Base;           DMA_InitStructure.DMA_MemoryBaseAddr = (u32)USART_INIT_MESSAGE;           DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;           DMA_InitStructure.DMA_BufferSize = sizeof(Received_Data_Buffer);         DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;           DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;           DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;           DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;           DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;           DMA_InitStructure.DMA_Priority = DMA_Priority_Low;           DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;           DMA_Init(DMA1_Channel4, &DMA_InitStructure);         DMA_Cmd(DMA1_Channel4, ENABLE);        //触发一次,使TC置1 } void USART1_DMASendData(u8 *buf,u8 size) {         if ((DMA1->ISR & DMA1_FLAG_TC4)==0) return;//上次DMA发送未完成         DMA1->IFCR |= DMA1_FLAG_TC4;                 //清除发送完成标记         DMA_Cmd(DMA1_Channel4, DISABLE);         DMA1_Channel4->CMAR = (u32)buf;         DMA1_Channel4->CNDTR= size;         DMA_Cmd(DMA1_Channel4, ENABLE); } void USART1_SendByteData(u8 dat) {         if ((DMA1->ISR & DMA1_FLAG_TC4)==0) return;//DMA发送未完成         while ((USART1->SR & USART_FLAG_TXE)==0);         USART1->DR=dat; } 我的。。。  详情 回复 发表于 2012-3-23 07:09
点赞 关注
 

回复
举报

7

帖子

0

TA的资源

一粒金砂(中级)

沙发
 
求求
此帖出自stm32/stm8论坛
 
 

回复

7

帖子

0

TA的资源

一粒金砂(中级)

板凳
 
求回帖
此帖出自stm32/stm8论坛
 
 

回复

4008

帖子

0

TA的资源

版主

4
 
#include "stm32f10x_lib.h"
#include "USART1.h"

#define USART1_DR_Base  0x40013804
const u8 USART_INIT_MESSAGE[2]="OK";
u8 Received_Data_Buffer[COM_PACKET_SIZE];
/*
void DMA1_Channel5_IRQHandler(void)
{
        if (Received_Data_Buffer[0]==RUNCOMMAND_FIRST_CHAR) {//COM Command
               

        }
          //DMA1->IFCR |= DMA1_FLAG_TC5;        //清除传输完成标记
}*/
void DMA_USART_Config(void) {
        GPIO_InitTypeDef GPIO_InitStructure;
        USART_InitTypeDef USART_InitStructure;
        DMA_InitTypeDef DMA_InitStructure;
        //clock
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA , ENABLE);
        RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
          /* Configure USART1 Tx (PA.09) as alternate function push-pull */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
          GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
          GPIO_Init(GPIOA, &GPIO_InitStructure);
          GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
          GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
          GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
          GPIO_Init(GPIOA, &GPIO_InitStructure);
        //usart init
        USART_InitStructure.USART_BaudRate = 9600;
          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(USART1, &USART_InitStructure);
        /***************DMA*************************/
          USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);
          USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE);
          /* DMA1 Channel5 RX Config */
          DMA_DeInit(DMA1_Channel5);
          DMA_InitStructure.DMA_PeripheralBaseAddr = USART1_DR_Base;
          DMA_InitStructure.DMA_MemoryBaseAddr = (u32)Received_Data_Buffer;
          DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
          DMA_InitStructure.DMA_BufferSize = COM_PACKET_SIZE;
        DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
          DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
          DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
          DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
          DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;//循环模式
          DMA_InitStructure.DMA_Priority = DMA_Priority_Low;
          DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
          DMA_Init(DMA1_Channel5, &DMA_InitStructure);
        DMA_Cmd(DMA1_Channel5, ENABLE);
          USART_Cmd(USART1, ENABLE);
        //DMA_ITConfig(DMA1_Channel5, DMA_IT_TC, ENABLE);
          /* DMA1 Channel4 TX Config */
          DMA_DeInit(DMA1_Channel4);
          DMA_InitStructure.DMA_PeripheralBaseAddr = USART1_DR_Base;
          DMA_InitStructure.DMA_MemoryBaseAddr = (u32)USART_INIT_MESSAGE;
          DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
          DMA_InitStructure.DMA_BufferSize = sizeof(Received_Data_Buffer);
        DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
          DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
          DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
          DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
          DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
          DMA_InitStructure.DMA_Priority = DMA_Priority_Low;
          DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
          DMA_Init(DMA1_Channel4, &DMA_InitStructure);
        DMA_Cmd(DMA1_Channel4, ENABLE);        //触发一次,使TC置1
}
void USART1_DMASendData(u8 *buf,u8 size) {
        if ((DMA1->ISR & DMA1_FLAG_TC4)==0) return;//上次DMA发送未完成
        DMA1->IFCR |= DMA1_FLAG_TC4;                 //清除发送完成标记
        DMA_Cmd(DMA1_Channel4, DISABLE);
        DMA1_Channel4->CMAR = (u32)buf;
        DMA1_Channel4->CNDTR= size;
        DMA_Cmd(DMA1_Channel4, ENABLE);
}
void USART1_SendByteData(u8 dat) {
        if ((DMA1->ISR & DMA1_FLAG_TC4)==0) return;//DMA发送未完成
        while ((USART1->SR & USART_FLAG_TXE)==0);
        USART1->DR=dat;
}

我的。。。
此帖出自stm32/stm8论坛
 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/9 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表