⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 thermometer._c

📁 AVRkaifashili.rar
💻 _C
字号:
#include <io8535v.h>
#include <macros.h>
#define uchar unsigned char
#define uint  unsigned int
//SPI-AVR interface
//sbit CE=PB^4;    0001 0000  0x10  0xef
//sbit SCLK=PB^7;  1000 0000  0x80  0x7f
//sbit SDO=PB^6;   0100 0000  0x40  0xbf
//sbit SDI=PB^5;   0010 0000  0x20  0xdf
uchar Table[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar Data[4]={0,0,0,0};
uchar Temp=0;

void DelayMs(uint i)  
{uchar j;
 for(;i!=0;i--)
  {for(j=100;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(20);
   sel=sel<<1;
  }
}

void SPI_Init(void)
{//CE=0;//Disable Chip_Select
 PORTB=PORTB&0xef;
 //SCLK=1;//Clock Polarity(CPOL)=1
 PORTB=PORTB|0x80;
}

void SPI_Process(uchar p1,uchar *p2)
{uchar i;
 i=p1;
 p2[0]=0;
 p2[1]=i/100;
 p2[2]=(i-i/100*p2[1])/10;
 p2[3]=i-p2[1]*100-p2[2]*10;
}

void SPI_Write1Byte(uchar Address,uchar Data)
{uchar i;
 PORTB=PORTB&0xdf;//SDI=0;
 PORTB=PORTB|0x80;//SCLK=1;//CPOL=1
 PORTB=PORTB|0x10;//CE=1;//Enable Chip_Select
 for(i=0;i<8;i++)             //送地址;
   {PORTB=PORTB&0x7f;//SCLK=0;//时钟恢复
    if(Address&0x80) PORTB=PORTB|0x20;//SDI=1;//MSB first
	else       PORTB=PORTB&0xdf;//SDI=0;
	Address=Address<<1;//左移
    PORTB=PORTB|0x80;//SCLK=1;//上升沿完成,锁存
    DelayMs(1);}
 for(i=0;i<8;i++)             //送地址;
   {PORTB=PORTB&0x7f;//SCLK=0;//时钟恢复
    if(Data&0x80) PORTB=PORTB|0x20;//SDI=1;//MSB first
	else       PORTB=PORTB&0xdf;//SDI=0;
	Data=Data<<1;//左移
    PORTB=PORTB|0x80;//SCLK=1;//上升沿完成,锁存
    DelayMs(1);}
  PORTB=PORTB&0xef;//CE=0;
} 

uchar SPI_Read1Byte(uchar Address)
{uchar i,temp;
// SDO=1;
 PORTB=PORTB|0x80;//SCLK=1;//CPOL=1
 PORTB=PORTB|0x10;//CE=1;//Enable Chip_Select
 for(i=0;i<8;i++)             //送地址;
   {PORTB=PORTB&0x7f;//SCLK=0;//时钟恢复
    if(Address&0x80) PORTB=PORTB|0x20;//SDI=1;//MSB first
	else       PORTB=PORTB&0xdf;//SDI=0;
	Address=Address<<1;//左移
    PORTB=PORTB|0x80;//SCLK=1;//上升沿完成,锁存
   DelayMs(1);}
 for(i=0;i<8;i++)
   {PORTB=PORTB&0x7f;//SCLK=0;//提供一个下降沿接收一位数据
    temp=temp<<1;
    NOP();
	NOP();
	//if(SDO) temp=temp|0x01;//采样
	if(PINB&0x40) temp=temp|0x01;
	PORTB=PORTB|0x80;//SCLK=1;//时钟恢复
    DelayMs(1);}
 PORTB=PORTB&0xef;//CE=0;//Disbale Chip_Select
 return(temp);
}

void main(void)
{uchar Read;
 DDRA=0xff;
 DDRC=0xff;
 DDRB=0xbf;//1011 1111
 SPI_Init();
 SPI_Write1Byte(0x80,0xf0);//1 1 1 1SHOT R2 R1 R0 SD
                   //1 1 1 1     0  0  0  0//SD=0 indicate continuous convertion   
 while(1)
  {Read=SPI_Read1Byte(0x02);//Low Byte
   Temp=SPI_Read1Byte(0x00);//Hign Byte
   SPI_Process(Temp,Data);
   Display(Data);
   }
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -