资源简介
利用STM8S005 PWM做LED呼吸灯的IAR 源码
代码片段和文件信息
#include “iostm8s005k6.h“
/************************常用数据类型定义*************************/
#define I8 int8_t
#define u8 uint8_t
#define u16 uint16_t
#define u32 uint32_t
typedef signed char int8_t;
typedef signed char int16_t;
typedef signed char int32_t;
typedef unsigned char uint8_t;
typedef unsigned int uint16_t;
typedef unsigned long uint32_t;
u8 PWM = 0;
u8 Set_LightSet = 0; //set LED default light value
u8 Set_LightSet_ChangeOnce = 0; //set UP/DOWN step
u8 Set_LightSet_HoldTime_Min = 0x20; //
u8 Set_LightSet_HoldTime_Max = 0x20;
u16 Light_CurLevel_Percentage_u16;
u8 Light_Out_StateLight_CurLevel_Percentage
LIGHT_STATE_MIN_OUTLight_Min_Max_HoldTimeLIGHT_STATE_MAX_OUT;
u8 gEnableChangeLED;
u8 LIGHT_STATE_UP_OUT = 1;
u8 LIGHT_STATE_DOWN_OUT = 0;
u8 Light_Min_Max_HoldTime = 0x0;
#define MAX 50
#define MIN 1
void Delay_Ms (unsigned int ms);
void Delay_Us(unsigned char t);
void Tim1_PWM_Output( unsigned char x);
void Tim1_init(void);
void Tim4_init(void);
void Pwm_BreatheCtrl(void);
void SetCurLightShow(u8 Value_LED u8 Value_ChangeOnce u8 HoldTime_Min u8 HoldTime_Max);
int main( void )
{
unsigned char FW = 1;
CLK_ICKR = 0x01; //HSIEN
CLK_CKDIVR = 0x18; //fHSI= fHSI RC output fCPU=fMASTER
CLK_PCKENR1 |= 0x80; //enable timer1 clock
Tim1_init();
Tim4_init();
SetCurLightShow(0x7F 0x01 0x10 0x50);
// asm(“rim“); //开全局中断
asm(“sim“); //关全局中断
while (1)
{
if (FW)
{
PWM++;
if (PWM>MAX)
{
FW = 0;
}
}
else
{
PWM--;
if (PWM < MIN)
{
FW = 1;
}
}
Delay_Ms(200);
Tim1_PWM_Output(PWM);
}
}
void Delay_Ms (unsigned int ms)
{
unsigned int xy;
for (x=ms;x>0;x--)
{
for (y=100;y>0;y--)
{
}
}
}
void Delay_Us(unsigned char t)
{
unsigned char m = t;
while (m--);
}
void Tim1_PWM_Output( unsigned char x)
{
TIM1_CR1 = 0x00; //disable timer
TIM1_PSCRH = 0x00; //Prescaler High fCK_CNT = fCK_PSC / (PSCR[15:0]+1).
TIM1_PSCRL = 0x00; //Prescaler Low
TIM1_ARRH = MAX/256; //Auto-reload register
TIM1_ARRL = MAX%256; //Auto-reload register
TIM1_CCMR1 = 0x68; //PWM MODE 1ENABLE CCR PRELOAD
//OC1M=110; OC1PE=1 Output compare 1 preload enable
TIM1_CCR1H = x/256; //Duty cycle
TIM1_CCR1L = x%256;
TIM1_CCER1 = 0x03; //Capture/compare enable register ;CC1P=1 OC1 active low; CC1E=1 OC1 signal is output
TIM1_BKR = 0x80; //Break registerMOE=1Main output enable
TIM1_EGR = 0x01; //Event generation registerUG=1Update generationRe-initializes the counter and generates an update of the registers.
TIM1_CR1 |= 0x01; //Enable timer
}
void Tim1_init(void)
{
TIM1_CR1 = 0x80; //disable timer enable auto reload
TIM1_PSCRH = 0x00; //Prescaler High fCK_CNT = fCK_PSC / (PSCR[15:0]+1).
TIM1_PS
- 上一篇:DS18B20温度控制风扇 原理图及源代码
- 下一篇:武汉某三个地区的矢量化数据
评论
共有 条评论