极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9069|回复: 0

STM8S003F3P6 控制电脑4PIN风扇 PWM

[复制链接]
发表于 2018-1-7 19:43:51 | 显示全部楼层 |阅读模式
读取电位器电压,然后调整占空比




  1. #include "stm8s.h"
  2. #include "stm8s_gpio.h"

  3. #define PWM_F  640

  4. uint8_t HexTable[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
  5. uint16_t CCR1_Val = 100;

  6. void DelayApi(uint16_t uwDelayCnt);
  7. void TIM2_Config(void);

  8. void Init_UART1(void)
  9. {
  10.         UART1_DeInit();
  11.         UART1_Init((u32)9600, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, UART1_PARITY_NO, UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TX_ENABLE);
  12. //        UART1_Cmd(ENABLE);
  13. }
  14. void Send(uint8_t dat)
  15. {
  16.     while(( UART1_GetFlagStatus(UART1_FLAG_TXE)==RESET));
  17.         
  18.     UART1_SendData8(dat);
  19.         
  20. }
  21. void Init_ADC(void)
  22. {
  23.         //GPIO_Init(GPIOD, GPIO_PIN_2, GPIO_MODE_IN_FL_NO_IT);
  24.         ADC1_DeInit();
  25.         ADC1_Init(ADC1_CONVERSIONMODE_SINGLE, ADC1_CHANNEL_3, ADC1_PRESSEL_FCPU_D2, ADC1_EXTTRIG_TIM, DISABLE, ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_CHANNEL3, ENABLE);
  26.         ADC1_Cmd(ENABLE);
  27. }
  28. void UART1_sendhex(unsigned char dat)
  29. {
  30.     Send('0');
  31.                
  32.     Send('x');
  33.                
  34.     Send(HexTable[dat>>4]);
  35.                
  36.     Send(HexTable[dat&0x0f]);
  37.                
  38.     Send(' ');
  39. }
  40. void UART1_sendstr(unsigned char *dat)
  41. {
  42.     while(*dat!='\0')
  43.                
  44.     {
  45.                                 
  46.       Send(*dat);
  47.                                 
  48.       dat++;
  49.       //delay2us();
  50.                
  51.     }
  52. }
  53. void main(void)
  54. {
  55.     //FlagStatus flag_status;
  56.     u16 u16_adc1_value;
  57.     //Init_UART1();
  58.     Init_ADC();
  59.    
  60.     CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);//16/1=16MHZ
  61.     GPIO_DeInit(GPIOD);
  62.     GPIO_Init(GPIOD,GPIO_PIN_2,GPIO_MODE_IN_PU_NO_IT);
  63.     //GPIO_DeInit(GPIOD);
  64.     GPIO_Init(GPIOD,GPIO_PIN_4,GPIO_MODE_OUT_PP_HIGH_SLOW);
  65.    
  66.     TIM2_Config();
  67.     TIM2_SetCompare1(CCR1_Val);
  68. /* Infinite loop */
  69.     while (1)
  70.     {
  71.                 //Send(0xf0);
  72.          ADC1_StartConversion();
  73.          //flag_status = ADC1_GetFlagStatus(ADC1_FLAG_EOC);
  74.         // while(flag_status == RESET); // SET or RESET
  75.          while(RESET == ADC1_GetFlagStatus(ADC1_FLAG_EOC));
  76.              u16_adc1_value = ADC1_GetConversionValue();
  77.          //CCR1_Val=u16_adc1_value*2/3-66;
  78.          //CCR1_Val=101+u16_adc1_value*10/19;
  79.          /*if(u16_adc1_value<180)
  80.          {
  81.             CCR1_Val=70+u16_adc1_value*2;
  82.          }else
  83.          {
  84.           CCR1_Val=460+(u16_adc1_value-180)*10/47;
  85.          }
  86.          */
  87.           if(u16_adc1_value<180)
  88.           {
  89.             if(u16_adc1_value<100){u16_adc1_value=100;}
  90.             CCR1_Val=u16_adc1_value+((u16_adc1_value-100)*36/10);
  91.           }else
  92.           {
  93.             CCR1_Val=460+(u16_adc1_value-180)*10/47;
  94.           }
  95.            
  96.          if(CCR1_Val>30000)
  97.            CCR1_Val=0;
  98.          if(CCR1_Val>PWM_F && CCR1_Val<2000)
  99.            CCR1_Val=PWM_F-2;
  100.           TIM2_SetCompare1(CCR1_Val);
  101.          //UART1_sendhex((u16_adc1_value>>8));
  102.          //UART1_sendhex((u16_adc1_value&0xff));
  103.          //UART1_sendstr("\r\n");
  104.          
  105.          //GPIO_WriteReverse(GPIOB,GPIO_PIN_5);
  106.          
  107.   }
  108.   
  109. }

  110. void TIM2_Config(void)
  111. {

  112.   TIM2_TimeBaseInit(TIM2_PRESCALER_1, PWM_F);

  113.   TIM2_OC1Init(TIM2_OCMODE_PWM1, TIM2_OUTPUTSTATE_ENABLE,CCR1_Val, TIM2_OCPOLARITY_HIGH);
  114.   TIM2_OC1PreloadConfig(ENABLE);

  115. //  TIM2_OC2Init(TIM2_OCMODE_PWM1, TIM2_OUTPUTSTATE_ENABLE,CCR2_Val, TIM2_OCPOLARITY_HIGH);
  116. //  TIM2_OC2PreloadConfig(ENABLE);
  117.          
  118. //  TIM2_OC3Init(TIM2_OCMODE_PWM1, TIM2_OUTPUTSTATE_ENABLE,CCR3_Val, TIM2_OCPOLARITY_HIGH);
  119. //  TIM2_OC3PreloadConfig(ENABLE);
  120.   TIM2_ARRPreloadConfig(ENABLE);

  121.   TIM2_Cmd(ENABLE);
  122. }

  123. void DelayApi(uint16_t uwDelayCnt)
  124. {
  125.   while(uwDelayCnt--);
  126. }

  127. #ifdef USE_FULL_ASSERT

  128. /**
  129.   * @brief  Reports the name of the source file and the source line number
  130.   *   where the assert_param error has occurred.
  131.   * @param file: pointer to the source file name
  132.   * @param line: assert_param error line source number
  133.   * @retval : None
  134.   */
  135. void assert_failed(u8* file, u32 line)
  136. {
  137.   /* User can add his own implementation to report the file name and line number,
  138.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  139.   /* Infinite loop */
  140.   while (1)
  141.   {
  142.   }
  143. }
  144. #endif
复制代码


程序打包下载(IAR):
链接:https://pan.baidu.com/s/1i5aShzn 密码:t8rc

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-20 07:51 , Processed in 0.043355 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表