[mw_shl_code=c,true]void System_Clk_DivSet(U8 div)
{
CLK_CKDIVR=0; //清除原来的设置
CLK_CKDIVR=div&0X07; //设置DIV
}
void stm8_clock_init(U8 clksrc,U8 div)
{
System_Clk_DivSet(div); //设置分频
while(System_Clk_AutoSwitch(clksrc)); //切换时钟
// CLK_CSSEnable();//开启时钟安全系统
}
/自动切换新时钟源(仅HSI/HSE)
//newsrc:0,HSI
// 1,HSE
// 2,LSI
// 3,LSE
//返回值:0,修改成功;1,修改失败.
U8 System_Clk_AutoSwitch(U8 newsrc)
{
U16 timeout=0xffff;
U8 oldsrc=CLK_SCSR; //老的时钟源
CLK_SWCR|=1<<1; //使能时钟切换功能
CLK_SWCR&=~(1<<2); //关闭切换中断
if(newsrc==0)
{
CLK_SWR=0X01; //HSI
}
else if(newsrc==1)CLK_SWR=0X04; //HSE
else if(newsrc==2)
{
CLK_ICKCR |= 1<<2;//使能LSI
while(!(CLK_ICKCR&1<<3));
CLK_SWR=0X02; //LSI
}
else if(newsrc==3)CLK_SWR=0X08; //LSE
while((CLK_SWCR&(1<<0))&&timeout)timeout--; //等待切换完成
if(oldsrc==0X01&&newsrc!=0)CLK_ICKCR&=~(1<<0); //关闭HSI
else if(oldsrc==0X04&&newsrc!=1)CLK_ECKCR&=~(1<<0); //关闭HSE
else if(oldsrc==0X02&&newsrc!=2)CLK_ICKCR&=~(1<<2); //关闭LSI
else if(oldsrc==0X08&&newsrc!=3)CLK_ECKCR&=~(1<<2); //关闭LSE
if(timeout)return 0;
else return 1;
}[/mw_shl_code]
[mw_shl_code=c,true]main()
{
stm8_clock_init(2,3); //切换时钟到内部LSI 8分频
while()
{
}
}[/mw_shl_code]
我想把默认的系统时钟从HSI切换到LSI,为了降低功耗,可是在切换的时候到
[mw_shl_code=c,true]CLK_SWR=0X02; //LSI[/mw_shl_code]
就出错了,
我用的单片机是STM8L152C6,这是什么原因呢?求帮助哦!
|