请问各种大神关于C8051F040定时器相关问题
时间:10-02
整理:3721RD
点击:
/////////////////////////////////////
// Generated Initialization File //
/////////////////////////////////////
#include "C8051F040.h"
sbit LED2=P4^0;
unsigned char count,sfrt;
// Peripheral specific initialization functions,
// Called from the Init_Device() function
void Port_IO_Init()
{
// P0.0 - Unassigned, Open-Drain, Digital
// P0.1 - Unassigned, Open-Drain, Digital
// P0.2 - Unassigned, Open-Drain, Digital
// P0.3 - Unassigned, Open-Drain, Digital
// P0.4 - Unassigned, Open-Drain, Digital
// P0.5 - Unassigned, Open-Drain, Digital
// P0.6 - Unassigned, Open-Drain, Digital
// P0.7 - Unassigned, Open-Drain, Digital
// P1.0 - Unassigned, Open-Drain, Digital
// P1.1 - Unassigned, Open-Drain, Digital
// P1.2 - Unassigned, Open-Drain, Digital
// P1.3 - Unassigned, Open-Drain, Digital
// P1.4 - Unassigned, Open-Drain, Digital
// P1.5 - Unassigned, Open-Drain, Digital
// P1.6 - Unassigned, Open-Drain, Digital
// P1.7 - Unassigned, Open-Drain, Digital
// P2.0 - Unassigned, Open-Drain, Digital
// P2.1 - Unassigned, Open-Drain, Digital
// P2.2 - Unassigned, Open-Drain, Digital
// P2.3 - Unassigned, Open-Drain, Digital
// P2.4 - Unassigned, Open-Drain, Digital
// P2.5 - Unassigned, Open-Drain, Digital
// P2.6 - Unassigned, Open-Drain, Digital
// P2.7 - Unassigned, Open-Drain, Digital
// P3.0 - Unassigned, Open-Drain, Digital
// P3.1 - Unassigned, Open-Drain, Digital
// P3.2 - Unassigned, Open-Drain, Digital
// P3.3 - Unassigned, Open-Drain, Digital
// P3.4 - Unassigned, Open-Drain, Digital
// P3.5 - Unassigned, Open-Drain, Digital
// P3.6 - Unassigned, Open-Drain, Digital
// P3.7 - Unassigned, Open-Drain, Digital
SFRPAGE = CONFIG_PAGE;
P4MDOUT = 0x01;
P6MDOUT = 0x18;
XBR2 = 0x40;
}
void Timer_Init()
{
sfrt = SFRPAGE;
SFRPAGE = TIMER01_PAGE;
CKCON = 0X01;
TMOD = 0x10; //T1·?ê?1
TH1 = 0x3c;
TL1 = 0xb0; //3??μ ?¨ê±50ms ????1?ê? 2μ?16′?·?-50oá??/1us = 65536-50000000/1000 = 65536-50000=15536=0x3CB0;
ET1 = 1; //?êDíTIMER1?¨ê±?÷?D??
EA = 1; //×ü?D???a?a??
TR1 = 1; //???ˉ?¨ê±?÷
SFRPAGE = sfrt;
}
void Oscillator_Init()
{
int i = 0;
SFRPAGE = CONFIG_PAGE;
OSCXCN = 0x67;
for (i = 0; i < 3000; i++); // Wait 1ms for initialization
while ((OSCXCN & 0x80) == 0);
CLKSEL = 0x01;
OSCICN = 0x00;
}
void delay()
{
unsigned int tn = 8000;
while ( --tn != 0 );
}
// Initialization function for device,
// Call Init_Device() from your main program
void Init_Device(void)
{
Port_IO_Init();
}
void main()
{
Init_Device();
delay();
Timer_Init();
Oscillator_Init();
while(1);
/* {
P4 = 0x00;
P6 = 0x00;
delay();delay();delay();delay();delay();
LED2 = 1;
P6 = 0xff;
P4 = 0xff;
delay();delay();delay();delay();delay();
};*/
}
void Timer1_ISR() interrupt 3
{
SFRPAGE = TIMER01_PAGE;
ET1 = 0; //?êDíTIMER1?¨ê±?÷?D??
TF1 = 0;
TH1 = 0x3c;
TL1 = 0xb0; //3??μ ?¨ê±50ms ????1?ê? 2μ?16′?·?-50oá??/1us = 65536-50000000/1000 = 65536-50000=15536=0x3CB0;
count++;
if(count >= 20)
{
count = 0;
P6 = ~P6;
}
ET1 = 1;
}
上述是我C8051F040单片机定时器50MS程序 现在问题的定时器并没有生产50MS的定时,简单来说就是定时器并没有工作, 求个位大神给予帮助 希望能指出相关问题 并给出作答
// Generated Initialization File //
/////////////////////////////////////
#include "C8051F040.h"
sbit LED2=P4^0;
unsigned char count,sfrt;
// Peripheral specific initialization functions,
// Called from the Init_Device() function
void Port_IO_Init()
{
// P0.0 - Unassigned, Open-Drain, Digital
// P0.1 - Unassigned, Open-Drain, Digital
// P0.2 - Unassigned, Open-Drain, Digital
// P0.3 - Unassigned, Open-Drain, Digital
// P0.4 - Unassigned, Open-Drain, Digital
// P0.5 - Unassigned, Open-Drain, Digital
// P0.6 - Unassigned, Open-Drain, Digital
// P0.7 - Unassigned, Open-Drain, Digital
// P1.0 - Unassigned, Open-Drain, Digital
// P1.1 - Unassigned, Open-Drain, Digital
// P1.2 - Unassigned, Open-Drain, Digital
// P1.3 - Unassigned, Open-Drain, Digital
// P1.4 - Unassigned, Open-Drain, Digital
// P1.5 - Unassigned, Open-Drain, Digital
// P1.6 - Unassigned, Open-Drain, Digital
// P1.7 - Unassigned, Open-Drain, Digital
// P2.0 - Unassigned, Open-Drain, Digital
// P2.1 - Unassigned, Open-Drain, Digital
// P2.2 - Unassigned, Open-Drain, Digital
// P2.3 - Unassigned, Open-Drain, Digital
// P2.4 - Unassigned, Open-Drain, Digital
// P2.5 - Unassigned, Open-Drain, Digital
// P2.6 - Unassigned, Open-Drain, Digital
// P2.7 - Unassigned, Open-Drain, Digital
// P3.0 - Unassigned, Open-Drain, Digital
// P3.1 - Unassigned, Open-Drain, Digital
// P3.2 - Unassigned, Open-Drain, Digital
// P3.3 - Unassigned, Open-Drain, Digital
// P3.4 - Unassigned, Open-Drain, Digital
// P3.5 - Unassigned, Open-Drain, Digital
// P3.6 - Unassigned, Open-Drain, Digital
// P3.7 - Unassigned, Open-Drain, Digital
SFRPAGE = CONFIG_PAGE;
P4MDOUT = 0x01;
P6MDOUT = 0x18;
XBR2 = 0x40;
}
void Timer_Init()
{
sfrt = SFRPAGE;
SFRPAGE = TIMER01_PAGE;
CKCON = 0X01;
TMOD = 0x10; //T1·?ê?1
TH1 = 0x3c;
TL1 = 0xb0; //3??μ ?¨ê±50ms ????1?ê? 2μ?16′?·?-50oá??/1us = 65536-50000000/1000 = 65536-50000=15536=0x3CB0;
ET1 = 1; //?êDíTIMER1?¨ê±?÷?D??
EA = 1; //×ü?D???a?a??
TR1 = 1; //???ˉ?¨ê±?÷
SFRPAGE = sfrt;
}
void Oscillator_Init()
{
int i = 0;
SFRPAGE = CONFIG_PAGE;
OSCXCN = 0x67;
for (i = 0; i < 3000; i++); // Wait 1ms for initialization
while ((OSCXCN & 0x80) == 0);
CLKSEL = 0x01;
OSCICN = 0x00;
}
void delay()
{
unsigned int tn = 8000;
while ( --tn != 0 );
}
// Initialization function for device,
// Call Init_Device() from your main program
void Init_Device(void)
{
Port_IO_Init();
}
void main()
{
Init_Device();
delay();
Timer_Init();
Oscillator_Init();
while(1);
/* {
P4 = 0x00;
P6 = 0x00;
delay();delay();delay();delay();delay();
LED2 = 1;
P6 = 0xff;
P4 = 0xff;
delay();delay();delay();delay();delay();
};*/
}
void Timer1_ISR() interrupt 3
{
SFRPAGE = TIMER01_PAGE;
ET1 = 0; //?êDíTIMER1?¨ê±?÷?D??
TF1 = 0;
TH1 = 0x3c;
TL1 = 0xb0; //3??μ ?¨ê±50ms ????1?ê? 2μ?16′?·?-50oá??/1us = 65536-50000000/1000 = 65536-50000=15536=0x3CB0;
count++;
if(count >= 20)
{
count = 0;
P6 = ~P6;
}
ET1 = 1;
}
上述是我C8051F040单片机定时器50MS程序 现在问题的定时器并没有生产50MS的定时,简单来说就是定时器并没有工作, 求个位大神给予帮助 希望能指出相关问题 并给出作答
问题可能出在两个地方,1.定时初始化 2,定时中断 你可以把断点放在中断里,看看进没进来
void Timer1_ISR() interrupt 3
{
SFRPAGE = TIMER01_PAGE;
ET1 = 0; //?êDíTIMER1?¨ê±?÷?D??
TF1 = 0;
TH1 = 0x3c;
TL1 = 0xb0; //3??μ ?¨ê±50ms ????1?ê? 2μ?16′?·?-50oá??/1us = 65536-50000000/1000 = 65536-50000=15536=0x3CB0;
count++;
if(count >= 20)
{
count = 0;
P6 = ~P6;
}
ET1 = 1;
}
并没有进入定时器中断 能否找出这段程序的问题? 定时器启动我已经看了很多遍 不知道是哪出的问题
没进入中断的话 就是初始化问题了 你看看是哪里配置错了