Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] UART and I2C are not working together

Status
Not open for further replies.

vinay bs

Member level 3
Member level 3
Joined
May 22, 2012
Messages
65
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Visit site
Activity points
1,751
hi folks,

I am facing problem wen i club UART and I2C together in my compiler. I am using mikroC Compiler and PIC16f883 microchip IC. I2C is used for reading the time from DS1307 IC and displaying it on LCD, UART used for serial communication to send the data (PC Hyper terminal).
It works good wen i compile individually I2C and UART but not together, and i am using mikroc inbuilt routines to read and write both i2c and uart.

Please help me out on this...

Below is my code
Code:
// LCD module connections
sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;

sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections

unsigned short read_ds1307(unsigned short address)
{

  unsigned short r_data;
  I2C1_Start();
  I2C1_Wr(0xD0); //address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 --> 0xD0
  I2C1_Wr(address);
  I2C1_Repeated_Start();
  I2C1_Wr(0xD1); //0x68 followed by 1 --> 0xD1
  r_data=I2C1_Rd(0);
  I2C1_Stop();
  return(r_data);
}

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


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

unsigned char BCD2LowerCh(unsigned char bcd)
{
  return ((bcd & 0x0F) + '0');
}

void space_fun(unsigned char x)
{
    char j;
    for(j=0;j<x;j++)
    {
       UART1_Write(32);
    }
}
int second;
int minute;
int hour;
int hr;
int day;
int dday;
int month;
int year;
int d_rec;

unsigned short set_count = 0;
short set;

char time[] = "00:00:00";
char date[] = "00-00-2000";
char i;
char a = 0;

void main()
{
   I2C1_Init(100000); //DS1307 I2C is running at 100KHz
   UART1_Init(2400);  // Initialize UART module at 9600 bps
   Delay_ms(50);      // Wait for UART module to stabilize

   C1ON_bit = 0;   // To turn off comparators
   C2ON_bit = 0;
   ADCON0 = 0x80;  // To turn off analog to digital converters
   ANSEL = 0x00;
   ANSELH = 0x00;

   TRISB = 0x00;
   TRISA = 0x2F;
   TRISC = 0x00;
   PORTA = 0x00;

   Lcd_Init();                        // Initialize LCD
   Lcd_Cmd(_LCD_CLEAR);               // Clear display
   Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
   Lcd_out(1,1,"Time:");
   Lcd_out(2,1,"Date:");
   
   write_ds1307(6, 0x12); // write year

   UART1_Write_text("To Set Time and Date");
   UART1_Write(13);
   UART1_Write_text("Press 5 to Set");
   UART1_Write(13);
   UART1_Write_text("Press 8 to increase");
   UART1_Write(13);
   UART1_Write_text("Press 2 to decrease");
   UART1_Write(13);

   while(1)
   {

         if(UART1_Data_Ready()==1)
         {
          d_rec = UART1_Read();
          UART1_Write(d_rec);
         }

          set = 0;
          if(d_rec == 53) //set
          {
             //UART1_Write_text("set button pressed");
             set_count++;
             if(set_count == 1)
             {
                Lcd_cmd(_LCD_FIRST_ROW);
                for(i=0;i<5;i++)
                {
                  Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
                }
                Lcd_Cmd(_LCD_BLINK_CURSOR_ON);
                Delay_ms(1000);
             }
             if(set_count == 2)
             {
                Lcd_cmd(_LCD_FIRST_ROW);
                for(i=0;i<8;i++)
                {
                  Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
                }
                Lcd_Cmd(_LCD_BLINK_CURSOR_ON);
                Delay_ms(1000);
             }
             if(set_count == 3)
             {
                Lcd_cmd(_LCD_FIRST_ROW);
                for(i=0;i<11;i++)
                {
                  Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
                }
                Lcd_Cmd(_LCD_BLINK_CURSOR_ON);
                Delay_ms(1000);
             }
             if(set_count == 4)
             {
                Lcd_cmd(_LCD_SECOND_ROW);
                for(i=0;i<5;i++)
                {
                  Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
                }
                Lcd_Cmd(_LCD_BLINK_CURSOR_ON);
                Delay_ms(1000);
             }
             if(set_count == 5)
             {
                Lcd_cmd(_LCD_SECOND_ROW);
                for(i=0;i<8;i++)
                {
                  Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
                }
                Lcd_Cmd(_LCD_BLINK_CURSOR_ON);
                Delay_ms(1000);
             }
             if(set_count == 6)
             {
                Lcd_cmd(_LCD_SECOND_ROW);
                for(i=0;i<13;i++)
                {
                  Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
                }
                Lcd_Cmd(_LCD_BLINK_CURSOR_ON);
                Delay_ms(1000);
             }
             if(set_count >= 7)
             {
                set_count = 0;
             }
             d_rec = 0;
          }

     if(set_count)
     {
        Lcd_Cmd(_LCD_CURSOR_OFF);
        if(d_rec == 56)
        {
              //UART1_Write_text("increment");
              set = 1;
              d_rec = 0;
        }

        if(d_rec == 50)
        {
              //UART1_Write_text("decrement");
              set = -1;
              d_rec = 0;
        }

        if(set_count && set)
        {
          switch(set_count)
          {
            case 1:
                    hour = BCD2Dec(hour);
                    hour = hour + set;
                    hour = Dec2BCD(hour);
                    if(hour > 0x23)
                    {
                      hour = 0x00;
                    }
                    else if(hour < 0x00)
                    {
                      hour = 0x23;
                    }
                    write_ds1307(2, hour); //write hour
                    break;

            case 2:
                    minute = BCD2Dec(minute);
                    minute = minute + set;
                    if(minute >= 60)
                       minute = 0;
                    if(minute < 0)
                       minute = 59;
                    minute = Dec2BCD(minute);
                    write_ds1307(1, minute); //write min
                    break;

            case 3:
                    if(abs(set))
                    write_ds1307(0,0x00); //Reset second to 0 sec. and start Oscillator
                    break;

            case 4:
                    day = BCD2Dec(day);
                    day = day + set;
                    day = Dec2BCD(day);
                    if(day >= 0x32)
                      day = 1;
                    if(day <= 0)
                      day = 0x31;
                    write_ds1307(4, day); // write date
                    break;

            case 5:
                    month = BCD2Dec(month);
                    month = month + set;
                    month = Dec2BCD(month);
                    if(month > 0x12)
                      month = 1;
                    if(month <= 0)
                      month = 0x12;
                    write_ds1307(5,month); // write month
                    break;

            case 6:
                    year = BCD2Dec(year);
                    year = year + set;
                    year = Dec2BCD(year);
                    if(year < 0x00)
                       year = 0x99;
                    if(year >= 0x99)
                       year = 0;
                    write_ds1307(6, year); // write year
                    break;

          }
        }
     }

      second = read_ds1307(0);
      minute = read_ds1307(1);
      hour = read_ds1307(2);
      day = read_ds1307(4);
      month = read_ds1307(5);
      year = read_ds1307(6);

      time[0] = BCD2UpperCh(hour);
      time[1] = BCD2LowerCh(hour);
      time[3] = BCD2UpperCh(minute);
      time[4] = BCD2LowerCh(minute);
      time[6] = BCD2UpperCh(second);
      time[7] = BCD2LowerCh(second);

      date[0] = BCD2UpperCh(day);
      date[1] = BCD2LowerCh(day);
      date[3] = BCD2UpperCh(month);
      date[4] = BCD2LowerCh(month);
      date[8] = BCD2UpperCh(year);
      date[9] = BCD2LowerCh(year);

      if(a == 0)
      {
      a++;
      UART1_Write_text("Time :");
      space_fun(1);
      UART1_Write(time[0]);
      UART1_Write(time[1]);
      UART1_Write(58);
      UART1_Write(time[3]);
      UART1_Write(time[4]);
      UART1_Write(58);
      UART1_Write(time[6]);
      UART1_Write(time[7]);
      UART1_Write(13);
      UART1_Write_text("Date :");
      space_fun(1);
      UART1_Write(date[0]);
      UART1_Write(date[1]);
      UART1_Write(58);
      UART1_Write(date[3]);
      UART1_Write(time[4]);
      UART1_Write(58);
      UART1_Write(date[8]);
      UART1_Write(date[9]);
      UART1_Write(13);
      }
      Lcd_out(1, 6, time);
      Lcd_out(2, 6, date);

      Delay_ms(50);

   }
}
 

You haven't set TRISC properly. Bits corresponding to SDA and SCK should be set.
Put TRISC = 0x99;
 
Last edited:

Even though if TRISC bits are not set for SDA and SCK it works fine individually...
 

I tried your code in Proteus, when I changed TRISC it worked. Try & see.
 

Thanx ADGAN it worked...
But my question is how TRISC bits relates to this...?
It will work even if it is not set as input or output...
 

Thanx ADGAN it worked...
But my question is how TRISC bits relates to this...?
It will work even if it is not set as input or output...

Selection of any I2C mode, with the SSPEN bit set,
forces the SCL and SDA pins to be open-drain, provided
these pins are programmed to inputs by setting
the appropriate TRISC bits. To ensure proper operation
of the module, pull-up resistors must be provided
externally to the SCL and SDA pins.

More info page 86 of pic 16f877a data sheet
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top