/******************************************************************************/
//中断向量:
//iv_TIM0_OVF:10
//iv_TIM1_OVF:9
//iv_TIM2_OVF:5
//int0: PD2
//int1: PD3
#include <iom16v.h>
#include <macros.h>
#define CTLPORTD PORTD
#define SW9 0
#define SW10 1
#define SET_SW9 CTLPORTD|=(1<<SW9)
#define CLR_SW9 CTLPORTD&=~(1<<SW9)
#define SET_SW10 CTLPORTD|=(1<<SW10)
#define CLR_SW10 CTLPORTD&=~(1<<SW10)
unsigned char t0_ovfnum;//t0软件定时计数器
unsigned char t1_ovfnum;//t1软件定时计数器
unsigned char t2_ovfnum;//t1软件定时计数器
unsigned char K9_fnum=0,K10_fnum=0;//K9为右侧翻身按键,K10为左侧翻身按键
void timer0_init(void);//32.68ms
void timer1_init(void);//10ms
void timer2_init(void);//10ms
void interrupt_init(void);
void main(void)
{
unsigned char n;
timer1_init();
port_init();
interrupt_init();
while(1)
{
;
}
}
void port_init(void)
{
DDRD = 0xF3; //INT0,INT1对应的上拉电阻有效
PORTD = 0xFF;
PORTA =0x00;
}
void interrupt_init(void)
{
CLI();
MCUCR = 0x0A;//外部中断1,外部中断0均为下降沿产生中断
GICR = 0xC0;//开外部中断0,1;
TIMSK = 0;
SEI(); //开总中断
}
#pragma interrupt_handler int1_isr:3
void int1_isr (void)
{
PORTA =0x00;
CLI();
K9_fnum++;
TIMSK |= 0x04; //timer interrupt sources
GICR = 0xC0;
}
//TIMER1 initialize - prescale:64
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 10mSec
// actual value: 10.000mSec (0.0%)
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0x00; //setup
TCNT1L = 0x67;
OCR1AH = 0x69;
OCR1AL = 0xE2;
OCR1BH = 0x04;
OCR1BL = 0xE2;
ICR1H = 0x04;
ICR1L = 0xE2;
TCCR1A = 0x00;
TCCR1B = 0x05; //start Timer
//TIMSK |= 0x04; //timer interrupt sources
}
#pragma interrupt_handler timer1_ovf_isr:9
void timer1_ovf_isr(void)
{
TCNT1H = 0x67; //reload counter high value
TCNT1L = 0x69; //reload counter low value
//t1_ovfnum++;
PORTA =0xFF;
}
//TIMER2 initialize - prescale:1024
// WGM: Normal
// desired value: 10mSec
// actual value: 9.984mSec (0.2%)
void timer2_init(void)
{
TCCR2 = 0x00; //stop
ASSR = 0x00; //set async mode
TCNT2 = 0xB2; //setup
OCR2 = 0x4E;
TCCR2 = 0x07; //start
TIMSK |= 0x40; //timer interrupt sources
}
#pragma interrupt_handler timer2_ovf_isr:5
void timer2_ovf_isr(void)
{
TCNT2 = 0xB2; //reload counter value
t2_ovfnum ++;
if(t2_ovfnum == 200)//500ms
{
t2_ovfnum = 0;
PORTD^=BIT(2);//LED电平取反
}
}
/*
程序功能:
k9按键按下,继电器K1动作,平衡信号为高电平;
当平衡信号为低电平时,即到达平恒位置,此时k9按键5S时间内失灵,即K1断开;
5s延时后,再次按K9按键,按键正常工作。
*/
|