2012年7月1日日曜日

Wake-up timer trial 1

Special wake-up timer

I try to make wake-up timer for studying PIC IC.

Function
The function I want apply is that PIC set LED light intensity high step by step before wake-up alarm sounds.


Every people can wake up easily if they can recognize the day has already start. The LED play a roll of morning sun.
I try to buy such timer, but there are no items which can satisfy my request.
So... I start to make it by myself.

Preparations
PIC18F452 : Main pic
PIC16F88 : Used for beeping the sounder
LCD Module SD1602 : displaying the necessary information, such as time and time to wake-up.
RTC : RTC-8564-NB timer-IC

Report
Below is the B.B pictures I build-up.
It seems work well, but the necessary function, i.e. lighting up LED, is not implemented. Next step I'd like to introduced it.
After I have finishing building up and debugging them, I'd like to disclose the circuit and code.




Date setting is very hard for me... 


Below is the program code for accessing RTC-IC.
//I don't guarantee any problems which are caused by below function, thanks.


unsigned int i2c_write(char addr, unsigned char data);
unsigned char i2c_read(char addr, unsigned char* data);
unsigned char i2c_read_time(char addr, unsigned char* data, unsigned char size);
unsigned int i2c_bit_write(char addr, unsigned char data,unsigned char bit);

//I2C write function

unsigned int i2c_write(char addr, unsigned char data)
{
//Start Condition
    IdleI2C();
    StartI2C();
    while(SSPCON2bits.SEN){;}
       
//Write Slave Address wtih Write mode
if(WriteI2C(SLV_RTC)<0){return 1;}
IdleI2C();
    //Write address of device
    if(WriteI2C(addr)){return 2;}
IdleI2C();
//Write address of data
if(WriteI2C(data)){return 3;}
//Send Stop
IdleI2C();
    StopI2C();
while(SSPCON2bits.PEN);
Delay10TCYx(4);
   
return 0;

}

//I2C read function
unsigned char i2c_read(char addr, unsigned char* data)
{
//Start I2C
IdleI2C();
    StartI2C();
    while(SSPCON2bits.SEN);
    //Write Slave Address wtih Write mode
if(WriteI2C(SLV_RTC)<0){return 1;}
IdleI2C();
    //Write address of device
    if(WriteI2C(addr)){return 2;}
IdleI2C();
    RestartI2C();
while(SSPCON2bits.RSEN);
    //Write Slave Address wtih Read mode
    if(WriteI2C(SLV_RTC | 0x01)){return 3;}
IdleI2C();      
//Read Data
while(DataRdyI2C());
    *data = ReadI2C();
Delay10TCYx(20);
    //Send NAck signal
SSPCON2bits.ACKDT = 1;
SSPCON2bits.ACKEN = 1;
while(SSPCON2bits.ACKEN);
    //Send Stop Signal
    StopI2C();
while(SSPCON2bits.PEN);
   
return 0;
}


//Read data sequentially
unsigned char i2c_read_time(char addr, unsigned char* data, unsigned char size)
{
unsigned char i;
//Start I2C
IdleI2C();
    StartI2C();
    while(SSPCON2bits.SEN);
    //Write Slave Address wtih Write mode
if(WriteI2C(0xA2)<0){return 1;}
IdleI2C();
    //Write address of device
    if(WriteI2C(addr)){return 2;}
IdleI2C();
    RestartI2C();
while(SSPCON2bits.RSEN);
    //Write Slave Address wtih Read mode
    if(WriteI2C(0xA3)){return 3;}
IdleI2C();        
//Read Data
for(i=0;i<=size;i++){
while(DataRdyI2C());
data[i] = ReadI2C();
Delay10TCYx(20);
if(i < size){
SSPCON2bits.ACKDT = 0;
}else
{
SSPCON2bits.ACKDT = 1;
}
SSPCON2bits.ACKEN = 1;
while(SSPCON2bits.ACKEN);
}

    //Send Stop Signal
    StopI2C();
while(SSPCON2bits.PEN);

return 0;
}

//Change only target bit of inputed address

unsigned int i2c_bit_write(char addr, unsigned char bitshift, unsigned char data)
{
unsigned char read_data;

//Read the registered value
IdleI2C();
    StartI2C();
    while(SSPCON2bits.SEN);
    //Write Slave Address wtih Write mode
if(WriteI2C(SLV_RTC)<0){return 1;}
IdleI2C();
    //Write address of device
    if(WriteI2C(addr)){return 2;}
IdleI2C();
    RestartI2C();
while(SSPCON2bits.RSEN);
    //Write Slave Address wtih Read mode
    if(WriteI2C(SLV_RTC | 0x01)){return 3;}
IdleI2C();      
//Read Data
while(DataRdyI2C());
    read_data = ReadI2C();
Delay10TCYx(20);
    //Send NAck signal
SSPCON2bits.ACKDT = 1;
SSPCON2bits.ACKEN = 1;
while(SSPCON2bits.ACKEN);
    //Send Stop Signal
    StopI2C();
while(SSPCON2bits.PEN);

//Write target bit
if(data){
read_data |= (data << bitshift);
}
else{
data = 0xFF - (0x01 << bitshift);
read_data &= data;
}

//Start Condition
    IdleI2C();
    StartI2C();
    while(SSPCON2bits.SEN){;}
       
//Write Slave Address wtih Write mode
    //if(WriteI2C(SLV_RTC)<0){return -1;}
if(WriteI2C(SLV_RTC)<0){return 1;}
IdleI2C();
    //Delay10TCYx(20);
    //Write address of device
    if(WriteI2C(addr)){return 2;}
IdleI2C();
//Write address of data
if(WriteI2C(read_data)){return 3;}
//Send Stop
IdleI2C();
    StopI2C();
while(SSPCON2bits.PEN);
Delay10TCYx(4);
   
return 0;

}