|
![](static/image/common/ico_lz.png)
楼主 |
发表于 2012-6-22 00:19:10
|
显示全部楼层
网上搜了下这个问题,找到了c8051f060的晶振设置的程序例子,自己改了下,终于能用了。
/////////////////////////////////////
// Generated Initialization File //
/////////////////////////////////////
#include "C8051F020.h"
// Peripheral specific initialization functions,
// Called from the Init_Device() function
sbit Power5 = P1^4;
static int t=0;
void Reset_Sources_Init()
{
WDTCN = 0xDE;
WDTCN = 0xAD;
}
void Port_IO_Init()
{
P1MDOUT = 0x10;
XBR2 = 0x40;
}
void Oscillator_Init()
{
int i;
//设置内部时钟
OSCICN = 0x05; // Configure internal oscillator for 4MHZ
RSTSRC = 0x04; //启用时钟丢失检测
//设置外部时钟源
OSCXCN = 0x67; //晶振频率大于6.7MHZ
for (i = 0; i < 3000; i++); // Wait 1ms for initialization
while ((OSCXCN & 0x80) == 0); //
//切换时钟源到外部时钟
OSCICN = 0x0C;
}
void Timer_Init()
{
TCON = 0x10;
TMOD = 0x01;
//定时寄存器初值设定
TL0 = 0xE0;
TH0 = 0xFF;
}
void Interrupts_Init()
{
IE = 0x02; //启用0号定时器中断
}
timer0() interrupt 1
{
TF0=0; //0#定时器溢出寄存器复位
t++;
if (t==30) //接近1秒钟一次
{
Power5=1;
}
if (t>=60)
{
Power5=0;
t=0;
}
}
// Initialization function for device,
// Call Init_Device() from your main program
void Init_Device(void)
{
Oscillator_Init();
Reset_Sources_Init();
Timer_Init();
Port_IO_Init();
Interrupts_Init();
}
void delayms(int de)
{
int i,j;
for (i=0; i<=30; i++)
{
for (j=0; j<=de; j++)
{ }
}
}
void main()
{
Init_Device();
ET0=1; //启用0#定时器中断功能
EA=1; //开全局中断
TR0=1; //启动0#定时器计数
while(1)
{
}
}
|
|