当前位置:首页 > 单片机 > 单片机
[导读]用HI-TECH C写的使用PIC12C508读写93LC46范例程式/************************************************************ * Processer : Microchip PIC12C508 * * Compiler : Hi-TECH PICC 8.00 PL2 *

ce="Verdana">用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 (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))

ce="Verdana">#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;   
    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);
 }

}

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

柏林2025年9月9日 /美通社/ -- 2025年9月5日,纳斯达克上市公司优克联集团(NASDAQ: UCL)旗下全球互联品牌GlocalMe,正式亮相柏林国际消费电子展(IFA 2025),重磅推出融合企...

关键字: LOCAL LM BSP 移动网络

深圳2025年9月9日 /美通社/ -- PART 01活动背景 当技术的锋芒刺穿行业壁垒,万物互联的生态正重塑产业疆域。2025年,物联网产业迈入 "破界创造"与"共生进化" 的裂变时代——AI大模型消融感知边界,...

关键字: BSP 模型 微信 AIOT

"出海无界 商机无限"助力企业构建全球竞争力 深圳2025年9月9日 /美通社/ -- 2025年8月28日, 由领先商业管理媒体世界经理人携手环球资源联合主办、深圳•前海出海e站通协办的...

关键字: 解码 供应链 AI BSP

柏林2025年9月9日 /美通社/ -- 柏林当地时间9月6日,在2025德国柏林国际电子消费品展览会(International Funkausstellung...

关键字: 扫地机器人 耳机 PEN BSP

武汉2025年9月9日 /美通社/ -- 7月24日,2025慧聪跨业品牌巡展——湖北•武汉站在武汉中南花园酒店隆重举办!本次巡展由慧聪安防网、慧聪物联网、慧聪音响灯光网、慧聪LED屏网、慧聪教育网联合主办,吸引了安防、...

关键字: AI 希捷 BSP 平板

上海2025年9月9日 /美通社/ -- 9月8日,移远通信宣布,其自研蓝牙协议栈DynaBlue率先通过蓝牙技术联盟(SIG)BQB 6.1标准认证。作为移远深耕短距离通信...

关键字: 蓝牙协议栈 移远通信 COM BSP

上海2025年9月9日 /美通社/ -- 为全面落实党中央、国务院和上海市委、市政府关于加快发展人力资源服务业的决策部署,更好发挥人力资源服务业赋能百业作用,8月29日,以"AI智领 HR智链 静候你来&quo...

关键字: 智能体 AI BSP 人工智能

北京2025年9月8日 /美通社/ -- 近日,易生支付与一汽出行达成合作,为其自主研发的"旗驭车管"车辆运营管理平台提供全流程支付通道及技术支持。此次合作不仅提升了平台对百余家企业客户的运营管理效率...

关键字: 一汽 智能化 BSP SAAS

深圳2025年9月8日 /美通社/ -- 晶泰科技(2228.HK)今日宣布,由其助力智擎生技制药(PharmaEngine, Inc.)发现的新一代PRMT5抑制剂PEP0...

关键字: 泰科 AI MT BSP

上海2025年9月5日 /美通社/ -- 由上海市经济和信息化委员会、上海市发展和改革委员会、上海市商务委员会、上海市教育委员会、上海市科学技术委员会指导,东浩兰生(集团)有限公司主办,东浩兰生会展集团上海工业商务展览有...

关键字: 电子 BSP 芯片 自动驾驶
关闭