欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

stm32F103片上串口USART1通信实验

程序员文章站 2023-11-18 19:50:28
硬件说明:如原理图所示, 103的PA10 PA11分别接CP2102usb转串口芯片的TXD RXD引脚。CP2102芯片是将串口和电脑USB接口进行了转接,方便应用灵活。 片上串口驱动代码如下。 1 #include "usart.h" 2 #include "stm32f10x_usart.h ......

硬件说明:stm32F103片上串口USART1通信实验如原理图所示,

103的pa10 pa11分别接cp2102usb转串口芯片的txd rxd引脚。cp2102芯片是将串口和电脑usb接口进行了转接,方便应用灵活。

片上串口驱动代码如下。

  1 #include "usart.h"    
  2 #include "stm32f10x_usart.h"
  3 
  4 #if system_support_ucos
  5 #include "includes.h"    //ucos 
  6 #endif
  7 
  8 #if 1
  9 #pragma import(__use_no_semihosting)             
 10                
 11 struct __file 
 12 { 
 13     int handle; 
 14 
 15 }; 
 16 
 17 file __stdout;         
 18 _sys_exit(int x) 
 19 { 
 20     x = x; 
 21 } 
 22 
 23 int fputc(int ch, file *f)
 24 {      
 25         while(usart_getflagstatus(usart1,usart_flag_tc)==reset); 
 26     usart_senddata(usart1,(uint8_t)ch);   
 27       return ch;
 28 }
 29 #endif 
 30 
 31 
 32 #if en_usart1_rx   
 33 //´®¿ú1öð¶ï·þîñ³ìðò
 34 //×¢òâ,¶áè¡usartx->sräü±üãâäªãûæäãîµä´íîó       
 35 u8 usart_rx_buf[usart_rec_len];  //½óêõ»º³å,×î´óusart_rec_len¸ö×ö½ú.
 36 
 37 
 38 
 39 u16 usart_rx_sta=0;       //receiving status marker
 40   
 41 void usart1_irqhandler(void)
 42 {
 43     
 44     u8 res;    
 45 #ifdef os_critical_method     
 46     osintenter();    
 47 #endif
 48     if(usart_getitstatus(usart1, usart_it_rxne) != reset)  //½óêõöð¶ï(½óêõµ½µäêý¾ý±øðëêç0x0d 0x0a½áî²)
 49     {
 50         res =usart_receivedata(usart1);//(usart1->dr);    //¶á衽óêõµ½µäêý¾ý
 51         
 52         if((usart_rx_sta&0x8000)==0)//½óêõî´íê³é
 53             {
 54             if(usart_rx_sta&0x4000)//½óêõµ½áë0x0d
 55                 {
 56                 if(res!=0x0a)usart_rx_sta=0;//½óêõ´íîó,öøð⿪ê¼
 57                 else usart_rx_sta|=0x8000;    //½óêõíê³éáë 
 58                 }
 59             else //»¹ã»êõµ½0x0d
 60                 {    
 61                 if(res==0x0d)usart_rx_sta|=0x4000;
 62                 else
 63                     {
 64                     usart_rx_buf[usart_rx_sta&0x3fff]= res;
 65                     usart_rx_sta++;
 66                     if(usart_rx_sta>(usart_rec_len-1))usart_rx_sta=0;//½óêõêý¾ý´íîó,öøð⿪꼽óêõ      
 67                     }         
 68                 }
 69             }   
 70             
 71      } 
 72 #ifdef os_critical_method     
 73     osintexit();                                               
 74 #endif
 75 } 
 76 #endif                                         
 77 
 78 //a11 rx  a10 tx
 79 
 80 void uart_init()
 81 {
 82     
 83 
 84   gpio_inittypedef gpio_initstructure;
 85     usart_inittypedef usart_initstructure;
 86     nvic_inittypedef nvic_initstructure;
 87      
 88     rcc_apb2periphclockcmd(rcc_apb2periph_usart1|rcc_apb2periph_gpioa, enable);    //ê¹äüusart1£¬gpioaê±öó
 89   usart_deinit(usart1);  //¸´î»´®¿ú1
 90 
 91     gpio_initstructure.gpio_pin = gpio_pin_10; //pc.10
 92     gpio_initstructure.gpio_speed = gpio_speed_50mhz;
 93     gpio_initstructure.gpio_mode = gpio_mode_af_pp;    //¸´óãíæíìêä³ö
 94     gpio_init(gpioa, &gpio_initstructure); //³õê¼»¯pa9
 95    
 96 
 97     gpio_initstructure.gpio_pin = gpio_pin_11;
 98     gpio_initstructure.gpio_mode = gpio_mode_in_floating;//¸¡¿õêäèë
 99     gpio_init(gpioa, &gpio_initstructure);  //³õê¼»¯pa10
100     usart_initstructure.usart_baudrate = 9600;//ò»°ãéèöãîª9600;
101     usart_initstructure.usart_wordlength = usart_wordlength_8b;//×ö³¤îª8î»êý¾ý¸ñê½
102     usart_initstructure.usart_stopbits = usart_stopbits_1;//ò»¸öí£ö¹î»
103     usart_initstructure.usart_parity = usart_parity_no;//îþææå¼ð£ñéî»
104     usart_initstructure.usart_hardwareflowcontrol = usart_hardwareflowcontrol_none;//îþó²¼þêý¾ýá÷¿øöæ
105     usart_initstructure.usart_mode = usart_mode_rx | usart_mode_tx;    //êõ·¢ä£ê½
106 
107     usart_init(usart1, &usart_initstructure); //³õê¼»¯´®¿ú
108 #if en_usart1_rx          //èç¹ûê¹äüáë½óêõ  
109    //usart1 nvic åäöã
110   nvic_initstructure.nvic_irqchannel = usart1_irqn;
111     nvic_initstructure.nvic_irqchannelpreemptionpriority=3 ;//çàõ¼óåï輶3
112     nvic_initstructure.nvic_irqchannelsubpriority = 3;        //×óóåï輶3
113     nvic_initstructure.nvic_irqchannelcmd = enable;            //irqí¨µàê¹äü
114     nvic_init(&nvic_initstructure);    //¸ù¾ýö¸¶¨µä²îêý³õê¼»¯vic¼ä´ææ÷
115    
116   usart_itconfig(usart1, usart_it_rxne, enable);//¿ªæôöð¶ï
117 #endif
118   usart_cmd(usart1, enable);                    //ê¹äü´®¿ú 
119 
120 }
 1 #ifndef __usart_h
 2 #define __usart_h
 3 #include "sys.h"
 4 #include "stdio.h"     
 5 
 6 //////////////////////////////////////////////////////////////////////////////////     
 7 #define usart_rec_len              200      
 8 #define en_usart1_rx               1        
 9           
10 extern u8  usart_rx_buf[usart_rec_len]; 
11 extern u16 usart_rx_sta;                 
12 
13 void uart_init();
14 
15 #endif       
 1                         #include "sys.h"
 2                         #include "usart.h"
 3                         #include "delay.h"
 4                         
 5 #define led0 paout(8)// ds0
 6 #define led1 pbout(1)// ds1    
 7 #define led2 pbout(3)// ds1
 8 #define led3 paout(11)// ds1    
 9 
10                         int main(void)
11                         {
12                             
13                         u8 t=0;
14                         u8 y=0;
15                         stm32_clock_init(9); 
16                         delay_init(72); 
17                         uart_init(); 
18     
19                         while(1)
20                         {
21                         printf("t999:%d\n",t);
22                         delay_ms(1000);
23                         t++;
24                             if(y ==4)y=0;
25                             switch(y)
26                             {
27                                 case 0:  led0 = 1;led1 =0; led2 =0; led3=0;break;
28                                 case 1:     led1 = 1;led0 =0; led2 =0; led3=0; break;
29                                 case 2:  led2 = 1;led0 =0; led1 =0; led3=0;break;
30                                 case 3:     led3 = 1;led1 =0; led0 =0; led2=0;break;
31                             }
32                             y++;
33                         }
34 
35                         return 0;
36                         }