?? x5043.c
字號(hào):
#include <intrins.h>
#include "STC12C5410.H"
#include "headfile1.c"
//5043的操作注意事項(xiàng)
/*
1. 任何寫(xiě)操作之前必須執(zhí)行WREN指令,該指令將寫(xiě)允許鎖存位WEL置位,WEL在上電后,
一個(gè)寫(xiě)周期,及WP被拉低時(shí)都將復(fù)位
2. 在寫(xiě)入WREN和WESR指令之間,CS上必須有LOW-HIGH的跳變,否則WRSR指令將會(huì)被忽略。
WEL和WP的任意一個(gè)有效時(shí),所有的MEMORY BLOCK和 STATUS REGISTER 都將被保護(hù)而
當(dāng)兩位都無(wú)效時(shí),只有由BL決定的保護(hù)區(qū)被寫(xiě)保護(hù)。
3. CS片選后,依次寫(xiě)入READ指令以及要讀的ADDRESS,連續(xù)的讀操作將會(huì)使地址轉(zhuǎn)向下一
個(gè)更高的地址,讀到最高地址后將會(huì)翻轉(zhuǎn)到000X,將CS置位將會(huì)結(jié)束讀操作
4. 寫(xiě)操作之前,首先復(fù)位CS,用WREN指令置位WEL,置位CS。后再將CS復(fù)位,依次寫(xiě)入
WRITE指令,address,以及 DATA ( WRITE 指令的BIT.3代表address的BIT.8 ),可以
執(zhí)行連續(xù)寫(xiě)操作,但是所有的寫(xiě)入ADDRESS只能在一頁(yè)中,寫(xiě)操作執(zhí)行過(guò)程中CS的置
位將導(dǎo)致寫(xiě)操作中斷
5. 上電后,在開(kāi)始任何操作之前,都必須使CS引腳上有一個(gè)HIGH-LOW的變化,相當(dāng)預(yù)先
執(zhí)行一個(gè)喂狗操作
*/
//------------------//
bit flow;
//------------------//
//5043的操作指令
#define WREN 0X06 //寫(xiě)允許
#define WRDI 0X04 //寫(xiě)禁止
#define RSDR 0X05 //讀狀態(tài)寄存器
#define WRSR 0X01 //寫(xiě)狀態(tài)寄存器
#define READ 0X03 //讀操作
#define WRITE 0X02 //寫(xiě)操作
sbit CS=P1^2; //片選端,低電平有效
sbit SCK=P1^3; //時(shí)鐘信號(hào) The Serial Clock controls the serial bus timing for data input and output
sbit SI=P1^4; //數(shù)據(jù)輸入端 Data is latched by the rising edge of the serial clock.
sbit SO=P1^5; //數(shù)據(jù)輸出端 During a read cycle, data is shifted out on this pin.
// Data is clocked out by the falling edge of the serial clock.
sbit WP=P1^6; //寫(xiě)保護(hù),低電平有效 When WP is low, nonvolatile writes to the X5043/45 are
//disabled, but the part otherwise functions normally.When WP is held high,
//all functions, including non volatile writes operate normally
//us級(jí)延時(shí)函數(shù)delay_us(BYTE),仿真結(jié)果為每一循環(huán)3us(11.0592MHZ,STC12C5410)
static void delay_us(BYTE timeout)
{
volatile BYTE del;
for(del=timeout;del>0;del--)
{
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
}
//ms級(jí)延時(shí)函數(shù)
static void delay_ms(WORD timeout)
{
volatile WORD del;
for(del=timeout;del>0;del--)
{
delay_us(166); //1/2ms
_WDT_();
}
}
static void open(void) //片選及關(guān)閉寫(xiě)保護(hù)
{
SI = 0;
SO = 1;
SCK= 0;
CS = 0;
WP = 1;
}
static void close(void) //禁止片選,打開(kāi)寫(xiě)保護(hù)
{
SI = 0;
SO = 1;
SCK= 0;
CS = 1;
WP = 0;
}
static void outbyte(BYTE val) //輸出字節(jié)
{
BYTE over;
for (over=8;over>0;over--)//8次循環(huán),輸出一個(gè)字節(jié)
{
SCK = 0; //時(shí)鐘低電平
if(val&0x80) SI = 1;
else SI = 0;
delay_us(0x05); //延時(shí),等待數(shù)據(jù)穩(wěn)定
SCK = 1; //數(shù)據(jù)輸出
delay_us(0x05); //延時(shí)
val <<= 1; //輸出下一位
}
SI = 0;
SCK = 0;
delay_us(0x10);
}
static BYTE inbyte(void) //輸入字節(jié)
{
BYTE temp, val=0;
for(temp=8;temp>0;temp--) //8次循環(huán),完成字節(jié)讀入
{
if(SO) val |= 0x01;
else val &= 0x0FE; //讀入下一位,當(dāng)addr的最后一位寫(xiě)完的falling edge,數(shù)據(jù)已經(jīng)
//開(kāi)始輸出,所以進(jìn)入后直接先移入
SCK = 1;
delay_us(0x02);
SCK = 0; //讀數(shù)據(jù)時(shí)鐘
delay_us(0x02);
if(temp>1) val <<= 1;
}
SCK = 0;
delay_us(0x10);
return val; //返回讀入值
}
void start_5043 (void)
{
//It should be noted that after power-up,a high to low transition
// on CS is required prior to the start of any operation
CS = 1;
delay_us(10);
CS = 0;
open(); //片選,關(guān)閉寫(xiě)保護(hù)
outbyte(WREN);
CS = 1;
delay_us(10); //WREN與WRSR之間CS拉高
CS = 0;
outbyte(WRSR); //寫(xiě)狀態(tài)寄存器
outbyte(0x30);
delay_ms(20); //寫(xiě)操作延時(shí)
close();
}
BYTE read(BYTE addr) //從5043讀出單字節(jié)數(shù)據(jù)
{
BYTE temp;
open();
outbyte(READ); //寫(xiě)入讀指令
outbyte(addr); //寫(xiě)入讀起始地址
temp = inbyte(); //讀數(shù)據(jù)
close();
delay_ms(20);
return temp;
}
void save_to_5043(BYTE addr,BYTE val) //向5043寫(xiě)入單字節(jié)數(shù)據(jù)
{
open(); //寫(xiě)允許
outbyte(WREN);
CS = 1;
delay_us(10);
CS = 0;
//If CS does not go HIGH between WREN and WRITE, the WRITE
//instruction is ignored
outbyte(WRITE); //寫(xiě)入寫(xiě)操作命令
outbyte(addr); //寫(xiě)入寫(xiě)操作地址
outbyte(val); //寫(xiě)入數(shù)據(jù)
delay_ms(20); //此處不做對(duì)WIP的判斷,直接做延時(shí)處理
close(); //寫(xiě)操作禁止
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -