專注電子技術(shù)學(xué)習(xí)與研究
當(dāng)前位置:單片機(jī)教程網(wǎng) >> MCU設(shè)計實例 >> 瀏覽文章

STM32串口通信和I/O口簡單代碼

作者:佚名   來源:本站原創(chuàng)   點擊數(shù):  更新時間:2013年10月22日   【字體:

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

 

 

 
int main(void)
{
 
  u8 k;     //點亮的燈位數(shù)
  u8 i;
  Stm32_Clock_Init(9); //系統(tǒng)時鐘初始化
  delay_init(72);   //延時初始化
  uart_init(72,9600);   //串口初始化
  led_init(); //燈I/O配置初始化
  while(1)
  {
    if(USART_RX_STA&0x80)   //接受到數(shù)據(jù)判斷
{
 printf("\n you send massage is :\n");
 for(i=0;i<(USART_RX_STA&0x3f);i++)
 {
  USART1->DR=USART_RX_BUF[i];   //把接受到的數(shù)據(jù)傳給電腦
  k=USART_RX_BUF[2]-0x30;   //接受到要開發(fā)的燈泡值
 }
 
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;

 
}
}

//-------------------------------------燈配置庫函數(shù)---------------

#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
關(guān)閉窗口

相關(guān)文章