|
发表于 2011-4-8 18:01:50
|
显示全部楼层
找到了源程序,只是没有注释,贴上来给楼主参考下
/*****************************************************
This program was produced by the
CodeWizardAVR V1.25.9 Professional
Automatic Program Generator
?Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
Project :
Version :
Date : 2010-2-6
Author : huangqi
Company : wxhx
Comments:
Chip type : ATmega8L
Program type : Application
Clock frequency : 8.000000 MHz
Memory model : Small
External SRAM size : 0
Data Stack size : 256
*****************************************************/
#include <mega8.h>
unsigned int num[4],snum=0,cpcnt=0,prcnt=0,sdata=0;
unsigned int time=0,np=0,ncp=00;
unsigned char bnum=0,maxnum=3;
long dcnt=0;
bit upd=0,fcp=0,fcal=0;
// Timer 1 output compare A interrupt service routine
interrupt [TIM1_COMPA] void timer1_compa_isr(void)
{
// Place your code here
PORTD=(char)snum;
PORTC=(char)(snum>>8);
upd=1;
np++;
}
// Timer 1 input capture interrupt service routine
interrupt [TIM1_CAPT] void timer1_capt_isr(void)
{
// Place your code here
/* if(restart==1)
{
TCNT1=0;
ncp=0;
np=0;
dcnt=0;
time=0;
restart=0;
}
else
{ */
cpcnt=ICR1;
fcp=1;
// }
}
// Declare your global variables here
trans()
{
unsigned int i;
unsigned char t[4],ii;
i=sdata;
t[3]=(i/1000)%10;
i%=1000;
t[2]=i/100;
i%=100;
t[1]=i/10;
t[0]=i%10;
for (ii=0;ii<4;ii++)
{
switch(t[ii])
{
case 0:
num[ii]=0x0005;
break;
case 1:
num[ii]=0x061D;
break;
case 2:
num[ii]=0x0406;
break;
case 3:
num[ii]=0x0414;
break;
case 4:
num[ii]=0x021C;
break;
case 5:
num[ii]=0x2014;
break;
case 6:
num[ii]=0x2004;
break;
case 7:
num[ii]=0x041D;
break;
case 8:
num[ii]=0x0004;
break;
case 9:
num[ii]=0x0014;
break;
}
}
maxnum=3;
if(sdata<1000) maxnum=2;
if(sdata<100) maxnum=1;
if(sdata<10) maxnum=0;
}
void main(void)
{
// Declare your local variables here
double f1;
// Input/Output Ports initialization
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=P State6=P State5=P State4=P State3=P State2=P State1=P State0=P
PORTB=0xFF;
DDRB=0x02;
// Port C initialization
// Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTC=0x00;
DDRC=0x7F;
// Port D initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTD=0x00;
DDRD=0xFF;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
TCCR0=0x00;
TCNT0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 8000.000 kHz
// Mode: CTC top=OCR1A
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: On
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: On
// Compare A Match Interrupt: On
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x89;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x9C;
OCR1AL=0x3F;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
MCUCR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x30;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// Global enable interrupts
#asm("sei")
while (1)
{
// Place your code here
if (upd)
{
switch(bnum)
{
case 0: PORTD.5=1;PORTD.2=PINB.0;break;
case 1: PORTC.4=1;break;
case 2: PORTC.3=1;break;
case 3: PORTC.0=1;break;
}
bnum++;time++;
if(bnum>maxnum)bnum=0;
snum=num[bnum];
upd=0;
if(time>199)
{
time=0;
if(ncp>0)fcal=1;
else{sdata=0;trans();}
}
}
if (fcp)
{
ncp++;
dcnt+=cpcnt;
dcnt-=prcnt;
prcnt=cpcnt;
while(np>0)
{
dcnt+=40000;
np--;
}
fcp=0;
}
if (fcal)
{
f1=24e7*ncp;
f1/=dcnt;
sdata=(unsigned int)f1;
f1-=sdata;
if(f1>=0.5)sdata++;
dcnt=0;
ncp=0;
fcal=0;
trans();
}
//if((time%4)==0)PORTB.1=0;
//else PORTB.1=1;
};
}
// num PC PD
// 0 00 05
// 1 06 1D
// 2 04 06
// 3 04 14
// 4 02 1C
// 5 20 14
// 6 20 04
// 7 04 1D
// 8 00 04
// 9 00 14
// . bit PD2
// N3 N2 N1 N0
// PC0 PC3 PC4 PD5 |
|