欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

 找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 2113|回復(fù): 0
收起左側(cè)

STM32 GPIO/USART/timer 控制(寄存器版)

[復(fù)制鏈接]
ID:964867 發(fā)表于 2021-9-8 14:34 | 顯示全部樓層 |閱讀模式
本文出自:zhishi-zhishi點(diǎn)net
在開(kāi)發(fā)板上試驗(yàn)了GPIO點(diǎn)亮LED,串口中斷和定時(shí)器。
  1. #include "stm32f10x.h"

  2. /*
  3. GPIO :  
  4.       PC.14  PC.15 <---> LED

  5.               PB.7 <---> Button


  6.         PA.9 PA.10 <---> USART
  7. */
  8. #define RCC_APB2ENR    *(uint32_t*)(0x40021000+0x18)
  9. #define RCC_APB1ENR    *(uint32_t*)(0x40021000+0x1C)
  10. #define GPIOC_CRH    *(uint32_t*)(0x40011000+0x04)
  11. #define GPIOC_ODR    *(uint32_t*)(0x40011000+0x0C)
  12. #define GPIOB_CRL    *(uint32_t*)(0x40010C00+0x00)
  13. #define GPIOB_ODR    *(uint16_t*)(0x40010C00+0x0C)
  14. #define GPIOB_BSRR   *(uint16_t*)(0x40010C00+0x10)
  15. #define SETENA_ISER0    *(uint32_t*)(0xe000E100+0x00)
  16. #define EXTI_IMR        *(uint32_t*)(0x40010400+0x00)
  17. #define EXTI_RTSR       *(uint32_t*)(0x40010400+0x08)
  18. #define EXTI_FTSR       *(uint32_t*)(0x40010400+0x0C)
  19. #define EXTI_PR         *(uint32_t*)(0x40010400+0x14)
  20. #define AFIO_EXTICR2    *(uint32_t*)(0x40010000+0x0C)

  21. #define GPIOA_IDR    *(uint32_t*)(0x40010800+0x08)
  22. #define GPIOA_ODR    *(uint32_t*)(0x40010800+0x0C)
  23. #define GPIOA_CRH    *(uint32_t*)(0x40010800+0x04)
  24. #define SETENA_ISER1    *(uint32_t*)(0xe000E100+0x04)


  25. //#define USART1_BASE    *(uint32_t*)(0x40013800+0x00)  //bug : redefinition
  26. #define USART1_SR    *(uint32_t*)(0x40013800+0x00)
  27. #define USART1_DR    *(uint32_t*)(0x40013800+0x04)
  28. #define USART1_BRR    *(uint32_t*)(0x40013800+0x08)
  29. #define USART1_CR1    *(uint32_t*)(0x40013800+0x0C)
  30. #define USART1_CR2    *(uint32_t*)(0x40013800+0x10)
  31. #define USART1_CR3    *(uint32_t*)(0x40013800+0x14)

  32. #define TIM1_CR1      *(uint32_t*)(0x40012c00+0x00)
  33. #define TIM1_DIER     *(uint32_t*)(0x40012c00+0x0C)
  34. #define TIM1_SR       *(uint32_t*)(0x40012c00+0x10)
  35. #define TIM1_PSC      *(uint32_t*)(0x40012c00+0x28)
  36. #define TIM1_ARR      *(uint32_t*)(0x40012c00+0x2C)

  37. #define TIM3_CR1      *(uint32_t*)(0x40000400+0x00)
  38. #define TIM3_DIER      *(uint32_t*)(0x40000400+0x0C)
  39. #define TIM3_SR      *(uint32_t*)(0x40000400+0x10)
  40. #define TIM3_PSC      *(uint32_t*)(0x40000400+0x28)
  41. #define TIM3_ARR      *(uint32_t*)(0x40000400+0x2C)



  42. #if 1
  43. int m = 0;
  44. void EXTI9_5_IRQHandler(void)
  45. {
  46.    EXTI_PR = 0x1 << 7;
  47.      m++;
  48.      if(m%2 == 1)
  49.         GPIOC_ODR   = 0x0 << 14;
  50.      else if(m%2 == 0)
  51.           GPIOC_ODR = 0x3 << 14;
  52. }
  53. #endif


  54. char test_str[128];
  55. char* p;
  56. int i;

  57. volatile int mm = 0;
  58. volatile int old_mm  = 0;
  59. char recv_buf[64];
  60. int  num = 0;
  61. int is_one_line = 0;
  62. void USART1_IRQHandler(void)
  63. {
  64.       USART_ClearITPendingBit(USART1, USART_IT_RXNE);
  65.     mm += 10;
  66.      
  67.       recv_buf[num++] = USART1_DR;
  68.       if(num >= 64)
  69.              num = 0;
  70.         if(recv_buf[num-1] == 0x0a && recv_buf[num-2] == 0x0d)
  71.              is_one_line = 1;
  72. }



  73. int time1_out    = 0;
  74. int old_time1_out= 0;

  75. #if 1
  76. void TIM1_UP_IRQHandler(void)
  77. {
  78.     TIM_ClearITPendingBit(TIM1, TIM_FLAG_Update);
  79.     time1_out++;
  80.      
  81. }
  82. #else
  83. void TIM3_IRQHandler(void)
  84. {
  85.     TIM_ClearITPendingBit(TIM3, TIM_FLAG_Update);
  86.     time1_out++;
  87.      
  88. }


  89. #endif




  90. int main()
  91. {
  92.      

  93.      
  94.     RCC_APB2ENR = 0x1 << 4 | 0x1 << 3 | 0x1 << 0 | 0x1 << 2 | 0x1 << 14  /*| 0x1 << 11 */ ;     //p53
  95.   GPIOC_CRH = 0x03 << 28 | 0x03 << 24;  //p68
  96.      
  97.     GPIOB_CRL   = 0x00 <<28 |  /*0x01*/ /*0x10 --bug */0x02 << 30;  
  98.      
  99.   GPIOC_ODR = 0x3 << 14;            //p69
  100.      
  101.      
  102.     EXTI_IMR  = 0x01 << 7;
  103. //EXTI_RTSR = 0x01 << 7;
  104.     EXTI_FTSR = 0x01 << 7;
  105.      
  106.     AFIO_EXTICR2 = 0x01  << 12;


  107.   GPIOA_ODR = 0x1 << 9;

  108.   GPIOA_CRH = 0x3 << 4 | 0x2 << 6 | 0x0 << 8 | 0x1 << 10;



  109.     SETENA_ISER0 = 0x1 << 23;
  110.     SETENA_ISER1 = 0x1 << 5;
  111.      
  112.      
  113.      
  114.      
  115.      
  116.             USART1_BRR = 0x0271;
  117.             USART1_CR1 |= 0x03 << 2;  //TE/RE
  118.          
  119.             USART1_CR1 |= 0x1 << 5 /* | 0x1 <<7*/;
  120.             
  121.             USART1_CR1 |= 0x1 << 13;
  122.          

  123. #if 1



  124.   RCC_APB2ENR |= (0x1 << 11);
  125.   TIM1_CR1  |= 0x1 << 0;   //CEN
  126.     TIM1_DIER |= 0x1 << 0;   //UIE
  127.     TIM1_SR   &= 0xFFFE;     //UIF
  128.      
  129.     TIM1_PSC  = 7199;
  130.     TIM1_ARR  = 9999;

  131.   SETENA_ISER0  |=  0x01 << 25;
  132.    
  133. #else
  134.   RCC_APB1ENR |= 0x1 << 1;
  135.   TIM3_CR1  |= 0x1 << 0;   //CEN
  136.     TIM3_DIER |= 0x1 << 0;   //UIE
  137.     TIM3_SR   &= 0xFFFE;     //UIF
  138.      
  139.     TIM3_PSC  = 7199;
  140.     TIM3_ARR  = 9999;

  141.   SETENA_ISER0  |=  0x01 << 29;

  142. #endif

  143.   
  144.   
  145.    while(1)
  146.      {

  147.          if(is_one_line == 1)
  148.              {
  149.                         memset(test_str, 0, sizeof(test_str));

  150.                   sprintf( test_str, "%s", recv_buf);

  151.                         p = test_str;
  152.                         while((*p)!='\0')
  153.                         {
  154.                             USART_SendData(USART1, *(p++));
  155.                             for(i=0; i < 1000; i++)
  156.                                  ;
  157.                         }           
  158.                         num = 0;
  159.                         is_one_line = 0;
  160.                         memset(recv_buf, 0, sizeof(recv_buf));

  161.              }
  162.               
  163.               
  164.               
  165.              if(old_time1_out == time1_out)
  166.                    ;
  167.              else
  168.              {
  169.                         memset(test_str, 0, sizeof(test_str));

  170.                     sprintf( test_str, "old: %d, time1_out:%d", old_time1_out, time1_out);

  171.                         p = test_str;
  172.                         while((*p)!='\0')
  173.                         {
  174.                             USART_SendData(USART1, *(p++));
  175.                             for(i=0; i < 1000; i++)
  176.                                  ;
  177.                         }           
  178.                     
  179.                   
  180.                  old_time1_out = time1_out;
  181.              }
  182.         
  183.               
  184.      }

  185.      return 0;
  186.       
  187.      

  188. }
復(fù)制代碼
至此試驗(yàn)就告完成。用過(guò)仿真軟件Proteus(后來(lái)好象串口COMPIM失效了。)和編譯器Keil MDK5,表示謝意。
源文件打包在此: test_timer.7z (268.11 KB, 下載次數(shù): 8)
本文出自:www點(diǎn)zhishi-zhishi點(diǎn)net/working/stm32_timer.html

評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表