图发上来,不知道大家能不能看清。
程序是想实现led的动态显示,除了display()函数,剩下的都是初始化,display()函数中端口的清零和置位主要是为spi提供时序的,现在的问题是led显示的码不对,如果把display()函数中的SPDR = num_code[pp]语句的pp改成固定的数字(如0-9),就能显示正确,如果改成变量,就显示错误。
#include <iom16v.h>
#include <macros.h>
unsigned char light =0xFF;
unsigned char ocr_t = 0;
unsigned char num_code[20]={0XBF,0X86,0XDB,0XCF,0XE6,0XED,0XFD,0X87,0XFF,0XEF};
unsigned char datadisp[5] = {0X01,0X02,0X03,0X04};
int pp;
void delay_ms(void)
{
unsigned int cnt;
for(cnt=0;cnt<255;cnt++);
}
void port_init(void)
{
PORTA = 0x00;
DDRA = 0x00;
PORTB = 0x00;
DDRB = 0xFF;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0xff;
DDRD = 0xFF;
}
void timer2_init(void)
{
TCCR2 = 0x00; //stop
ASSR = 0x00; //set async mode
TCNT2 = 0x01; //setup
OCR2 = 0xFF;
TCCR2 = 0x6A; //start
}
void spi_init(void)
{
SPCR = 0x50; //setup SPI
SPSR = 0x00; //setup SPI
}
void display(void)
{
unsigned char q=0,j;
for(pp=0;pp<4;pp++)
{
OCR2 = 0X00;
// p = datadisp;
PORTB |= BIT(pp);
PORTB &= ~BIT(6);
delay_ms();
PORTB |= BIT(6);
SPDR = num_code[pp];
while(!(SPSR & 0X80));
PORTB &= ~BIT(4);
delay_ms();
PORTB |= BIT(4);
OCR2 =0X3f;
for(j=0;j<10;j++)
delay_ms();
PORTB &= BIT(pp);
delay_ms();
}
}
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
//uart0_init();
spi_init();
timer2_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
void main(void)
{
init_devices();
while(1)
{
display();
}
} |