新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > 基于HI-TECH C的PIC12C508讀寫93LC4程序

        基于HI-TECH C的PIC12C508讀寫93LC4程序

        作者: 時間:2012-07-07 來源:網絡 收藏

        C寫的使用6范例程式

        本文引用地址:http://www.104case.com/article/171195.htm

        /************************************************************
        * Processer : Microchip *
        * Compiler : Hi-TECH PICC 8.00 PL2 *
        * Writer : Jason Kuo *
        * Description : It can read/write 6 (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 (17)
        #define GPPU (16)
        #define T0CS (15)
        #define T0SE (14)
        #define PSA (13)
        #define PS2 (12)
        #define PS1 (11)
        #define PS0 (10)

        /* 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

        /* 6 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__argh);
        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();
        }


        上一頁 1 2 下一頁

        評論


        相關推薦

        技術專區

        關閉
        主站蜘蛛池模板: 堆龙德庆县| 姜堰市| 南川市| 黄大仙区| 绥江县| 华蓥市| 三明市| 庆城县| 额济纳旗| 内江市| 平山县| 新竹市| 珲春市| 新密市| 恩平市| 株洲市| 垦利县| 明溪县| 顺平县| 基隆市| 桐梓县| 临海市| 伊通| 罗山县| 元江| 眉山市| 友谊县| 和静县| 金寨县| 晋宁县| 齐齐哈尔市| 阜城县| 神农架林区| 牙克石市| 汕头市| 巴塘县| 涞水县| 高邮市| 江山市| 濮阳县| 武平县|