串口同步异步c语言程序,同步串口spi的c语言编程

本文介绍了一种使用C语言编程实现SPI串口显示的方法。通过定义字符映射表和延时函数,实现了四位数码管的动态显示,并且包含了SPI读写字节的具体操作流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原标题:同步串口spi的c语言编程

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

}

责任编辑:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值