#include <iom16.h>
#include<intrinsics.h>
#include "lcd1602_8bit.c"
uchar __flash title1[]={"ICP test"};
uchar __flash title2[]={"ICP1:"};
#define uchar unsigned char
#define uint unsigned int
#define xtal 8
long value;
long value1;
long value2;
long voltage;
long sp;
uchar flag=0x00;
uchar Nub=0;
uint k=0 ;
uint t=0;
long wide;
void port_init(void)
{
DDRA=0xff;
PORTA=0x00;
DDRB=0xff;
PORTB=0x00;
PORTD = 0xff;
DDRD = 0xff;
__no_operation();
DDRD = 0x80;
}
void timer2_init(void)
{
TCCR2 = 0x00; //stop
ASSR = 0x00; //set async mode
TCNT2 = 0x01; //setup
OCR2 = 0xFF;
TCCR2 = 0x61; //start
}
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0x00; //setup
TCNT1L = 0x00;//set up
OCR1AH = 0x1E;// output compare register data
OCR1AL = 0x84;//output compare register data
OCR1BH = 0x1E;
OCR1BL = 0x84;
ICR1H = 0x1E;// input cpmpare register data
ICR1L = 0x84;// input cpmpare register data
TCCR1A = 0x00;
TCCR1B = 0xc5; //start Timer this need to change.
}
/*********************************************/
void init_devices(void)
{
__disable_interrupt(); //disable all interrupts
port_init();
timer1_init();
timer2_init();
MCUCR = 0x00; //int0/int1 set up for interrupt
GICR = 0x00; //分中断
TIMSK = 0x24; //timer interrupt sources
__enable_interrupt();//re-enable interrupts
}
/******************主函数******************/
void main(void)
{
init_devices();
InitLcd();
DisplayOneChar(1,1,'o');
DisplayOneChar(2,1,'c');
DisplayOneChar(3,1,'r');
DisplayOneChar(4,1,'2');
DisplayOneChar(5,1,':');
DisplayOneChar(12,1,'.');
DisplayOneChar(15,1,'V');
while(1)
{
DisplayOneChar(6,1,(wide/100)+0x30);
DisplayOneChar(7,1,(wide/10)%10+0x30);
DisplayOneChar(8,1,(wide%10)+0x30);
OCR2=wide;
voltage=(wide*196)/100;
DisplayOneChar(11,1,(voltage/100)+0x30);
DisplayOneChar(13,1,(voltage/10)%10+0x30);
DisplayOneChar(14,1,(voltage%10)+0x30);
}}
#pragma vector=TIMER1_CAPT_vect
__interrupt void timer1_capt_isr(void)
{
//timer 1 input capture event, read (int)value in ICR1 using;
value=(uint)ICR1L; //Read low byte first (important)
value|=(uint)ICR1H << 8; //Read high byte and shift into top byte
Nub++;
if(Nub==1)
{
value1=value;//第一次捕获
}
else if(Nub==2)
{
value2=value;//第2次捕获
Nub=0;
sp=(65536-value1)+(k-1)*65536+value2;
wide=19922/sp;
k=0;
}
}
#pragma vector=TIMER1_OVF_vect
__interrupt void timer1_ovf_isr(void)
{ __enable_interrupt();
//TIMER1 has overflowed
TCNT1H = 0x00; //reload counter high value
TCNT1L = 0x00; //reload counter low value
k++;
t++; if(t==10000)
{
t=0; wide=0;
}
}
|