标题: stm32利用pwm控制呼吸灯源程序 [打印本页]

作者: cxteng    时间: 2021-7-28 17:33
标题: stm32利用pwm控制呼吸灯源程序
  1. #include "stm32f10x.h"
  2. #include "BSP.h"
  3. #include "delay.h"
  4. #include "sys.h"
  5. #include "usart.h"
  6. #include "stm32f10x_it.h"
  7. #include "stm32f10x_tim.h"
  8. #define LED1    PAout(1)
  9. #define LED2    PAout(2)
  10. #define LED3    PAout(3)
  11. #define LED4    PAout(8)
  12. #define KEY1    GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)
  13. #define KEY2    GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_9)
  14. int fputc(int ch,FILE  *f);
  15. char *itoa(int value, char *string, int radix);
  16. unsigned int buf = 0;

  17. /*int main(void)
  18. {
  19.         LED_Init();
  20.         TIM_Init();
  21.         USART_Config();
  22.         TIM2_IRQHandler();
  23.         USART1_IRQHandler();
  24.         while(1)
  25.         {
  26.                 if(i==1000)
  27.                 {
  28.                         i=0;
  29.                         LED1=~LED1;
  30.                         printf("1\r\n");
  31.                 }
  32.         }
  33. }
  34. */
  35. ///////////////////////////////////////
  36. /*int main()
  37. {
  38.         LED_Init();
  39.         USART_Config();
  40.         USART1_IRQHandler();
  41.         while(1)
  42.         {
  43.                 if(USART_ReceiveData(USART1)=='1')
  44.                 {
  45.                         GPIO_SetBits(GPIOA,GPIO_Pin_8);
  46.                 }
  47.                 if(USART_ReceiveData(USART1)=='0')
  48.                 {
  49.                         GPIO_ResetBits(GPIOA,GPIO_Pin_8);
  50.                 }

  51.         }
  52. }*/
  53. ////////////////////////////////////


  54. uint16_t j;

  55. int i;

  56. int  main(void)
  57. {
  58.         delay_init();
  59.         bsp_Init();
  60.         while(1)
  61.         {
  62.                
  63.                 for(j=0;j<300;j++)
  64.                         {
  65.                                 TIM_SetCompare3(TIM2,j);
  66.                                 delay_ms(2);
  67.                         }
  68.                 for(j=300;j>0;j--)        
  69.                         {
  70.                                 TIM_SetCompare3(TIM2,j);
  71.                                 delay_ms(2);
  72.                         }
  73.                         //3表示通道//TIM2表示定时器2,j代表占空比
  74.                
  75.         }

  76. }


  77. /********************************************以下不要擅自乱修改*****************************************/
  78. /********************************************此段函数可以实现printf的输出*****************************************/
  79. //加入以下代码,支持printf函数,而不需要选择use MicroLIB
  80. #if 1
  81. #pragma import(__use_no_semihosting)
  82. //标准库需要的支持函数
  83. struct __FILE
  84. {
  85.         int handle;
  86.         /* Whatever you require here. If the only file you are using is */
  87.         /* standard output using printf() for debugging, no file handling */
  88.         /* is required. */
  89. };
  90. /* FILE is typedef’ d in stdio.h. */
  91. FILE __stdout;
  92. //定义_sys_exit()以避免使用半主机模式
  93. void _sys_exit(int x)
  94. {
  95.         x = x;
  96. }
  97. /*********************************************************
  98.                                                                                 printf重定义
  99. **********************************************************/
  100. //int fputc(int ch, FILE *f)
  101. //{
  102. //        while((USART2->SR&0X40)==0);//循环发送,直到发送完毕
  103. //        USART2->DR = (u8) ch;
  104. //        return ch;
  105. //}
  106. #endif

  107. /******************************************************
  108.                 整形数据转字符串函数
  109.         char *itoa(int value, char *string, int radix)
  110.                 radix=10 标示是10进制        非十进制,转换结果为0;

  111.             例:d=-379;
  112.                 执行        itoa(d, buf, 10); 后

  113.                 buf="-379"
  114. **********************************************************/
  115. char *itoa(int value, char *string, int radix)
  116. {
  117.     int     i, d;
  118.     int     flag = 0;
  119.     char    *ptr = string;

  120.     /* This implementation only works for decimal numbers. */
  121.     if (radix != 10)
  122.     {
  123.         *ptr = 0;
  124.         return string;
  125.     }

  126.     if (!value)
  127.     {
  128.         *ptr++ = 0x30;
  129.         *ptr = 0;
  130.         return string;
  131.     }

  132.     /* if this is a negative value insert the minus sign. */
  133.     if (value < 0)
  134.     {
  135.         *ptr++ = '-';

  136.         /* Make the value positive. */
  137.         value *= -1;
  138.     }

  139.     for (i = 10000; i > 0; i /= 10)
  140.     {
  141.         d = value / i;

  142.         if (d || flag)
  143.         {
  144.             *ptr++ = (char)(d + 0x30);
  145.             value -= (d * i);
  146.             flag = 1;
  147.         }
  148.     }

  149.     /* Null terminate the string. */
  150.     *ptr = 0;

  151.     return string;

  152. } /* NCL_Itoa */
复制代码

代码工程文件: program.7z (176.9 KB, 下载次数: 48)

作者: 7631001    时间: 2021-8-15 18:25
可以试试效果




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1