我使用的是C8051F120,我使用了两种方法来测1HZ的方波的偻,感觉第测一次就记了1-10个下降沿了(我是用12864屏显示的,能看到记数值)这是神马原因?我的硬件是这样设计的:在计数端口接了一个1K的上拉电阻,不知道跟硬件有没系没?
以下是我用定时器2来做计数功能的代码,求教
#include "pcf8563.h"
#include "myfun.h"
#include "FunctionLcd.h"
#include "intrins.h"
//========================//
unsigned char a[100]="matrix display ";//屏幕显示数组
unsigned char i=0;
unsigned int Counter = 0;
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This routine initializes the system clock to use the internal 16 MHz
// oscillator as its clock source. Also enables missing clock detector reset.
//
//-----------------------------------------------------------------------------
void OSCILLATOR_Init(void)
{
unsigned int i;
SFRPAGE = CONFIG_PAGE;
OSCXCN = 0x67;
for(i = 0; i<3000; i++);
while(!(OSCXCN & 0x80));
OSCICN = 0x88;
}
//-------------------------------------------------------------------------
//函数名称: PORT_Init ()
//函数功能: 端口初始化;使能交叉开关
//入口参数: 无
//出口参数: 无
//全局变量引用: 无
//调用模块: 无
//----------------------------------------------------------------------
void PORT_Init (void)
{
SFRPAGE = CONFIG_PAGE;
XBR0 = 0x04; //TX0 RX0
XBR1 = 0x20; // 定时器2配置到端口
XBR2 = 0x40; /*使能交叉开关*/
P7MDOUT =0xff; //12668数据端
P2MDOUT =0x3f;
}
void Timer2_Init(void)
{
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = TMR2_PAGE; // Set SFR page
TMR2CF &= ~0x18; // Timer2 uses SYSCLK/12
TMR2L = 0;
TMR2H = 0;
TMR2CN = 0x06; // Enable Timer2 in auto-reload mode
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}
void group (unsigned int b, unsigned char *DigitArray)
{
unsigned char i = 0;
while(b / 10)
{
DigitArray
= b % 10 + '0';
b /= 10;
i ++;
}
DigitArray = b % 10 + '0';
}
void main()
{
WDTCN = 0xde;
WDTCN = 0xad; //关看门狗
OSCILLATOR_Init();
PORT_Init();
Timer2_Init();
EA=1;
SP=0x60;
LCDBUS=0xff; // 初始化接口
RW=1;
E =0;
initLCM();
while(1) //循环
{
unsigned int temp1, temp2;
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = TMR2_PAGE; // Set SFR page
temp1 = TMR2H; //计数器的高8位
temp2 = TMR2L; //计数器的低8位
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
Counter = (temp1 << 8) + temp2; //计数值
group(Counter, a); //把计数值存储到字符串数组里面
PrintASCII(0,0,a); //把屏幕信息显示到屏幕上
delayms(1000);
}
}
[ 本帖最后由 jiangjieqazwsx 于 2011-12-28 20:33 编辑 ]
此帖出自小平头技术问答
一周热门 更多>