/********************************************************
Project :使用输入捕捉的信号周期测量
Chip type :ATmega8L (VCC=3V)
Clock frequency :8.000000 MHz
Author :
Edition :V1.0
Time :2008.06.02
Comments :
能正常的测量及显示,但误差较大
********************************************************/
#include <iom8v.h>
#include <macros.h>
#include "selffile.h"
#pragma data:code
const unsigned char table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,
0x77,0x7c,0x39,0x5e,0x79,0x71}; //七段译码字型表(共阴极)
#pragma data:data
unsigned char ledbuff[8];
unsigned int oldcount,newcount;
void main(void)
{
mcu_init(); //MCU初始化
led_display(0); //LED初始显示0
delay_nms(1000);
while(1)
{
if(newcount!=oldcount)
oldcount=newcount; //捕捉到新的数值则更新显示
led_display(oldcount);
delay_nms(1000);
}
}
void mcu_init(void)
{
CLI();
DDRB&=~(1<<PB0); //捕捉IO口为输入
DDRC|=(1<<PC4)|(1<<PC5); //LED-DATA、CLK为输出
DDRD|=(1<<PD4);
PORTB=0x00;
PORTC=0x00;
PORTD=0xff;
TIMSK=0x20; //使能T1捕捉中断
ICR1=0;
TCNT1=0;
TCCR1A=0x00;
TCCR1B=0xC1; //不分频,用8M进行采样,更准确,但有4K左右的误差
SEI();
}
void hextobcd(unsigned int Icp_count)
{
unsigned char i,temp;
for(i=0;i<4;i++)
{
temp=Icp_count%10;
ledbuff=table[temp];
Icp_count=Icp_count/10;
}
}
void led_display(unsigned int DData)
{
unsigned char i,j;
unsigned int Icp_data=0;
hextobcd(DData);
for(i=0;i<4;i++) //采用移位寄存器HC164实现串入并出
{
for(j=0;j<8;j++)
{
PORTC_LED_CLK_0;
if((ledbuff&0x80)==0x80)
PORTC_LED_DATA_1;
else
PORTC_LED_DATA_0;
PORTC_LED_CLK_1;
ledbuff=ledbuff<<1;
} //在此循环后无须加延时,否则会造成明显的闪动
}
}
void delay_nms(unsigned int n) //延时n ms
{
unsigned int i;
for(i=0;i<n;i++)
delay_1ms();
}
void delay_1ms(void) //8M.延时1ms
{
unsigned int i;
for(i=1;i<1142;i++)
;
}
#pragma interrupt_handler Icp_timer1:6
void Icp_timer1(void) //捕捉中断
{
newcount=ICR1;
ICR1=0;
TCNT1=0;
TCCR1B=0xC1;
PORTD&=~(1<<PD4); //指示灯亮
}
http://www.ouravr.com/bbs/bbs_content.jsp?bbs_sn=1133029&bbs_page_no=1&search_mode=1&search_text=测量&bbs_id=9999
文章评论(0条评论)
登录后参与讨论