AVR入门必备

#include<avr/io.h>

int main(void)

{

       DDRB=0xff;

       while(1)

       {

              PORTB=0x55;

       }

}

2.这里要注意的是二进制的使用方法

#include<avr/io.h>

int main(void)

{

       DDRB=0xff;

       while(1)

       {

              PORTB=0b00000111;//这里试过了用B也可以

       }

}

3.第一个闪烁灯

#include<avr/io.h>

int main(void)

{

       int aa,x,y;

       DDRB=0xff;

       aa=0x0e;

       while(1)

       {

              PORTB = 0x0e;

              for ( y = 0;y < 10;y ++ )

              {

                     for ( x = 0;x < 10000;x ++ );

              }

              PORTB =0x01;

              for ( y = 0;y < 10;y ++ )

              {

                     for ( x = 0;x < 10000;x ++ );

                

       }

}

4.独立按键实验

//普通io口检测独立键盘的实验

#include<avr/io.h>

int main(void)

{

       DDRA=0x00;

       PORTA=0xff;

       DDRB=0xff;

       PORTB=0xFF;

       while(1)

       {

              if((PINA & 0x04) == 0x04)//在这里决定那一个按键管用

              {

                     PORTB=0xff;//如果按键没被按下,灯不亮

              }

              else

              {

                     PORTB=0x00;//如果灯被按下了,灯亮

              }

       }

}

5.//普通io口检测独立键盘的实验(运用switch语句)

#include<avr/io.h>

void delay( );

int main(void)

{

       DDRA=0x00;

       PORTA=0xff;

       DDRB=0xff;

       PORTB=0xFF;

       while(1)

       {

              if( (PINA &0x07)!=0x07)

              {

                     delay();

                     if((PINA & 0x07) !=0x07)

                     {

                            switch( PINA )      //注意这里switch语句后面要跟大括号

                            {

                                   case 0xfe:

                                  

                                          PORTB=0xfe;

                                          break;

                                  

                                   case 0xfd:

                                  

                                         PORTB=0xfd;

                                          break;

                                  

                                   case 0xfb:

                                  

                                          PORTB =0xfb;

                                          break;

                                  

                                   default :break;

                            }

                     }

              }

       }

}

void delay()

{

       int x,y;

       for ( x = 100; x > 0 ;x --)

              for ( y = 100; y>0;y--);

}

6.外部中断的应用

#include<avr/io.h>

#include<avr/interrupt.h>

void delay( );

int main(void)

{

       DDRB=0xff;

       PORTB=0xFF;

      

       MCUCR|=(1<<ISC11)|(1<<ISC01);//INT1,0都是下降沿触发

       GICR|=(1<<INT1)|(1<<INT0);//使能中断1中断2

       sei();//使能全局中断

       while(1)

       {

              PORTB=0x00;

              delay();

              delay();

              delay();

       }

             

}

SIGNAL(SIG_INTERRUPT0)

{

       int i;

       PORTB=0xf7;

       for( i=0;i<10;i++)

       delay();//这里的延时是必须的这样才能看见现象

}

SIGNAL(SIG_INTERRUPT1)

{

       int i;

       PORTB=0xfd;

       for( i=0;i<10;i++)

       delay();

}

void delay()

{

       int x,y;

       for ( x = 100; x > 0 ;x --)

              for ( y = 100; y>0;y--);

}

7. 利用查询方式进行定时中断

#include<avr/io.h>

#include<avr/interrupt.h>

int main(void)

{

       unsigned int i;

       DDRB=0xff;

       PORTB=0xFF;

      

       TCNT0 = 55;

       TCCR0|=( 1 << CS01)|(1 << CS00);

       while(1)

       {

              PORTB^=0xff; //安位取反

              for( i=0;i<50000;i++)

              {

                     while(!(TIFR & (1 << TOV0)));

                     TCNT0 = 55;

                

                

}

8.定时中断

#include<avr/io.h>

#include<avr/interrupt.h>

void delay( );

int main(void)

{

       DDRB=0xff;

       PORTB=0x00;

      

       TCNT0 = 55;

       TIMSK |= (1 <<TOIE0);//使能TC0溢出中断

       sei();//开全局中断

       TCCR0|=( 1 << CS01)|(1 << CS00);//64分频

       while(1);       

}

volatile unsigned int cnt=0;    //全局变量如果要用在中断服务程序里必须加前面的volatile

SIGNAL(SIG_OVERFLOW0)

{

       TCNT0 = 55;

       cnt++;

       if(cnt>=10000)

       {

              cnt=0;

              PORTB^=0x00;      //安位取反

       }

}

9.系统中断定时器

#include<avr/io.h>

#include<avr/interrupt.h>//使用中断函数是要用到

int main()

{

       PORTB = 0x55;

       DDRB = 0xFF;

//定时器初始化

       TCNT0 = 55;          //计数器

       TIMSK |=(1 << TOIE0);//使能tc0中断

       sei();//开全局中断

       TCCR0 |= (1 << CS01);//设置8分频

      

       while(1);

}

volatile unsigned int cnt = 0; //在中断服务程序里的变量都需要加volatile

SIGNAL(SIG_OVERFLOW0)

{

       TCNT0 = 55;

       cnt ++;   

       if(cnt == 10000)

       {

              cnt = 0;

              PORTB = ~PORTB;

       }

}

10.USART 单片机发送程序

#include<avr/io.h>

#include<avr/interrupt.h>//使用中断函数时要用到

#define F_MCU 7372800

#define BAUD 9600

unsigned char aa[40]={"Congratulation ball ! you are success!"};

void Init_USART()

{

       UBRRH = (F_MCU/BAUD/16-1)/256;

       UBRRL = (F_MCU/BAUD/16-1)%256;

       UCSRB |= (1 << RXEN)|(1 << TXEN)|(1 << RXCIE);

}

void Send_Data(unsigned char data)

{

       while(!(UCSRA & (1 << UDRE)));

       UDR = data;  

}

int main()

{

unsigned char i=0;

Init_USART();

       sei();

       while(aa[i]!='\0')

              Send_Data(aa[i++]);

    while(1);

}

11.USART单片机接收上位机发送的数据并发回上位机

#include<avr/io.h>

#include<avr/interrupt.h>//使用中断函数时要用到

#define F_MCU 7372800

#define BAUD 9600

unsigned char aa[40]={"Congratulation ball ! you are success!"};

unsigned char i;

void Init_USART()

{

       UBRRH = (F_MCU/BAUD/16-1)/256;

       UBRRL = (F_MCU/BAUD/16-1)%256;

       UCSRB |= (1 << RXEN)|(1 << TXEN)|(1 << RXCIE);

}

void Send_Data(unsigned char data)

{

       while(!(UCSRA & (1 << UDRE)));

       UDR = data;

}

volatile unsigned char recv_data=0;

SIGNAL(SIG_UART_RECV)

{

       recv_data = UDR;

}

int main()

{

       unsigned char i=0;

       Init_USART();

       sei();

       while(aa[i]!='\0')

              Send_Data(aa[i++]);

    while(1)

       {

              if(recv_data!=0)

              {

                     Send_Data(recv_data);

                     recv_data=0;

              }

       };

}

<script type="text/javascript" id="wumiiRelatedItems"> </script>
 
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值