📄 93c46.c
字号:
#include <pic.h>
#define testbit(var,_bit) ((var)&(1<<(_bit))) //测出来的还是一个8位的数值,相应的位和被测数的相应位值一致,其余位全为0
#define setbit(var,_bit) ((var)|=(1<<(_bit))) //
#define clrbit(var,_bit) ((var)&= ~(1<<(_bit))) //
#define uchar unsigned char
#define uint unsigned int
#define CS RB1 //out
#define SK RB2 //out
#define DI RB4 //out
#define DO RB5 //in
#define de 200
uchar temp=0,InData=0; //i=0,temp_p=0;
uint result = 0;
bit buz=0;
void _delay(uchar _temp)
{
while(_temp)
{
_temp--;
for(temp=255;temp!=0;temp--)
asm("nop");
}
}
// Write enable must precede all programming modes.
void Ewen(void) //擦除/写 使能
{
CS=0;
SK=0;
CS=1;
InData=0x98; // 10011XXXX
for(temp=9;temp!=0;temp--)
{ // 9
DI = testbit(InData,7)?1:0;
SK=1; SK=0;
InData<<=1;
}
CS=0;
}
//********************************************************************
// Disables all programming instructions.
void Ewds(void) //擦除/写 禁止
{
CS=0;
SK=0;
CS=1;
InData=0x80; // 10000XXXX
for(temp=9;temp!=0;temp--)
{ // 9
DI = testbit(InData,7)?1:0;
SK=1; SK=0;
InData<<=1;
}
CS=0;
}
//**********************************************************************************
// Reads data stored in memory, at specified address.
uint Read(uchar address) //读
{
Ewen();
SK=0; DI=1; // 110 A5-A0
CS=0; CS=1;
SK=1; SK=0; // 1
address=address&0x3f|0x80;//good code!! see datasheet for details
//six bits are valid
for(temp=8;temp!=0;temp--)
{ // 8
DI = testbit(address,7)?1:0;
SK=1; SK=0;
address<<=1;
}
DO=1;
for(temp=16;temp!=0;temp--)
{ // 16
SK=1;
result=(result<<1)|DO;
SK=0;
}
CS=0;
Ewds();
return(result);
}
//***********************************************************************
// Writes memory location An - A0.
void Write(uchar address,uint _InData) //写
{
Ewen();
SK=0; DI=1; // 101 A5-A0
CS=0; CS=1;
SK=1; SK=0; // 1
address=address&0x3f|0x40;//first bit set 0; second bit set 1; other bits keep unchangable
for(temp=8;temp!=0;temp--)
{ // 8
DI = testbit(address,7)?1:0;
SK=1; SK=0;
address<<=1;
}
for(temp=16;temp!=0;temp--)
{ // 16
DI = testbit(_InData,15)?1:0;
SK=1; SK=0;
_InData<<=1;
}
CS=0; DO=1;
CS=1; SK=1;
while(DO==0)
{ // busy test
SK=0; SK=1;
}
SK=0; CS=0;
//delay(de);
Ewds();
}
//*********************************************************************************
void Erase(uchar address)
{
Ewen();
SK=0; DI=1; // 111 A5-A0
CS=0; CS=1;
SK=1; SK=0; // 1
address|=0xc0;
for(temp=8;temp!=0;temp--)
{ // 8
DI = testbit(address,7)?1:0;
SK=1; SK=0;
address<<=1;
}
CS=0; DO=1;
CS=1; SK=1;
while(DO==0)
{ //busy?
SK=0; SK=1;
}
SK=0; CS=0;
//delay(de);
Ewds();
}
//*****************************************
void Wral(uint _InData)
{
uchar address=0x88; // 10001XXXX
Ewen();
CS=0;
SK=0;
CS=1;
for(temp=9;temp!=0;temp--)
{ // 9
DI = testbit(address,7)?1:0;
SK=1; SK=0;
address<<=1;
}
for(temp=16;temp!=0;temp--)
{ // 16
DI = testbit(_InData,15)?1:0;
SK=1; SK=0;
_InData<<=1;
}
CS=0; DO=1;
CS=1; SK=1;
while(DO==0)
{
SK=0; SK=1;
}
SK=0; CS=0;
//delay(de);
Ewds();
}
//******************************************************************************
void Eral(void)
{
Ewen();
CS=0;
SK=0;
CS=1;
InData=0x90; // 10010XXXX
for(temp=9;temp!=0;temp--)
{ // 9
DI = testbit(InData,7)?1:0;
SK=1; SK=0;
InData<<=1;
}
CS=0; DO=1;
CS=1; SK=1;
while(DO==0)
{
SK=0; SK=1;
}
SK=0; CS=0;
//delay(de);
Ewds();
}
//******************************************************************************
void main(void)
{
TRISB = 0;
setbit(TRISB,5);
clrbit(TRISC,6);
PORTC = 0;
Write(0x20,20);
if(Read(0x20) == 20)
buz = 1;
else
buz = 0;
while(1)
{
if(buz == 1)
RC6 =!RC6;
else
RC6 = 1;
_delay(255);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -