⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 spi._c

📁 AVRkaifashili.rar
💻 _C
字号:
#include<io8535v.h>
#include<macros.h>
#define uchar unsigned char
#define uint  unsigned int

uchar Table[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar Data[4]={0,0,0,0};

void DelayMs(uint i)  
{uchar j;
 for(;i!=0;i--)
  {for(j=2000;j!=0;j--) {;}}
}

void Display(uchar *p)
{uchar i,sel=0x01;
 for(i=0;i<4;i++)
  {PORTA=sel;
   PORTC=0xff-Table[p[i]];
   DelayMs(10);
   sel=sel<<1;
  }
}

void DelayNOP(uchar i)
{for(;i!=0;i--)  
    { NOP();}
} 

void Convert(uchar *p,uchar i)
{p[0]=0;
 p[1]=i/100;
 p[2]=(i-p[1]*100)/10;
 p[3]=i-p[1]*100-p[2]*10;
}

void Write_Byte(uchar Address,uchar Data)
{uchar clear;
 PORTB=PORTB|0x10; //使能SPI器件
 SPDR=Address;
 while(!(SPSR&0x80)) {;}
 clear=SPSR;
 clear=SPDR;
 SPDR=Data;
 while(!(SPSR&0x80)) {;}
 clear=SPSR;
 clear=SPDR;
 PORTB=PORTB&0xef;
}
 
uchar Read_Byte(uchar Address)
{uchar clear;
 uchar Data;
 PORTB=PORTB|0x10;
 SPDR=Address;
 while(!(SPSR&0x80)) {;}
 clear=SPSR;
 clear=SPDR;
 SPDR=Address;//发空数据
 while(!(SPSR&0x80)) {;}
 clear=SPSR;
 Data=SPDR;
 PORTB=PORTB&0xef;
 return(Data);
}

void main(void)
{uchar value;
 DDRA=0xff;
 DDRC=0xff;
 DDRB=0xbf;
 PORTA=0xff;
 PORTC=0x00;
 PORTB=0xff;
 DelayMs(3000);
 PORTC=0xff;
 SPCR=0x5c;//0101 1100
 Write_Byte(0x80,0xf0);//8位,自由转换模式
 while(1)
  {value=Read_Byte(0x01);
   value=Read_Byte(0x02);
   Convert(Data,value);
   Display(Data);}
}
   
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -