www.pudn.com > TG12864.rar > spi_slave.c
#include "stdio.h"
#include "reg932.h"
#include "intrins.h"
#include "math.h"
#include "stdlib.h"
#define uchar unsigned char
#define ulong unsigned long
#define uint unsigned int
sbit SS = P2^4;
//sbit INT1FLAG=P0^0;
code uchar hz[32]=
{
/*-- 文字: 早 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
0x04,0x04,0x04,0x04,0x04,0x04,0x04,0xFF,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00,
0x00,0x00,0x00,0xFE,0x92,0x92,0x92,0x92,0x92,0x92,0x92,0x92,0xFE,0x00,0x00,0x00,
};
/****************************************************
函数名称:
函数功能:
传递参数:
*****************************************************/
void spi_slave_init(void)
{
P2M1=0x00;
P2M2=0x00;
//SS_MASTER = 1 ;
SPSTAT = 0xc0; //清除SPSTAT中的标志位
SPCTL = 0x5c;//主机,spi使能
}
/****************************************************
函数名称:
函数功能
传递参数:
*****************************************************/
void Delay(uint Count)
{
uint i,Timer_Count;
Timer_Count = Count;
for(;Timer_Count > 0;Timer_Count--)
for(i = 100;i > 0;i--)
{;}
}
/****************************************************
函数名称:
函数功能
传递参数:
*****************************************************/
void Spi_Write(uchar Write_Data)
{
SPSTAT = 0xc0;
SPDAT = Write_Data;
Delay(2); // Move byte to send to SPI data register
while((SPSTAT)&0x80 == 0) ; // Wait until SPI hs finished transmitting
}
/****************************************************
函数名称:
函数功能
传递参数:
*****************************************************/
uchar Spi_Read()
{
SPSTAT = 0xc0;
SPDAT = 0x00;
Delay(2); // Move byte to send to SPI data register
while((SPSTAT)&0x80 == 0) ; // Wait until SPI hs finished transmitting
return SPDAT;
}
/****************************************************
函数名称:
函数功能
传递参数:
*****************************************************/
//void Write_ack(uchar Write_Data)
//{
// SS = 0;
// Delay(2);
// Spi_Write(Write_Data);
// SS = 1;
//}
/****************************************************
函数名称:
函数功能
传递参数:
*****************************************************/
//uchar Read_command(void)
//{
// uchar ack;
// SS = 0;
// Delay(2);
// ack=Spi_Read();
// SS = 1;
// return(ack);
//}
/****************************************************
函数名称:
函数功能
传递参数:
*****************************************************/
void Write_master(uchar Wr_Number, uchar *Data)
{
uchar i;
// SS = 0;
Delay(1);
//Spi_Write(Command);
for(i = 0;i < Wr_Number;i++)
{
Delay(1);
Spi_Write(*Data);
Data++;
}
// SS = 1;
}
/****************************************************
函数名称:
函数功能
传递参数:
*****************************************************/
void Read_master(uchar Rd_Number,uchar *Payload)
{
uchar j;
// SS = 0;
Delay(1);
//Spi_Write(Command);
for(j = 0;j < Rd_Number;j++)
{
*Payload= Spi_Read();
Payload++;
}
// SS = 1;
//Wr_full = 1;
}
/****************************************************
函数名称:
函数功能:
传递参数:
*****************************************************/
void main()
{
uchar master_ack;
spi_slave_init();
//INT1=1;
while(1)
{
if(SS==0)
{
master_ack=Spi_Read();
if(master_ack==0xff)
{ //INT1FLAG=0;
Spi_Write(0xff);
//Delay(2);
Write_master(32,hz);
//while(1);
}
}
//while(1);
}
}