我用的是atmega16的芯片,用来测量速度的,传感器用的是霍尔开关,我但我把霍尔开关的输出接到ICP1上没反应,这是为什么,单片机不能读到值 下面是程序 哪位大神帮我看哈 我用protues仿真 没问题 但自己焊了一块板子 就没用了
#include<avr/io.h>
#include <avr/interrupt.h>
//#define BIT(x) (1 << (x))
//#define CLI() asm("cli")
//#define SEI() asm("sei")
int point=3;
char led_7[13]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40,0x39,0x00};
char position[4]={0xfe,0xfd,0xbf,0x7f};
char dis_buf[4]={0};
int v_ok=0;
unsigned int f_time=0,l_time=0;
float time=0;
double v;
#define sec 3906.25
#define R 0.5
void port_init()
{
DDRA=0xff;
PORTA=0x00;
DDRB=0x30;
PORTB=0x80;
DDRC=0xff;
PORTC=0x00;
PORTD=0x04;
DDRD=0x03;
}
void time1_init()
{
TCCR1B=0x00;
TCCR1A=0x00;
TCCR1B=0xC5;
}
void time0_init()
{
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x7D;
TCCR0=0x0B;
}
void init_devices(void)
{
//CLI();
SREG=SREG&0x7F;
port_init();
time1_init();
time0_init();
MCUCR=0x00;
//MCUCSR=0x40;
GICR=0x00;
TIMSK=0x22;
SREG=SREG|0x80;
//SEI();
}
//#pragma interrupt_handler timer0_ovf_isr:20//刷新速度
ISR(TIMER0_COMP_vect)
{
//TCNT0=OCR0;
static char i=0, j=0;
if((j++)==0)
{v_ok=1;}
PORTC=~0xff;
if(i>=4)
{i=0;}
PORTA=~(led_7[dis_buf[i]]);
PORTC=~position[i];
if(i==point)
{
PORTA&=~(0x80);
}
i++;
}
//#pragma interrupt_handler timer1_capt_isr:6//上升沿触发
ISR(TIMER1_CAPT_vect)
{
//value=ICR1L;//先读低字节
//value|=ICR1H<<8;//读高字节并移位到高字节
f_time=l_time;
l_time=ICR1;
}
void to_buf()
{
int i=0;
double tem_v=v;
if(v>=1000)
{
point=3;
}
if(v>=100&&v<1000)
{
point=2;
tem_v*=10;
}
if(v>=10&&v<100)
{
point=1;
tem_v*=100;
}
if(v<10)
{
point=0;
tem_v*=1000;
}
for(i=0;i<4;i++)
{
dis_buf[3-i]=(int)tem_v%10;
tem_v/=10;
}
}
void main(void)
{
init_devices();
while(1)
{
if((PINB&0x04))
{
v_ok=0;
PORTA=0x00;
PORTC=0x00;
PORTB&=0xfc;
SREG=SREG&0x7f;
}
else
{
SREG=SREG|0x80;
}
if(v_ok)
{
v_ok=0;
time=((l_time)-(f_time))/sec;
if(time==0)v=0;
else v=1.57*(1/time)*R;
if(v>10&&v<15)
PORTD|=0x0;
else if(v>15)
PORTD|=0x01;
if(!(PIND&0x04))
{v*=3.6;}
to_buf();
}
}
}
|