Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4263082
  • 博文数量: 1148
  • 博客积分: 25453
  • 博客等级: 上将
  • 技术积分: 11949
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-06 21:14
文章分类

全部博文(1148)

文章存档

2012年(15)

2011年(1078)

2010年(58)

分类: 嵌入式

2011-06-24 19:28:38

工程代码: 10_usart1中断发送.rar  

  1. * 修改Date:2011.6.24 19:30
  2. * 功能描述:打开串口调试软件(网上很多可以自己下一个),系统板下载完程序后,
  3.           按电源开关重起,看PC机上显示数据是否为07,试着改变main()
  4.          函数USART_SendData(USART1,(u16)7)中“7”这个数据,编译后下载到
  5.          系统板看效果如何。
  6.          注意16进制查看
  7.         
  8.          中断方式实现,中断函数中 取消中断标志位。
  9.     USART1_GPIO_Configuration();
        USART1_Configuration();
        USART1_NVIC_Configuration();

        while(1)
        {
            USART_SendData(USART1,(u16)7);     //发送数据    发送后中断
        
        //发送一个数据后,闪烁LED
            GPIO_WriteBit(GPIOA, GPIO_Pin_8,(BitAction)dat);
            GPIO_WriteBit(GPIOD, GPIO_Pin_2,(BitAction)dat);
            dat = 1 - dat;
            delay_s(1);
        }
        return 0;

  1. #ifndef __USART1_H
  2. #define __USART1_H

  3. #include "stm32f10x_type.h"
  4. #include "stm32f10x_gpio.h"
  5. #include "stm32f10x_usart.h"
  6. #include "stm32f10x_nvic.h"

  7. void USART1_GPIO_Configuration(void);//管脚配置
  8. void USART1_Configuration(void);
  9. void USART1_NVIC_Configuration(void); //中断配置


  10. /*usart1 中断 发送 函数例子*/
  11. #if 0
  12. void USART1_IRQHandler(void)
  13. {
  14.     /*中断发送*/
  15.     if(USART_GetITStatus(USART1, USART_IT_TC) != RESET) //是发送中断
  16.     {
  17.     // USART_SendData(USART1, (u16)DATA);             //发送数据
  18.          USART_ClearITPendingBit(USART1, USART_IT_TC);     //清标记
  19.     }

  20. }
  21. #endif

  1. #include "usart1.h"

  2. /**********************************************************************
  3. * 名 称:GPIO_Configuration()
  4. * 功 能:IO配置
  5. * 入口参数:
  6. * 出口参数:
  7. -----------------------------------------------------------------------
  8. * 说明:
  9. ***********************************************************************/
  10. void USART1_GPIO_Configuration(void)
  11. {
  12.     GPIO_InitTypeDef GPIO_InitStructure;
  13.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //管脚9
  14.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  15.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
  16.     GPIO_Init(GPIOA, &GPIO_InitStructure); //TX初始化
  17.     
  18.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //管脚10
  19.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入
  20.     GPIO_Init(GPIOA, &GPIO_InitStructure); //RX初始化    
  21. }

  22. /**********************************************************************
  23. * 名 称:USART_Configuration()
  24. * 功 能:串口配置
  25. * 入口参数:
  26. * 出口参数:
  27. -----------------------------------------------------------------------
  28. * 说明:
  29. ***********************************************************************/
  30. void USART1_Configuration(void) //串口初始化函数
  31. {
  32.     //串口参数初始化
  33.     USART_InitTypeDef USART_InitStructure; //串口设置恢复默认参数
  34.     USART_ClockInitTypeDef USART_ClockInitTypeDefStructure;
  35.     
  36.     //初始化参数设置
  37.     USART_InitStructure.USART_BaudRate = 9600; //波特率9600
  38.     USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字长8位
  39.     USART_InitStructure.USART_StopBits = USART_StopBits_1; //1位停止字节
  40.     USART_InitStructure.USART_Parity = USART_Parity_No; //无奇偶校验
  41.     USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无流控制
  42.     USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//打开Rx接收和Tx发送功能
  43.     //初始化时钟
  44.     USART_ClockInitTypeDefStructure.USART_Clock = USART_Clock_Disable;
  45.     USART_ClockInitTypeDefStructure.USART_CPOL = USART_CPOL_Low;
  46.     USART_ClockInitTypeDefStructure.USART_CPHA = USART_CPHA_2Edge;
  47.     USART_ClockInitTypeDefStructure.USART_LastBit = USART_LastBit_Disable;
  48.     
  49.     USART_Init(USART1, &USART_InitStructure); //初始化
  50.     USART_ClockInit(USART1, &USART_ClockInitTypeDefStructure);
USART_ITConfig(USART1, USART_IT_TC, ENABLE); 中断发送允许
  1.     USART_Cmd(USART1, ENABLE); //启动串口
  2.     
  3.     //使能 时钟,我们在 rcc中使能
  4.     //RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  5.     // RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE);
  6. }

  7. /*****************************************************************************************************************
  8. ** 函数名称:NVIC_Configuration()
  9. ** 功能描述:中断配置
  10. ** 接口参数:
  11. ** 全局变量:
  12. ** 作 者:
  13. ** 日 期:
  14. ------------------------------------------------------------------------------------------------------------------
  15. * 说明:
  16. *****************************************************************************************************************/
  17. void USART1_NVIC_Configuration(void)
  18. {
  19.     NVIC_InitTypeDef NVIC_InitStructure;
  20.     
  21. #ifdef VECT_TAB_RAM
  22.     /* Set the Vector Table base location at 0x20000000 */
  23.     NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
  24. #else /* VECT_TAB_FLASH */
  25.     /* Set the Vector Table base location at 0x08000000 */
  26.     NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
  27. #endif
  28.     
  29.     NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;//通道设置为串口1中断
  30.     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //中断占先等级0
  31.     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //中断响应优先级0
  32.     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //打开中断
  33.     NVIC_Init(&NVIC_InitStructure); //初始化

  34. }










阅读(919) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~