#include "stm8s.h" //包含用到的外设的头文件
//LED操作宏
#define LED1_ON() GPIO_WriteLow(GPIOB,GPIO_PIN_5)
#define LED1_OFF() GPIO_WriteHigh(GPIOB,GPIO_PIN_5)
#define LED2_ON() GPIO_WriteLow(GPIOB,GPIO_PIN_4)
#define LED2_OFF() GPIO_WriteHigh(GPIOB,GPIO_PIN_4)
u16 CCR1_Val = 300;
u16 CCR2_Val = 100;
u16 CCR3_Val = 100;
/**************************实现函数********************************************
*函数原型: void Delay(unsigned int time)
*功 能: 实现一定时间的延时,延时设定时长由输入time 决定
*******************************************************************************/
void Delay(unsigned int time)
{
volatile unsigned int i;
while(time--){
i=300;
while(i--); //做一些没有用的事,打发时间
}
}
/**************************实现函数********************************************
*函数原型: main() 主程序
*功 能: 设置LED的端口为输出,并使开发板上的LED2闪烁
*******************************************************************************/
main()
{
u16 i;
//CLK_HSECmd(ENABLE);//外部晶振有效
//CLK_ClockSecuritySystemEnable();//时钟安全开启
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);//内部振荡分频
CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1);//CPU分频
//CLK_ITConfig(CLK_IT_CSSD, ENABLE);//时钟失效中断
//复位GPIO* P*端口的设置恢复为初始值
CLK->;PCKENR1 =0xa0;
CLK->;PCKENR2 =0x00;
GPIO_DeInit(GPIOA);
GPIO_DeInit(GPIOB);
GPIO_DeInit(GPIOC);
//设置PB5为输出,
GPIO_Init(GPIOB,GPIO_PIN_5,GPIO_MODE_OUT_PP_LOW_FAST);
//GPIO_Init(GPIOA, GPIO_PIN_3, GPIO_MODE_IN_FL_IT);
//GPIO_Init(GPIOC, GPIO_PIN_5, GPIO_MODE_IN_FL_IT);
//GPIO_Init(GPIOC, GPIO_PIN_6, GPIO_MODE_IN_FL_NO_IT);
GPIO_Init(GPIOC, GPIO_PIN_3, GPIO_MODE_OUT_PP_LOW_FAST);
GPIO_Init(GPIOC, GPIO_PIN_4, GPIO_MODE_OUT_PP_LOW_FAST);
//GPIO_Init(GPIOD, GPIO_PIN_2, GPIO_MODE_OUT_OD_HIZ_SLOW);
GPIO_Init(GPIOD, GPIO_PIN_3, GPIO_MODE_OUT_PP_HIGH_FAST);
GPIO_Init(GPIOD, GPIO_PIN_4, GPIO_MODE_OUT_PP_HIGH_FAST);
GPIO_Init(GPIOA, GPIO_PIN_3, GPIO_MODE_OUT_PP_HIGH_FAST);
//设置TIM1PWM
TIM1_DeInit();
TIM1->;PSCRH = 0x00;
TIM1->;PSCRL = 0x00; //1分频
TIM1->CCMR3 = 0x60; /* Output mode PWM1.TIM_CNT<TIM_CCR时,输出有效电平,不使能预装载 */
TIM1->CCMR4 = 0x60; /* Output mode PWM1.TIM_CNT<TIM_CCR时,输出有效电平,不使能预装载 */
TIM1->CCER2 = 0x11; /* CC polarity high,enable PWM output */
TIM1->ARRH = 0x0f; /* Freq control register: ARR */
TIM1->ARRL = 0xff; /* Freq control register: ARR */
TIM1->CCR3H = 0x08; /* Dutycycle control register: CCR */
TIM1->CCR3L = 0XE8; /* Dutycycle control register: CCR */
TIM1->CCR4H = 0x04; /* Dutycycle control register: CCR */
TIM1->CCR4L = 0XE8; /* Dutycycle control register: CCR */
TIM1->BKR = 0X80; //MOE =1;主输出使能
TIM1->RCR = 0x01;
TIM1->EGR = 0x01;
TIM1->CR1 = 0x01; /* Enable TIM1. TIM3->ARR立 即写入影子寄存器 */
//CLK_PCKENR1 |= 0x60;
//设置TIM1PWM
TIM2_DeInit();
TIM2->;PSCR = 0x00; //1分频
TIM2->CCMR1 = 0x60;
TIM2->CCMR2 = 0x60; /* Output mode PWM1.TIM_CNT<TIM_CCR时,输出有效电平,不使能预装载 */
TIM2->CCMR3 = 0x60; /* Output mode PWM1.TIM_CNT<TIM_CCR时,输出有效电平,不使能预装载 */
TIM2->CCER1 = 0x11; /* CC polarity high,enable PWM output */
TIM2->CCER2 = 0x01; /* CC polarity high,enable PWM output */
TIM2->ARRH = 0x0f; /* Freq control register: ARR */
TIM2->ARRL = 0xff; /* Freq control register: ARR */
TIM2->CCR1H = 0x08; /* Dutycycle control register: CCR */
TIM2->CCR1L = 0XE8; /* Dutycycle control register: CCR */
TIM2->CCR2H = 0x08; /* Dutycycle control register: CCR */
TIM2->CCR2L = 0XE8; /* Dutycycle control register: CCR */
TIM2->CCR3H = 0x04; /* Dutycycle control register: CCR */
TIM2->CCR3L = 0XE8; /* Dutycycle control register: CCR */
TIM2->EGR =0x01;
TIM2->CR1 = 0x01; /* Enable TIM1. TIM3->ARR立 即写入影子寄存器 */
/* Time base configuration
TIM2_TimeBaseInit(TIM2_PRESCALER_1, 1024);
TIM2_OC1Init(TIM2_OCMODE_PWM1, TIM2_OUTPUTSTATE_ENABLE,CCR1_Val, TIM2_OCPOLARITY_HIGH);//PWM模式可设置PWM一周期的时钟数,并调可节占空比
TIM2_OC1PreloadConfig(ENABLE);
TIM2_OC2Init(TIM2_OCMODE_PWM1, TIM2_OUTPUTSTATE_ENABLE,CCR1_Val, TIM2_OCPOLARITY_HIGH);//PWM模式可设置PWM一周期的时钟数,并调可节占空比
TIM2_OC2PreloadConfig(ENABLE);
TIM2_ARRPreloadConfig(ENABLE);
TIM2_Cmd(ENABLE);
*/
//把LED1设置为高电平输出,关灯
LED1_OFF();
while (1){ //主循环体
if(i>0x0ffe){i=0;}else i++;
TIM2_SetCompare1(i);
TIM2_SetCompare2(i);
TIM2_SetCompare3(i);
TIM1_SetCompare3(0x04ff);
TIM1_SetCompare4(0x03ff);
LED1_ON();//PC2输出低电平,使LED2点亮
Delay(200);//延时,让用户观察LED2亮起
LED1_OFF();//PC2输出高电平,LED2熄灭
Delay(200);//延时。
}//如些反复,用户会观察到LED2一直在闪烁
} |
|