Help With My Program

Discussion in 'Homework Help' started by Ammo, May 20, 2011.

  1. Ammo

    Thread Starter New Member

    Aug 25, 2010
    4
    0
    Hi all,

    I've been trying to get my program working and for some reason it doesn't. It builds successfully and i've check it all but perhaps a fresh pair of eyes will do the trick.

    Posted below is the question and written program.

    Question
    Using two switches and 8 LED’s, write a program such that when switch 1 is pressed ON, LED 0 comes ON permanently, and LEDs 1 and 2 come ON for 2 seconds. When switch 2 is pressed on, LEDs 1 and 2 come ON for 1 second followed by the turn off of all LEDs.

    My program


    #include <htc.h>

    __CONFIG(UNPROTECT & BORDIS & PWRTEN & WDTDIS & INTIO & MCLREN & FCMDIS & IESODIS);

    #define _XTAL_FREQ 8000000

    void init(void)
    {
    // port directions: 1=input, 0=output
    TRISD = 0x00;
    TRISA = 0xFF;

    // 8Mhz Internal Clock
    OSCCON = 0b01110000;


    // Clear PortD
    PORTD = 0x00;

    // Turn off comparators
    CMCON0 = 0x07;

    //Turn off ADC
    ANSEL = 0x00;


    }
    char counter;
    void main(void)
    {

    init();

    while (1){ while(RA0) //Wait while switch off
    { __delay_ms(1); } //Debounce switch while(!RA0) //Wait while switch on
    { __delay_ms(1); } //Debounce switch

    counter = 0;

    // Switch ON PORTD

    PORTD = 0b11100000; // XOR (Flip) PORTD

    //2 second delay rotuine
    while (counter < 20){
    __delay_ms(98);
    counter = counter + 1;

    __delay_ms(40);
    }
    PORTD = 0b10000000;


    while (1){ while(RA1) //Wait while switch off
    { __delay_ms(1); } //Debounce switch while(!RA0) //Wait while switch on
    { __delay_ms(1); } //Debounce switch

    PORTD = 0b11100000;

    //1 second delay rotuine
    while (counter < 10) {
    __delay_ms(98);
    counter = counter + 1;
    }
    __delay_ms(20);

    // Clear PortD

    // Switch OFF PORTD

    PORTD = 0x00;



    }
    }
    }
     
  2. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    First off, when you post code go into advanced editing mode and use the code tag (#) to preserve your code formatting. This makes it easier to read and increases your chances of getting a helpful reply.
    Next, where possible keep the comments on the same line as the statement that they refer to. This makes also your code easier to read and makes it less likely that you will make simple errors.

    About half way down your code you have
    Code ( (Unknown Language)):
    1. while (1){while(RA0) //Wait while switch off
    2.          {  __delay_ms(1);}//Debounce switchwhile(!RA0)//Wait while switch on
    3.          {  __delay_ms(1);  } //Debounce switch
    The "while(!RA0)" is tangled up in the comments and effectively commented out.
    What I think you meant to write was
    Code ( (Unknown Language)):
    1. while (1)
    2.     {
    3.         while(RA0)                 //Wait while switch off
    4.         {                                
    5.             __delay_ms(1);
    6.         }                                 //Debounce switch
    7.         while(!RA0)             //Wait while switch on
    8.         {    
    9.             __delay_ms(1);
    10.         }                                 //Debounce switch
     
    Ammo likes this.
  3. RiJoRI

    Well-Known Member

    Aug 15, 2007
    536
    26
    Ah, yes, partial specs (often found in the Real World)!

    Questions to get answers for:

    (1) if either switch is released before the time is up, what happens? Do LEDs 1&2 go off immediately, or do they stay on until the time is up?

    (2) What happens if switch 2 is activated while switch 1's condition is still active? And vice-versa.

    (3) If LED 0 is on "permanently," does that mean it is not affected by switch 2 being activated? Or is it included in the "all other LEDs" statement?

    Also, see http://www.allaboutcircuits.com/vol_4/chpt_4/4.html for information on contact bounce. 1 mSec is nowhere near long enough.

    --Rich
     
    Ammo likes this.
  4. Ammo

    Thread Starter New Member

    Aug 25, 2010
    4
    0
    Thanks Alex i get what your saying. I'll edit the program and let you know if it works. Looking at the program do you think it solves the question, after i've changed what you suggested?

    Hi RiJoRI the answers to the questions are as followed

    (1) if either switch is released before the time is up, what happens? Do LEDs 1&2 go off immediately, or do they stay on until the time is up?

    No the LED's stay on until the time is up.

    (2) What happens if switch 2 is activated while switch 1's condition is still active? And vice-versa.

    Im quite sure the second switch will only work when the first switch is active then finished doing what it needs to.

    (3) If LED 0 is on "permanently," does that mean it is not affected by switch 2 being activated? Or is it included in the "all other LEDs" statement?

    LED 0 will be active when switch 1 is on but when switch 2 is pressed LED 0 switches off after LEDs 1 and 2 come on for 1 second.

    Hope this helps.
     
  5. RiJoRI

    Well-Known Member

    Aug 15, 2007
    536
    26
    OK, the next thing I would do is write a "program" in "Program Design Language" (PDL). This is a make-believe language that describes what the program is to do. e.g.,
    Code ( (Unknown Language)):
    1.  
    2. /*MAIN:*/
    3. /*    FOR n = 1 TO 20 */
    4. /*        IF SomeTest THEN */
    5. /*            DO Something */
    6. /*        ORELSE */
    7. /*            DO Nothing */
    8. /*        ENDIF */
    9. /*    NEXT n */
    10.  
    11. /*    REPEAT */
    12. /*       Do SomethingElse */
    13. /*    UNTIL Whenever */
    14.  
    Note that every IF has an ELSE portion! Of course, the Somethings and Whenevers get filled in properly. Each line is a comment so I can fill in the equivalent C (or ASM!) code once I'm finished. AND the additional comments -- which you will add -- will tell why you are doing this. I can usually figure out what is happening from the code itself. (PORT_D = 0; /* Clear PortD */ is NOT helpful. PORTD = 0: /* Keep all signals OFF */ is much better.)

    HTH,
    --Rich
    P.S. -- I am not around on weekends, so any questions/comments you have for me won't be answered until next Monday.
     
Loading...