0% found this document useful (0 votes)
20 views4 pages

PIC16F877A ADC and DS1307 I2C Code

The document contains a C program for a PIC microcontroller that reads analog values from a sensor and displays the current time and date using a DS1307 real-time clock. It configures the ADC and I2C communication, initializes an LCD display, and continuously updates the time and date on the LCD. The program includes functions to read from and write to the DS1307, as well as to convert BCD values to characters for display.

Uploaded by

Bruno Gomes
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views4 pages

PIC16F877A ADC and DS1307 I2C Code

The document contains a C program for a PIC microcontroller that reads analog values from a sensor and displays the current time and date using a DS1307 real-time clock. It configures the ADC and I2C communication, initializes an LCD display, and continuously updates the time and date on the LCD. The program includes functions to read from and write to the DS1307, as well as to convert BCD values to characters for display.

Uploaded by

Bruno Gomes
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include <16f877A.

h>
#device adc=10;

#fuses NOPUT,NOPROTECT,NOBROWNOUT,XT,NOWDT // fuses de configuração


USE ESTES!!
#use delay (clock = 4000000)

int Ad_valor;
void main(){
SET_TRIS_A(0b11111111); //configura IO do PORTA//RA0 a RA7
entradas
SET_TRIS_C(0b00000000);// configura IO do PORTC//Rc0 a RC7
saidas
SETUP_ADC_PORTS (AN0);//RA0 IO analógico
SETUP_ADC(ADC_CLOCK_INTERNAL);//configura clock de conversão
SET_ADC_CHANNEL(0);//Canal 0 para leitura do AD.

while (1){

delay_us(100);
Ad_valor=read_adc();
if (Ad_valor == 1023){
output_high(PIN_C1);
output_low (PIN_C2);
}
if(Ad_valor < 1023) {
output_low(PIN_C1);
output_high(PIN_C2);
}
}
}
unsigned short read_ds1307(unsigned short address );
void write_ds1307(unsigned short address,unsigned short w_data);
unsigned short sec;
unsigned short minute;
unsigned short hour;
unsigned short day;
unsigned short date;
unsigned short month;
unsigned short year;
unsigned short data;
char time[9];
char ddate[11];

unsigned char BCD2UpperCh(unsigned char bcd);


unsigned char BCD2LowerCh(unsigned char bcd);

void main(){

I2C_Init(100000); //DS1307 I2C is running at 100KHz


PORTB = 0;
TRISB = 0; // Configure PORTB as output
TRISC = 0xFF;
Lcd_Init(&PORTB); // Initialize LCD connected to PORTB
Lcd_Cmd(Lcd_CLEAR); // Clear display
Lcd_Cmd(Lcd_CURSOR_OFF); // Turn cursor off
Lcd_Out(1, 1, "TIME:");
Lcd_Out(2, 1, "DATE:");

//Set Time
write_ds1307(0,0x80); //Reset second to 0 sec. and stop Oscillator
write_ds1307(1,0x10); //write min 27
write_ds1307(2,0x01); //write hour 14
write_ds1307(3,0x02); //write day of week 2:Monday
write_ds1307(4,0x05); // write date 17
write_ds1307(5,0x01); // write month 6 June
write_ds1307(6,0x09); // write year 8 --> 2008
write_ds1307(7,0x10); //SQWE output at 1 Hz
write_ds1307(0,0x00); //Reset second to 0 sec. and start Oscillator

while(1)
{
sec=read_ds1307(0); // read second
minute=read_ds1307(1); // read minute
hour=read_ds1307(2); // read hour
day=read_ds1307(3); // read day
date=read_ds1307(4); // read date
month=read_ds1307(5); // read month
year=read_ds1307(6); // read year

time[0] = BCD2UpperCh(hour);
time[1] = BCD2LowerCh(hour);
time[2] = ':';
time[3] = BCD2UpperCh(minute);
time[4] = BCD2LowerCh(minute);
time[5] = ':';
time[6] = BCD2UpperCh(sec);
time[7] = BCD2LowerCh(sec);
time[8] = '\0';

ddate[0] = BCD2UpperCh(date);
ddate[1] = BCD2LowerCh(date);
ddate[2] ='/';
ddate[3] = BCD2UpperCh(month);
ddate[4] = BCD2LowerCh(month);
ddate[5] ='/';
ddate[6] = '2';
ddate[7] = '0';
ddate[8] = BCD2UpperCh(year);
ddate[9] = BCD2LowerCh(year);
ddate[10] = '\0';

Lcd_Out(1,6,time);
Lcd_Out(2,6,ddate);
Delay_ms(50);
}
}

unsigned short read_ds1307(unsigned short address)


{
I2C_Start();
I2C_Wr(0xd0); //address 0x68 followed by direction bit (0 for write, 1
for read) 0x68 followed by 0 --> 0xD0
I2C_Wr(address);
I2C_Repeated_Start();
I2C_Wr(0xd1); //0x68 followed by 1 --> 0xD1
data=I2C_Rd(0);
I2C_Stop();
return(data);
}

unsigned char BCD2UpperCh(unsigned char bcd)


{
return ((bcd >> 4) + '0');
}

unsigned char BCD2LowerCh(unsigned char bcd)


{
return ((bcd & 0x0F) + '0');
}
void write_ds1307(unsigned short address,unsigned short w_data)
{
I2C_Start(); // issue I2C start signal
//address 0x68 followed by direction bit (0 for write, 1 for read)
0x68 followed by 0 --> 0xD0
I2C_Wr(0xD0); // send byte via I2C (device address + W)
I2C_Wr(address); // send byte (address of DS1307 location)
I2C_Wr(w_data); // send data (data to be written)
I2C_Stop(); // issue I2C stop signal
}

You might also like