题目
用按键和数码管模拟方波发生器,按下后第一位不断产生高电平并不断向右移动,松手后,数码管显示当前波形并不断向右移动。
我的程序主要问题是结构不合理,移动时数码管点亮次数低能看出明显闪烁,另外松手后不能马上显示低电平。
求高手帮忙指点
我编的程序是
#include
#include
#define HC595_DATA_bit 0
#define HC595_LCLK_bit 1
#define HC595_SCLK_bit 2
#define HC595_DATA_HIGH PORTB |= (1<
#define HC595_DATA_LOW PORTB &= ~(1<
#define HC595_LCLK_HIGH PORTB |= (1<
#define HC595_LCLK_LOW PORTB &= ~(1<
#define HC595_SCLK_HIGH PORTB |= (1<
#define HC595_SCLK_LOW PORTB &= ~(1<
int LED[5]={0x88,0x87,0x01,0x31,0x88};
int w, d=0, t=0;
static into=0;
void delay(int time)
{
int k;
for (k=0;k
}
void SDT_595(int data)
{
int j;
for( j=0; j<8; j++ )
{
if( data&(1<<(7-j) ))
{HC595_DATA_HIGH;}
else HC595_DATA_LOW;
HC595_SCLK_HIGH;
NOP(); NOP();
HC595_SCLK_LOW;
}
}
void display0(int a)
{
for (w=7; w>=0; w--)
{
HC595_LCLK_LOW;
HC595_SCLK_LOW;
NOP(); NOP();
SDT_595(~(1<
NOP(); NOP();
SDT_595(LED[a]);
HC595_LCLK_HIGH;
}
}
void display1(int b)
{
for (d=0;d<=7;d++)
{
for (w=7; w>d; w--)
{
HC595_LCLK_LOW;
HC595_SCLK_LOW;
NOP(); NOP();
SDT_595(~(1<
NOP(); NOP();
SDT_595(LED[b]);
HC595_LCLK_HIGH;
}
HC595_LCLK_LOW;
HC595_SCLK_LOW;
NOP(); NOP();
SDT_595(~(1<
NOP(); NOP();
SDT_595(LED[b+1]);
HC595_LCLK_HIGH;
for (w=0; w
{
HC595_LCLK_LOW;
HC595_SCLK_LOW;
NOP(); NOP();
SDT_595(~(1<
NOP(); NOP();
SDT_595(LED[b+2]);
HC595_LCLK_HIGH;
}
if (into==0) break;
delay(1000);
}
}
int main(void)
{
CLI();
TCCR0 = 0B00001010;
TIMSK |= 0X02;
MCUCR &= ~(1<
MCUCR &= ~(1<
GICR |= (1<
SEI();
TCNT0 = 0X00;
OCR0 = 0X7D;
DDRB |= (1<
DDRB |= (1<
DDRB |= (1<
MCUCSR=0X80;
MCUCSR=0X80;
DDRC=0XFF;
PORTC=0X00;
HC595_LCLK_HIGH;
HC595_SCLK_HIGH;
NOP(); NOP();
while (1)
{
if (into)
{
PORTC = 0XFF;
display1(2);
into=0;
}
display0(0);
}
return 0;
}
#pragma interrupt_handler timer0_comp_isr:iv_TIM0_COMP
void timer0_comp_isr(void)
{
t++;
TCNT0 = 0X00;
}
#pragma interrupt_handler int0_isr:iv_INT0
void int0_isr(void)
{
into++;
if (into==1) display1(0);
else display0(2);
}