AVR单片机

单片机##


#include <iccioavr.h>
#include <macros.h>
#include<stdio.h>
void Show();
void Shift(unsigned char tmp);
void Delay(int num);
unsigned char tmp,flg=0;
unsigned char seg[10]={0x03,0x9f,0x25,0x0d,0x99,0x49,0x41,0x1f,0x01,0x09};
unsigned char buf[4]={0x9f,0x25,0x0d,0x99};   //预设学号数组: 1234 
unsigned char en[4]={0x7f,0xbf,0xdf,0xef};   //数码管扫描数组
unsigned int count;

void main()
{
 DDRA=DDRA|0x01;	  			  //设定PA0为输出   					  								  
 PORTA=PORTA|0x01;				  //起始时所有SER=PA0="Hi"
 DDRB=DDRB|0x03;                  //设定RCLK=PB0,SRCLK=PB1输出
 PORTB=PORTB&0xfc;				  //起始时使PB0=PB1="Lo"
 DDRD=0xf7;	  			          //设定PORT D为输出 
 PORTD=0xff;					  //起始时PB4~PB7="Hi"  					  								  
 GICR=GICR|0x80;
 MCUCR=MCUCR|0x08;
 SREG=SREG|0x80;
 
do{
 	Show();
   }while(1);

}
void Show()
{
 int i;
 
 for(i=0;i<=3;i++)
    {
     Shift(buf[i]);					//将显示数组送至该数码管
	 PORTB=PORTB|0x01;
	 PORTB=PORTB&0xfe;
     PORTD=PORTD&en[i];					//开启该位数码管
     Delay(80);						    //延迟5ms
     PORTD=PORTD|0xf0;					//关闭所有数码管
    }
}

void Shift(unsigned char tmp)
{
 int i;
 
 for(i=0;i<8;i++)
    {
	 if((tmp&(0x01<<i))==0)    //移位
	    PORTA=PORTA&0xfe;
     else
	    PORTA=PORTA|0x01;	
     PORTB=PORTB|0x02;
     PORTB=PORTB&0xfd;
	} 
}

void Delay(int num)                  //时间延迟子程序 
{
 int i,j;
     
 for(j=0;j<=num;j++);
     for(i=0;i<50;i++);
}
复制代码

  1. 关键的地方
 if((tmp&(0x01<<i))==0)    //移位
        PORTA=PORTA&0xfe;
复制代码

<<移位操作


2.这些不知道是干嘛的

GICR=GICR|0x80;
 MCUCR=MCUCR|0x08;
 SREG=SREG|0x80;
复制代码

3.Shitft函数

unsigned char en[4]={0x7f,0xbf,0xdf,0xef};   //数码管扫描数组

Shift(buf[i]);
复制代码

入口:一个16进制的显示数码管数字。 如Shift(buf[0])为Shift(0x7f)送到数码管显示为1


4.74HC595

转载于:https://juejin.im/post/5ce4fc756fb9a07ea4205431

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值