Ⅰ 關於用51單片機的IO口模擬SPI驅動NRF2401模塊,
這個是datasheet裡面的介面電路,黃色的埠表示和單片機相連,或者和電源/地相連。除了電源和地信號,其它都是單片機的介面,這些介面就可以在程序中定義成你前面說的樣子
sbitCE= P2^0;
sbit***= P2^5;
sbit***= P2^1;
sbit***=P2^4;
sbit***=P2^2;
sbit***=P2^3
根據實際情況,定義相應的IO口為相應的名字,然後應用就行了
Ⅱ 51單片機不具備SPI,那麼如何來實現這一功能呢
SPI協議在51單片機上的實現//
函數聲明,變數定義
#i nclude <reg51.h>
#i nclude <intrins.h>
sbit SCK=P1^0; // 將p1.0口模擬時鍾輸出
sbit MOSI=P1^1; // 將p1.1口模擬主機輸出
sbit MISO=P1^2; // 將p1.1口模擬主機輸入
sbit SS1=P1^3; // 將p1.1口模擬片選
#define delayNOP(); {_nop_();_nop_();_nop_();_nop_();};
// 函數名稱: SPISendByte
// 入口參數: ch
// 函數功能: 發送一個位元組
//void SPISendByte(unsigned char ch)
{
unsigned char idata n=8; // 向SDA上發送一位數據位元組,共八位
SCK = 1 ; //時鍾置高
SS1 = 0 ; //選擇從機
while(n--)
{
delayNOP();
SCK = 0 ; //時鍾置低
if((ch&0x80) == 0x80) // 若要發送的數據最高位為1則發送位1
{
MOSI = 1; // 傳送位1
}
else
{
MOSI = 0; // 否則傳送位0
}
delayNOP();
ch = ch<<1; // 數據左移一位
SCK = 1 ; //時鍾置高
}
}
// 函數名稱: SPIreceiveByte
// 返回接收的數據
// 函數功能: 接收一位元組子程序
Ⅲ 用51單片機來控制nrf14l01,為什麼程序裡面好像沒有對sck進行什麼操作。
1.sck要麼是通過串口模塊自動生成了,那麼你在程序里就需要先配置好串口模塊。
2.要麼就是程序寫的串口協議,那肯定是會操作sck的。
一般范常式序都是第2個情況。建議你仔細查最底層的函數,函數名一般是例如SPI_RW之類的,裡面應該操作了sck。
Ⅳ DHT95中的SCK應該連接51單片機中的什麼引腳
不清楚你用的是哪款51單片機,怎麼會有48號那麼多引腳(51一般都是40或者44個引腳)。
如何接,你首先要明白DHT95是用SPI同步串列通信來和單片機進行通信的,所以說,如果你的51單片機如果沒有SPI功能,那就需要用程序來模擬了(很多51單片機都沒有SPI功能的,你用的超過44個引腳的單片機我還真不好說有還是沒有,自己看看吧)。
這里只說沒有SPI功能的單片機和DHT95連接。因為SCK是感測器在SPI通信時候的頻率輸入(就是SPI時鍾線),時鍾線只能是用程序來模擬,比如說你看DHT95的的數據手冊,裡面說明了在SCK的下降沿發送數據,所以需要在單片機程序能控制的情況下發送和接受數據,因此不能使用你說的ACLK引腳。
另外看來你還沒認真看過DHT95的數據手冊,也不知道SPI通信的原理,要做這個感測器,先把上述的兩個知識看一看吧。
補充:MSP430單片機有些晶元集成了SPI口,如果用它來和感測器連接就比較簡單。而你說的89C51RC應該是STC89C51RC,這款晶元沒有集成SPI口,所以需要自己用程序做模擬SPI,DHT95的銷售商大連北方測控的網站上有關於51和這個感測器的通信程序,你可以去找來看看。但如果你是想學這東西,最好還是先看看SPI的通信原理,DHT95的數據手冊,還有這個文章
Ⅳ 51單片機 c語言看門狗程序怎麼寫
看門狗在51單片機電路里的作用是防止程序 「跑飛」、「死機」後,系統不動作,而採取復位的辦法「喚醒」系統。
89S51、89S52系列單片機自帶有看門狗功能,片內數據區A6H寄存器具有看門狗功能,使用很簡單:
#include<reg51.h>
...
sfr WDTRST = 0xA6;
...
void main()
{
WDTRST=0x1E;;//初始化看門狗
WDTRST=0xE1;//初始化看門狗
for(;;)
{
WDTRST=0x1E;;//喂狗指令
WDTRST=0xE1;//喂狗指令
}
}
可見,你只要在程序的大循環體內加一條喂狗指令就行。但這種看門狗功能有限,不是很可靠的,它依靠晶振工作,一旦晶振不起振,就無效了。
實踐中多採用外部看門狗的方法,可以選用的晶元很多:MAX708、MAX813
、X25045.....具體編程就要看晶元的參考資料了。
例如:X25045是SPI匯流排的看門狗晶元,復位端和單片機復位端連接,SPI數據輸入你可以選擇合適的IO介面。
WREN 0x06 設置寫允許位
WRDI 0x04 復位寫允許位
RDSR 0x05 讀狀態寄存器
WRSR 0x01 寫狀態寄存器
READ 0x03/0x0b 讀操作時內部EEPROM頁地址
WRITE 0x02/0x0a 寫操作時內部EEPROM頁地址
#include <reg51.h>
sbit CS= P2^7;
sbit SO= P2^6;
sbit SCK= P2^5;
sbit SI= P2^4;
#define WREN 0x06 //
#define WRDI 0x04 //
#define RDSR 0x05 //
#define WRSR 0x01 //
#define READ0 0x03 //
#define READ1 0x0b //
#define WRITE0 0x02 //
#define WRITE1 0x0a //
#define uchar unsigned char
uchar ReadByte() //read a byte from device
{
bit bData;
uchar ucLoop;
uchar ucData;
for(ucLoop=0;ucLoop<8;ucLoop++)
{
SCK=1;
SCK=0;
bData=SO;
ucData<<=1;
if(bData)
{ ucData|=0x01; }
}
return ucData;
}
void WriteByte(uchar ucData)//write a byte to device
{
uchar ucLoop;
for(ucLoop=0;ucLoop<8;ucLoop++)
{
if((ucData&0x80)==0) //the MSB send first
{SI=0;}
else
{SI=1;}
SCK=0;
SCK=1;
ucData<<=1;
}
}
uchar ReadReg() //read register
{
uchar ucData;
CS=0;
WriteByte(RDSR);
ucData=ReadByte();
CS=1;
return ucData;
}
uchar WriteReg(uchar ucData) //write register
{
uchar ucTemp;
ucTemp=ReadReg();
if((ucTemp&0x01)==1) //the device is busy
return 0;
CS=0;
WriteByte(WREN);//when write the WREN, the cs must have a high level
CS=1;
CS=0;
WriteByte(WRSR);
WriteByte(ucData);
CS=1;
return 1;
}
void WriteEpm(uchar cData,uchar cAddress,bit bRegion)
/* 寫入一個位元組,cData為寫入的數,cAddress為寫入地址,bRegion為頁 */
{
while((ReadReg()&0x01)==1); //the device is busy
CS=0;
WriteByte(WREN); //when write the wren , the cs must have a high level
CS=1;
CS=0;
if(bRegion==0)
{ WriteByte(WRITE0);} //write the page addr
else
{WriteByte(WRITE1);}
WriteByte(cAddress);
WriteByte(cData);
SCK=0; //
CS=1;
}
uchar ReadEpm(uchar cAddress,bit bRegion)
/* 讀入一個位元組,cAddress為讀入地址,bRegion為頁 */
{
uchar cData;
while((ReadReg()&0x01)==1);//the device is busy
CS=0;
if(bRegion==0)
{WriteByte(READ0); }
else
{WriteByte(READ1);}
WriteByte(cAddress);
cData=ReadByte();
CS=1;
return cData;
}
main()
{
WriteReg(0x00);//set the watchdog time as 1.4s
CS=1;
CS=0; //reset the watchdog
}
回復: xumin9514
所有的89S系列都帶狗,所有的80C系列都不帶狗。
所以89S51 89S52都帶狗,80C51、80C52都不帶狗。
Ⅵ 51單片機程序編寫
/*這是用LCD顯示所測溫度的代碼,你參考一下,如果沒問題的話,其他的功能你再添加就好了,不難*/
#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
#define Nack_number 10
//**************埠定義**************************************************
uchar flag; //LCD控制線介面
sbit RS=P1^0; //RS端
sbit RW=P1^1; //讀寫端
sbit LCDE=P2^5; //使能端
//mlx90614埠定義
sbit SCK=P2^1; //時鍾線
sbit SDA=P2^2; //數據線
//************數據定義****************************************************
bdata uchar flag1; //可位定址數據
sbit bit_out=flag1^7;
sbit bit_in=flag1^0;
uchar tempH,tempL,err;
//************************** LCD1602 ***********************************
//向LCD寫入命令或數據*****************************************************
#define LCD_COMMAND 0 //命令
#define LCD_DATA 1 // 數據
#define LCD_CLEAR_SCREEN 0x01 // 清屏
#define LCD_HOMING 0x02 // 游標返回原點
//設置顯示模式******* 0x08+ *********************************************
#define LCD_SHOW 0x04 //顯示開
#define LCD_HIDE 0x00 //顯示關
#define LCD_CURSOR 0x02 //顯示游標
#define LCD_NO_CURSOR 0x00 //無游標
#define LCD_FLASH 0x01 //游標閃動
#define LCD_NO_FLASH 0x00 //游標不閃動
//設置輸入模式********** 0x04+ ********************************************
#define LCD_AC_UP 0x02 //游標右移 AC+
#define LCD_AC_DOWN 0x00 //默認 游標左移 AC-
#define LCD_MOVE 0x01 //畫面可平移
#define LCD_NO_MOVE 0x00 //默認 畫面不移動
//************************** mlx90614 ***********************************
//command mode 命令模式
#define RamAccess 0x00 //對RAM操作
#define EepomAccess 0x20 //對EEPRAM操作
#define Mode 0x60 //進入命令模式
#define ExitMode 0x61 //退出命令模式
#define ReadFlag 0xf0 //讀標志
#define EnterSleep 0xff //進入睡眠模式
//ram address read only RAM地址(只讀)
#define AbmientTempAddr 0x03 //周圍溫度
#define IR1Addr 0x04
#define IR2Addr 0x05
#define LineAbmientTempAddr 0x06 //環境溫度
/*0x0000 0x4074 16500 0.01/單元
-40 125*/
#define LineObj1TempAddr 0x07 //目標溫度,紅外溫度
/*0x27ad-0x7fff 0x3559 22610 0.02/單元
-70.01-382.19 0.01 452.2*/
#define LineObj2TempAddr 0x08
//eepom address EEPROM地址
#define TObjMaxAddr 0x00 //測量范圍上限設定
#define TObjMinAddr 0x01 //測量范圍下限設定
#define PWMCtrlAddr 0x02 //PWM設定
#define TaRangeAddr 0x03 //環境溫度設定
#define KeAddr 0x04 //頻率修正系數
#define ConfigAddr 0x05 //配置寄存器
#define SMbusAddr 0x0e //器件地址設定
#define Reserverd1Addr 0x0f //保留
#define Reserverd2Addr 0x19 //保留
#define ID1Addr 0x1c //ID地址1
#define ID2Addr 0x1d //ID地址2
#define ID3Addr 0x1e //ID地址3
#define ID4Addr 0x1f //ID地址4
//************函數聲明*****************************************************
void start(); //MLX90614發起始位子程序
void stop(); //MLX90614發結束位子程序
uchar ReadByte(void); //MLX90614接收位元組子程序
void send_bit(void); //MLX90614發送位子程序
void SendByte(uchar number); //MLX90614接收位元組子程序
void read_bit(void); //MLX90614接收位子程序
void delay(uint N); //延時程序
uint readtemp(void); //讀溫度數據
void init1602(void); //LCD初始化子程序
void busy(void); //LCD判斷忙子程序
void cmd_wrt(uchar cmd); //LCD寫命令子程序
void dat_wrt(uchar dat); //LCD寫數據子程序
void display(uint Tem); //顯示子程序
void Print(uchar *str); //字元串顯示程序
//*************主函數*******************************************
void main()
{
uint Tem; //溫度變數
SCK=1;
SDA=1;
delay(4);
SCK=0;
delay(1000);
SCK=1;
init1602(); //初始化LCD
while(1)
{
Tem=readtemp(); //讀取溫度
cmd_wrt(0x01); //清屏
Print(" Temperature: "); //顯示字元串 Temperature: 且換行
display(Tem); //顯示溫度
Print(" ^C"); //顯示攝氏度
delay(10000); //延時再讀取溫度顯示
}
}
void Print(uchar *str) //字元串顯示程序
{
while(*str!='