Digital Clock code - Page 1
Forum section: electronics / general

author: radhika
author_rank: Regular Contributor
timestamp: January 24, 2019, 11:17:05 am
content: I am building digital clock code. I have followed this linkhttp://saeedsolutions.blogspot.com/2015/06/pic16f877a-based-digital-clock-using.htmlI 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:#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 I2Cvoid DisplayTimeToLCD(unsigned int,unsigned int,unsigned int,unsigned char);int h, m, s;char State;// Main Functionvoid main(void){unsigned char State = NORMAL_STATE; // To store current stateset_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 displaySSD1306_ClearDisplay();Init1msecTimerInterrupt(); // Start 1 msec timerwhile(1){if(State == NORMAL_STATE){if(input (PIN_D2)) // If 'Set Time' button pressed{State = CONFIG_HR_STATE; // Change state to Hr configwhile(input (PIN_D2)) // Wait for button to be releasedUpdateTimeCounters(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 releasedUpdateTimeCounters(State);// Keep updating time countersswitch(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 releasedUpdateTimeCounters(State);// Keep updating time countersswitch(State) // Decrement counter{case CONFIG_SEC_STATE: if(secCounter==0)secCounter = 59;elsesecCounter--;break;case CONFIG_MIN_STATE: if(minCounter==0)minCounter = 59;elseminCounter--;break;case CONFIG_HR_STATE: if(hrCounter==0)hrCounter = 23;elsehrCounter--;break;}}else if(input (PIN_D2)) // If 'Set Time' button pressed again{while(input (PIN_D2)) // Wait for button to be releasedUpdateTimeCounters(State);// Keep updating time countersswitch(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 countersif( msCounter == 0 ) // msCounter becomes zero after exact one sec{// Display time in HH:MM:SS formatSSD1306_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 experienceSSD1306_GotoXY(6, 4);DisplayTimeToLCD(hrCounter, minCounter, secCounter, State);}}}// Displays time in HH:MM:SS formatvoid DisplayTimeToLCD( unsigned int h, unsigned int m, unsigned int s, unsigned char State ){SSD1306_ClearDisplay(); // Move cursor to zero location and clear screenif(State == CONFIG_HR_STATE){SSD1306_PutC( ' ' ); // Display SpacesSSD1306_PutC( ' ' );}else{SSD1306_PutC( (h/10)+0x30 ); // Display HourSSD1306_PutC( (h%10)+0x30 );}//Display ':'SSD1306_PutC(':');if(State == CONFIG_MIN_STATE){SSD1306_PutC( ' ' ); // Display SpacesSSD1306_PutC( ' ' );}else{SSD1306_PutC( (m/10)+0x30 ); //Display MinutesSSD1306_PutC( (m%10)+0x30 );}//Display ':'SSD1306_PutC(':');if(State == CONFIG_SEC_STATE){SSD1306_PutC( ' ' ); // Display SpacesSSD1306_PutC( ' ' );}else{SSD1306_PutC( (s/10)+0x30 ); //Display SecondsSSD1306_PutC( (s%10)+0x30 );}}

author: Doctorandus_P
author_rank: Super Contributor
timestamp: January 24, 2019, 12:25:06 pm
content: 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.

author: radhika
author_rank: Regular Contributor
timestamp: January 24, 2019, 04:59:40 pm
content: Its working fine, but for this code its not displaying anything