智能车制作
标题:
用“输出比较+脉冲累加器”测风扇的转速,将转速在数码管上显示;忘指出错误,谢谢!
[打印本页]
作者:
伤痕我心
时间:
2013-8-31 18:44
标题:
用“输出比较+脉冲累加器”测风扇的转速,将转速在数码管上显示;忘指出错误,谢谢!
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
//***********************
//输入信号的频率测量***** 利用脉冲累加器在单位时间内对脉冲计数,用输出比较来定时
//***********************
unsigned int m = 0,a=0,n=0;
const byte LedData[18] = {0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xf6,0xee,0x3e,0x9c,0x7a,0x9e,0x8e,0xce,0x6e} ;
/* 0 1 2 3 4 5 6 7 8 9 A B C D E F P H */
const byte Error[5] = {0xee,0xfc,0xee,0xee,0x9e};
void spiDELAY(void)
{
unsigned int loop_i;
for (loop_i=0; loop_i< 50; loop_i++)
{
;
}
}
void spiWRITEbyte(unsigned int spiDATA){
unsigned int i =0;
for(i;i<8;i++){
PTP_PTP2 =0;
spiDELAY();
if(spiDATA%2){
PTP_PTP1=1;
}
else {
PTP_PTP1=0;
}
spiDELAY();
spiDATA = spiDATA/2;
PTP_PTP2 = 1;
spiDELAY();
}
PTP_PTP2 = 1;
}
void ledCONTROL(dword displayDATA){
unsigned int spiBYTE01,spiBYTE02,spiBYTE03,spiBYTE04,spiBYTE05;
spiBYTE05 = displayDATA /10000 ; //得到万位
spiBYTE04 = displayDATA /1000 - displayDATA /10000 *10 ; //获得千位
spiBYTE03 = displayDATA /100 - displayDATA /1000 *10 ; //获得百位
spiBYTE02 = displayDATA /10 - displayDATA /100 *10 ; //获得个位
spiBYTE01 = displayDATA % 10 ; //获得个位
// spiWRITEbyte(spiDATA);
spiWRITEbyte(LedData[spiBYTE01]);
spiWRITEbyte(LedData[spiBYTE02]);
spiWRITEbyte(LedData[spiBYTE03]);
spiWRITEbyte(LedData[spiBYTE04]);
spiWRITEbyte(LedData[spiBYTE05]);
}
void PLL_Init(void)
{
REFDV=0;
SYNR=4;
while(!(CRGFLG && 0x08));
CLKSEL=0x80; //****20MHZ
}
void ECT0_Init(void)
{ //***设置输出比较
TIOS=0x01;
TC0=0xFE;
TCTL1=0x00;
// OC7M = 0xFE;
TCTL2=0x02;
TSCR2=0x06;
TSCR1=0x80;
TIE=0x01;
//***脉冲累加器的设置
PACTL = 0x50;
}
/* static void IOC_Init(void)
{
PACTL=0x50;//PT7 PIN,PACN32 16BIT,FALLing edge,NOT INTERRUPT
// PACNT = 0x00;
TCTL3=0xc0;//c-输入捕捉7任何沿有效,
TCTL4=0xc0;//40表示ICx禁止, 1表示上升沿, 2表示下降沿, 3表示任何沿
TIE =0x00;//每一位对应相应通道中断允许,0表示禁止中断
TIOS =0x00;//每一位对应通道的: 0输入捕捉,1输出比较
TCTL3_EDG7x=1;//c-输入捕捉7任何沿有效,
}
*/
void PWM_Init(void)
{
PWMPOL = 0X20;
PWMCAE = 0X20;
PWMCLK = 0X20;
PWMPRCLK = 0X00;
PWMSCLA = 0X80;
PWMCTL = 0X00;
PWMPER5 = 128;
PWMDTY5 = 30;
PWME = 0X20;
}
void main(void) {
/* put your own code here */
unsigned char i,spiDATA,spiLENGTH;
DDRP_DDRP1 =1;
DDRP_DDRP2 =1;
DDRK_BIT1 =1;
PORTK_BIT1 =1;
PTP_PTP1 =0;
PTP_PTP2 =0;
PTT_PTT0 =1;
spiDATA = 0XFC;
spiLENGTH = 8;
for(i=0;i<6;i++)
{
spiWRITEbyte(spiDATA);
}
PLL_Init();
// IOC_Init();
ECT0_Init();
PWM_Init();
EnableInterrupts;
for(;;) {
_FEED_COP(); /* feeds the dog */
} /* loop forever */
/* please make sure that you never leave main */
}
#pragma CODE_SEG NON_BANKED
void interrupt 8 timer0_ISR(void)
{
TFLG1_C0F=1;
m=TCNT;
TC0=m+15625 ; //定时50ms
n++;
if(n==20) //1s
{
a=PACN32;
PAFLG_PAOVF = 1 ;
a = a/334;
ledCONTROL(1000);
// PACN32 = 0x00;
n=0;
}
}
#pragma CODE_SEG DEFAULF
作者:
伤痕我心
时间:
2013-8-31 18:46
其中的PTT_PTT0=1;是PT0这个IO口,用于测速的引脚
作者:
凌心
时间:
2013-8-31 18:49
你直接丢出来一堆代码没几个人会去看的,再说了,即使看了,没有环境也无法调试。
欢迎光临 智能车制作 (http://111.231.132.190/)
Powered by Discuz! X3.2