找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1302|回复: 2
收起左侧

PIC16F883做数码管掉电保存,直接调用的内部eep函数,可是没用。改怎么办呢?

[复制链接]
ID:261626 发表于 2017-12-26 14:50 | 显示全部楼层 |阅读模式

#include"pic.h"
__CONFIG(0xe824);
void Init();
void dis(void);
void display();
void ep_write();
void delay(unsigned int ms);
unsigned int flag_254us,value;
unsigned int n;
const unsigned char number[10] ={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //0~9共阳数码管段码表
unsigned char led7;
unsigned char led8;

void delay(unsigned int ms)
{
    unsigned int i,j;
    for(i=0;i<ms;i++)
    for(j=0;j<120;j++);   
}

void Init()
{
    TRISC = 0x00;
    PORTC = 0x7f;//段选口

    TRISA5 = 1;
    ANSEL = 0x00;   

    TRISB = 0x01;
    ANSELH = 0x00;//位选口

    //AD转换
    TRISA1 = 1;
    ANS1 = 1;
    ADCON0 = 0x44;//AN1  f8
    ADCON1 = 0x80;//右对齐,VDD VSS
    ADIE = 0;//中断关


    T2CON = 0x01;//16分频
    PR2 = 250;
    TMR2 = 0;//1*16*250*500ns =254us
    TMR2IE = 1;
    TMR2IF = 0;
    PEIE = 1;//外设中断
    TMR2ON = 1;
}
unsigned int AD_convert()
{
    value = 0;
    ADON = 1;
    GODONE = 1;
    while(!GODONE);
    NOP();
    NOP();
    value = ADRESH;
    value = value << 8;
    value += ADRESL;
    return value;
}
void interrupt t0()
{
    if(TMR2IF)
    {
        TMR2IF = 0;
        flag_254us++;
        if(flag_254us>1000)
        {
            flag_254us = 0;
            n++;
                        if(n==100) n=0;
       }
    }
}

void display(led8,led7)
{
    RB3 = 1;
    PORTC = number[led8];
    delay(1);
    RB3 = 0;
    PORTC = 0x00;

    RB2 = 1;
    PORTC = number[led7];
    delay(1);
    RB2 = 0;
    PORTC = 0x00;

}
void dis(void)
{

    led8 = n/10;
    led7 = n%10;
    display(led8,led7);
}
void main()
{
    GIE = 1;
    TMR2IE = 1;
    Init();
    n=EEPROM_READ(0x05);
    while(1)
    {
        if(AD_convert() > 400)
        {
               
                        dis();

        }
                else//低于4V
                {
                        RB2 = 0;
                        RB3 = 0;//关数码管
                        GIE = 0;//关中断
                        EEPROM_WRITE(0x05,n);//保存数据
    }
}


// This header file should not be included directly
// Inclusion of this file is provided indirectly by including htc.h

/***********************************************************************/
/****** EEPROM memory read/write macros and function definitions *******/
/***********************************************************************/
/* NOTE WELL:

   The macro EEPROM_READ() is NOT safe to use immediately after any
   write to EEPROM, as it does NOT wait for WR to clear.  This is by
   design, to allow minimal code size if a sequence of reads is
   desired.  To guarantee uncorrupted writes, use the function
   eeprom_read() or insert
        while(WR)continue;
   before calling EEPROM_READ().
*/
#if        EEPROM_SIZE > 0

#ifdef        __FLASHTYPE
        // macro versions of EEPROM write and read
#define        EEPROM_WRITE(addr, value) \
do{ \
        while(WR)continue;EEADRL=(addr);EEDATA=(value); \
        EECON1&=0x3F;CARRY=0;if(GIE)CARRY=1;GIE=0; \
        WREN=1;EECON2=0x55;EECON2=0xAA;WR=1;WREN=0; \
        if(CARRY)GIE=1; \
}while(0)
        #define        EEPROM_READ(addr) ((EEADRL=(addr)),(EECON1&=0x3F),(RD=1),EEDATA)
#else        // else doesn't write flash
#define        EEPROM_WRITE(addr, value) \
do{ \
        while(WR)continue;EEADRL=(addr);EEDATA=(value); \
        CARRY=0;if(GIE)CARRY=1;GIE=0; \
        WREN=1;EECON2=0x55;EECON2=0xAA;WR=1;WREN=0; \
        if(CARRY)GIE=1; \
}while(0)
        #define        EEPROM_READ(addr) ((EEADRL=(addr)),(RD=1),EEDATA)
#endif

/* library function versions */
extern void eeprom_write(unsigned char addr, unsigned char value);
extern unsigned char eeprom_read(unsigned char addr);
#endif        // end EEPROM routines



回复

使用道具 举报

ID:261626 发表于 2017-12-27 09:01 | 显示全部楼层
help me
回复

使用道具 举报

ID:138119 发表于 2019-1-22 23:24 | 显示全部楼层
楼主的问题描述的不具体,看代码,应该是小问题。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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