CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

PIC12F675+PCF8574+LCD 16x2+ RTC DS1307
Goto page Previous  1, 2, 3  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Mon Nov 21, 2016 11:49 am     Reply with quote

Hi Mahatia,

Fundamentally, this is a 'help you do it' forum, not a 'do it for you' forum. IOW, we are happy to make suggestions and comment on *your* code, but we aren't going to write code for you.

I suspect you lost the interest of many with these words: "when I simulate it with Proteus". I know that many people, particularly students, favor Proteus for simulations, but those of us that build hardware and write code for a living recognize that Proteus can be a rat hole of wasted time, and so we tend to tune out.....

If you want to add some input buttons to your project, I recommend that you search for and implement PCM programmers 'button' routine. For a couple of switches it works great, and handles all the debounce issues you are likely to face.

Use the buttons to change some temporary time variables, eg. TempHours, TempMinutes, TempSeconds, etc., and then pass these variable to a routine that updates the real time clock. This is a fairly advanced topic, so we are here to help, but you've got to run with the ball first!

FWIW, I built a nice clock based on the DS3231 RTC and a 128x64 GLCD display a couple of years ago. It's got three push buttons, 'Mode/Enter', 'Inc', and 'Dec'. The Mode/Enter button allows me to step thru menus (Time set, Alarm set, etc.), and settings within menus, and the Inc(rement) and Dec(rement) buttons allow me to change the values contained on the menus. Once all the values have been set to my satisfaction, I use all these temporary values to update the RTC.
_________________
John

If it's worth doing, it's worth doing in real hardware!
temtronic



Joined: 01 Jul 2010
Posts: 9246
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Nov 21, 2016 4:11 pm     Reply with quote

re:
Quote:

But could tell me why the seconds takes a long time to increments when I simulate it with Proteus? this is my code:

the simple answer is that Proteus is BUSTED !! You should never,ever trust Proteus to be 100% accurate and functional. I could write a book about the shortfalls and errors within Proteus,actually thought that, but life is way to short.
Besides too few these days actually read books, or 500 page datasheets...

There are 2 certainties about Proteus

1) just because it 'works' within the simulation there is NO guarantee it WILL work in the 'real world'.

2) if it does work in the 'real world', it might not in Proteus.

Jay
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Nov 21, 2016 6:01 pm     Reply with quote

mahatia wrote:

#define Device_Address_DS1307_EEPROM 0xD0
void Write_Byte_To_DS1307_RTC(unsigned char, unsigned char);
unsigned char Read_Byte_From_DS1307_RTC(unsigned char);
void Write_Bytes_To_DS1307_RTC(unsigned char,unsigned char*,unsigned char);
void Read_Bytes_From_DS1307_RTC(unsigned char,unsigned char*,unsigned int);
void Set_DS1307_RTC_Time(unsigned char,unsigned char,unsigned char,unsigned char);
unsigned char* Get_DS1307_RTC_Time(void);
void Set_DS1307_RTC_Date(unsigned char,unsigned char,unsigned char,unsigned char);
unsigned char* Get_DS1307_RTC_Date(void);

The ds1307.h file (above) has no relation to the functions in ds1307.c
that you posted. Are you secretly using a different ds1307 driver than
the one you posted ? You said earlier about the ds1307.c you posted:
Quote:
I found this driver in this forum but I get many errors when I use it with my code

What ds1307 driver are you actually using ? Post it or post a link to it.
If you're using one that has the same functions as shown in bold above
then post it. I want to see it.
temtronic



Joined: 01 Jul 2010
Posts: 9246
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Nov 22, 2016 6:12 am     Reply with quote

this...
#define Device_Address_DS1307_EEPROM 0xD0

isn't quite true
At least I'm not aware of any EEPROM in a DS1307.
There is a small amount of battery backed CMOS RAM that I use for 'parameter' storage though. It's faster,simpler and 'byte' accessable unlike most EEPROMs

My gut tells me the OP has a mishmash of driver code and really neds to erase ALL of it and just use the 100% WORKING CCS driver.

Jay
mahatia



Joined: 14 Nov 2016
Posts: 23

View user's profile Send private message

PostPosted: Wed Nov 23, 2016 4:38 am     Reply with quote

Hello PCM programmer! I want to give you thanks for the several helps you've give for me!
At the beginning, I used the code that I've found here but I had got some problems with the PIC12F's ROM and I've changed it!
Now, i try to adjust minute and hour, I wrote this code but it doesn't work normally! I have already done all that I know! These are my codes:
I2C_LCD.c
Code:
#include <12F675.h>
#device *= 16
#fuses NOWDT, PROTECT, CPD, NOMCLR, INTRC_IO, BROWNOUT, PUT
#use delay (internal = 4MHz)
#byte GPIO = 0x00
#include "lcd.c"                         
#include "DS1307.c"
       
 void setup();

int i = 0;

void main()

 byte day;
        byte mth;
        byte year;
        byte hr;
        byte min;
        byte sec;
       

   
LCD_init();
return_:
ds1307_init();
ds1307_get_date(day,mth,year);  // Read date
ds1307_get_time(hr,min,sec);     
LCD_goto(4,0);
printf(LCD_putchar," %02d:\%02d:\%02d",hr,min,sec); // Display time
LCD_goto(5,1);
printf(LCD_putchar,"\%02d/\%02d/20\%02d",day,mth,year); // Display date
 char txt3 = "M";
char txt4 = "H";
 while(TRUE)
    {
   
    if(input(PIN_A1) == 0)
    i++;
    if (i == 1)
    {
     LCD_command(clear_display);
     LCD_goto(7,0);
     LCD_putchar(txt3);
     LCD_goto(7,1);
     printf(LCD_putchar, "%02d",min);
     delay_ms(99);
      if(input(PIN_A0) == 0)
     {min++;
     min = min;
     }
     }
     if(input(PIN_A1) == 0)
     i++;
     if (i == 2)
   {
     LCD_command(clear_display);
   LCD_goto(7,0);
     LCD_putchar(txt4);
     LCD_goto(7,1);
     printf(LCD_putchar, "%02d",hr);
     delay_ms(100);
     if(input(PIN_A0) == 0)
     {hr++;
     hr = hr;
     }
     }
     
   
     if(input(PIN_A1) == 0)
    i++;
    if (i == 3)
    goto return_;
   
   
 };
 }                                                                           
void setup()
{
    set_tris_a(0b01110000);         // GP0 GP1 GP2 input, All outputs
    disable_interrupts(GLOBAL);
    setup_comparator(NC_NC_NC_NC);                   
    setup_ADC(ADC_off);
    setup_ADC_ports(no_analogs);
    setup_timer_0(T0_internal);     
    setup_timer_1(T1_disabled);
    set_timer0(0); 
    set_timer1(0);
    LCD_init(); 
    delay_ms(100);       

 

lcd.c
Code:
#include "lcd.h"
                             

void LCD_init()
{                                     
       
   
    data_value = 0x30;
    PCF8574_write(data_value); 
                             

    data_value = 0x20;
    PCF8574_write(data_value); 
                   
    data_value |= 0x08;
    PCF8574_write(data_value); 
    delay_ms(dly);
    data_value &= 0xF7;
    PCF8574_write(data_value); 
    delay_ms(dly);

    LCD_command(_4_pin_interface | _2_row_display | _5x7_dots);         
    LCD_command(display_on | cursor_off | blink_off);     
    LCD_command(clear_display);                           
    LCD_command(cursor_direction_inc | display_no_shift);       
}   


void LCD_command(unsigned char value)
{                                   
    data_value &= 0xFB;
    PCF8574_write(data_value); 
    LCD_4bit_send(value);           
}
   

void LCD_send_data(unsigned char value)
{                               
    data_value |= 0x04;
    PCF8574_write(data_value); 
    LCD_4bit_send(value);
}                                       
   

void LCD_4bit_send(unsigned char lcd_data)       
{
    unsigned char temp = 0x00;
     
    temp = (lcd_data & 0xF0);
    data_value &= 0x0F;   
    data_value |= temp;   
    PCF8574_write(data_value); 
                                         
    data_value |= 0x08;
    PCF8574_write(data_value); 
    delay_ms(dly);
    data_value &= 0xF7;
    PCF8574_write(data_value); 
    delay_ms(dly);
   
   
   
    temp = (lcd_data & 0x0F);
    temp <<= 0x04;     
    data_value &= 0x0F; 
    data_value |= temp;                           
    PCF8574_write(data_value); 
               
     data_value |= 0x08;
    PCF8574_write(data_value); 
    delay_ms(dly);
    data_value &= 0xF7;
    PCF8574_write(data_value); 
    delay_ms(dly);




void LCD_putchar(char char_data)
{
    LCD_send_data(char_data);
}


void LCD_clear_home()
{
    LCD_command(clear_display);
    LCD_command(goto_home);
}


void LCD_goto(unsigned char x_pos,unsigned char y_pos)
{                                                   
    if(y_pos == 0)   
    {                             
        LCD_command(0x80 | x_pos);
       
    }
    else
    {                                             
        LCD_command(0x80 | 0x40 | x_pos);
    }
}

lcd.h
Code:
#include "PCF8574.c"


#define clear_display                           0x01
#define goto_home                               0x02
         
#define cursor_direction_inc                    (0x04 | 0x02)
#define cursor_direction_dec                    (0x04 | 0x00)
#define display_shift                           (0x04 | 0x01)
#define display_no_shift                        (0x04 | 0x00)

#define display_on                              (0x08 | 0x04)
#define display_off                             (0x08 | 0x02)
#define cursor_on                               (0x08 | 0x02)                           
#define cursor_off                              (0x08 | 0x00)
#define blink_on                                (0x08 | 0x01)
#define blink_off                               (0x08 | 0x00)
                                   
#define _8_pin_interface                        (0x20 | 0x10)
#define _4_pin_interface                        (0x20 | 0x00)
#define _2_row_display                          (0x20 | 0x08)
#define _1_row_display                          (0x20 | 0x00)
#define _5x10_dots                              (0x20 | 0x40)
#define _5x7_dots                               (0x20 | 0x00)
                                                   
#define dly                                     0x01
       


unsigned char data_value = 0x00;
                                   
                                                                         
void LCD_init(); 
void LCD_command(unsigned char value);
void LCD_send_data(unsigned char value);
void LCD_4bit_send(unsigned char lcd_data);                                     
void LCD_putstr(char *lcd_string);               
void LCD_putchar(char char_data);             
void LCD_clear_home();           
void LCD_goto(unsigned char x_pos, unsigned char y_pos);

ds1307.c
Code:
#include <DS1307.h>
#define RTC_SDA  pin_A5
#define RTC_SCL  pin_A4

#use i2c(master, sda=RTC_SDA, scl=RTC_SCL)

byte bin2bcd(byte binary_value);
byte bcd2bin(byte bcd_value);

void ds1307_init(void)
{
 
   output_float(RTC_SCL);
   output_float(RTC_SDA);//niverina indroa io teo

}



void ds1307_get_date(byte &day, byte &mth, byte &year)

{
  i2c_start();
  i2c_write(0xD0);
  //delay_us(1);
  i2c_write(0x04);            // Start at REG 3 - Day of week
  i2c_start();
  i2c_write(0xD1);
 
  day  = bcd2bin(i2c_read(1) & 0x3f);   // REG 4
  mth  = bcd2bin(i2c_read(1) & 0x1f);   // REG 5
  year = bcd2bin(i2c_read(0));            // REG 6
  i2c_stop();
}

void ds1307_get_time(byte &hr, byte &min, byte &sec)
{
  i2c_start();
  i2c_write(0xD0);
  i2c_write(0x00);            // Start at REG 0 - Seconds
  i2c_start();
  i2c_write(0xD1);
  sec = bcd2bin(i2c_read(1) & 0x7f);
  min = bcd2bin(i2c_read(1) & 0x7f);
  hr  = bcd2bin(i2c_read(0) & 0x3f);
  i2c_stop();

}

byte bin2bcd(byte binary_value)
{
  byte temp;
  byte retval;

  temp = binary_value;
  retval = 0;

  //while(1) IO NO TEO
  while(TRUE)
  {
    // Get the tens digit by doing multiple subtraction
    // of 10 from the binary value.
    if(temp >= 10)
    {
      temp -= 10;
      retval += 0x10;
    }
    else // Get the ones digit by adding the remainder.
    {
      retval += temp;
      break;
    }
  };

  return(retval);
}


// Input range - 00 to 99.
byte bcd2bin(byte bcd_value)
{
  byte temp;

  temp = bcd_value;
  // Shifting upper digit right by 1 is same as multiplying by 8.
  temp >>= 1;
  // Isolate the bits for the upper digit.
  temp &= 0x78;

  // Now return: (Tens * 8) + (Tens * 2) + Ones

  return(temp + (temp >> 2) + (bcd_value & 0x0f));
  }

ds1307.h
Code:
#ifndef __DS1307_H
#define __DS1307_H



// Define DS1307 i2c device address
#define Device_Address_DS1307_EEPROM   0xD0
// Function Declarations
void ds1307_init(void);
void ds1307_get_date(byte &day, byte &mth, byte &year);
void ds1307_get_time(byte &hr, byte &min, byte &sec);
byte bin2bcd(byte binary_value);
byte bcd2bin(byte bcd_value);

PCF8574.c
Code:
#include "PCF8574.h"
     
                                       
unsigned char PCF8574_read()
{
    unsigned char port_byte = 0;       
    i2c_start();                             
    i2c_write(PCF8574_read_cmd);       
    port_byte = i2c_read(0);             
    i2c_stop();
    return port_byte;
}                                                         


void PCF8574_write(unsigned char data_byte)                     
{
    i2c_start();
    i2c_write(PCF8574_write_cmd);               
    i2c_write(data_byte);
    i2c_stop();
}                             

PCF8574.h
Code:
#define PCF8574_address       0x40 //A2, A1, A0, vers la masse

#define PCF8574_write_cmd      PCF8574_address             
#define PCF8574_read_cmd      (PCF8574_address + 1)   

#define SDA_pin               pin_A5
#define SCL_pin                 pin_A4     


#use I2C(Master, SDA = SDA_pin, SCL = SCL_pin)

             
unsigned char PCF8574_read();               
void PCF8574_write(unsigned char data_byte);                                   

thank you PCM programmer, please guide me!
mahatia



Joined: 14 Nov 2016
Posts: 23

View user's profile Send private message

PostPosted: Thu Nov 24, 2016 1:20 am     Reply with quote

Hello PCM programmer! At last, I can finally do it! Razz I used the pin GP2 interrupt to configure the digital clock!
this is my code!
I2C_LCD.c
Code:
#include <12F675.h>
#device *= 16
#fuses NOWDT, PROTECT, CPD, NOMCLR, INTRC_IO, BROWNOUT, PUT
#use delay (internal = 4MHz)
//#byte GPIO = 0x00
#bit gpio5 =0x05.5
#bit gpio4 =0x05.4
#bit gpio3 =0x05.3
#bit gpio2 =0x05.2
#bit gpio1 =0x05.1
#bit gpio0 =0x05.0
#include "lcd.c"                         
#include "DS1307.c"
//#define INT_RA3 0x080B08
//#define INT_GP0          0x0010B08
//#define Int_Ext1 GP0
//void setup();

int i = 0;
int cnt;

//#INT_RA3

//void int_on_change_GP03 (void)
//{

 //}
 #INT_EXT
  void EXT_ISR(void)
 {
   disable_interrupts(GLOBAL);
   
 i++;
 cnt=0;
 enable_interrupts(GLOBAL);
 }

#byte ADCON0 = 0x1F
#byte ANSEL = 0x9F
#byte CMCON = 0x19
void main()
 {
    ADCON0 = 0; // ADC off
    ANSEL = 0; // GPIO pins 0,1,2 and 4 set to all digital
    CMCON = 7; // Comparators off
    set_tris_a(0b001100);
    setup_counters(rtcc_internal,rtcc_div_2);
    enable_interrupts(int_ext);
    ext_int_edge(h_to_l);
    enable_interrupts(global);
    //enable_interrupts(INT_RA3);




        byte day;
        byte mth;
        byte year;
        byte hr;
        byte min;
        byte sec;
        //setup();
        LCD_init();

char txt3 = "M";
char txt4 = "O";
char txt5 = "D";
char txt6 = "v";
char txt7 = "T";

   while(TRUE)
    {
  if((i == 0)||(i == 6))
  {
      ds1307_init();
      ds1307_get_date(day,mth,year);  // Read date
      ds1307_get_time(hr,min,sec);     
      LCD_goto(4,0);
      printf(LCD_putchar," %02d:\%02d:\%02d",hr,min,sec); // Display time
      LCD_goto(5,1);
      printf(LCD_putchar,"\%02d/\%02d/20\%02d",day,mth,year); // Display date
   }
else
    {
      if(cnt==0)
       {
         LCD_command(clear_display);
         cnt=1;
       }
       if (i==1)
        {
           LCD_goto(7,0);
           LCD_putchar(txt3);
           LCD_goto(7,1);
           printf(LCD_putchar, "%02d",min);
         }
       if (i==2)
        {
           LCD_goto(7,0);
           LCD_putchar(txt4);
           LCD_goto(7,1);
           printf(LCD_putchar, "%02d",hr);
         }
        if (i ==3)
        {
           LCD_goto(7,0);
           LCD_putchar(txt5);
           LCD_goto(7,1);
           printf(LCD_putchar, "%02d",day);
          }
          if (i ==4)
        {
           LCD_goto(7,0);
           LCD_putchar(txt6);
           LCD_goto(7,1);
           printf(LCD_putchar, "%02d",mth);
          }
           if (i ==5)
        {
           LCD_goto(7,0);
           LCD_putchar(txt7);
           LCD_goto(7,1);
           printf(LCD_putchar, "%02d",year);
          }
      }
   
 };   

}

Now, I am trying to use GP3 interrupt to increment variables (hr, min, and so on) but I always get error! How to activate interrupt with GP3? Thank you so much! Mahatia
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Thu Nov 24, 2016 2:41 am     Reply with quote

First thing. Repeat fifty times.

Code must _never_ 'enable_interrupts(GLOBAL)' inside an interrupt. Doing so, is a sure way of triggering a chip crash.
The disable is pointless, but does no harm.

The PIC is different from many other chips. On the PIC (PIC12, 14 & 16 - slightly different on the PIC18), the _hardware_ disables the global interrupt bit as soon as the call to the interrupt handler occurs. It also re-enables this the instruction _after_ the chip returns from the interrupt. Enabling the interrupt inside the handler means (since the interrupt flag is still set), the code will loop and interrupt itself. Uurgh.....

There is not a separate interrupt on GP3. You have to use the 'interrupt on change' ability. This will interrupt on all changes on the pin (both rising and falling).
Code:

//global declaration
static int1 last3;

//To enable this in main
     last=input(PIN_A3); //latch must be read before starting
     clear_interrupts(INT_RA);
     enable_interrupts(INT_RA3); //the '3' here says to enable only on A3


//then to handle it:

//This will be called for _all_ enabled RA interrupts
#INT_RA
void change(void)
{
     //Now for just one bit high to low
     if (input(PIN_A3))
     {
          //pin is high
          if (last3==0)
               last3=1;
     }
     else
     {
          //pin is low
          if (last3==1)
          {
               last3=0;
               //code here for 'high to low'
           
          }
     }
}


As a general comment, get rid of all your register declarations. Just use CCS code:
Code:


       //ADCON0 = 0; // ADC off
       setup_adc(ADC_OFF);
       //ANSEL = 0; // GPIO pins 0,1,2 and 4 set to all digital
       setup_adc_ports(NO_ANALOGS);
       //CMCON = 7; // Comparators off
       setup_comparators(NC_NC);


This would work if you move to another chip, while the direct register accesses would have you diving for the data sheet.
If you 'must' do direct accesses, then let the compiler look them up for you:

#byte ADCON0 = getenv("SFR:ADCON0")
#byte ANSEL = getenv("SFR:ANSEL")
#byte CMCON = getenv("SFR:CMCON")
mahatia



Joined: 14 Nov 2016
Posts: 23

View user's profile Send private message

PostPosted: Thu Nov 24, 2016 2:58 am     Reply with quote

Thanks a lot! I will try it.
mahatia



Joined: 14 Nov 2016
Posts: 23

View user's profile Send private message

PostPosted: Thu Nov 24, 2016 3:47 am     Reply with quote

Could you explain it more please? I have tried it and get an error! maybe I don't realy understand what you mean! I use PIC12F675 and I have use an interruption with GP2, and now, I need some interruption with GP3 or GP0, thanks a lot!
mahatia



Joined: 14 Nov 2016
Posts: 23

View user's profile Send private message

PostPosted: Thu Nov 24, 2016 1:35 pm     Reply with quote

so, I can't use any interruption with GP3? can I have external interrupt with GP0?
temtronic



Joined: 01 Jul 2010
Posts: 9246
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Nov 24, 2016 2:23 pm     Reply with quote

I know English is not everyone's 1st language but for PIC datasheets you NEED to know it.
Microchip very clearly show on table 1-1 on page 6 (of the version I have) that any/all 6 of the GPpins (GP0 ... GP5) do have 'interrupt-on-change' functionality. Section 3.2.2 details what is required and how to access this feature. As such, when properly configured any of those pins can be used for say 'push button pressed' for increment/decrement a counter.
As for using CCS C to do this, simply open the manual (press F11 with your project open) and search for interrupts, there are several paragraphs and sections about this.
You can also look in the examples folder, pretty sure CCS supplies an example. If not you can always try 'Google' as you are NOT the 1st one to have ever used interrupts in the 12F675!


Jay
mahatia



Joined: 14 Nov 2016
Posts: 23

View user's profile Send private message

PostPosted: Fri Nov 25, 2016 12:15 am     Reply with quote

ok!
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Fri Nov 25, 2016 2:53 am     Reply with quote

The reason it'd not compile (in a suitable program), is one error:

last=input(PIN_A3); //latch must be read before starting

I left the '3' out of the variable name. Should be 'last3', not 'last'.

This is because while typing the code I decided you might want to use the same code on multiple pins. When I originally started typing I was doing it for one pin, then decided the same code might be wanted for multiple pins, so added the '3', and missed this one....

However the code as posted cannot compile anyway. Look at the big comment I have added when you try to load the LCD driver... Sad

Temtronic has correctly pointed out what has to happen. It is a fundamental case of understanding the chip. There is _one_ standard interrupt (INT_EXT), on GP2. Just one. There is one other interrupt called an 'interrupt on change'. There is not actually an 'INT_RA3'. Just INT_RA. The point about the '3' is that uised when enabling the interrupt, it programs which pin(s) it is to happen on. This can be used on one or more of the pins on the chip. However unlike INT_EXT, it triggers on every 'change' on the selected pins. So the code in the handler has to work out which pins have changed, and which 'direction' the signal has changed in. The code I posted is to do this for GP3. It records what level is on the pin before starting, and then when the interrupt handler is called, sees whether the signal has changed 'high to low' or 'low to high'. You could actually get rid of INT_EXT, and use just a single handler. As:
Code:

#include <12F675.h>
#device *= 16
#fuses NOWDT, PROTECT, CPD, NOMCLR, INTRC_IO, BROWNOUT, PUT
#use delay (internal = 4MHz)

//Now there are problems here. You are including an LCD driver
//but there are not enough pins on the chip for this. Duh.....
//Even in 'nibble' mode, an LCD needs 6 output pins to drive it. The
//whole chip only has six pins (five output), and you are using two pins
//for interrupts and two for the bus to the clock chip....
//it can't compile from this point on.... :(
#include "lcd.c"                         
#include "DS1307.c"

//global variables
int8 i = 0;
int8 cnt;
static int1 last2, last3; //single bits to record the 'last'' state of the pins

//This will be called for _all_ enabled RA interrupts
#INT_RA
void change(void)
{
     //Now first RA2
     if (input(PIN_A2))
     {
          //pin is high
          if (last2==0)
               last2=1;
     }
     else
     {
          //pin is low
          if (last2==1)
          {
               last2=0;
               //code here for 'high to low' on RA2
               i++;
               cnt=0;
          }
     }
     //Now RA3
     if (input(PIN_A3))
     {
          //pin is high
          if (last3==0)
               last3=1;
     }
     else
     {
          //pin is low
          if (last3==1)
          {
               last3=0;
               //code here for 'high to low' on RA3

          }
     }
}

void main(void)
{
     byte day;
     byte mth;
     byte year;
     byte hr;
     byte min;
     byte sec; //variables should only be declared at the start of a
     //code section. CCS will accept other declarations, but it can
     //give problems and is not standard (historical) 'C'.


     char txt3 = 'M';
     char txt4 = 'O';
     char txt5 = 'D';
     char txt6 = 'v';
     char txt7 = 'T;
     //read a C textbook, and understand the difference between a
     //single inverted comma, for a 'character' declaration, and double
     //inverted commas.....

     setup_adc(ADC_OFF); //ADC off
     setup_adc_ports(NO_ANALOGS);
     setup_comparators(NC_NC); //comparators off

     set_tris_a(0b001100);
     setup_counters(rtcc_internal,rtcc_div_2);
     last3=input(PIN_A3); //latch must be read before starting
     last2=input(PIN_A2); //and the same for A2
     clear_interrupts(INT_RA);
     enable_interrupts(INT_RA3 | INT_RA2); //enable on RA3 and RA2
     enable_interrupts(global);

     while(TRUE)
     {
         if((i == 0)||(i == 6))
         {
             ds1307_init();
             ds1307_get_date(day,mth,year);  // Read date
             ds1307_get_time(hr,min,sec);     
             LCD_goto(4,0);
             printf(LCD_putchar," %02d:\%02d:\%02d",hr,min,sec); // Display time
             LCD_goto(5,1);
             printf(LCD_putchar,"\%02d/\%02d/20\%02d",day,mth,year); // Display date
        }
        else
        {
             if(cnt==0)
             {
                 LCD_command(clear_display);
                 cnt=1;
             }
             switch (i)
             {
             case 1:
                 LCD_goto(7,0);
                 LCD_putchar(txt3);
                 LCD_goto(7,1);
                 printf(LCD_putchar, "%02d",min);
                 break;
             case 2:
                 LCD_goto(7,0);
                 LCD_putchar(txt4);
                 LCD_goto(7,1);
                 printf(LCD_putchar, "%02d",hr);
                 break;
             case 3:
                 LCD_goto(7,0);
                 LCD_putchar(txt5);
                 LCD_goto(7,1);
                 printf(LCD_putchar, "%02d",day);
                 break;
             case 4:
                 LCD_goto(7,0);
                 LCD_putchar(txt6);
                 LCD_goto(7,1);
                 printf(LCD_putchar, "%02d",mth);
                 break;
             case 5:
                 LCD_goto(7,0);
                 LCD_putchar(txt7);
                 LCD_goto(7,1);
                 printf(LCD_putchar, "%02d",year);
                 break;
             } //end of switch
        }
    }
}


Now this can't compile, since you just don't have enough pins on the chip.
The LCD interface alone, needs more pins than the chip has.
mahatia



Joined: 14 Nov 2016
Posts: 23

View user's profile Send private message

PostPosted: Fri Nov 25, 2016 4:41 am     Reply with quote

Ok! Thank you very much. About the LCD, I've used an expander PCF8574 and it works fine. I can display date and hour information on it. My only problem was the interruption. Now, I am going to try it. Thank you very much.
mahatia



Joined: 14 Nov 2016
Posts: 23

View user's profile Send private message

PostPosted: Wed Nov 30, 2016 2:35 am     Reply with quote

Hello everybody! I don't know what happened with my code but when I simulate it, variables don't increment. Could someone say me the reason please?
lcd_I2c.c
Code:
#include <12F675.h>
#device *= 16
#fuses NOWDT, PROTECT, CPD, NOMCLR, INTRC_IO, BROWNOUT, PUT
#use delay (internal = 4MHz)

#byte GPIO = 0x00

#include "lcd.c"                         
#include "DS1307.c"

//global variables
int8 i = 0;
int8 cnt;
static int1 last2, last0; //single bits to record the 'last'' state of the pins
     byte day;
     byte mth;
     byte year;
     byte hr;
     byte min;
     byte sec;

//This will be called for _all_ enabled RA interrupts
#INT_RA
void change(void)
{
     //Now first RA2
     if (input(PIN_A2))
     {
          //pin is high
          if (last2==0)
               last2=1;
     }
     else
     {
          //pin is low
          if (last2==1)
          {
               last2=0;
               //code here for 'high to low' on RA2
               disable_interrupts(GLOBAL);
               i++;
               cnt=0;
               enable_interrupts(GLOBAL);
          }
     }
     //Now RA3
     if (input(PIN_A0))
     {
          //pin is high
          if (last0==0)
               last0=1;
     }
     else
     {
          //pin is low
          if (last0==1)
          {
               last0=0;
               //code here for 'high to low' on RA3
               
               if (i == 1)
            {
           
            if(min<59)min++;
            else min = 0;
            }
              if (i == 2)
            {
            if(hr<24)hr++;
            else hr = 0;
            }
             if (i == 3)
            {
            if(day<31)day++;
            else day = 1;
            }
              if (i == 4)
                  {
                     if(mth<13)mth++;
                     {
                        if (mth ==13)
                           {
                           mth = 1;
                           year++;
                               }
            }
           
            }
          }
     }
     //clear_interrupt(INT_RA); vo napetaka
}

void main(void)
{
      char txt3 = "M";
      char txt4 = "O";
      char txt5 = "D";
      char txt6 = "V";

     setup_adc(ADC_OFF); //ADC off
     setup_adc_ports(NO_ANALOGS);
     setup_comparator(NC_NC_NC_NC);                     //comparators off

     set_tris_a(0b001100);
     setup_counters(rtcc_internal,rtcc_div_2);
     last0=input(PIN_A0); //latch must be read before starting
     last2=input(PIN_A2); //and the same for A2
     clear_interrupt(INT_RA);
     enable_interrupts(INT_RA0 | INT_RA2); //enable on RA3 and RA2
     enable_interrupts(global);
     LCD_init();
     ds1307_init();
     ds1307_get_date(day,mth,year);  // Read date
     ds1307_get_time(hr,min,sec);
           
     while(TRUE)
     {
         if((i == 0)||(i == 5))
         {
             LCD_goto(4,0);
             printf(LCD_putchar," %02d:\%02d:\%02d",hr,min,sec); // Display time
             LCD_goto(5,1);
             printf(LCD_putchar,"\%02d/\%02d/20\%02d",day,mth,year); // Display date
        }
        else
        {
             if(cnt==0)
             {
                 LCD_command(clear_display);
                 cnt=1;
             }
             switch (i)
             {
             case 1:
                 LCD_goto(7,0);
                 LCD_putchar(txt3);
                 LCD_goto(7,1);
                 printf(LCD_putchar, "%02d",min);
                 break;
             case 2:
                 LCD_goto(7,0);
                 LCD_putchar(txt4);
                 LCD_goto(7,1);
                 printf(LCD_putchar, "%02d",hr);
                 break;
             case 3:
                 LCD_goto(7,0);
                 LCD_putchar(txt5);
                 LCD_goto(7,1);
                 printf(LCD_putchar, "%02d",day);
                 break;
             case 4:
                 LCD_goto(7,0);
                 LCD_putchar(txt6);
                 LCD_goto(7,1);
                 printf(LCD_putchar, "%02d",mth);
                 break;
            case 5:
            i = 0;
            break;
//!                 LCD_goto(7,0);
//!                 LCD_putchar(txt7);
//!                 LCD_goto(7,1);
//!                 printf(LCD_putchar, "%02d",year);
//!                 break;
             } //end of switch
        }
    };
}

Thank you so much.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2, 3  Next
Page 2 of 3

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group