اطلاعیه

Collapse
No announcement yet.

مشکل با DS1307 و LPC1343

Collapse
X
 
  • فیلتر
  • زمان
  • Show
Clear All
new posts

    مشکل با DS1307 و LPC1343

    سلام خدمت دوستان گلم
    کسی میدونه این برنامه چشه ؟
    اصلا کار نمیکنه


    کد:
    #include <LPC13xx.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    /*************************************************
    LIB
    *************************************************/
    #include "mylib\sysclk.h"
    #include "mylib\iodef.h"
    #include "mylib\delay.h"
    #include "mylib\type.h"
    #include "mylib\i2c.h"
    /*************************************************
    DEFINE
    *************************************************/	
    #define BV(BIT)								(1UL<<BIT)
    #define SBI(BYTE,BIT)					(BYTE|=BV(BIT))
    #define CBI(BYTE,BIT)					(BYTE&=~BV(BIT))
    #define RBI(BYTE,BIT)					((BYTE&BV(BIT))>>BIT)
    #define BIS(BYTE,BIT)					(while(!RBI(BYTE,BIT)))
    #define BIC(BYTE,BIT)					(while(RBI(BYTE,BIT)))	
    	
    
    #define STROBE								SBI(LPC_GPIO0->DATA,IO6),CBI(LPC_GPIO0->DATA,IO6)
    #define CLOCK								 SBI(LPC_GPIO0->DATA,IO7),CBI(LPC_GPIO0->DATA,IO7)
    #define DATAH									SBI(LPC_GPIO0->DATA,IO8)
    #define DATAL									CBI(LPC_GPIO0->DATA,IO8)
    #define LOAD_DATA							CLOCK,STROBE
    #define COL_START							DATAH,LOAD_DATA,DATAL
    
    typedef struct {
    	unsigned int	year;	/* 2000..2099 */
    	unsigned char	month;	/* 1..12 */
    	unsigned char	mday;	/* 1.. 31 */
    	unsigned char	wday;	/* 1..7 */
    	unsigned char	hour;	/* 0..23 */
    	unsigned char	min;	/* 0..59 */
    	unsigned char	sec;	/* 0..59 */
    } RTC;
    
    
    int rtc_settime (RTC rtc);
    int rtc_gettime (RTC *rtc);
    void rtc_start(void);
    void rtc_set_irq(char prescale);
    void write_uln(unsigned char data);
    
    const char segment_data[11]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x83,0xf8,0x80,0x98,0xff};
    
    RTC rtc;
    char segbuff[15];
    
    int main(void)
    
    {	
    	int i=0,j=0;
    	sys_clk_freq=12000000;//Mhz	
    	LPC_SYSCON->SYSAHBCLKCTRL |= 1UL<<6|1UL<<16;
    	
    	LPC_GPIO1->DIR=0xff;
    	LPC_GPIO0->DIR|=BV(6)|BV(7)|BV(8);
    	LPC_GPIO2->DIR&=~BV(0)|~BV(1)|~BV(2);	
    	
    	
    	P1_0CFG=0xd1;
    	P1_1CFG=0xd1;
    	P1_2CFG=0xd1;
    	P1_3CFG=0xd1;
    	P1_4CFG=0xd0;
    	P1_5CFG=0xd0;	
    	P1_6CFG=0xd0;	
    	P1_7CFG=0xd0;		
    	
    	
    	
    	COL_START;
    	for(i=0;i<14;i++)
    		LOAD_DATA;
    	
    	
     if ( I2CInit( (uint32_t)I2CMASTER ) == FALSE )	/* initialize I2c */
     {
    		while ( 1 );				/* Fatal error */
     }
    	
    	rtc.sec = 10;
    	rtc.min = 5;
    	rtc.hour = 12;
    	rtc.wday = 2;
    	rtc.mday = 12;
    	rtc.month = 3;
    	rtc.year = 2015;	
    	rtc_settime(rtc);	
    	
    	rtc_start();
    	rtc_set_irq(8);
    	
    	
    	while(1)
    	{		
    		rtc_gettime(&rtc);
    		sprintf(segbuff,"%02d%02d%02d%04d%02d%02d",rtc.hour,rtc.min,rtc.sec,rtc.year,rtc.month,rtc.mday);				
    		COL_START;			
    		for(i=0;i<15;i++)
    		{			
    			for(j=0;j<30;j++)
    				write_uln(segment_data[segbuff[i]-48]);
    			LOAD_DATA;
    		};		
    	}		
    }
    
    
    
    void write_uln(unsigned char data)
    {
    		LPC_GPIO1->DATA=data;
    }
    
    
    void rtc_set_irq(char prescale)
    {
    	I2CWriteLength=3;
    	I2CReadLength=0;
    	I2CMasterBuffer[0]=DS_W;
    	I2CMasterBuffer[1]=0x07;			
    	if(prescale==8)
    		I2CMasterBuffer[2]=0x90;
    	if(prescale==4)
    		I2CMasterBuffer[2]=0x91;
    	if(prescale==2)
    		I2CMasterBuffer[2]=0x92;
    	if(prescale==1)
    		I2CMasterBuffer[2]=0x93;		
    	I2CEngine();		
    }
    
    void rtc_start(void)
    {	
    
    	I2CWriteLength=3;
    	I2CReadLength=0;
    	I2CMasterBuffer[0]=DS_W;
    	I2CMasterBuffer[1]=0x00;	
    	I2CMasterBuffer[2]=0x00;
    	I2CEngine();			
    }
    
    int rtc_gettime (RTC *rtc)
    {
    	int i=0;
    	I2CWriteLength=2;
    	I2CReadLength=7;
    	I2CMasterBuffer[0]=DS_W;
    	I2CMasterBuffer[1]=0x00;
    	I2CEngine();
     //while (I2CEngine() == I2CSTATE_SLA_NACK)
    	 //i++;
    	rtc->sec = (I2CSlaveBuffer[0] & 0x0F) + ((I2CSlaveBuffer[0] >> 4) & 7) * 10;
    	rtc->min = (I2CSlaveBuffer[1] & 0x0F) + (I2CSlaveBuffer[1] >> 4) * 10;
    	rtc->hour = (I2CSlaveBuffer[2] & 0x0F) + ((I2CSlaveBuffer[2] >> 4) & 3) * 10;
    	rtc->wday = (I2CSlaveBuffer[2] & 0x07);
    	rtc->mday = (I2CSlaveBuffer[4] & 0x0F) + ((I2CSlaveBuffer[4] >> 4) & 3) * 10;
    	rtc->month = (I2CSlaveBuffer[5] & 0x0F) + ((I2CSlaveBuffer[5] >> 4) & 1) * 10;
    	rtc->year = 2000 + (I2CSlaveBuffer[6] & 0x0F) + (I2CSlaveBuffer[6] >> 4) * 10;	
    	return i;
    }
    
    
    
    
    int rtc_settime (RTC rtc)
    {
    	I2CWriteLength=9;
    	I2CReadLength=0;
    	I2CMasterBuffer[0]=DS_W;
    	I2CMasterBuffer[1]=0x00;		
    	I2CMasterBuffer[2] = rtc.sec / 10 * 16 + rtc.sec % 10;
    	I2CMasterBuffer[3] = rtc.min / 10 * 16 + rtc.min % 10;
    	I2CMasterBuffer[4] = rtc.hour / 10 * 16 + rtc.hour % 10;
    	I2CMasterBuffer[5] = rtc.wday & 7;
    	I2CMasterBuffer[6] = rtc.mday / 10 * 16 + rtc.mday % 10;
    	I2CMasterBuffer[7] = rtc.month / 10 * 16 + rtc.month % 10;
    	I2CMasterBuffer[8] = (rtc.year - 2000) / 10 * 16 + (rtc.year - 2000) % 10;
    	I2CEngine();	
    	return 1;
    }
    دلا خو کن به تنهایی که از تنها بلا خیزد

    #2
    پاسخ : مشکل با DS1307 و LPC1343

    محض رضای خدا یکی جواب بده
    دلا خو کن به تنهایی که از تنها بلا خیزد

    دیدگاه


      #3
      پاسخ : مشکل با DS1307 و LPC1343

      حل شد :applause:
      دلا خو کن به تنهایی که از تنها بلا خیزد

      دیدگاه

      لطفا صبر کنید...
      X