标题:
求教,哪位高手帮我看一下这个程序,刚开始学习AVR
[打印本页]
作者:
LWJ
时间:
2013-8-15 11:38
标题:
求教,哪位高手帮我看一下这个程序,刚开始学习AVR
哪位高手哪位高手帮我看一下这个程序,用定时器0的CTC模式产生方波,波形从PB3口输出
#include <iom16v.h>
#include <macros.h>
void port_init(void)
{
PORTA = 0x00;
DDRA = 0x00;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}
//TIMER0 initialize - prescale:64
// WGM: CTC
// desired value: 20KHz
// actual value: 19.231KHz (-4.0%)
void timer0_init(void)
{
TCCR0 = 0x00; //stop
TCNT0 = 0x00; //set count
OCR0 = 0xFD; //set compare
TCCR0 = 0x1B; //start timer
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer0_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x02; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//
void main(void)
{
init_devices();
PORTB=0x00;
DDRB=0xff;
while(1)
{};
//insert your functional code here...
}
作者:
vmbjn888
时间:
2013-12-2 22:51
for(i=0;i<=7;i++) //轮流
{
PORTA=~(1<<i); //各位依次置 0
_delay_ms(200); //延是200ms
}
这个好像依次不起来吧
i=0时,i变成二进制为0000 0000左移一位还是0000 0000再取反PORTA变为1111 1111,灯全灭
i=1时,i变成二进制为0000 0001左移一位是0000 0010再取反PORTA变为1111 1101,第二个灯亮
i=2时,i变成二进制为0000 0010左移一位是0000 0100再取反PORTA变为1111 1011,第三个灯亮
i=3时,i变成二进制为0000 0011左移一位是0000 0110再取反PORTA变为1111 1001,第二个和第三个灯同时亮
i=4时,i变成二进制为0000 0100左移一位是0000 1000再取反PORTA变为1111 0111,第四个灯亮
i=5时,i变成二进制为0000 0101左移一位是0000 1010再取反PORTA变为1111 0101,第二个和第四个灯同时亮
i=6时,i变成二进制为0000 0110左移一位是0000 1100再取反PORTA变为1111 0011,第三个和第四个灯同时亮
i=7时,i变成二进制为0000 0111左移一位是0000 1110再取反PORTA变为1111 0001,第二个、第三个和第四个灯同时亮
改成这样应该可以
int i,j,k;
i=1;
for(j=0;j<8;j++) //循环8次,8位LED轮流点亮
{
PORTA=~i; //反向输出
_delay_ms(200); //延时200ms
i=1<<i; //左移一位
}
作者:
vmbjn888
时间:
2013-12-2 22:53
int i,j,k;
i=1;
for(j=0;j<8;j++) //循环8次,8位LED轮流点亮
{
PORTA=~i; //反向输出
_delay_ms(200); //延时200ms
i=i<<1; //左移一位
}
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1