 |
级别:
|
|
积分:59分 |
|
注册:2008年07月29日
|
|
|
用于增强板(带hc164)的8*8点阵的滚动显示i love you <!----> //ICC-AVR application builder : 2007-4-29 23:17:37 // Target : M16 // Crystal: 1.0000Mhz
#include <iom16v.h> #include <macros.h> unsigned char const DLY=10; /*此表为字模*/ unsigned char const X_TAB[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x82,0xFE,0x82, 0x00,0x00,0x00,0xFE,0x80,0x80,0x80,0x80,0x00,0x7C,0x82,0x82,0x82,0x7C,0x00,0x3E,0x40, 0x80,0x40,0x3E,0x00,0xFE,0x92,0x92,0x92,0x82,0x00,0x00,0x00,0x0E,0x10,0xE0,0x10,0x0E, 0x00,0x7C,0x82,0x82,0x82,0x7C,0x00,0x7E,0x80,0x80,0x80,0x7E,0x00,0x0C,0xBE,0x0C,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00};
void port_init(void) { PORTA = 0x00; DDRA = 0xFF; PORTB = 0x00; DDRB = 0x00; PORTC = 0x00; //m103 output only DDRC = 0x00; PORTD = 0x00; DDRD = 0x00; }
//call this routine to initialize all peripherals void init_devices(void) { //stop errant interrupts until set up CLI(); //disable all interrupts port_init();
MCUCR = 0x00; GICR = 0x00; TIMSK = 0x00; //timer interrupt sources SEI(); //re-enable interrupts //all peripherals are now initialized }
void delay(unsigned int dly)
{ unsigned int i;
for(; dly>0; dly--)
for(i=0; i<500; i++);
}
/*------------------------------------------------------------------------------------------------ NAME : SPI_MasterInit() FUNCTION :初始化spi接口模式,MSTR=1,CPOL=1,CPHA=0,LSBF=0 --------------------------------------------------------------------------------------------------*/
void SPI_MasterInit(void) {unsigned char temp; DDRB=(1<<5)|(1<<7)|(1<<4); //初始化,mosi、sck输出?~~(ss)为高 SPCR=(1<<6)|(1<<4)|(1<<0)|(1<<1)|(1<<3); // SPSR = 0x00; temp = SPSR; temp = SPDR; //清空SPI,和中断标志,使SPI空闲 }
/*------------------------------------------------------------------------------------------------- NAME : msend_data() FUNCTION :向SPI总线发送数据 -------------------------------------------------------------------------------------------------*/
void msend_data(unsigned char data)
{ SPDR = data;
while(!(SPDR&(1<<7))); //等待SPIF置位,即等待数据发送完毕
}
/*---------------------------------------------------------------------------------------------------------------- NAME : main()
FUNCTION :使用硬件SPI接口输出0-F的数据,控制LED显示。 ---------------------------------------------------------------------------------------------------------------*/
void main(void)
{ unsigned char i=0,dly=DLY; unsigned char fly=0; init_devices(); SPI_MasterInit(); //初始化SPI接口
while(1) { while(dly) { dly--; for(i=0; i<8; i++) { PORTA=~(X_TAB[i+fly]); msend_data(BIT(i)); // 发送显示数据 delay(1); } } fly>60?fly=0:fly++;dly=DLY; }
}
ps: 连接方式:porta0~7接点阵r7~r0;portb5接A/B;portb7接CLK,MR接vcc. <!---->
|