This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

programming about RTC

I am going to do a project with 8052,DS89C450 and RTC CDP68HC68T1 which is communication with SPI. and i am going to program LCD can show the calendar and time and even from keypad we can change time and date. however i am really a freshman with this device at all especially the programming about SPI. can anybody help me?

Parents
  • now i am programming some, but the problem is that the time shown on LCD cannot run by itself and the shown time is not match what i set in initial clock time in programming. i dun know why it happen and hw it happen
    can i send you my program to have a check?

    program:
    #include<DS89C4xx.h>
    #include<stdio.h>
    #include<intrins.h> //keil library (is used for _nop_()_operation)
    #include<string.h> //strlen()function
    #include<absacc.h> //XBYTE function

    #define BYTE unsigned char
    #define SECONDS 0x20
    #define MINUTES 0x21
    #define HOURS 0x22
    #define DAYOFWEEK 0x23
    #define DATE 0x24
    #define MONTH 0x25
    #define YEARS 0x26
    #define SEC_ALARM 0x28 //set second to alarm
    #define MIN_ALARM 0x29 //set minute to alarm
    #define HRS_ALARM 0x2A //set hour to alarm
    #define STATUS_REG 0x30 //status register
    #define CONTROL_REG 0x31 //control register
    #define INTERRUPT 0x32 //interrupt Control Register

    sbit SCK = P1^7; //Serial Clock
    sbit MOSI = P3^4; //master output, slave input
    sbit MISO = P3^5; //master input, slave output
    sbit CS2 = P1^1; //chip enable high RTC

    #define PortA 0x4000
    #define PortB 0x4001
    #define PortC 0x4002
    #define Control_Register 0x4003

    #define TurnOn 0x0C //set the display
    #define setfunction 0x38 //8-bit bus & 2 line display and 5x8 dots format display mode
    #define clr 0x01 //clear all the display data
    #define reHome 0x02 //return cursor to original site and return display to original status
    #define Inc 0x06
    #define IncMode 0x07
    #define goline2 0xC0

    //===============================================================================//
    //*SPI Operation for Active Power*//
    //===============================================================================//

    unsigned long Read_Data;
    void s_delay(int num);
    void Init_SPI(void);
    void SPI_Out(BYTE b);
    BYTE SPI_In(void);
    void Write_SPI(BYTE b);
    void Read_SPI (BYTE b);
    void Delay(int count);
    void Init_Uart();
    void Uart_Tx1(BYTE c);
    void LCD_WriteInstructionReg(unsigned char Instruction);
    void LCD_WriteDataReg(unsigned char Value);
    void InitialiseLCD(void);
    void InitialiseLCD2(void);
    void LCD_DisplayString(char*String);
    BYTE tc,hh;
    void Init_Clock(void);

    void main(void)
    { BYTE sec, min, hrs; BYTE xdata s[50]; int i; PMR = PMR | 0x81; Delay(100); Init_Uart(); Init_Clock(); //Edge Trigger while(1) { CS2 = 1; SPI_Out(SECONDS); sec = SPI_In(); min = SPI_In(); hrs = SPI_In();

    InitialiseLCD2(); sprintf(s, "Time:%02x:%02x:%02x \0", (int)hrs, (int)min, (int)sec); LCD_DisplayString(s); for(i=0; i<strlen(s); i++) Uart_Tx1(s[i]); CS2 = 0; Delay(750); }
    }

    void s_delay(int num)
    { int i; for(i=0; i<num; i++) _nop_();
    } /*The master sets a bit on the MOSI and then generates a clock pulse, after which the next bit is set and another clock pulse is generated, etc.
    A clock pulse is generated by simply setting the SCK bit high and then low again after a few microseconds.
    */
    void SPI_Out(BYTE b)
    { int i; SCK = 0; for(i=7; i>0; i--) { if(b&(1<<i)) MOSI = 1; else MOSI = 0; _nop_(); _nop_(); SCK = 1; _nop_(); _nop_(); _nop_(); SCK = 0; _nop_(); _nop_(); }
    }

    BYTE SPI_In(void)
    { int i; BYTE b = 0; SCK = 0; for(i=7; i>=0; i--) { SCK = 1; _nop_(); _nop_(); if(MISO) b |= 1<<i; _nop_(); _nop_(); _nop_(); SCK = 0; _nop_(); _nop_(); } return(b);
    }

    //===============================================================================================
    /*Serial Port Initialization*/

    void Delay(int count)
    { int i, j; for(i=0; i<count; i++) { for(j=0; j<1200; j++); }
    }

    void Init_Uart()
    //------------------------------------------------------------------------------------------------
    //19200 bps @11.059MHz
    { PCON |= 0x80; //Set SMOD
    SCON1 = 0x50; //8-bit,rx enable
    TL1 = 0x00; //Lower Byte
    TH1 = 0xFD; //Upper Byte
    TR1 = 1; //Run timer //Baud Rate: 19200 bps
    }

    void Uart_Tx1(BYTE c)
    { while(TI_1==0); TI_1 = 0; SBUF1 = c;
    } //---------------------------------------------------------------------------------

    void LCD_WriteInstructionReg(unsigned char Instruction)
    { XBYTE[PortB] = 0x00; XBYTE[PortB] = 0x04; Delay(20); XBYTE[PortA] = Instruction; XBYTE[PortB] = 0x00;
    }

    void LCD_WriteDataReg(unsigned char Value)
    { XBYTE[PortB] = 0x01; XBYTE[PortB] = 0x05; Delay(20); XBYTE[PortA] = Value; XBYTE[PortB] = 0x00;
    }

    void InitialiseLCD(void)
    { XBYTE[Control_Register] = 0x81; LCD_WriteInstructionReg(TurnOn); //TurnOn display, No Blink LCD_WriteInstructionReg(setfunction); //8bits bus mode, 2line display, 5x8dots format LCD_WriteInstructionReg(clr); LCD_WriteInstructionReg(IncMode); //Incmode LCD_WriteInstructionReg(Inc); //Inc LCD_WriteInstructionReg(reHome); //return home
    }

    void InitialiseLCD2(void)
    { XBYTE[Control_Register] = 0x81; LCD_WriteInstructionReg(TurnOn); //turn on display, no blink LCD_WriteInstructionReg(setfunction); //8bits bus mode, 2line display, 5x8dots format LCD_WriteInstructionReg(IncMode); //IncMode LCD_WriteInstructionReg(Inc); //Inc LCD_WriteInstructionReg(reHome); //return home
    }

    void LCD_DisplayString(char*String)
    { int k=0; while(String[k]) { LCD_WriteDataReg(String[k++]); }
    }

    //===========================================================================================================

    void Init_Clock(void)
    { CS2 = 1; SPI_Out(SECONDS); SPI_Out(0x30); CS2 = 0; _nop_(); _nop_();

    CS2 = 1; SPI_Out(MINUTES); SPI_Out(0x10); CS2 = 0; _nop_(); _nop_();

    CS2 = 1; SPI_Out(HOURS); SPI_Out(0x01); CS2 = 0; _nop_(); _nop_();

    CS2 = 1; SPI_Out(CONTROL_REG); SPI_Out(0x85); CS2 = 0; _nop_(); _nop_();

    }

Reply
  • now i am programming some, but the problem is that the time shown on LCD cannot run by itself and the shown time is not match what i set in initial clock time in programming. i dun know why it happen and hw it happen
    can i send you my program to have a check?

    program:
    #include<DS89C4xx.h>
    #include<stdio.h>
    #include<intrins.h> //keil library (is used for _nop_()_operation)
    #include<string.h> //strlen()function
    #include<absacc.h> //XBYTE function

    #define BYTE unsigned char
    #define SECONDS 0x20
    #define MINUTES 0x21
    #define HOURS 0x22
    #define DAYOFWEEK 0x23
    #define DATE 0x24
    #define MONTH 0x25
    #define YEARS 0x26
    #define SEC_ALARM 0x28 //set second to alarm
    #define MIN_ALARM 0x29 //set minute to alarm
    #define HRS_ALARM 0x2A //set hour to alarm
    #define STATUS_REG 0x30 //status register
    #define CONTROL_REG 0x31 //control register
    #define INTERRUPT 0x32 //interrupt Control Register

    sbit SCK = P1^7; //Serial Clock
    sbit MOSI = P3^4; //master output, slave input
    sbit MISO = P3^5; //master input, slave output
    sbit CS2 = P1^1; //chip enable high RTC

    #define PortA 0x4000
    #define PortB 0x4001
    #define PortC 0x4002
    #define Control_Register 0x4003

    #define TurnOn 0x0C //set the display
    #define setfunction 0x38 //8-bit bus & 2 line display and 5x8 dots format display mode
    #define clr 0x01 //clear all the display data
    #define reHome 0x02 //return cursor to original site and return display to original status
    #define Inc 0x06
    #define IncMode 0x07
    #define goline2 0xC0

    //===============================================================================//
    //*SPI Operation for Active Power*//
    //===============================================================================//

    unsigned long Read_Data;
    void s_delay(int num);
    void Init_SPI(void);
    void SPI_Out(BYTE b);
    BYTE SPI_In(void);
    void Write_SPI(BYTE b);
    void Read_SPI (BYTE b);
    void Delay(int count);
    void Init_Uart();
    void Uart_Tx1(BYTE c);
    void LCD_WriteInstructionReg(unsigned char Instruction);
    void LCD_WriteDataReg(unsigned char Value);
    void InitialiseLCD(void);
    void InitialiseLCD2(void);
    void LCD_DisplayString(char*String);
    BYTE tc,hh;
    void Init_Clock(void);

    void main(void)
    { BYTE sec, min, hrs; BYTE xdata s[50]; int i; PMR = PMR | 0x81; Delay(100); Init_Uart(); Init_Clock(); //Edge Trigger while(1) { CS2 = 1; SPI_Out(SECONDS); sec = SPI_In(); min = SPI_In(); hrs = SPI_In();

    InitialiseLCD2(); sprintf(s, "Time:%02x:%02x:%02x \0", (int)hrs, (int)min, (int)sec); LCD_DisplayString(s); for(i=0; i<strlen(s); i++) Uart_Tx1(s[i]); CS2 = 0; Delay(750); }
    }

    void s_delay(int num)
    { int i; for(i=0; i<num; i++) _nop_();
    } /*The master sets a bit on the MOSI and then generates a clock pulse, after which the next bit is set and another clock pulse is generated, etc.
    A clock pulse is generated by simply setting the SCK bit high and then low again after a few microseconds.
    */
    void SPI_Out(BYTE b)
    { int i; SCK = 0; for(i=7; i>0; i--) { if(b&(1<<i)) MOSI = 1; else MOSI = 0; _nop_(); _nop_(); SCK = 1; _nop_(); _nop_(); _nop_(); SCK = 0; _nop_(); _nop_(); }
    }

    BYTE SPI_In(void)
    { int i; BYTE b = 0; SCK = 0; for(i=7; i>=0; i--) { SCK = 1; _nop_(); _nop_(); if(MISO) b |= 1<<i; _nop_(); _nop_(); _nop_(); SCK = 0; _nop_(); _nop_(); } return(b);
    }

    //===============================================================================================
    /*Serial Port Initialization*/

    void Delay(int count)
    { int i, j; for(i=0; i<count; i++) { for(j=0; j<1200; j++); }
    }

    void Init_Uart()
    //------------------------------------------------------------------------------------------------
    //19200 bps @11.059MHz
    { PCON |= 0x80; //Set SMOD
    SCON1 = 0x50; //8-bit,rx enable
    TL1 = 0x00; //Lower Byte
    TH1 = 0xFD; //Upper Byte
    TR1 = 1; //Run timer //Baud Rate: 19200 bps
    }

    void Uart_Tx1(BYTE c)
    { while(TI_1==0); TI_1 = 0; SBUF1 = c;
    } //---------------------------------------------------------------------------------

    void LCD_WriteInstructionReg(unsigned char Instruction)
    { XBYTE[PortB] = 0x00; XBYTE[PortB] = 0x04; Delay(20); XBYTE[PortA] = Instruction; XBYTE[PortB] = 0x00;
    }

    void LCD_WriteDataReg(unsigned char Value)
    { XBYTE[PortB] = 0x01; XBYTE[PortB] = 0x05; Delay(20); XBYTE[PortA] = Value; XBYTE[PortB] = 0x00;
    }

    void InitialiseLCD(void)
    { XBYTE[Control_Register] = 0x81; LCD_WriteInstructionReg(TurnOn); //TurnOn display, No Blink LCD_WriteInstructionReg(setfunction); //8bits bus mode, 2line display, 5x8dots format LCD_WriteInstructionReg(clr); LCD_WriteInstructionReg(IncMode); //Incmode LCD_WriteInstructionReg(Inc); //Inc LCD_WriteInstructionReg(reHome); //return home
    }

    void InitialiseLCD2(void)
    { XBYTE[Control_Register] = 0x81; LCD_WriteInstructionReg(TurnOn); //turn on display, no blink LCD_WriteInstructionReg(setfunction); //8bits bus mode, 2line display, 5x8dots format LCD_WriteInstructionReg(IncMode); //IncMode LCD_WriteInstructionReg(Inc); //Inc LCD_WriteInstructionReg(reHome); //return home
    }

    void LCD_DisplayString(char*String)
    { int k=0; while(String[k]) { LCD_WriteDataReg(String[k++]); }
    }

    //===========================================================================================================

    void Init_Clock(void)
    { CS2 = 1; SPI_Out(SECONDS); SPI_Out(0x30); CS2 = 0; _nop_(); _nop_();

    CS2 = 1; SPI_Out(MINUTES); SPI_Out(0x10); CS2 = 0; _nop_(); _nop_();

    CS2 = 1; SPI_Out(HOURS); SPI_Out(0x01); CS2 = 0; _nop_(); _nop_();

    CS2 = 1; SPI_Out(CONTROL_REG); SPI_Out(0x85); CS2 = 0; _nop_(); _nop_();

    }

Children