//ATMEGA128数码管显示函数动态扫描(存在故障)
#include
#define uchar unsigned char
#define uint unsigned int
#define wei4_en {DDRB|=(1<<7);PORTB|=(1<<7);}
#define wei4_dis {DDRB|=(1<<7);PORTB&=~(1<<7);}
#define wei3_en {DDRB|=(1<<6);PORTB=(1<<6);}
#define wei3_dis {DDRB|=(1<<6);PORTB&=~(1<<6);}
#define wei2_en {DDRB|=(1<<5);PORTB|=(1<<5);}
#define wei2_dis {DDRB|=(1<<5);PORTB&=~(1<<5);}
#define wei1_en {DDRB|=(1<<4);PORTB|=(1<<4);}
#define wei1_dis {DDRB|=(1<<4);PORTB&=~(1<<4);}
uchar wei[]={0x80,0x40,0x20,0x10};
uchar buffer3=0x00;
uchar buffer2=0x00;
uchar buffer1=0x00;
uchar buffer0=0x00;
const unsigned char tt[]={0x3F,0x06,0x5B,0x4F,0x66, //0,1,2,3,4
0x6D,0x7D,0x07,0x7F,0x6F,0X00}; //5,6,7,8,9
void HC_595_OUT(unsigned char data)
{
DDRC |= (1<<7);//OE=7,OE_DDR=DDRC
PORTC &= (1<<7);//OE_PORT=PORTC
PORTB = 0x0f;
DDRB |=((1<<2)|(1<<1)|(1<<0));//设置MOSI,SCK输出
/*上面的是正确的写法,原来的代码是:
DDRB |=(1<<2)|(1<<1)|(1<<0);
注:一开始之所以显示的顺序不对,就是没有对右边的代码再加一层括号,致使位码显示不对
又是由于一个小疏忽导致了一个大的错误。看来编程还得更加严紧了啊*/SPCR = (1<
PORTB|=(1<<0);
/* 启动数据传输 */
SPDR = data;
/* 等待传输结束 */
while(!(SPSR & (1< PORTB&=~(1<<0);
//PORTB=0X70;
}
void display(uint tmp)
{
uint buffer;
DDRB=0X80;
PORTB=0X80;
buffer3=tt[tmp/1000];
HC_595_OUT(buffer3);
PORTB&=~(1<<7);
DDRB=0X40;
PORTB=0X40;
buffer2=tt[tmp%1000/100];
HC_595_OUT(buffer2);
PORTB&=~(1<<6);
DDRB=0X20;
PORTB=0X20;
buffer1=tt[tmp%100/10];
HC_595_OUT(buffer1);
PORTB&=~(1<<5);
DDRB=0X10;
PORTB=0X10;
buffer0=tt[tmp%10];
HC_595_OUT(buffer0);
PORTB&=~(1<<4);
}
void delay( unsigned int z)
{
unsigned int x,y;
for(x=z;x>0;x--)
for(y=1140;y>0;y--);
}
void delay1(unsigned int z)
{
while(z--);
}
void main()
{
unsigned int cnt;
while(1)
{
display(1234);
}
}
阅读(1531) | 评论(0) | 转发(0) |