hi, here is a code, can any one tell me why its not working properly, its digital clock
same code like this i use in ASM that working prfct, but its not working
//#include <AT89Cstm.h> /* special function register declarations */ //#include <stdio.h> #ifndef __REG52_H__ #define __REG52_H__ sfr DataPort = 0xA0; // Port 2 sfr p0 = 0x80; sfr p1 = 0x90; sfr p2 = 0xA0; sfr p3 = 0xB0; sbit Seg1 = 0x90; // Port 1 pin 0, Sec1 sbit Seg2 = 0x91; // Port 1 pin 1, sbit Seg3 = 0x92; // Port 1 pin 2, sbit Seg4 = 0x93; // Port 1 pin 3, sbit Seg5 = 0x94; // Port 1 pin 4, sbit Seg6 = 0x95; // Port 1 pin 5, Hor 2 sbit ShowTime = 0xb6; // port 0.6 sbit SetTime = 0xb7; // port 0.7 sbit PulseIn = 0xB2; // port 3.2 sbit IncrementPressed = 0x97; // port 3.3 Min or hor Increment sbit DecrementPressed = 0x96; // port 3.4 Min or hor Decrement sbit MINorHOr = 0xB5; // port 3.5 Min or hor selection sbit SegHL = 0xB3; // Segment common High or Low port 3.2 sbit DataHL = 0xB4; // Segment Data High or Low #endif unsigned char sec=0,min=10,hour=15,day=1,mnth=1,year=2011; unsigned char SegData[12] = {0x3F,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00,0xB9}; // 0 1 2 3 4 5 6 7 8 9 blnk C // gfedcba gfedcba hgfedcba // 0111111 1001111 .. .. .. 10111001 bit SecStored = 0; bit IncrementStored = 0; bit DecrementStored = 0; bit TimeStop = 0; bit TimeSaved = 0; bit SendData = 0; unsigned char SendCount = 0; void ShowSec(); void ShowMin(); void ShowHour(); void IncSec(); void IncMin(); void IncHour(); //void DecSec(); void DecMin(); void DecHour(); void Delay(); void ScanSecond(); void ScanIncrement(); void ScanInputs(); void ScanDecrement(); void ScanDecrement(); void main() { unsigned int temp=0; p0 = 255; p1 = 0; p2 = 0; p3 = 255; while (temp !=1) { if (SetTime == 0 || ShowTime == 0) { ShowSec(); ShowMin(); ShowHour(); if (SetTime == 0) { ScanIncrement(); ScanDecrement(); } } else { if(TimeStop == 1) { TimeStop = 0; } p1 = 255; p2 = 255; } ScanSecond(); } } void ScanSecond() { if (PulseIn == 1 && SecStored == 0 && TimeStop == 0) // Scan Second Pulse { IncSec(); SecStored = 1; } else { if (PulseIn == 0 && SecStored == 1) { SecStored = 0; } } } void ScanIncrement() { if (IncrementPressed == 0 && IncrementStored == 0 ) // Scan Increment Pulse { if (MINorHOr == 1 ) { IncMin(); SendData = 1; } else { IncHour(); SendData = 1; } IncrementStored = 1; TimeStop = 1; sec = 0; } else { if (IncrementPressed == 1 && IncrementStored == 1) { IncrementStored = 0; } } } void ScanDecrement() { if (DecrementPressed == 0 && DecrementStored == 0 ) // Scan Decrement Pulse { if (MINorHOr == 1 ) { DecMin(); SendData = 1; } else { DecHour(); SendData = 1; } DecrementStored = 1; TimeStop = 1; sec = 0; } else { if (DecrementPressed == 1 && DecrementStored == 1) { DecrementStored = 0; } } } void ShowSec() { Seg6 = (~SegHL); if(DataHL == 0) DataPort = SegData[sec % 10]; else DataPort = (~(SegData[sec % 10])); Seg1 = SegHL; Delay(); Seg1 = (~SegHL); if(DataHL == 0) DataPort = SegData[sec / 10]; else DataPort = (~(SegData[sec / 10])); Seg2 = SegHL; Delay(); } void ShowMin() { Seg2 = (~SegHL); if(DataHL == 0) DataPort = SegData[min % 10]; else DataPort = (~(SegData[min % 10])); Seg3 = SegHL; Delay(); Seg3 = (~SegHL); if(DataHL == 0) DataPort = SegData[min / 10]; else DataPort = (~(SegData[min / 10])); Seg4 = SegHL; Delay(); } void ShowHour() { Seg4 = (~SegHL); if(DataHL == 0) DataPort = SegData[hour % 10]; else DataPort = (~(SegData[hour % 10])); Seg5 = SegHL; Delay(); Seg5 = (~SegHL); if(DataHL == 0) DataPort = SegData[hour / 10]; else DataPort = (~(SegData[hour / 10])); Seg6 = SegHL; Delay(); } void IncSec() { sec = sec + 1; if(sec==60) { sec = 0; if (SetTime == 1 ) IncMin(); } } void IncMin() { min = min + 1; if(min==60) { min=0; if (SetTime == 1 ) IncHour(); } } void IncHour() { hour = hour + 1; if(hour==24) { hour = 0; //IncHour(); } } /* void DecSec() { sec = sec - 1; if(sec==255) { sec = 59; if (SetTime == 1 ) DecMin(); } } */ void DecMin() { min = min - 1; if(min==255) { min=59; if (SetTime == 1 ) DecHour(); } } void DecHour() { hour = hour - 1; if(hour==255) { hour = 23; //IncHour(); } } void Delay() { unsigned int i; for(i=0; i<100; i++) { } }
One thing you are not considering when saying "it's working prfctly [sic!]" is what safety margins you have, and what unexpected changes that may result in the code failing.
You are not in control of the delay times for your software delay. It may be enough with a version change of the compiler for the program to fail. Or you may notice that some other part of the code is too large or too slow and change optimization settings. And oops, your previously working and "well tested" code isn't working anymore.
An important thing to learn: When something seems to work, it doesn't prove that you have a good solution. Just that it seems to work.
You might have a car. And maybe the brake system seems to work. But what safety margin does it have? What happens if the brake lines ruptures if you press just one percent harder on the brake pedal? Would you say such a brake system works? Wouldn't you expect the car manufacturer to have added a lot of safety margin?
Your computer is working. But how hot is it? Would you consider it a good computer if a two degrees higher ambient temperature (just a tiny bit more sun on the window) would make the processor overheat and either have the computer burn or start to produce incorrect answers? Don't you expect the computer to have a bit of safety margin?
So think again. Exactly what does it mean when your code is working perfectly? That you haven't seen a problem yet, or that you are not expected to see any problems even if you increase or decrease the supply voltages, or temperature, or switches to a different compiler or compiler setting? Or maybe you need to increase the signal trace length? Or you switch to a different processor? Do you know how many ns or us your delay gives? How do you know? How do you prove that it is the correct amount of delay? How do you prove that you still have the same delay even after having made changes to the project, which may change which registers the processor uses for the delay?
In the end: Exactly what does "perfectly" mean? The world just is not perfect, so we all other developers just do not manage to create perfect solutions. How can you be so sure that you can?
... of how stuff can appear to "work" (sic) and yet be fundamentally flawed, see: www.8052.com/.../178415
an old adage: "succesful testing does not prove the abscence of bugs, it proves the abscence of known bugs"
Erik