zoukankan      html  css  js  c++  java
  • 串口通信DMA中断

    这是以前学32的时候写的,那时候学了32之后感觉32真是太强大了,比51强的没影。关于dma网上有许多的资料,亲们搜搜,这里只贴代码了,其实我也想详详细细地叙述一番,但是自己本身打字就慢,还有好多事情要做!代码是我亲自都在板子上测试过的,,当然粘贴/复制过去可能也不会尽如人意,知识这东西总是有许多道不清说不明的东西在里头,往往总是不经一番彻骨寒,哪得梅花扑鼻香。推荐一本书吧!这是野火出的。(是为了凑够150个字,否则不让提交)

    这本书自从在图书馆借来就从来没有再放回去,总是在续借。像是在打广告了

    #include <stm32f10x.h>
    #include "usart1.h"
    #include "qq1.h"
    #include "GPIO.h"
    
    extern uint8_t SendBuff[SENDBUFF_SIZE];
    
    uint16_t i;    
    int main()
    {
        USART1_Config();
      DMA_Config();
        GPIOinit();
        
     for(i=0 ; i<SENDBUFF_SIZE ; i++)
        {
       SendBuff[i] = 0xdd;
      }
        USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);    
        
        GPIO_Write(GPIOB,0xffff);  //将GPIOB 16个端口全部置为高电
        
        while(1);
    }
    /* 其他函数里 USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);     */
    #include "usart1.h"
    
    uint8_t SendBuff[SENDBUFF_SIZE];
    
    /*
     * 函数名:DMA_Config
     * 描述  :DMA 串口的初始化配置
     * 输入  :无
     * 输出  : 无
     * 调用  :外部调用
     */
    void DMA_Config(void)
    {
        DMA_InitTypeDef DMA_InitStructure;
    
          RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);    //开启DMA时钟
          NVIC_Config();                   //配置DMA中断
    
         /*设置DMA源:内存地址&串口数据寄存器地址*/
        DMA_InitStructure.DMA_PeripheralBaseAddr = USART1_DR_Base;       
    
        /*内存地址(要传输的变量的指针)*/
        DMA_InitStructure.DMA_MemoryBaseAddr = (u32)SendBuff;
        
        /*方向:从内存到外设*/        
        DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;    
        
        /*传输大小DMA_BufferSize=SENDBUFF_SIZE*/    
        DMA_InitStructure.DMA_BufferSize = SENDBUFF_SIZE;
        
        /*外设地址不增*/        
        DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; 
        
        /*内存地址自增*/
        DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;    
        
        /*外设数据单位*/    
        DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
        
        /*内存数据单位 8bit*/
        DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;     
        
        /*DMA模式:一次传输,循环*/
        DMA_InitStructure.DMA_Mode = DMA_Mode_Normal ;     
        
        /*优先级:中*/    
        DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;  
        
        /*禁止内存到内存的传输    */
        DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
        
        /*配置DMA1的4通道*/           
        DMA_Init(DMA1_Channel4, &DMA_InitStructure);        
        
          DMA_Cmd (DMA1_Channel4,ENABLE);                    //使能DMA
          DMA_ITConfig(DMA1_Channel4,DMA_IT_TC,ENABLE);  //配置DMA发送完成后产生中断
    
    }
    /*
     * 函数名:NVIC_Config
     * 描述  :DMA 中断配置
     * 输入  :无
     * 输出  : 无
     * 调用  :外部调用
     */
    static void NVIC_Config(void)
    {
      NVIC_InitTypeDef NVIC_InitStructure;
      
      /* Configure one bit for preemption priority */
      NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
      
      /* 配置P[A|B|C|D|E]0为中断源 */
      NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_IRQn;
      NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
      NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
      NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
      NVIC_Init(&NVIC_InitStructure);
    }
    #ifndef __USART1_H
    #define    __USART1_H
    
    #include "stm32f10x.h"
    
    #define USART1_DR_Base  0x40013804
    #define SENDBUFF_SIZE 5000
    
    void DMA_Config(void);
    static void NVIC_Config(void);
    
    #endif /* __USART1_H */
    #include "qq1.h"
    
    /*
     * 函数名:USART1_Config
     * 描述  :USART1 GPIO 配置,工作模式配置。115200 8-N-1
     * 输入  :无
     * 输出  : 无
     * 调用  :外部调用
     */
    void USART1_Config(void)
    {
        GPIO_InitTypeDef GPIO_InitStructure;
        USART_InitTypeDef USART_InitStructure;
        
        /* config USART1 clock */
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
        
        /* USART1 GPIO config */
        /* Configure USART1 Tx (PA.09) as alternate function push-pull */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);    
        /* Configure USART1 Rx (PA.10) as input floating */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
          
        /* USART1 mode config */
        USART_InitStructure.USART_BaudRate = 115200;
        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); 
        USART_Cmd(USART1, ENABLE); 
    }
    #ifndef __QQ1_H
    #define __QQ1_H
    
    #include <stm32f10x.h>
    
    
    void USART1_Config(void);
    
    #endif
    #include"GPIO.h"
         
    void GPIOinit(void)
    {
        
        GPIO_InitTypeDef GPIO_InitStructure;
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE);
                            
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All; //所有GPIO为同一类型端口
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;     //推挽输出
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;     //输出的最大频率为50HZ
        
        GPIO_Init(GPIOB, &GPIO_InitStructure);   //初始化GPIOB端口
        
        
    }
    #ifndef __GPIO_H
    #define __GPIO_H
    
    #include "stm32f10x.h"
    
    void GPIOinit(void);
    
    #endif
    #include "stm32f10x_it.h"
    
    
    void DMA1_Channel4_IRQHandler(void)
    {    
    
       if(DMA_GetFlagStatus(DMA1_FLAG_TC4)==SET) 
       {  
      
        GPIOB ->BRR = GPIO_Pin_All;
         
        DMA_ClearFlag(DMA1_FLAG_TC4); 
             
        }    
    }
  • 相关阅读:
    096实战 在windows下新建maven项目
    095实战 ETL的数据来源,处理,保存
    094实战 关于js SDK的程序,java SDK的程序
    093实战 Nginx日志切割,以及脚本上传nginx的切割日志
    092实战 数据收集(各种事件)
    091实战 Nginx配置(日志服务器中关于日志的产生)
    android64位机子兼容32位.so库文件
    给 Android 初学者的 Gradle 知识普及
    Android重力感应开发
    随笔之Android平台上的进程调度探讨
  • 原文地址:https://www.cnblogs.com/yangfengwu/p/5297155.html
Copyright © 2011-2022 走看看