Big Picture

should be kept small

You are getting into it, get out for a sec, lets realize how development will go on.

We have an idea of the tools, their usage. We can use them. The board set up is tested, we can download code and see it working. We can control port pins. LEDs, buttons, whatever. Building on that we can control the 7-segment display. We have a way of testing our code at runtime.

Building on that we can write a high level routine that displays numbers. This one should hide all low level complexity and should be called like this:


Display (num);
 

A good point would be writing a "for" loop for counting from 0 to 99. If we extend this by adding the minus symbol, we can display as low as -9C. Hmm, or we may use the extra LED for sign indication. When we are happy with our routine, we forget that and we start reading the 1-wire protocol. No plans for the 1-wire now ... first the reading, then the plans.

After making it work we write another high level routine that would be called like this:


temp = ReadTemp();
 

Again this one should hide the protocol mess. With these two routines life will be easier for us, no Bush will stay a bit more but, we will be able to average temperature over time, convert Celcius to Fahrenheit with the click of a button, etc. I have a few more ideas, hmmm, or ... well, let's play with the LEDs first.

Well, I am sure that if you play with the LEDs a bit, you will appreciate the need for a single high-level routine. I think, it is best to use the extra led as a sign indicator. Here follows my attempt for this routine.


void DispRight (u8 digit)
{
    switch (digit)
    {
        case 0 : a1; b1; c1; d1; e1; f1; break;
        case 1 : b1; c1; break;
        case 2 : a1; b1; d1; e1; g1; break;
        case 3 : a1; b1; c1; d1; g1; break;
        case 4 : b1; c1; f1; g1; break;
        case 5 : a1; c1; d1; f1; g1; break;
        case 6 : a1; c1; d1; e1; f1; g1; break;
        case 7 : a1; b1; c1; break;
        case 8 : a1; b1; c1; d1; e1; f1; g1; break;
        case 9 : a1; b1; c1; f1; g1; break;
        default: break;
    }
}

void DispLeft (u8 digit)
{
    switch (digit)
    {
        // no need to display 0 left
        case 0 : break;
        case 1 : b0; c0; break;
        case 2 : a0; b0; d0; e0; g0; break;
        case 3 : a0; b0; c0; d0; g0; break;
        case 4 : b0; c0; f0; g0; break;
        case 5 : a0; c0; d0; f0; g0; break;
        case 6 : a0; c0; d0; e0; f0; g0; break;
        case 7 : a0; b0; c0; break;
        case 8 : a0; b0; c0; d0; e0; f0; g0; break;
        case 9 : a0; b0; c0; f0; g0; break;
        default: break;
    }
}

void Display (s8 num)
{
    s8 dec, uni;

    // all leds off
    PORTB |= 0x17; PORTC |= 0x37; PORTD |= 0xFC;

    if (num>-100 && num<100)
    {
        // if negative just switch LED on
        if (num<0)
        {
            ledon;
            num=-num;
        }

        dec = num/10;
        uni = num%10;

        DispLeft (dec);
        DispRight (uni);

        Delay (50000);
    }
}
 

The first two routines are identical, each routine is controling the respective 7-segment display. We don't really need to display 0s at the decades digit. As I said all we need is to decide which LEDs to switch on for each digit.

Now, the Display routine starts by clearing off all LEDs, it checks if the input number is within acceptable limits and displays it. It just gets the decades and units digits and invokes the respective routines. If the number is negative, the sign LED is switched on and the negative sign is removed. Eventually, there is a delay for us humble humans to notice things.Not to forget, have you noticed the s8 at the routine declaration?

 
 
victor@avrtutor.com