专注电子技术学习与研究
当前位置:单片机教程网 >> MCU设计实例 >> 浏览文章

STM32串口通信和I/O口简单代码

作者:佚名   来源:本站原创   点击数:  更新时间:2013年10月22日   【字体:

 
#include <stm32f10x_lib.h>
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"

 

 

 
int main(void)
{
 
  u8 k;     //点亮的灯位数
  u8 i;
  Stm32_Clock_Init(9); //系统时钟初始化
  delay_init(72);   //延时初始化
  uart_init(72,9600);   //串口初始化
  led_init(); //灯I/O配置初始化
  while(1)
  {
    if(USART_RX_STA&0x80)   //接受到数据判断
{
 printf("\n you send massage is :\n");
 for(i=0;i<(USART_RX_STA&0x3f);i++)
 {
  USART1->DR=USART_RX_BUF[i];   //把接受到的数据传给电脑
  k=USART_RX_BUF[2]-0x30;   //接受到要开发的灯泡值
 }
 
USART_RX_STA=0;  //接受位清零
}
if(k==1)LED1=0;  //判断点亮那个灯
if(k==2)LED2=0;
if(k==3)LED3=0;
if(k==4)LED4=0;
if(k==5)LED5=0;
if(k==6)LED6=0;
if(k==7)LED7=0;
if(k==8)LED8=0;

 
}
}

//-------------------------------------灯配置库函数---------------

#include <stm32f10x_lib.h>
#include "led.h"

 

 
void led_init()
{
  RCC->APB2ENR|=1<<3; //时能PORTB时钟
  GPIOB->CRL&=0x00000000;  //清空寄存器值
  GPIOB->CRL|=0x33333333;  //推挽输出
  GPIOB->ODR=0xffff;

 
}


#ifndef _led_h
#define _led_h

 

 
 void led_init(void);
#define LED1 PBout(0)
#define LED2 PBout(1)
#define LED3 PBout(2)
#define LED4 PBout(3)
#define LED5 PBout(4)
#define LED6 PBout(5)
#define LED7 PBout(6)
#define LED8 PBout(7)

 
#endif
关闭窗口

相关文章