基于LD3320芯片的声电密码门设计 下载本文

}

//Watchdog initialize // prescale: 1024K void watchdog_init(void) {

WDR (); //this prevents a timeout on enabling

WDTCSR |= (1<

//call this routine to initialize all peripherals void init_devices(void) {

//stop errant interrupts until set up CLI(); //disable all interrupts port_init(); // watchdog_init();

MCUCR = 0x00;

EICRA = 0x00; //extended ext ints EIMSK = 0x00;

TIMSK0 = 0x00; //timer 0 interrupt sources TIMSK1 = 0x00; //timer 1 interrupt sources TIMSK2 = 0x00; //timer 2 interrupt sources

PCMSK0 = 0x00; //pin change mask 0 PCMSK1 = 0x00; //pin change mask 1 PCMSK2 = 0x00; //pin change mask 2 PCICR = 0x00; //pin change enable PRR = 0x00; //power controller

22

SEI(); //re-enable interrupts //all peripherals are now initialized }

//LD3320 接受命令是0x04 读命令是0x05 //SDCK 下降沿有效。 //读数据 上升沿数据不稳定。 void delayms(unsigned int ms) {

unsigned int i,j; for(i=0;i

asm(\ asm(\ } }

void delayus(unsigned int uldata) {

unsigned int j = 0; unsigned int g = 0; for (j=0;j<10;j++) {

for (g=0;g

asm(\ asm(\ asm(\ } } }

23

/*********************LD3320底层驱动******************************************/

void LD3320_mode_set(void) {

LD3320_MD_ON; //SPI模式 }

void LD3320_rest(void) {

LD3320_RES_ON; delayms(5); LD3320_RES_OFF; delayms(5); LD3320_RES_ON;

delayms(5); LD3320_SCS_OFF; delayms(5); LD3320_SCS_ON; delayms(5); }

void LD3320_write_byte(unsigned char date) {

unsigned char i = 0; for(i=0;i<8;i++) {

if((date & 0x80)==0x80) LD3320_SDI_ON; else

LD3320_SDI_OFF;

24

delay_nop; delay_nop;

LD3320_SDCK_OFF; date = (date<<1); delay_nop; delay_nop;

LD3320_SDCK_ON; } }

void LD3320_write(unsigned char add,unsigned char date) {

LD3320_SCS_ON; delay_nop; LD3320_SCS_OFF; delay_nop;

LD3320_write_byte(0x04); LD3320_write_byte(add); LD3320_write_byte(date); delay_nop; LD3320_SCS_ON; }

unsigned char LD3320_read(unsigned char add) {

unsigned char date_temp = 0,i = 0; LD3320_SCS_ON; delay_nop; LD3320_SCS_OFF; delay_nop;

LD3320_write_byte(0x05);

25