智能车制作
标题:
XS128程序求教脉冲输入计数程序
[打印本页]
作者:
天神也辉煌
时间:
2013-1-25 00:45
标题:
XS128程序求教脉冲输入计数程序
本帖最后由 天神也辉煌 于 2013-1-25 00:47 编辑
程序的用意是从T7输入脉冲,数码管显示脉冲数,但是进行程序时总是自动跳出,求指导(数码管的部分还没写好,估计是中断的哪儿出问题了)
/*---------------------------------------------------------*/
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
#define CONT1 PORTK_PK3
#define CONT2 PORTK_PK2
#define CONT3 PORTK_PK1
#define CONT4 PORTK_PK0
#define CONT1_dir DDRK_DDRK3
#define CONT2_dir DDRK_DDRK2
#define CONT3_dir DDRK_DDRK1
#define CONT4_dir DDRK_DDRK0
#define DATA PTP
#define DATA_dir DDRP
#define LEDCPU PORTK_PK4
#define LEDCPU_dir DDRK_DDRK4
#define BUS_CLOCK 32000000 //总线频率
#define OSC_CLOCK 16000000 //晶振频率
unsigned int data1,data2;
unsigned int pulsenum;
unsigned int i;
byte da1 = 0;
byte da2 = 0;
byte da3 = 0;
byte da4 = 0;
byte single = 1;
byte shuma[20]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f, //0~9对应的段码
0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef}; //0~9后加小数点对应的段码
/*************************************************************/
/* 初始化锁相环 */
/*************************************************************/
void INIT_PLL(void)
{
CLKSEL &= 0x7f; //set OSCCLK as sysclk
PLLCTL &= 0x8F; //Disable PLL circuit
CRGINT &= 0xDF;
#if(BUS_CLOCK == 40000000)
SYNR = 0x44;
#elif(BUS_CLOCK == 32000000)
SYNR = 0x43;
#elif(BUS_CLOCK == 24000000)
SYNR = 0x42;
#endif
REFDV = 0x81; //PLLCLK=2×OSCCLK×(SYNR+1)/(REFDV+1)=64MHz ,fbus=32M
PLLCTL =PLLCTL|0x70; //Enable PLL circuit
asm NOP;
asm NOP;
while(!(CRGFLG&0x08)); //PLLCLK is Locked already
CLKSEL |= 0x80; //set PLLCLK as sysclk
}
/************************************************************/
/* 初始化ECT模块 */
/************************************************************/
void initialize_ect(void){
TSCR1_TFFCA = 1; // 定时器标志位快速清除
TSCR1_TEN = 1; // 定时器使能位. 1=允许定时器正常工作; 0=使主定时器不起作用(包括计数器)
TIOS = 0x0f; //IOC7必须设置为输入,否则无法捕捉
TCTL1 = 0x00; // 后四个通道设置为定时器与输出引脚断开
TCTL2 = 0x00; // 前四个通道设置为定时器与输出引脚断开
TIE = 0x02; // 禁止除1通道定时中断
TSCR2 = 0x07; // 预分频系数pr2-pr0:111,,时钟周期为4us,
TFLG1 = 0xff; // 清除各IC/OC中断标志位
TFLG2 = 0xff; // 清除自由定时器中断标志位
}
/*************************************************************/
/* 初始化端口 */
/*************************************************************/
void INIT_port(void)
{
CONT1_dir = 1;
CONT2_dir = 1;
CONT3_dir = 1;
CONT4_dir = 1;
CONT1 = 0;
CONT2 = 0;
CONT3 = 0;
CONT4 = 0;
DATA_dir = 0xff;
DATA = 0x00;
}
/*************************************************************/
/* 初始化累加器 */
/*************************************************************/
void INIT_PAI(void)
{
PACTL = 0x50; //始能脉冲累加器,累加上升沿
}
/*************************************************************/
/* 延时函数 */
/*************************************************************/
void delay(void)
{
unsigned int i,j;
for(j=0;j<200;j++)
for(i=0;i<60000;i++);
}
/*************************************************************/
/* 中断扫描函数 */
/*************************************************************/
#pragma CODE_SEG __NEAR_SEG NON_BANKED
interrupt void scan(void)
{
if(TFLG1_C0F == 1)
{
TFLG1_C0F = 1;
TC0 = TCNT + 1250; //设置输出比较时间为5ms
}
switch(single)
{
case 1:
CONT1 = 1;
CONT2 = 0;
CONT3 = 0;
CONT4 = 0;
DATA=shuma[da1];
break;
case 2:
CONT1 = 0;
CONT2 = 1;
CONT3 = 0;
CONT4 = 0;
DATA=shuma[da2];
break;
case 3:
CONT1 = 0;
CONT2 = 0;
CONT3 = 1;
CONT4 = 0;
DATA=shuma[da3];
break;
case 4:
CONT1 = 0;
CONT2 = 0;
CONT3 = 0;
CONT4 = 1;
DATA=shuma[da4];
break;
default:
break;
}
single +=1;
if(single == 5) single = 1;
}
#pragma CODE_SEG DEFAULT
/*************************************************************/
/* 主函数 */
/*************************************************************/
void main(void) {
DisableInterrupts;
INIT_PLL();
INIT_PAI();
initialize_ect();
INIT_port();
LEDCPU_dir = 1;
LEDCPU=0;
EnableInterrupts;
LEDCPU = 0;
TFLG1_C0F = 1;
//TC0 = TCNT+62500; //时间间隔为0.25s
TC0 = TCNT+12500;
for(;;)
{
/*for(i=0;i<4;i++) //延时1秒
{
while(TFLG1_C0F == 0);
TFLG1_C0F = 1;
TC0 = TCNT+62500;
} */
data1 = data2;
data2 = PACNT;
pulsenum = data2-data1; //计算1秒钟采集的上升沿的数量
da1 = data2/1000;
da2 = (data2%1000)/100;
da3 = (data2%100)/10;
da3=4;
da4 = data2%10;
// da1 = data2+10; //加小数点
}
}
欢迎光临 智能车制作 (http://dns.znczz.com/)
Powered by Discuz! X3.2