使用T/C0定时器,256分频。PB3上的LED不断闪烁。
/**/
/*****************************************************
Chip type : ATmega32
Clock frequency : 4.000000 MHz
*****************************************************/
#include < mega32.h >
// 延时程序
void Delay()
... {
unsigned char a, b;
for (a = 1; a; a++)
for (b = 1; b; b++)
;
}
void delayms(unsigned char m)
... {
unsigned char a, b;
for (a = 1; a<m; a++)
for (b = 1; b; b++)
;
}
unsigned int k = 0 ;
void main( void )
... {
DDRB = 0xFF; // 方向,0-输入;1-输出
PORTB = 0xFF; // 输入-上拉电阻,0-没有;1-有
DDRA = 0xFF; /**//* output */
PORTA = 0xFF; /**//* all off */
for(k=0; k<5; k++)
...{
PORTB = 0xF7;
delayms(100);
PORTB = 0xFF;
delayms(100);
}
PORTB = 0xFB;
Delay();
PORTB = 0xFF;
Delay();
TCCR0 = 0x04; // 100B 256分频
TCNT0 = 0x06; // 初值
TIMSK = 0x01; // T/C0 溢出中断使能
SREG.7 = 1; // 全局中断使能
DDRA = 0x00;
PORTA = 0x00;
while(1);
}
interrupt [TIM0_OVF] void Timer0( void )
... {
PORTB.3 = ~PORTB.3;
}
Chip type : ATmega32
Clock frequency : 4.000000 MHz
*****************************************************/
#include < mega32.h >
// 延时程序
void Delay()
... {
unsigned char a, b;
for (a = 1; a; a++)
for (b = 1; b; b++)
;
}
void delayms(unsigned char m)
... {
unsigned char a, b;
for (a = 1; a<m; a++)
for (b = 1; b; b++)
;
}
unsigned int k = 0 ;
void main( void )
... {
DDRB = 0xFF; // 方向,0-输入;1-输出
PORTB = 0xFF; // 输入-上拉电阻,0-没有;1-有
DDRA = 0xFF; /**//* output */
PORTA = 0xFF; /**//* all off */
for(k=0; k<5; k++)
...{
PORTB = 0xF7;
delayms(100);
PORTB = 0xFF;
delayms(100);
}
PORTB = 0xFB;
Delay();
PORTB = 0xFF;
Delay();
TCCR0 = 0x04; // 100B 256分频
TCNT0 = 0x06; // 初值
TIMSK = 0x01; // T/C0 溢出中断使能
SREG.7 = 1; // 全局中断使能
DDRA = 0x00;
PORTA = 0x00;
while(1);
}
interrupt [TIM0_OVF] void Timer0( void )
... {
PORTB.3 = ~PORTB.3;
}