#include <STC_12.h>
#include <stdio.h>
#include "tft_9325.h"
#include "temp75.h"
void delay_t(u32 t);
char str2[18];
char str1[18];
char str3[18];
char str4[18];
//sbit SPIF = SPSTAT^7;
//sbit WCOL = SPSTAT^6;
//sfr AUXR = 0x8e;
#define FOSC 18432000L
#define BAUD (256-FOSC/32/115200)
#define SPIF 0x80
#define WCOL 0x40
#define SSIG 0x80
#define SPEN 0x40
#define DORD 0x20
#define MSTR 0x10
#define CPOL 0x08
#define CPHA 0x04
#define SPDHH 0x00
#define SPDH 0x01
#define SPDL 0x02
#define SPDLL 0x03
sbit SPISS = P1^3;
void InitUart();
void init_spi(void);
void SendUart(u8 dat); //Send data to PC
u8 RecvUart(); //receive data from PC
u8 SPISwap(u8 dat); //swap SPI data between master
void SPI_write(uchar dat);
uchar SPI_read();
sbit SCK = P1^7;
sbit MOSI = P1^5;
sbit MISO = P1^6;
sbit CSN = P1^4 ;// 28J60 -- CS
void WriteByte(u8 temp);
u8 ReadByte(void);
int main (void)
{
u8 i = 0;
float temp = 0;
TFT_Init(); //显示屏初始化
TMP75_Init(); //温度传感器初始化
ClearScreen(0xffff);//清屏(白色)
SPCTL = 0xfc; //设置为主机,并且最低位先发送,数据
//在SPICLK前时钟沿驱动,在后时钟沿采样
SPSTAT = 0xc0;
 1M0 = 0x00;
 1M0 = 0x00; //将P1口设置为准双向IO口
// InitUart(); //initial UART
init_spi(); //initial SPI
while(1)
{
float tmp = TMP75_Read(); //调用读取温度的函数
tmp = tmp*0.062; //因为TMP温度传感器的分辨率为0.0625
TFT_PutStr(100,100,"Current TMP:",0,0xffff); //显示“Current TMP:” (分别为横坐标、纵坐标,所要显示的字符串,字体颜色,背景颜色);
sprintf(str2,"%.1f ^C",tmp); //把当前温度存放到str里
TFT_PutStr(200,100,str2,0,0xffff); //显示str里存放的当前温度数据
delay_t(0xfff); //延时
tmp = tmp * 10;
str4[0] = (int)tmp / 100;
str4[1] = (int)tmp % 100 / 10;
str4[2] = (int)tmp % 10;
for(i = 0; i < 3; i++)
{
WriteByte(str4);
}
// SendUart('1');
/* WCOL = 1;
while(str2 != '\0')
{
SPDAT = str2;
}
while(SPSTAT = 0x40);
// SPIF = 1;
for(i = 0; i < 4; i++)
{
while(SPSTAT = 0x40);
// while(str2 != '\0')
// {
str1 = SPDAT;
// }
}
// while(SPSTAT = 0x40); */
/** if(RI)
{
SPCTL = SPEN|MSTR; //SET AS MASTER
SendUart(SPISwap(RecvUart()));
SPCTL = SPEN; //reset as slave
}
if(SPSTAT & SPIF)
{
SPSTAT = SPIF|WCOL; //clear SPI status
SPDAT = SPDAT; //mov data from receive buffer to send buffer
}
*/
for(i = 0; i < 3; i++)
{
str1 = ReadByte();
}
temp = (str1[0] * 100 + str1[1] * 10 + str1[2]) / 10;
TFT_PutStr(100,80,"Remote TMP:",0,0xffff); //显示“Current TMP:” (分别为横坐标、纵坐标,所要显示的字符串,字体颜色,背景颜色);
sprintf(str3,"%.1f ^C",temp); //把当前温度存放到str里
TFT_PutStr(190,80,str3,0,0xffff); //显示str里存放的当前温度数据
delay_t(0xfff); //延时
}
return 0;
}
void delay_t(u32 t)
{
while(t--);
}
/*
void InitUart()
{
SCON = 0x5a; //set UART mode as 8-bit variable baudrate
TMOD = 0x20; //timer1 as 8-bit auto reload mode
AUXR = 0x40; //timer1 work at 1T mode
TH1 = TL1 = BAUD; //115200bps
TR1 = 1;
} */
void init_spi(void)
{
//SSIG = 1; //忽略SS脚
//SPEN = 1; //允许SPI工作
//DORD = 0; //先传高位MSB
//MSTR = 1; //设置单片机为主机
SPCTL = 0xD0; //SPI Control Register SSIG SPEN DORD MSTR CPOL CPHA SPR1 SPR0 0000,0100
SPSTAT = 0xC0; //
//IE2 |= 0x02; //允许SPI中断控制位
}
u8 ReadByte(void)
{
idata u8 temp;
//SPSTAT = 0xC0;
SPDAT = 0x00;
while(!(SPSTAT & 0x80));
temp = SPDAT;
SPSTAT = 0xC0;
return temp;
}
void WriteByte(u8 temp)
{
SPDAT = temp;
while(!(SPSTAT & 0x80));
SPSTAT = 0xC0;
}
/*
void SPI_write(uchar dat)
{
uchar a;
for(a=0;a<8;a++)
{
SCK=0;
MOSI=dat&0x80;
SCK=1;
delay_t(5);
dat<<=1;
}
}
uchar SPI_read()
{
uchar a,dat;
for (a=0;a<8;a++)
{
SCK=0;
delay_t(5);
SCK=1;
dat<<=1;
dat|=MISO;
}
return dat;
} */
|