当前位置:首页 > 单片机 > 单片机
[导读]用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);
 }

}

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

上海2024年5月14日 /美通社/ -- 固特异 SightLine 智能轮胎技术解决方案荣获中国电子行业主流媒体《中国电子报》颁发的 2024 汽车芯片优秀产品奖。本次获奖是对固特异研发成果的高度认可。固特异致力于引...

关键字: 汽车芯片 轮胎 BSP SI

慕尼黑2024年5月9日 /美通社/ -- TÜV南德意志集团(以下简称"TÜV南德")持续保障安全、可靠及可持续发展。作为全球化的服务提供商,TÜV南德2023年全年营收达约31亿欧元,首次突破30亿欧元大关,同比增长...

关键字: BSP 可持续发展 数字化 人工智能

凭借深度学习技术和SmartBid产品,百度国际MediaGo获得美国商业奖认可 旧金山2024年5月6日 /美通社/ -- 第22届美国商业奖(American Business Award®)近日发布获奖名...

关键字: MEDIA GO SMART BSP

上海2024年4月17日 /美通社/ -- 在2024 F1中国站即将拉开帷幕之际,高端全合成润滑油品牌美孚1号今日举办了品牌50周年庆祝活动。三届F1年度车手总冠军马克斯•维斯塔潘也亲临现场,共同庆祝这一里程...

关键字: BSP 汽车制造 行业标准 产品系列

北京2024年4月17日 /美通社/ -- 2024年4月13日,由北京康盟慈善基金会主办的"县域诊疗,规范同行"——肿瘤诊疗学术巡讲项目首站在广州隆重召开。本次会议邀请全国多位肺癌领域专家和县域同道...

关键字: AI技术 医疗服务 BSP 互联网

海口2024年4月16日 /美通社/ -- 4月14日,在中法建交60周年之际,科学护肤先锋品牌Galenic法国科兰黎受邀入驻第四届中国国际消费品博览会(以下简称"消博会")法国馆。Galenic法...

关键字: NI IC BSP ACTIVE

上海2024年4月17日 /美通社/ -- 每年4月17日是世界血友病日。今年,世界血友病日以"认识出血性疾病,积极预防和治疗"为主题,呼吁关注所有出血性疾病,提升科学认知,提高规范化诊疗水平,让每一位出血性疾病患者享有...

关键字: VII 动力学 软件 BSP

伦敦2024年4月16日 /美通社/ -- ATFX宣布任命Siju Daniel为首席商务官。Siju在金融服务行业拥有丰富的经验和专业知识,曾在全球各地的高管职位上工作了19年以上。Siju之前担任FXCM首席商务官...

关键字: NI AN SI BSP

全球领先的科技公司默克推出了同类产品中首个经验证的全新一体化遗传稳定性分析。 Aptegra™ CHO遗传稳定性检测利用全基因组测序和生物信息学,显著加快了客户的生物医药安全性测试,从而帮助加快了客户进入商业生产的步伐。...

关键字: 稳定性分析 BSP 人工智能
关闭
关闭