TA的每日心情 | 无聊 2018-11-16 10:48 |
---|
签到天数: 2 天 连续签到: 1 天 [LV.1]初来乍到
|
个很简单的例子读取电位器电压,然后调整占空比
程序代码:
- #include "stm8s.h"
- #include "stm8s_gpio.h"
- #define PWM_F 640
- uint8_t HexTable[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
- uint16_t CCR1_Val = 100;
- void DelayApi(uint16_t uwDelayCnt);
- void TIM2_Config(void);
- void Init_UART1(void)
- {
- UART1_DeInit();
- UART1_Init((u32)9600, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, UART1_PARITY_NO, UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TX_ENABLE);
- // UART1_Cmd(ENABLE);
- }
- void Send(uint8_t dat)
- {
- while(( UART1_GetFlagStatus(UART1_FLAG_TXE)==RESET));
- UART1_SendData8(dat);
- }
- void Init_ADC(void)
- {
- //GPIO_Init(GPIOD, GPIO_PIN_2, GPIO_MODE_IN_FL_NO_IT);
- ADC1_DeInit();
- ADC1_Init(ADC1_CONVERSIONMODE_SINGLE, ADC1_CHANNEL_3, ADC1_PRESSEL_FCPU_D2, ADC1_EXTTRIG_TIM, DISABLE, ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_CHANNEL3, ENABLE);
- ADC1_Cmd(ENABLE);
- }
- void UART1_sendhex(unsigned char dat)
- {
- Send('0');
- Send('x');
- Send(HexTable[dat>>4]);
- Send(HexTable[dat&0x0f]);
- Send(' ');
- }
- void UART1_sendstr(unsigned char *dat)
- {
- while(*dat!='\0')
- {
- Send(*dat);
- dat++;
- //delay2us();
- }
- }
- void main(void)
- {
- //FlagStatus flag_status;
- u16 u16_adc1_value;
- //Init_UART1();
- Init_ADC();
- CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);//16/1=16MHZ
- GPIO_DeInit(GPIOD);
- GPIO_Init(GPIOD,GPIO_PIN_2,GPIO_MODE_IN_PU_NO_IT);
- //GPIO_DeInit(GPIOD);
- GPIO_Init(GPIOD,GPIO_PIN_4,GPIO_MODE_OUT_PP_HIGH_SLOW);
- TIM2_Config();
- TIM2_SetCompare1(CCR1_Val);
- /* Infinite loop */
- while (1)
- {
- //Send(0xf0);
- ADC1_StartConversion();
- //flag_status = ADC1_GetFlagStatus(ADC1_FLAG_EOC);
- // while(flag_status == RESET); // SET or RESET
- while(RESET == ADC1_GetFlagStatus(ADC1_FLAG_EOC));
- u16_adc1_value = ADC1_GetConversionValue();
- //CCR1_Val=u16_adc1_value*2/3-66;
- //CCR1_Val=101+u16_adc1_value*10/19;
- /*if(u16_adc1_value<180)
- {
- CCR1_Val=70+u16_adc1_value*2;
- }else
- {
- CCR1_Val=460+(u16_adc1_value-180)*10/47;
- }
- */
- if(u16_adc1_value<180)
- {
- if(u16_adc1_value<100){u16_adc1_value=100;}
- CCR1_Val=u16_adc1_value+((u16_adc1_value-100)*36/10);
- }else
- {
- CCR1_Val=460+(u16_adc1_value-180)*10/47;
- }
- if(CCR1_Val>30000)
- CCR1_Val=0;
- if(CCR1_Val>PWM_F && CCR1_Val<2000)
- CCR1_Val=PWM_F-2;
- TIM2_SetCompare1(CCR1_Val);
- //UART1_sendhex((u16_adc1_value>>8));
- //UART1_sendhex((u16_adc1_value&0xff));
- //UART1_sendstr("\r\n");
- //GPIO_WriteReverse(GPIOB,GPIO_PIN_5);
- }
- }
- void TIM2_Config(void)
- {
- TIM2_TimeBaseInit(TIM2_PRESCALER_1, PWM_F);
- TIM2_OC1Init(TIM2_OCMODE_PWM1, TIM2_OUTPUTSTATE_ENABLE,CCR1_Val, TIM2_OCPOLARITY_HIGH);
- TIM2_OC1PreloadConfig(ENABLE);
- // TIM2_OC2Init(TIM2_OCMODE_PWM1, TIM2_OUTPUTSTATE_ENABLE,CCR2_Val, TIM2_OCPOLARITY_HIGH);
- // TIM2_OC2PreloadConfig(ENABLE);
- // TIM2_OC3Init(TIM2_OCMODE_PWM1, TIM2_OUTPUTSTATE_ENABLE,CCR3_Val, TIM2_OCPOLARITY_HIGH);
- // TIM2_OC3PreloadConfig(ENABLE);
- TIM2_ARRPreloadConfig(ENABLE);
- TIM2_Cmd(ENABLE);
- }
- void DelayApi(uint16_t uwDelayCnt)
- {
- while(uwDelayCnt--);
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval : None
- */
- void assert_failed(u8* file, u32 line)
- {
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* Infinite loop */
- while (1)
- {
- }
- }
- #endif
[color=rgb(51, 102, 153) !important]复制代码
[color=rgb(51, 102, 153) !important]
|
|