Chinaunix首页 | 论坛 | 博客
  • 博客访问: 99918
  • 博文数量: 87
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2013-12-20 10:54
文章分类
文章存档

2016年(19)

2015年(2)

2013年(66)

我的朋友

分类: 嵌入式

2016-09-06 10:22:44

工程代码:  8_usart1_查询发送.rar  

 一个数据字(8或9位),最低有效位在前



  1. * 修改Date:2011.6.23 21:00
  2. * 功能描述:打开串口调试软件(网上很多可以自己下一个),系统板下载完程序后,
  3. 按电源开关重起,看PC机上显示数据是否为03,试着改变main() 中
  4. 函数USART_SendData(USART1,(u16)3)中“3”这个数据,编译后下载到
  5. 系统板看效果如何。
  6. 发送一个u16数据,闪烁led,延时1s 时间
  7. 注意16进制查看

  8. 使能查询方式,没有使用中断方式
  9. #include "stm32f10x_lib.h"
    #include "delay.h"
    #include "rcc.h"
    #include "led.h"
    #include "usart1.h"

    int main()
    {   
        u8 dat = 0;
        RCC_Configuration();
        delay_init();
        LED_Init();
        USART1_GPIO_Configuration();
        USART1_Configuration();

        while(1)
        {
            USART_SendData(USART1,(u16)3);     //发送数据
              while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);//等待发送完毕   
       
            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. void USART1_GPIO_Configuration(void);//管脚配置
  7. void USART1_Configuration(void);

  8. #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);
  51.     USART_Cmd(USART1, ENABLE); //启动串口
  52.     
  53.     //使能 时钟,我们在 rcc中使能
  54.     //RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  55.     // RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE);
  56. }






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