串行外设接口(SPI:Serial Peripheral interface)是一种串行同步通讯协议(同时读入和写出),由SDI(串行数据输入),SDO(串行数据输出),SCK(串行移位时钟),CS(从使能信号)四种信号构成。CS 决定了唯一的与主设备通信的从设备,如没有CS 信号,则只能存在一个从设备,主设备通过产生移位时钟来发起通讯。
主要影响SPI通讯的两个参数是时钟极性(CPOL)和时钟相位(CPHA)两个参数,SPI传输串行数据时首先传输最高位。时钟极性(CPOL)对传输协议没有重大的影响,如果CPOL=0,串行同步时钟的空闲状态为低电平;如果CPOL=1,串行同步时钟的空闲状态为高电平。时钟相位(CPHA)能够配置用于选择两种不同的传输协议之一进行数据传输,如果CPHA=0,在串行同步时钟的第一个跳变沿(上升或下降)数据被采样;如果CPHA=1,在串行同步时钟的第二个跳变沿(上升或下降)数据被采样。图示如下:
一般的ARM系统对SPI的支持方式分三种:轮询(POLLING),中断(INTERRUPT,需要定义SPI的中断号),和DMA(配置好SPI和DMA对应的寄存器,和DMA的源头和目的地址,就可以开始进行DMA传输)。
功 能:把一整数转换为字符串
用 法:char *itoa(int value, char *string, int radix);
详细解释:itoa是英文integer to array(将int整型数转化为一个字符串,并将值保存在数组string中)的缩写. 参数:
value: 待转化的整数。
radix: 是基数的意思,即先将value转化为radix进制的数,范围介于2-36,比如10表示10进制。 * string: 保存转换后得到的字符串。 返回值:
char * : 指向生成的字符串, 同*string。 备注:该函数的头文件是\
实现itoa函数的源代码
char *my_itoa(int num,char *str,int radix)
{
const char table[]=\char *ptr = str; bool negative = false; if(num == 0) { //num=0 *ptr++='0';
*ptr='\\0'; // don`t forget the end of the string is '\\0'!!!!!!!!! return str; }
if(num<0)
{ //if num is negative ,the add '-'and change num to positive *ptr++='-'; num*=-1; negative = true; }
while(num) {
*ptr++ = table[num%radix]; num/=radix; }
*ptr = '\\0'; //if num is negative ,the add '-'and change num to positive
// in the below, we have to converse the string
char *start = (negative ? str+1 : str); //now start points the head of the string ptr--; //now prt points the end of the string
while(start char temp = *start; *start = *ptr; *ptr = temp; start++; ptr--; } return str; }