DIY编程器网

 找回密码
 注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 193|回复: 0
打印 上一主题 下一主题

用HI-TECH C写的使用PIC12C508读写93LC4

[复制链接]
跳转到指定楼层
楼主
发表于 2011-4-30 10:45:14 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
用HI-TECH C写的使用PIC12C508读写93LC46范例程式
/************************************************************
* 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 (10) 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>= 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>= 1;
}
temp = 0x8000; /* DDDDDDDDDDDDDDDD(16bits data)*/
for(i=0; i>= 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>= 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;
CS = 0;
CLK = 0;
DI = 0;
}
/* Main routine */
void main(void)
{
unsigned char addr;
unsigned int rx_buf;

InitPIC();
/* Read a word then +1 and write back to 93LC46 */
for (addr = 0; addr < 10; addr++)
{
rx_buf = Read93LC46(addr);
rx_buf = rx_buf+1;
Write93LC46(addr, rx_buf);
}
}
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|文字版|手机版|DIY编程器网 ( 桂ICP备14005565号-1 )

GMT+8, 2024-6-6 10:15 , 耗时 0.090331 秒, 19 个查询请求 , Gzip 开启.

各位嘉宾言论仅代表个人观点,非属DIY编程器网立场。

桂公网安备 45031202000115号

DIY编程器群(超员):41210778 DIY编程器

DIY编程器群1(满员):3044634 DIY编程器1

diy编程器群2:551025008 diy编程器群2

QQ:28000622;Email:libyoufer@sina.com

本站由桂林市临桂区技兴电子商务经营部独家赞助。旨在技术交流,请自觉遵守国家法律法规,一旦发现将做封号删号处理。

快速回复 返回顶部 返回列表