counter paradox
Working on a MCU 8051 personal project. I have a Silabs 8051 MCU connected to a "black box" via a serial connection.
The MCU has a program on it that checks for a missing letter in an entire string of data sent to the black box. If one of the letters are not detected an LED lights up to indicate that one of the letters were missing when the data was recieved. The code that is responsible for this is below:
void OnData(void) interrupt 4
{
if(RI)
{
theData = SBUF;
if(count < "eight")
{
if(theData == 'A') seconds = 0; // reset counter
if(next == 0)
{
if(theData == 'A') { next = 'B'; }
}
else
{
if(theData == next)
{
if(next == 'Z') next = 'A'; else next++;
}
else
{
next = 0; count++;
}
}
RefreshLED();
}
RI = 0;
}
}
There is another situation in which if the MCU does not detect a letter in 3 seconds then this will trigger an led to light up for every three seconds that a letter was not received. The code that is responsible for this is below:
void main()
{
long i;
int psec = 0;
InitPort();
InitTimer1();
LED_PORT = 255; // lit all LED
for(i=0;i<100000;i++); //delay
LED_PORT = 0; //switch off all LED
ES = 1; // enable serial interrupt
t1Count = 0;
/****END OF LOOP***********/
for( ; ; )
{
if(seconds >= 3)
{
seconds = 0;
count++;
if(count <= "eight") RefreshLED();
}
}
/**********************************/
}
There is a timer (MCU Timer 1) set such that an overflow occur when t1Count reaches 14. What this means is that an over flow occurs each second (.0715*14) that will increase the value of count. The code that is responsible for this is below:
void OnTimerOverflow(void) interrupt 3
{
t1Count++;
if(t1Count >= 14)
{
t1Count = 0;
seconds++;
}
}
So basically t1Count is my timer. t1Count overflows every 1 second. When seconds is greater than or equal than three, count increments by one for every three seconds (in the main for loop, only if the OnData Function is not recieving any data). Every time count is incremented it triggers the refreshLED function. THe refresh led function is just a simple function that is tied with the MCU's ports. My mcu ports have leds connected to indicate when a letter was not received. Here is my LED function.
void RefreshLED()
{
char c,pattern=0;
c = count;
while(c > 0)
{
pattern = (pattern << 1) | 1;
c--;
}
LED_PORT = pattern;
}
So my question is....what if i want count to to ignite an led every .250 seconds instead of 3 seconds how do I do this? Keep in mind I dont have floating point available.