Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
I think you are driving Ian bonkers trying to help you.Hello,
what you think??
So, you think it is difficult project?Perhaps someone should start off with a much less ambitious project such as an LED roulette wheel. No perhaps an decision maker with 4 LEDs. One for "yes", "no", "maybe" and "definitely" ...
And some!!!!I think you are driving Ian bonkers trying to help you.
I think it is perhaps not for you if you lack basic computer programming understanding. What do I do? Well I would like to open an electronics shop and sell parts and offer custom engineering services. Perhaps if I get too busy I can bring some jobs to this forum...So, you think it is difficult project?
anyway what you do sir?
//Sends the start and device address.
//If the device is busy then it resends until accepted.
void SendID(char DeviceID)
{
SendStart();
if(SendByte(DeviceID)==1) // Send ID until accepted..
return;
do
{
SendRestart();
}while(SendByte(DeviceID)==0);
}
//Write a byte to The RTC
char WriteRTC(char Address, char Byte)
{
SendID(0b11010000); // Send 0xD0
if(SendByte(Address&0xff)==0) // Send address
return(0);
if(SendByte(Byte)==0) // Send byte
return(0);
SendStop(); // All done
return(1);
}
ReadClock(&Time[0]); // Get time ( BCD format as
delayMs(250); // we are using 7 seg )
DISP[5] = Time[2]>>4; DISP[4] = Time[2] & 0xf; // fill display buffer
DISP[3] = Time[1]>>4; DISP[2] = Time[1] & 0xf; // backwards so it's
DISP[1] = Time[0]>>4; DISP[0] = Time[0] & 0xf; // readable...
void I2C_init(void)
{
SSPCON = 0x38; // set I2C master mode
SSPCON2 = 0x00;
SSPADD = 0x0C; //400KHZ 20MHz xtal
SSPSTAT|=0X80;
PSPIF=0; // clear SSPIF interrupt flag
BCLIF=0; // clear bus collision flag
}
#include <htc.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
#define _XTAL_FREQ 20000000
#define SDATA RC4
#define SCLK RC3
void InitI2C(),I2C_start(void),I2C_write(char x), I2cSTOP(void);
void i2c_Wait(void);
unsigned char DISP[3];
unsigned char Time[] = {01, 11 ,33, 22 , 10, 4, 15};
unsigned char digits[] = { 0b00010000,0b01111101,0b00100011,0b00101000,0b01001101,0b10001000,
0b10000000, 0b00111101,0X00,0b00001000};
void InitI2C()
{
TRISC3=1;
TRISC4=1;
SSPCON=0b00101000;
SSPCON2=0;
SSPSTAT=0b10000000;
SSPADD=50; //20000/(4*100); //Fosc/(4*baud)
SSPIF=0;
BCLIF=0;
}
void WaitSSP()
{
while(SSPIF==0);
SSPIF=0;
}
void SendStart()
{
SEN=1;
WaitSSP();
}
void SendStop()
{
PEN=1;
WaitSSP();
}
void SendRestart()
{
RSEN=1;
WaitSSP();
}
char SendByte(char Byte)
{
SSPBUF=Byte;
WaitSSP();
return(!ACKSTAT);
}
char ReceiveByte()
{
RCEN=1; // get byte
WaitSSP();
return(SSPBUF);
}
void SendNack()
{
ACKDT=1; // Not Acknowledge
ACKEN=1;
WaitSSP();
}
void SendAck()
{
ACKDT=0; // Acknowledge
ACKEN=1;
WaitSSP();
}
void SendID(char DeviceID)
{
SendStart();
if(SendByte(DeviceID)==1) // Send ID until accepted..
return;
do
{
SendRestart();
}while(SendByte(DeviceID)==0);
}
char ReadRTC(char Address)
{
char Byte;
SendID(0b11010000); // Send 0xD0
if(SendByte(Address&0xff)==0) // Send address
return(0);
SendRestart(); // Re-start
if(SendByte(0b11010001)==0) // Read 0xD1
return(0);
Byte=ReceiveByte(); // Get byte
SendNack(); // No more
SendStop(); // Stop bus
return(Byte); // All done
}
void WriteClock(char *array) // Not used
{
char x;
int tmp;
for(x=0;x<7;x++)
{
tmp = array[x] / 10;
tmp << = 4;
tmp += array[x] % 16;
WriteRTC(x,tmp);
}
}
void ReadClock(char *array)
{
char x;
for(x=0;x<7;x++)
array[x] = ReadRTC(x); // No need to BCD-BIN
}
void main(void)
{
ANSELH = 0;
TRISC3=1; //direction to input have be changed
TRISC4=1;
RD0=0;
RD1=0;
RD2=0;
RD3=0;
TRISB=0X00;
TRISD=0X00;
PORTB=0B00000000;
unsigned char ch=0;
unsigned char i=0;
int idx;
unsigned char DIGIT; // A temporary store for manipulation
TRISC = 0; // portc tris bits
TRISD = 0; // portd tris bits
InitI2C();
while(1)
{
ReadClock(&Time[0]);
__delay_ms(250);
DISP[2] = Time[1] & 0xf;
DISP[1] = Time[0]>>4; DISP[0] = Time[0] & 0xf;
RD0=1;
RD1=0;
RD2=0;
RD3=0;
PORTB = digits[DISP[0]]; // retrive 7 seg code
__delay_ms(250);
RD0=0;
RD1=1;
RD2=0;
RD3=0;
PORTB = digits[DISP[1]]; // retrive 7 seg code
__delay_ms(250);
RD0=0;
RD1=0;
RD2=1;
RD3=0;
PORTB = digits[DISP[2]]; // retrive 7 seg code
__delay_ms(250);
}
}
#include <htc.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
#define _XTAL_FREQ 20000000
#define SDATA RC4
#define SCLK RC3
void InitI2C(),I2C_start(void),I2C_write(char x), I2cSTOP(void);
void i2c_Wait(void);
unsigned char DISP[3];
unsigned char Time[] = {01, 0x11 ,0x3, 0x22 , 0x10, 0x4, 0x15};
unsigned char digits[] = { 0b00010000,0b01111101,0b00100010,0b00101000,0b01001101,0b10001000,
0b10000000, 0b00111101,0X00,0b00001000};
void InitI2C()
{
TRISC3=1;
TRISC4=1;
SSPCON=0b00101000;
SSPCON2=0;
SSPSTAT=0b10000000;
SSPADD=50; //20000/(4*100); //Fosc/(4*baud)
SSPIF=0;
BCLIF=0;
}
void WaitSSP()
{
while(SSPIF==0);
SSPIF=0;
}
void SendStart()
{
SEN=1;
WaitSSP();
}
void SendStop()
{
PEN=1;
WaitSSP();
}
void SendRestart()
{
RSEN=1;
WaitSSP();
}
char SendByte(char Byte)
{
SSPBUF=Byte;
WaitSSP();
return(!ACKSTAT);
}
char ReceiveByte()
{
RCEN=1; // get byte
WaitSSP();
return(SSPBUF);
}
void SendNack()
{
ACKDT=1; // Not Acknowledge
ACKEN=1;
WaitSSP();
}
void SendAck()
{
ACKDT=0; // Acknowledge
ACKEN=1;
WaitSSP();
}
void SendID(char DeviceID)
{
SendStart();
if(SendByte(DeviceID)==1) // Send ID until accepted..
return;
do
{
SendRestart();
}while(SendByte(DeviceID)==0);
}
//Write a byte to The RTC
char WriteRTC(char Address, char Byte)
{
SendID(0b11010000); // Send 0xD0
if(SendByte(Address&0xff)==0) // Send address
return(0);
if(SendByte(Byte)==0) // Send byte
return(0);
SendStop(); // All done
return(1);
}
char ReadRTC(char Address)
{
char Byte;
SendID(0b11010000); // Send 0xD0
if(SendByte(Address&0xff)==0) // Send address
return(0);
SendRestart(); // Re-start
if(SendByte(0b11010001)==0) // Read 0xD1
return(0);
Byte=ReceiveByte(); // Get byte
SendNack(); // No more
SendStop(); // Stop bus
return(Byte); // All done
}
void WriteClock(char *array) // Not used
{
char x;
int tmp;
for(x=0;x<7;x++)
{
WriteRTC(x,array[x]);
}
}
void ReadClock(char *array)
{
char x;
for(x=0;x<7;x++)
array[x] = ReadRTC(x); // No need to BCD-BIN
}
void main(void)
{
unsigned char ch=0;
unsigned char i=0;
int idx;
unsigned char DIGIT; // A temporary store for manipulation
ANSELH = 0;
TRISC3=1; //direction to input have be changed
TRISC4=1;
RD0=0;
RD1=0;
RD2=0;
RD3=0;
TRISB=0X00;
TRISD=0X00;
PORTB=0B00000000;
TRISC = 0; // portc tris bits
TRISD = 0; // portd tris bits
InitI2C();
WriteClock(&Time[0]);
while(1)
{
ReadClock(&Time[0]);
__delay_ms(250);
DISP[2] = Time[1] & 0xf;
DISP[1] = Time[0]>>4; DISP[0] = Time[0] & 0xf;
PORTD = 1;
PORTB = digits[DISP[0]]; // retrive 7 seg code
__delay_ms(250);
PORTD = 2;
PORTB = digits[DISP[1]]; // retrive 7 seg code
__delay_ms(250);
PORTD = 4;
PORTB = digits[DISP[2]]; // retrive 7 seg code
__delay_ms(250);
}
}
Hello,The write clock function is BEFORE the while loop so it is executed once only... If the seconds keep resetting then the micro is resetting...