下面的代码用来脉冲计数并输出,串口调试助手总是没有输出,不知道是不是代码的问题,哪位大神可以帮忙看一下是否有问题~~
//ICC-AVR application builder : 2007-9-20 10:30:53
// Target : m64
// Crystal: 8.0000Mhz
#include <iom64v.h>
#include <macros.h>
#include "head.h"
#define uchar unsigned char
#define uint unsigned int
#define xtal 8
/*********************变量定义*****************************/
/*uchar Start=0,WorkFlag;
uint WorkTime,DisTime,Counter;
uint DisVal;
uint cnt;
uchar Flag,IntFlag,JS=0;
uint tx=10,cx;
uint disx[100];
uchar DisFlag=1,EndFlag,BeginFlag=0;*/
uchar WorkFlag;
uchar Start=0;
uchar EndFlag=0;
uint cnt,WorkTime=0;
uint cx,tx=10;
uint disx[100];
uint address=100;
uint j=0;
/*********************定时器0初始化*****************************/
//TIMER0 initialize - prescale:64
// WGM: Normal
// desired value: 1mSec
// actual value: 1.000mSec (0.0%)
void timer0_init(void)//T/C0,见用户手册P99
{
TCCR0 = 0x00; //stop
ASSR = 0x00; //set async mode
TCNT0 = 0x83; //set count
OCR0 = 0x7D;// set compare
TCCR0 = 0x04; //start timer
}
/*********************uart0初始化****************************/
void uart0_init()//
{
UCSR0B = 0x00; //禁止UART发送和接收
UCSR0A = 0x00; //倍率U2X=0
UCSR0C = 0x86; //8位数据位,1位停止位
UBRR0L = 0x2F; //47,波特率9600
UBRR0H = 0x00;
UCSR0B = 0x98; //允许UART发送和接收;接收缓冲自动清空,接收允许
}
/**********************初始化端口*****************************/
void port_init(void) //I/O设置,见P62
{
//PORTD = 0xFF;
//DDRD = 0xFE;//外部中断0
PORTE = 0xFF;
DDRE = 0xFF;
DDRE = 0x7E;//UART0,外部中断7
}
/*********************初始化器件************************/
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer0_init();
MCUCSR = 0x03;//控制和状态寄存器初始化
EICRA = 0x00;
EICRB = 0x40;//INT7下降沿和上升沿中断
EIMSK = 0x80;//外部中断7时使能
TIMSK = 0x01; //timer0溢出中断使能
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//****************************************
void Delay_1ms(void) //1mS延时子函数
{ uint i;
for(i=1;i<(uint)(xtal*124);i++)
;
}
//=============================================
void Delay_nms(uint n) //n*1mS延时子函数
{
uint i=0;
while(i<n)
{Delay_1ms();WDR();
i++;
}
}
//***************写EEPROM子函数*****************
void eeprom_write(uint add,uchar dat)
{
while(EECR & (1<<EEWE)); //等待上一次写操作结束
EEAR = add; //设定地址寄存器和数据寄存器
EEDR = dat;
EECR |= (1<<EEMWE); //置位EEMWE
EECR |= (1<<EEWE); //置位EEWE以启动写操作
}
//****************读EEPROM子函数*******************
uchar eeprom_read(uint add)
{
while(EECR & (1<<EEWE));
EEAR = add;
EECR |= (1<<EERE); //设置EERE以启动读操作
return EEDR; //自数据寄存器返回数据
}
void str_send(char *s) //发送字符串子函数
{
while(*s)
{
uart_send(*s);
s++;
}
}
/**********************主函数*****************************/
void main(void)
{ uchar temp,temp1,temp2;
float count,time,x;
//Delay_nms(400);
int runr=1;
init_devices();
WorkFlag=1;
while(1)
{
/********************************************/
while(j<101) //100次循环
{ //WDR();
if(WorkFlag==1)//如果工作标志为1
{
count=(float) cnt;
disx[j]=cnt/2;//保存数据
WorkFlag=0;WorkTime=0;EndFlag=0;
Start=0;cnt=0;
j++;
WorkFlag=0;//清工作标志
}
else //否则进行取样
{
do{temp=PINE &0x80 ;
//WDR();
/*JS=1;//等待下降沿后下一次测试
Counter=0;JS=0;DisFlag=1;cx=0;*/
}
while(temp==0x80);
Start=1;EIMSK=0x80;//重开INT7
}
}
for(j=0;j<101;j++)
{
eeprom_write((address+j),disx[j]);
}
uart0_init();
str_send("story data is finished");
do{
temp1=uart_receive();
if (temp1=='1')
{
for(j=0;j<101;j++)
{
temp2=eeprom_read((address+j));
uart_send(temp2);
}
runr=0;
}
else
{
str_send("command error,please send the number 1");
}}
while (runr=1);
}
}
#pragma interrupt_handler timer0_ovf_isr:17
void timer0_ovf_isr(void)
{
TCNT0 = 0x83; // 重新加载定时器值
SEI();
/*if(BeginFlag==1)
{
BeginFlag=0;
// if(DisTime>1300){DisFlag=1;DisTime=0;BeginFlag=0;} //开显示
}*/
if(Start==1){WorkTime++;}
if(WorkTime==tx)
{EndFlag=1;WorkFlag=1;}
//if(JS==1)Counter++;
}
#pragma interrupt_handler int7_isr:9
void int7_isr(void)
{
uchar y;
SEI();
//external interupt on INT0
y=PINE&0x80;
//S=0;Counter=0;
if((Start==1)&&(y==0x00))cnt++;//输入脉冲下降沿判断
//**********************
if((Start==1)&&(y==0x80))//输入脉冲上升沿判断
{
cnt++;
if(EndFlag==1){ cx=cnt;EndFlag=0;
Start=0; EIMSK=0x00;}//关闭INT7中断
}
}
|