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.

[Help Needed] ADC and RTC using PIC18F4520 in MikroC

Status
Not open for further replies.

sarah.sanchez

Junior Member level 2
Joined
May 16, 2011
Messages
23
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,588
Hi all,

I'm working on a project that makes use of RTC and the PIC18F4520's ADC module on MikroC. The code that I drafted (please see below) builds in MikroC but doesn't work on breadboard. The RTC counts up but does not have a display in the LCD the whole time. Instead, it follows the delay of the ADC so the output in the LCD only appears after a few seconds. Does anyone know how to debug this problem? My guess is there's a problem with the placing of the while statements. Any help will be greatly appreciated. Thank you!

Code:
unsigned short temp_h1, temp_h2, temp_h3, temp_c1, temp_c2, temp_c3;
unsigned short tempdiff1, tempdiff2, tempdiff3;
char hot1[5], hot2[5], hot3[4], cold1[4], cold2[4], cold3[4];

char seconds, minutes, hours, day, month, year;    // Global date/time variables
unsigned short move, counter1, counter2;
unsigned short initial = 0;

void Init_ADC() {
  ADCON1 = 0x00;
  TRISA = 0xFF;
  TRISE = 0x01;

  TRISD = 0x00;
  Lcd_Init(&PORTD);
  Delay_ms(200);
  Lcd_Cmd(Lcd_CLEAR);
  Lcd_Cmd(Lcd_CURSOR_OFF);
}

void ADC() {
  Init_ADC();

  while(1) {
  ADCON0 = 0x03;
  temp_h1 = ADC_Read(0);
  EEPROM_Write(0x00, temp_h1);
  ShortToStr(EEPROM_Read(0x00), hot1);
  Delay_ms(500);

  ADCON0 = 0x0F;
  temp_c1 = ADC_Read(3);
  EEPROM_Write(0x06, temp_c1);
  ShortToStr(EEPROM_Read(0x06), cold1);
  Delay_ms(500);

  Lcd_Out(1,1,"h1=");
  Lcd_Out(1,5,hot1);
  Lcd_Out(2,1,"c1=");
  Lcd_Out(2,5,cold1);
  Delay_ms(1000);

  ADCON0 = 0x07;
  temp_h2 = ADC_Read(1);
  EEPROM_Write(0x02, temp_h2);
  ShortToStr(EEPROM_Read(0x02), hot2);
  Delay_ms(500);

  ADCON0 = 0x13;
  temp_c2 = ADC_Read(4);
  EEPROM_Write(0x08, temp_c2);
  ShortToStr(EEPROM_Read(0x08), cold2);
  Delay_ms(500);

  Lcd_Out(1,1,"h2=");
  Lcd_Out(1,5,hot2);
  Lcd_Out(2,1,"c2=");
  Lcd_Out(2,5,cold2);
  Delay_ms(1000);

  ADCON0 = 0x0B;
  temp_h3 = ADC_Read(2);
  EEPROM_Write(0x04, temp_h3);
  ShortToStr(EEPROM_Read(0x04), hot3);
  Delay_ms(500);

  ADCON0 = 0x17;
  temp_c3 = ADC_Read(5);   //RE0
  EEPROM_Write(0x0A, temp_c3);
  ShortToStr(EEPROM_Read(0x0A), cold3);
  Delay_ms(500);

  Lcd_Out(1,1,"h3=");
  Lcd_Out(1,5,hot3);
  Lcd_Out(2,1,"c3=");
  Lcd_Out(2,5,cold3);
  Delay_ms(1000);

  /*tempdiff1 = temp_h1 - temp_c1;
  tempdiff2 = temp_h2 - temp_c2;
  tempdiff3 = temp_h3 - temp_c3;*/

/*  if (tempdiff1 >= 80 && tempdiff2 >= 80 && tempdiff3 >= 80)
     {
      PORTE.F1 = 1;              // port RE1 on
     }
    else
      PORTE.F2 = 1;              // port RE2 on   */
  }
}


//--------------------- Reads time and date information from RTC (PCF8583)
void Read_Time() {

  Soft_I2C_Config(&PORTC, 4, 3); // Initialize full master mode
  Soft_I2C_Start();               // Issue start signal
  Soft_I2C_Write(0xA0);           // Address PCF8583, see PCF8583 datasheet
  Soft_I2C_Write(2);              // Start from address 2
  Soft_I2C_Start();               // Issue repeated start signal
  Soft_I2C_Write(0xA1);

  seconds = Soft_I2C_Read(1);     // Read seconds byte
  minutes = Soft_I2C_Read(1);     // Read minutes byte
  hours = Soft_I2C_Read(1);       // Read hours byte
  day = Soft_I2C_Read(1);         // Read year/day byte
  month = Soft_I2C_Read(0);       // Read weekday/month byte
  Soft_I2C_Stop();                // Issue stop signal

}

//-------------------- Formats date and time
void Transform_Time() {
  seconds  =  ((seconds & 0xF0) >> 4)*10 + (seconds & 0x0F);  // Transform seconds
  minutes  =  ((minutes & 0xF0) >> 4)*10 + (minutes & 0x0F);  // Transform months
  hours    =  ((hours & 0xF0)  >> 4)*10  + (hours & 0x0F);    // Transform hours
  year     =   (day & 0xC0) >> 6;                             // Transform year
  day      =  ((day & 0x30) >> 4)*10    + (day & 0x0F);       // Transform day
  month    =  ((month & 0x10)  >> 4)*10 + (month & 0x0F);     // Transform month

  EEPROM_Write(0x20, seconds);
  EEPROM_Write(0x21, minutes);
  EEPROM_Write(0x22, hours);

}

//-------------------- Output values to LCD
void Display_Time() {
   Lcd_Chr(1, 6, (EEPROM_Read(0x22) / 10) + 48);
   Lcd_Chr(1, 7, (EEPROM_Read(0x22) % 10) + 48);
   Lcd_Chr(1, 9, (EEPROM_Read(0x21) / 10) + 48);
   Lcd_Chr(1,10, (EEPROM_Read(0x21) % 10) + 48);
   Lcd_Chr(1,12, (EEPROM_Read(0x20) / 10) + 48);
   Lcd_Chr(1,13, (EEPROM_Read(0x20) % 10) + 48);
}

//------------------ Performs project-wide init
void Init_Main() {

  TRISD = 0;
  PORTD = 0xFF;
  TRISD = 0xFF;

  TRISB = 0x00;
  TRISE = 0x00;
  ADCON1 = 0x0F;
  T0CON = 0xF8;

  Delay_ms(200);

  Soft_I2C_Config(&PORTC, 4, 3);         // Initialize Soft I2C communication
  Lcd_Init(&PORTD);                // Initialize LCD
  Delay_ms(200);
  Lcd_Cmd(Lcd_CLEAR);       // Clear LCD display
  Lcd_Cmd(Lcd_CURSOR_OFF);  // Turn cursor off

  Lcd_Out(1,1,"Time:");
  Lcd_Chr(1,8,':');
  Lcd_Chr(1,11,':');

}

//----------------- Main procedure
void RTC() {
  Delay_ms(500);

  Init_Main();               // Perform initialization
  while(1) {
    Read_Time();             // Read time from RTC(PCF8583)
    Transform_Time();        // Format date and time
    Display_Time();          // Prepare and display on LCD
    //determine which LED yung maglight
    if((EEPROM_Read(0x22)==10) && (00<=EEPROM_Read(0x21)<=30))
      PORTB = 0x01;
      else if((EEPROM_Read(0x22)==10) && (30<=EEPROM_Read(0x21)<=59))
      PORTB = 0x02;
      else if((EEPROM_Read(0x22)==11) && (00<=EEPROM_Read(0x21)<=30))
      PORTB = 0x04;
      else if((EEPROM_Read(0x22)==11) && (30<=EEPROM_Read(0x21)<=59))
      PORTB = 0x08;
      else if((EEPROM_Read(0x22)==12) && (00<=EEPROM_Read(0x21)<=30))
      PORTB = 0x10;
      else if((EEPROM_Read(0x22)==12) && (30<=EEPROM_Read(0x21)<=59))
      PORTB = 0x20;
      else if((EEPROM_Read(0x22)==13) && (00<=EEPROM_Read(0x21)<=30))
      PORTB = 0x40;
      else if((EEPROM_Read(0x22)==13) && (30<=EEPROM_Read(0x21)<=59))
      PORTB = 0x80;
      else if((EEPROM_Read(0x22)==14) && (00<=EEPROM_Read(0x21)<=30))
      PORTE = 0x01;
      else if((EEPROM_Read(0x22)==14) && (30<=EEPROM_Read(0x21)<=59))
      PORTE = 0x02;
      else
      PORTE = 0x04;

    //determine pang-ilang counter
    if((EEPROM_Read(0x22)==10) && (00<=EEPROM_Read(0x21)<=30))
      counter1 = 0;
      else if((EEPROM_Read(0x22)==10) && (30<=EEPROM_Read(0x21)<=59))
      counter1 = 1;
      else if((EEPROM_Read(0x22)==11) && (00<=EEPROM_Read(0x21)<=30))
      counter1 = 2;
      else if((EEPROM_Read(0x22)==11) && (30<=EEPROM_Read(0x21)<=59))
      counter1 = 3;
      else if((EEPROM_Read(0x22)==12) && (00<=EEPROM_Read(0x21)<=30))
      counter1 = 4;
      else if((EEPROM_Read(0x22)==12) && (30<=EEPROM_Read(0x21)<=59))
      counter1 = 5;
      else if((EEPROM_Read(0x22)==13) && (00<=EEPROM_Read(0x21)<=30))
      counter1 = 6;
      else if((EEPROM_Read(0x22)==13) && (30<=EEPROM_Read(0x21)<=59))
      counter1 = 7;
      else if((EEPROM_Read(0x22)==14) && (00<=EEPROM_Read(0x21)<=30))
      counter1 = 8;
      else if((EEPROM_Read(0x22)==14) && (30<=EEPROM_Read(0x21)<=59))
      counter1 = 9;
      else
      counter1 = 10;
      //counter1 in seconds
      /*for(counter1=0; 0<counter1<10; counter1++) {
      move = initial + counter1;
      if (10<=(EEPROM_Read(0x22))<=13)
      PORTB = move;
      else PORTE = move;
      //counter2 in minutes
      }
      if(((EEPROM_Read(0x21)==30) && (EEPROM_Read(0x20)==00)) ||
      ((EEPROM_Read(0x21)==59) && (EEPROM_Read(0x20)==59)))
      move++; */
   }
}

void main() {
  ADC();
  RTC();
}
 
Last edited by a moderator:

You can break the codes up and see where the cause of it not working. With a full code like this, you might have trouble finding where :)
 

thank you very much for the help :) it finally worked :)
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top