Author Topic: Digital Clock code  (Read 1166 times)

0 Members and 1 Guest are viewing this topic.

Offline radhikaTopic starter

  • Regular Contributor
  • *
  • Posts: 105
  • Country: in
Digital Clock code
« on: January 24, 2019, 11:17:05 am »
I am building digital clock code. I have followed this link http://saeedsolutions.blogspot.com/2015/06/pic16f877a-based-digital-clock-using.html

I am using PIC8F4520 and OLED display.
I understand the logic and made changes in code. But, when I upload this code in Proteus, Their is nothing on OLED.
Can you suggest me that is SSD1306_PutC is right command to display time or not and what else are changes I HAVE TO MAKE?
Code: [Select]
#include "Includes.h"
#include <SSD1306.c>
#include "Timer.c"
#include "ISR.c"

#fuses HS,NOWDT,NOPROTECT,NOLVP                       
#use delay(clock = 20MHz)
#use I2C(MASTER, I2C1, FAST = 400000, stream = SSD1306_STREAM)  // Initialize I2C

void DisplayTimeToLCD(unsigned int,unsigned int,unsigned int,unsigned char);

int h, m, s;
char State;

// Main Function
void main(void)
{
   unsigned char State = NORMAL_STATE;      // To store current state

   set_tris_d(0x8c); // set direction registers of bits RD2,RD3,RD4 to be input button RD2
   
    // Initialize the SSD1306 OLED with an I2C addr = 0x7A (default address)
    SSD1306_Init(SSD1306_SWITCHCAPVCC, SSD1306_I2C_ADDRESS); 
    // clear the display
    SSD1306_ClearDisplay();
           
   Init1msecTimerInterrupt();      // Start 1 msec timer

   while(1)
   {
      if(State == NORMAL_STATE)
      {
         if(input (PIN_D2))            // If 'Set Time' button pressed
         {
            State = CONFIG_HR_STATE;   // Change state to Hr config

            while(input (PIN_D2))         // Wait for button to be released
               UpdateTimeCounters(State);// Keep updating time counters
         }
      }
      else               // If state is not NORMAL_STATE
      {
         if(input (PIN_D3))         // If Up button pressed
         {
            while(input (PIN_D3))      // Wait for button to be released
               UpdateTimeCounters(State);// Keep updating time counters

            switch(State)   // Increment counter
            {
            case CONFIG_HR_STATE:    hrCounter++;   break;
            case CONFIG_MIN_STATE:   minCounter++;   break;
            case CONFIG_SEC_STATE:   secCounter++;   break;
            }
         }
         else if(input (PIN_D7))      // If Down button pressed
         {
            while(input (PIN_D7))      // Wait for button to be released
               UpdateTimeCounters(State);// Keep updating time counters

            switch(State)   // Decrement counter
            {
            case CONFIG_SEC_STATE:   if(secCounter==0)
                                 secCounter = 59;
                              else
                                 secCounter--;
                              break;
            case CONFIG_MIN_STATE:   if(minCounter==0)
                                 minCounter = 59;
                              else
                                 minCounter--;
                              break;
            case CONFIG_HR_STATE:    if(hrCounter==0)
                                 hrCounter = 23;
                              else
                                 hrCounter--;
                              break;
            }
         }
         else if(input (PIN_D2))      // If 'Set Time' button pressed again
         {
            while(input (PIN_D2))      // Wait for button to be released
               UpdateTimeCounters(State);// Keep updating time counters

            switch(State)   // Shift to next state
            {
            case CONFIG_HR_STATE:    State = CONFIG_MIN_STATE;   break;
            case CONFIG_MIN_STATE:   State = CONFIG_SEC_STATE;   break;
            case CONFIG_SEC_STATE:   State = NORMAL_STATE;      break;
            }
           
         }   
      }

      UpdateTimeCounters(State);       // Update sec, min, hours counters

      if( msCounter == 0 )    // msCounter becomes zero after exact one sec
      {
         // Display time in HH:MM:SS format
         SSD1306_GotoXY(6, 4);
         DisplayTimeToLCD(hrCounter, minCounter, secCounter, NORMAL_STATE);   
      }
      else if( msCounter == 500 && State != NORMAL_STATE )   
      {
         // Display time with either HH or MM or SS missing
         // according to the State, to create an animated experience
            SSD1306_GotoXY(6, 4);
         DisplayTimeToLCD(hrCounter, minCounter, secCounter, State);   
      }
     }
       
}

// Displays time in HH:MM:SS format
void DisplayTimeToLCD( unsigned int h, unsigned int m, unsigned int s, unsigned char State )   
{
   SSD1306_ClearDisplay();     // Move cursor to zero location and clear screen

   if(State == CONFIG_HR_STATE)
   {
      SSD1306_PutC( ' ' );   // Display Spaces
      SSD1306_PutC( ' ' );     
   }
   else
   {
      SSD1306_PutC( (h/10)+0x30 );   // Display Hour
      SSD1306_PutC( (h%10)+0x30 );
   }

   //Display ':'
   SSD1306_PutC(':');

   if(State == CONFIG_MIN_STATE)
   {
      SSD1306_PutC( ' ' );   // Display Spaces
      SSD1306_PutC( ' ' );     
   }
   else
   {
      SSD1306_PutC( (m/10)+0x30 );   //Display Minutes
      SSD1306_PutC( (m%10)+0x30 );
   }

   //Display ':'
   SSD1306_PutC(':');

   if(State == CONFIG_SEC_STATE)
   {
      SSD1306_PutC( ' ' );   // Display Spaces
      SSD1306_PutC( ' ' );     
   }
   else
   {
      SSD1306_PutC( (s/10)+0x30 );   //Display Seconds
      SSD1306_PutC( (s%10)+0x30 );
   }
}
 

Offline Doctorandus_P

  • Super Contributor
  • ***
  • Posts: 3387
  • Country: nl
Re: Digital Clock code
« Reply #1 on: January 24, 2019, 12:25:06 pm »
If your OLED display does not work in Proteus, then simplify things.

Make a new project (or copy the clock project) and remove all clock related stuff.

Start by printing a line of text on the display, or even a single pixel.
If you can draw a pixel, then you know that the simulator works, the schematic in the simulator works, and a whole bunch of other stuff on some low hardware like levels.

Once you have a working foundation start addding more complicated stuff.
 

Offline radhikaTopic starter

  • Regular Contributor
  • *
  • Posts: 105
  • Country: in
Re: Digital Clock code
« Reply #2 on: January 24, 2019, 04:59:40 pm »
Its working fine, but for this code its not displaying anything
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf