×
单片机 > 单片机程序设计 > 详情

同步串口spi的c语言编程

发布时间:2020-08-21 发布时间:
|
#include

#include
#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;
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);}
}


『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
51单片机CO2检测显示程序解析