ATmega88简单引脚设置
2022-07-22 来源:csdn
// 熔丝位 F7 DC F9 FF
// pb0 或 pd7 低 为故障 -》》 PD5输出低 PC0 PC1 PC2输出高电平
//PD3为高电平 应急 -》》 PD6 30%
//PD2为低电平 正常 -》》 PD6 拨码开关
//pb6高 -->> PB2低
#include #include unsigned char yingji_sign; unsigned char normal_sign; unsigned char pwm_1; unsigned char pwm_2; unsigned char pwm_3; unsigned char pwm_4; unsigned int count1=0; unsigned char PWM_T=0; void timer0_init(void) { TCCR0A = 0x00; //TCCR0B = 0x03; //预分频/64 启动TIME0 普通模式 TCCR0B|=BIT(1); TCNT0 = 249; //晶振12MHz, 频率为1KHz 256 - 1200000*1ms / 8 / 1000 TIMSK0 = 0x01; //使能时钟中断 SREG |= 0x80; //使能全局中断 } void port_init(void) { // 0110 0000 PD3 PD4 PD7 in PD2 PD5 PD6out DDRD = 0x64; PORTD = 0x00; // 0000 0111 PC0 PC1 PC2 PC5out PC3 PC4in(switch) DDRC = 0x27; PORTC = 0x00; // 0000 0000 PB0 PB2 in PB7测试 DDRB = 0x80; PORTB = 0x00; } /*按键检测*/ void check(void) { switch(PINC&0x18) //PC3,PC4组合有四种状态(11,10,01,00)对应不同占空比,低四位屏蔽 { case 0x18: PWM_T=99;break; case 0x10: PWM_T=90;break; case 0x08: PWM_T=80;break; default: PWM_T=70;break; } } void main(void) { port_init(); timer0_init(); //拉高PD5 PORTD |= 0x20; //单片机运行指示灯 PD2 1111 1011 PORTD &= 0xFB; SEI(); while(1) { //PB0 or PD7 == 0 ,wrong ;判断故障 if((PINB & 0x01) != 0x01 || (PIND & 0x80) != 0x80) { PORTD &= 0xDF; //PD5为低 PORTC |= 0x07; } else { PORTD |= BIT(PD5); if((PIND&0x08)==0x08)//PD3为高电平是应急 { PWM_T = 30; } else { check(); } } } } /*向量号在 53到54页 ATmega88的手册中*/ #pragma interrupt_handler timer0_ovf:17 void timer0_ovf(void) //0.01ms到来 { static unsigned char count=0; count++; TCNT0=249; if(PWM_T==count && PWM_T!=100) { PORTD |= BIT(PD6); //灯亮 if(PINB&(1< PORTC|= BIT(PC5); } else { PORTC&=~BIT(PC5); } } if(count>=100) { count=0; count1++; if(PWM_T!=0) { PORTD&=~BIT(PD6); //灯灭 if(PINB&(1< PORTC|= BIT(PC5); } else { PORTC&=~BIT(PC5); } } } }