单片机##
#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++);
}
复制代码
- 关键的地方
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