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?
void Init_Uart() //19200 bps { SCON0 = 0x52; PCON |= 0X80;//set baud rate 19200 SCON1 = 0x52; PCON |= 0X80;//set SMOD =1 to get larger baud rate TMOD = 0X21; TH1 = 0XFD; //Baud Rate: 19200 bps TR1 = 1; }
void Uart_Tx1(BYTE c) // transmit data { 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 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) { BYTE keys,keym,keyh; keys=trans_keypad(); keym=trans_keypad(); keyh=trans_keypad(); CS2 = 1; //initialize time for testing : 03:40:00 Write_SPI(SECONDS); SPI_Out(keys); CS2 = 0; _nop_(); _nop_(); CS2 = 1; Write_SPI(MINUTES); SPI_Out(keym); CS2 = 0; _nop_(); _nop_(); CS2 = 1; Write_SPI(HOURS); SPI_Out(keyh); CS2 = 0; _nop_(); _nop_(); CS2 = 1; Write_SPI(CONTROL_REG); SPI_Out(0x85); CS2 = 0; _nop_(); _nop_(); }
void Alarm_Clock(void) { tc = 0x01; hh = 0x00; CS2 = 1; //set alarm to 00:00:60 Write_SPI(SEC_ALARM); SPI_Out(0x00); CS2 = 0; _nop_(); _nop_(); CS2 = 1; //set alarm to 00:00:60 Write_SPI(MIN_ALARM); SPI_Out(tc); CS2 = 0; _nop_(); _nop_(); CS2 = 1; //set alarm to 00:00:60 Write_SPI(HRS_ALARM); SPI_Out(hh); CS2 = 0; _nop_(); _nop_(); CS2 = 1; Write_SPI(INTERRUPT); //set alarm SPI_Out(0x10); CS2 = 0; _nop_(); _nop_(); } void SMS(void) { int i; LCD_WriteInstructionReg(reHome); CS2=1; SPI_Out(SECONDS); sec=SPI_In(); min=SPI_In(); hrs=SPI_In(); LCD_WriteInstructionReg(goline2);
sprintf (s,"%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(500); } unsigned char keypad(void) { int val0,val1,val2,val3; XBYTE[PortC]=0x80; Delay(1); val0=XBYTE[PortC]; if(val0 != 0x80) { switch(val0) { case 0x88 : return('1'); //key 1 case 0x84 : return('4'); //key 4 case 0x82 : return('7'); //key 7 case 0x81 : return('*'); //key * } } XBYTE[PortC]=0x40; Delay(1); val1=XBYTE[PortC]; if(val1 != 0x40) { switch(val1) { case 0x48 : return('2'); //key 2 case 0x44 : return('5'); //key 5 case 0x42 : return('8'); //key 8 case 0x41 : return('0'); //key 0 } } XBYTE[PortC]=0x20; Delay(1); val2=XBYTE[PortC]; if(val2 != 0x20) { switch(val2) { case 0x28 : return('3'); //key 3 case 0x24 : return('6'); //key 6 case 0x22 : return('9'); //key 9 case 0x21 : return('#'); //key # } } XBYTE[PortC]=0x10; Delay(1); val3=XBYTE[PortC]; if(val3 != 0x10) { switch(val3) { case 0x18 : return('A'); //key A case 0x14 : return('B'); //key B case 0x12 : return('C'); //key C case 0x11 : return('D'); //key D } } return(0); } unsigned char trans_keypad(void) { BYTE key; while(1) { key=keypad(); //wait until the kay be pressed while(keypad()!=0); //wait tilldepress if (key!=0) Uart_Tx1(key); //transmit key to COM2 Delay(100); } return (key); }
void ext_int0(void) interrupt 0 { int i; int idata mini; mini=0; mini++; while(mini==2) { SMS(); mini=0; } //the following reset interrupt of RTC CS2 = 1; //Read Status Reg to activate interrupt 1 (set alarm interrupt) Read_SPI(STATUS_REG); SPI_Out(0x0A); CS2 = 0; _nop_(); _nop_(); } //------------------------------------------------------------------------------------------------ .