按键点亮数码管(测试成功)
#define OE_PORT PORTC
#define OE_PIN PINC
#define OE_DDR DDRC
#define OE 7
#define Seg7_Bit0 4
#define Seg7_Bit1 5
#define Seg7_Bit2 6
#define Seg7_Bit3 7
#define dp 7
#define Seg7_Bitselect_PORT PORTB
#define Seg7_Bitselect_PIN PINB
#define Seg7_Bitselect_DDR DDRB
#define Seg7_Bit0_En() {Seg7_Bitselect_DDR|=(1<<4);Seg7_Bitselect_PORT|=(1<<4);}
#define Seg7_Bit0_Dis() {Seg7_Bitselect_DDR|=(1<<4);Seg7_Bitselect_PORT&=~(1<<4);}
#define Seg7_Bit1_En() {Seg7_Bitselect_DDR|=(1<<5);Seg7_Bitselect_PORT|=(1<<5);}
#define Seg7_Bit1_Dis() {Seg7_Bitselect_DDR|=(1<<5);Seg7_Bitselect_PORT&=~(1<<5);}
#define Seg7_Bit2_En() {Seg7_Bitselect_DDR|=(1<<6);Seg7_Bitselect_PORT|=(1<<6);}
#define Seg7_Bit2_Dis() {Seg7_Bitselect_DDR|=(1<<6);Seg7_Bitselect_PORT&=~(1<<6);}
#define Seg7_Bit3_En() {Seg7_Bitselect_DDR|=(1<<7);Seg7_Bitselect_PORT|=(1<<7);}
#define Seg7_Bit3_Dis() {Seg7_Bitselect_DDR|=(1<<7);Seg7_Bitselect_PORT&=~(1<<7);}
#define SS 0
#define SCK 1
#define MOSI 2
#define MISO 3
#define SS_H() PORTB|=(1<#define SS_L() PORTB&=~(1<#include
const unsigned char duan[]={0x3F,0x06,0x5B,0x4F,0x66, //0,1,2,3,4
0x6D,0x7D,0x07,0x7F,0x6F}; //5,6,7,8,9
unsigned char buffer,temp;
void spi_init()
{
DDRB |= (1< SPCR = (1<}
void SPI_MasterTransmit(char Data)
{
/* 启动数据传输 */
SPDR = Data;
/* 等待传输结束 */
while(!(SPSR & (1<}
void HC_595_init(void)
{
DDRC |= (1<<7); //DDRC7=1 output
PORTC &= (1<<7);//PORTC7=1 , 595 enable
PORTB = 0x0F;
//PORTB |=((1<<7)|(1<<6)|(1<<5)); //位选,高四位控制四个数码管,高电平有效
spi_init();
}
void HC_595_OUT(unsigned char data)
{
SS_L(); //PORTB0=0
SPI_MasterTransmit(data);
SS_H(); //PORTB0=1
}
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 ini_key()
{
DDRD=0X00;
PORTD=0XFF;
}
void key_scan()
{
if((PIND&0x01)==0)
{
buffer=1;
}
////////////////////////////////////key2
if((PIND&0x02)==0)
{
buffer=2;
//delay(200);
}
////////////////////////////////////key3
if((PIND&0x04)==0)
{
buffer=3;
//delay(200);
}
if((PIND&0x08)==0)
{
buffer=4;
}
if((PIND&0x10)==0)
{
buffer=5;
}
if((PIND&0x20)==0)
{
buffer=6;
}
if((PIND&0x40)==0)
{
buffer=7;
}
if((PIND&0x80)==0)
{
buffer=8;
}
}
void main()
{
unsigned int cnt;
buffer=0;
ini_key();
HC_595_init();
while(1)
{
key_scan();
//for(cnt=0;cnt<10;cnt++)
{
Seg7_Bit0_En(); //PORTB4=1,选中最后一个数码管
temp=duan[buffer];
HC_595_OUT(temp);
//delay(600);
//Seg7_Bit0_Dis();
}
}
}
阅读(1216) | 评论(0) | 转发(0) |