历史上的今天
今天是:2024年10月21日(星期一)
2019年10月21日 | 用HI-TECH C写的使用PIC12C508单片机读写93LC46范例程式
2019-10-21 来源:eefocus
/************************************************************
* Processer : Microchip PIC12C508 *
* Compiler : Hi-TECH PICC 8.00 PL2 *
* Writer : Jason Kuo *
* Description : It can read/write 93LC46 (64 x 16-bit organization) *
*************************************************************/
static volatile unsigned char RTCC @ 0x01;
static volatile unsigned char TMR0 @ 0x01;
static volatile unsigned char PCL @ 0x02;
static volatile unsigned char STATUS @ 0x03;
static unsigned char FSR @ 0x04;
static volatile unsigned char OSCCAL @ 0x05;
static volatile unsigned char GPIO @ 0x06;
static unsigned char control OPTION @ 0x00;
static volatile unsigned char control TRIS @ 0x06;
/* STATUS bits */
static bit GPWUF @ (unsigned)&STATUS*8+7;
static bit PA0 @ (unsigned)&STATUS*8+5;
static bit TO @ (unsigned)&STATUS*8+4;
static bit PD @ (unsigned)&STATUS*8+3;
static bit ZERO @ (unsigned)&STATUS*8+2;
static bit DC @ (unsigned)&STATUS*8+1;
static bit CARRY @ (unsigned)&STATUS*8+0;
/* OPTION bits */
#define GPWU (1<<7)
#define GPPU (1<<6)
#define T0CS (1<<5)
#define T0SE (1<<4)
#define PSA (1<<3)
#define PS2 (1<<2)
#define PS1 (1<<1)
#define PS0 (1<<0)
/* OSCCAL bits */
static volatile bit CAL3 @ (unsigned)&OSCCAL*8+7;
static volatile bit CAL2 @ (unsigned)&OSCCAL*8+6;
static volatile bit CAL1 @ (unsigned)&OSCCAL*8+5;
static volatile bit CAL0 @ (unsigned)&OSCCAL*8+4;
static volatile bit GP5 @ (unsigned)&GPIO*8+5;
static volatile bit GP4 @ (unsigned)&GPIO*8+4;
static volatile bit GP3 @ (unsigned)&GPIO*8+3;
static volatile bit GP2 @ (unsigned)&GPIO*8+2;
static volatile bit GP1 @ (unsigned)&GPIO*8+1;
static volatile bit GP0 @ (unsigned)&GPIO*8+0;
#define CONFIG_ADDR 0xFFF
/* code protection */
#define MCLREN 0xFFFF // memory clear enable
#define MCLRDIS 0xFFEF // memory clear disable
/*watchdog*/
#define WDTEN 0xFFFF // watchdog timer enable
#define WDTDIS 0xFFFB // watchdog timer disable
/* code protection */
#define PROTECT 0xFFF7 // protect the program code
#define UNPROTECT 0xFFFF // do not protect the program code
/*osc configurations*/
#define EXTRC 0xFFFF // external resistor/capacitor
#define INTRC 0xFFFE // internal
#define XT 0xFFFD // crystal/resonator
#define LP 0xFFFC // low power crystal/resonator
/* 93LC46 I/O pin define */
#define CS GP0 //Chip Select
#define CLK GP1 //Serial Data Clock
#define DI GP2 //Serial Data Input
#define DO GP4 //Serial Data Output
void Delay(unsigned int counter);
void Pulse(void);
void StartBit(void);
void EWEN(void);
void EWDS(void);
extern void Write93LC46(unsigned char Offset_Addr, unsigned int tx_data);
extern unsigned int Read93LC46(unsigned char Offset_Addr);
void InitPIC(void);
#define CLRWDT() asm(" clrwdt")
#define SLEEP() asm(" sleep")
#define ___mkstr1(x) #x
#define ___mkstr(x) ___mkstr1(x)
#define __CONFIG(x) asm("tpsect config,class=CONFIG,delta=2");
asm("tglobaltconfig_word");
asm("config_word");
asm("tdw "___mkstr(x))
#define __IDLOC(w) asm("tpsect idloc,class=IDLOC,delta=2");
asm("tglobaltidloc_word");
asm("idloc_word");
asm("tirpct__arg," ___mkstr(w));
asm("tdw 0&__arg&h");
asm("tendm")
__CONFIG(MCLRDIS & WDTDIS & EXTRC & PROTECT);
/*----------------------------------------------------
Function : Delay
Input : unsigned int (counter)
Output : None
Description : Delay routine
if counter=1 delay 35us , if counter=10 delay 134us,
if counter=100 delay 1.12ms,
These delay is base on internal 4MHz
------------------------------------------------------*/
void Delay(unsigned int counter)
{
while(counter>0) counter--;
}
/*----------------------------------------------------
Function : Pulse
Input : None
Output : None
Description : Send a pulse (10) to Serial Data Clock(CLK)
------------------------------------------------------*/
void Pulse(void)
{
CLK = 1;
Delay(25);
CLK = 0;
}
/*----------------------------------------------------
Function : StartBit
Input : None
Output : None
Description :
1. Set Chip Select(CS) = 1 (high)
2. Set a Start Bit(1) to Serial Data Input(DI)
------------------------------------------------------*/
void StartBit(void)
{
CS = 1;
DI = 1;
Pulse();
}
/*----------------------------------------------------
Function : EWEN
Input : None
Output : None
Description : ERASE/WRITE Enable
------------------------------------------------------*/
void EWEN(void)
{
unsigned char i,temp;
StartBit(); /* 1 */
temp = 0x80; /* 0011xxxx ,(opcode:00, Address:11xxxx) */
for(i=0; i<8; i++) {
if(0x30 & temp)
DI = 1;
else
DI = 0;
Pulse();
temp >>= 1;
}
CS = 0;
}
/*----------------------------------------------------
Function : EWDS
Input : None
Output : None
Description : ERASE/WRITE Disable
------------------------------------------------------*/
void EWDS(void)
{
unsigned char i;
StartBit(); /* 1 */
DI = 0; /* 0000xxxx, (opcode:00, Address:00xxxx) */
for(i=0; i<8; i++)
Pulse();
CS = 0;
}
/*----------------------------------------------------
Function : Write93LC46
Input : unsigned char Offset Address, unsigned int tx_data
Output : None
Description : Write 16bits data in to 93LC46 Offset Address
------------------------------------------------------*/
void Write93LC46(unsigned char Offset_Addr, unsigned int tx_data)
{
unsigned char Addr, i;
unsigned int temp;
EWEN();
StartBit(); /* 1 */
Offset_Addr=Offset_Addr&0x3F; /* 6bits address */
Addr = Offset_Addr + 0x40; /* 01AAAAAA ,(opcode:01, address:AAAAAA) */
temp = 0x0080;
for(i=0; i<8; i++) {
if(Addr & temp)
DI = 1;
else
DI = 0;
Pulse();
temp >>= 1;
}
temp = 0x8000; /* DDDDDDDDDDDDDDDD(16bits data)*/
for(i=0; i<16; i++) {
if(tx_data & temp)
DI = 1;
else
DI = 0;
Pulse();
temp >>= 1;
}
CS = 0;
EWDS();
}
/*----------------------------------------------------
Function : Read93LC46
Input : unsigned char Offset Address
Output : unsigned int
Description : Read 16bits data from 93LC46 offset address
------------------------------------------------------*/
unsigned int Read93LC46(unsigned char Offset_Addr)
{
unsigned char Addr, i, temp;
unsigned int rx_data;
StartBit(); /* 1 */
Offset_Addr = Offset_Addr&0x3F; /* 6bits address */
Addr = Offset_Addr + 0x80; /* 10AAAAAA ,(opcode:10, address:AAAAAA) */
temp = 0x80;
for(i=0; i<8; i++) {
if(Addr & temp)
DI = 1;
else
DI = 0;
Pulse();
temp >>= 1;
}
rx_data = 0x0000; /* DDDDDDDDDDDDDDDD(16bits data)*/
for(i=0; i<16; i++) {
Pulse();
if(DO)
rx_data |= 0x0001;
if(i < 15)
rx_data <<= 1;
}
CS = 0;
return(rx_data);
}
void InitPIC(void)
{
OPTION = (GPWU | GPPU | PS2 | PS1 | PS0);
TRIS = 0x10;
史海拾趣
|
选购摄像机总是有人存在一些误区,比如洋品牌就好,还有价格要越低越好,下面我就说说真正需要注意的几点是什么吧! 第一、经济性 当前安防产品同质化很严重,进口的与国产的相差无几,所以朋友们不要盲目的崇拜洋 ...… 查看全部问答> |
|
现在我只有一个NK.BIN文件,想了解怎样才能把它下载至2440的开发板…我现在可以下载NK.NB0文件,但是不了解怎样下载NK.BIN文件,希望了解的大拿给指点指点…… 查看全部问答> |
|
WinCE的explorer.exe不启动直接启动自己的应用程序会有什么样的影响? 提一个关于WinCE启动的问题,在很多地方看到WinCE不启动explorer.exe,用自定义的应用程序替换,我有几个问题想寻求解答: 1、explorer.exe的完整的功能是哪些? 2、如果不启动这个功能,在VS2005下写的程序(基于compact Frame work)还能不能用 ...… 查看全部问答> |
|
这个图形加速器的驱动TI给出了现成的module,基于Linux的 里边基本全了,module源文件,app-test,看看README就都明白啦 还没尝试过POWERVR的杀伤力到底有多强 不知道SEED提供的底板是否有LCD,如果有就基本完美啦,否则还得自己想办法弄L ...… 查看全部问答> |
|
我使用MDK3.22,首先已按照lut1lut的帖子修改了汇编代码。数据源参考了圈圈的“FFT结果的物理意义”中的数据,我把圈圈的256个数据*1024然后作的四舍五入取整。结果与圈圈文章中分析的基本一致,第0点、50点、75点数据比较大,此三点数据为( ...… 查看全部问答> |
|
stm32串口1,2,3,4为什么1,2可以,3,4就不行啊。 USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure. ...… 查看全部问答> |




