|
昨晚把输入检测写好了,现在手机上网,明天贴上来。
可以贴上了:
//ICC-AVR application builder : 2012/7/27 17:49:05
// Target : M16
// Crystal: 16.000Mhz
#include <iom16v.h>
#include <macros.h>
int out_f = 100,
//设置输出频率,单位Hz
ch_in_01,
//存储ch1的输入值
ch_in_02,
//存储ch2的输入值
ch_out_01,
//存储ch1的输出值
ch_out_02,
//存储ch2的输出值
int0_rising,
int0_falling,
int1_rising,
int1_falling;
char int0_ovf_count,
int1_ovf_count;
//这里记录上升沿后timer1的溢出次数
void port_init(void)
{
PORTA = 0x0F;
DDRA = 0xF0;
//PORTA最高4位为输出端口
PORTB = 0xFF;
DDRB = 0x00;
PORTC = 0xFF;
DDRC = 0x00;
PORTD = 0xFF;
DDRD = 0x30;
}
//TIMER1 initialize - prescale:8
// WGM: 14) PWM fast, TOP=ICRn
// desired value: 10000uSec
// actual value: 10000.000uSec (0.0%)
void timer1_init(void)
{
TCCR1B = 0x00;
//stop
TCNT1H = 0x00;
//setup
TCNT1L = 0x00;
OCR1A = ch_out_01;
OCR1B = ch_out_02;
ICR1 = out_f*100*2;
//16M晶振;8分频;实际TOP数值为20000;TOP值可以用“out_f”进行修改;
TCCR1A|= (1<<COM1A1)|(1<<COM1B1)|(1<<WGM11);
//OC1A/B 比较匹配时输出低电平;WGM13:WGM10配置TC1为快速PWM、TOP值=ICR1;
TCCR1B|= (1<<WGM13)|(1<<WGM12)|(1<<CS11);
//时钟8分频,每2个时钟=1uS;
}
#pragma interrupt_handler timer1_ovf_isr:iv_TIM1_OVF
void timer1_ovf_isr(void)
{
//TIMER1 has overflowed
TCNT1H = 0x00 /*INVALID SETTING*/; //reload counter high value
TCNT1L = 0x00 /*INVALID SETTING*/; //reload counter low value
int0_ovf_count++;
int1_ovf_count++;
ch_out_01 = (unsigned long)ch_in_01/100*75+(ch_in_02>>1);
//更新ch1的输出值
ch_out_02 = (unsigned long)ch_in_01/100*75-(ch_in_02>>1);
//更新ch2的输出值
}
#pragma interrupt_handler int0_isr:iv_INT0
void int0_isr(void)
{//external interupt on INT0 PD2
if(PIND & 0x04)//如果PD2为高电平,证明是上升沿触发
{
int0_rising = TCNT1;
int0_ovf_count = 0;
}
else
{
int0_falling = TCNT1;
//读取当前寄存器数值
ch_in_01 =(unsigned long)int0_ovf_count*ICR1+(unsigned long)int0_falling-(unsigned long)int0_rising;
}
}
#pragma interrupt_handler int1_isr:iv_INT1
void int1_isr(void)
{//external interupt on INT1 PD3
if(PIND & 0x08)//如果PD3为高电平,证明是上升沿触发
{
int1_rising = TCNT1;
int1_ovf_count = 0;
}
else
{
int1_falling = TCNT1;
//读取当前寄存器数值
ch_in_02 =(unsigned long)int1_ovf_count*ICR1+(unsigned long)int1_falling-(unsigned long)int1_rising;
}
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer1_init();
TIMSK |= 1<<TOIE1;
//开timer1溢出中断
GICR |= (1<<INT1)|(1<<INT0);
//开启外部中断int1,int0
MCUCR |= (1<<ISC10)|(1<<ISC00);
//设置int1,int0电平变化触发中断
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
void main(void)
{
init_devices();
while(1)
{
;
}
}
[ 本帖最后由 n44303 于 2012-8-1 23:43 编辑 ] |
|