你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

串口代码分析

[复制链接]
tjhbwjk 提问时间:2015-5-19 16:59 /
#include "stm32f30x.h"
#define uint unsigned int
#define uchar unsigned char

       
uint i,j,k;
uchar h;
/*void Delay(i)
       
                {
                       
                for(j=0;j<=i;j++)
                        {
                        for(k=0;k<=1000;k++);
                  }
          }*/

                                void NVIC_Configuration(void);
                                void USART1_IRQHandler(void);
                    void RCC_Configuration(void);
                    void UASRT_Configuration(void);
                    void GPIO_Configyration(void);
               
                        int main()
                        {
                                RCC_Configuration();
                               
                                USART1_IRQHandler();
                    GPIO_Configyration();
                    UASRT_Configuration();
                    NVIC_Configuration();
                               
                                //USART_SendData(USART1, 0X99);
                                while(1);
                                         
                        }   


     void RCC_Configuration(void)
     {
                                RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);//enable clk
                                RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
                                GPIO_DeInit(GPIOA);
                                USART_DeInit(USART1);
                         
                         
                 }
    void NVIC_Configuration(void)
                {
      NVIC_InitTypeDef USART_InitStructure;
                        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
                       
                        USART_InitStructure.NVIC_IRQChannel=USART1_IRQn;
                        USART_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
                        USART_InitStructure.NVIC_IRQChannelSubPriority=5;
                        USART_InitStructure.NVIC_IRQChannelCmd=ENABLE;
                       
                        NVIC_Init(&USART_InitStructure);
               
                }
               
                void USART1_IRQHandler(void)
                         {
                                         if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)
                                         {
                                                USART_SendData(USART1,USART_ReceiveData(USART1));
                                                while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
                                         }
                        }
                         
                void UASRT_Configuration(void)
    {                       
                        USART_InitTypeDef USART_InitStructure;       
                        USART_InitStructure.USART_BaudRate = 115200;//UART
                        USART_InitStructure.USART_WordLength = USART_WordLength_8b;
                        USART_InitStructure.USART_StopBits = USART_StopBits_1;
                        USART_InitStructure.USART_Parity = USART_Parity_No ;
                        USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
                        USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
                       
                        USART_Init(USART1, &USART_InitStructure);
                        USART_ITConfig(USART1, USART_FLAG_RXNE, ENABLE);
                        USART_Cmd(USART1, ENABLE);//EN UART
                        USART_ClearFlag(USART1,USART_FLAG_TC);
                }
               
                void GPIO_Configyration(void)
                       
                {
                        GPIO_InitTypeDef GPIO_InitStructure;
                       
                        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//RX
                        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
                        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
               
                        GPIO_Init(GPIOA, &GPIO_InitStructure);
                        GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_7);
               
                        GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_9;//TX
                        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
                        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
                        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
               
                        GPIO_Init(GPIOA, &GPIO_InitStructure);
                        GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_7);
          
                }       
        请大神帮我看一下,为什么不能收发数据啊?谢谢了
收藏 评论7 发布时间:2015-5-19 16:59

举报

7个回答
沐紫 回答时间:2015-5-19 17:51:44
帮顶
tjhbwjk 回答时间:2015-5-19 18:53:32
用示波器看了下,没进中断,具体原因未知,走过路过的帮我看下哈,帮我找找
那片清茶 回答时间:2015-5-19 21:55:44
#include "uart.h"

void USART_InitConfig(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        USART_InitTypeDef USART_InitStructure;
        NVIC_InitTypeDef NVIC_InitStructure;
       
        RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
       
  GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_7);
  GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_7);       
  /*
  *  USART1_TX -> PA9 , USART1_RX ->        PA10
  */                               
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;                
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);       
       
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;                
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;   
  GPIO_Init(GPIOA, &GPIO_InitStructure);
       
        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);

        USART_Cmd(USART1, ENABLE);
       
  USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
        USART_ITConfig(USART1, USART_IT_TXE, DISABLE);

  /*Enable the USART1 Interrupt(??USART1??)*/                                                                                                        
  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;   
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;                
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}

void UART_send_byte(uint8_t byte) //µ¥×Ö½Ú·¢ËÍ
{
while(!((USART1->ISR)&(1<<7)));
USART1->TDR=byte;       
}               

void UART_Send(uint8_t *Buffer, uint32_t Length)
{
        while(Length != 0)
        {
                while(!((USART1->ISR)&(1<<7)));//µÈ´ý·¢ËÍÍê
                USART1->TDR= *Buffer;
                Buffer++;
                Length--;
        }
}


对照看一下,你的程序写的有点乱。。
为什么是EEFOCUS小白 回答时间:2015-5-20 08:53:39
不懂帮顶
tjhbwjk 回答时间:2015-5-20 09:32:21
那片清茶 发表于 2015-5-19 21:55
#include "uart.h"

void USART_InitConfig(void)

好的 版主谢谢
tjhbwjk 回答时间:2015-5-20 10:22:10
那片清茶 发表于 2015-5-19 21:55
#include "uart.h"

void USART_InitConfig(void)

版主 你的中断函数呢?  能给我看一下吗
那片清茶 回答时间:2015-5-20 11:22:58
tjhbwjk 发表于 2015-5-20 10:22
版主 你的中断函数呢?  能给我看一下吗

        if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
        {
                rx[i++] = USART_ReceiveData(USART1);
        }
        if(i == 8)
        {
                i = 0;
                uart_flag = 1;
        }

所属标签

相似问题

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
微信公众号二维码 微信公众号
手机版二维码 手机版