Could anybody an help to write C code

Discussion in 'Homework Help' started by blacksole15, Jul 27, 2014.

  1. blacksole15

    Thread Starter New Member

    Jul 27, 2014
    6
    0
    Design the software for an electronic torch that has a push button and 5 LEDs. With each successive push of the button an extra LED comes on. When all LEDS are on, the next button push will bring the system full circle so that all LEDs are off. It is required to design the software using a state machine strategy. Write the C code for your state machine design Implement an automatic power down mode as part of the design. The power down option would happen if the system is in manual mode and the light has been on for 5 minutes. In this case the LEDS are all switched off.

    Thanks
     
  2. NorthGuy

    Active Member

    Jun 28, 2014
    604
    121
    Is that a homework?

    Have you tried to write at least some of the code?
     
  3. MrChips

    Moderator

    Oct 2, 2009
    12,447
    3,363
    Sounds like homework to me, hence moved to Homework Help.
     
  4. DerStrom8

    Well-Known Member

    Feb 20, 2011
    2,428
    1,328
    You're not going to get any direct answers here. In order for us to help, you need to show us what you have so far, and we can point you in the right direction. We will not hand you the answers on a silver platter. It's homework, so you need to do the majority of the work yourself.
     
    blacksole15 likes this.
  5. blacksole15

    Thread Starter New Member

    Jul 27, 2014
    6
    0
    Filename:- st_mch.c
    Program Description:-
    Simple state machine exmple to control a torch
    with 5 LEDs
    Code ( (Unknown Language)):
    1.  
    2.  /****************************************************/
    3.  /****************************************************
    4.   Libraries included
    5. *****************************************************/
    6. #include <system.h>
    7.  /***********************************************
    8.   Configuration Selection bits
    9. ************************************************/
    10.  
    11. #ifdef _PIC18F4520_H_
    12.  #pragma config OSC = HS, LVP = OFF, WDT = OFF
    13.  #pragma config PWRT= OFF, BOREN = OFF
    14.  //#pragma config DEBUG = OFF
    15.  #pragma DATA _CONFIG4L, 0x80
    16. #endif
    17.  /************************************************
    18.    Function Prototypes
    19. *************************************************/
    20. void Initial(void);
    21.  
    22. /*****************************************
    23.     Main Function
    24. ******************************************/
    25.  void main ( void )
    26. {
    27.    
    28.     unsigned char state = 0;  //current state value is 0 (possible states 0,1,2,3,4,5)
    29.     //typedef  enum {OFF = 0,ONE,TWO, THREE,FOUR,FIVE} states;
    30.     //states  state;
    31.    
    32.  Initial();
    33.    
    34.     while(1) //superloop
    35.     {
    36.   //state Machine
    37.  
    38.   //wait for event (button rising edge)
    39.   while(!(porta.0));
    40.    
    41.   //update the state
    42.   state = (state+1)% 4;  
    43.  
    44.  
    45.   // switch(state)
    46.   // {
    47.   //  case 0:
    48.   //   state = 1;
    49.   //   break;
    50.   //              case 1:
    51.   //   state = 2;
    52.   //   break;
    53.   //              case 2:
    54.                                         state = 3;
    55.                                         break;
    56.                                 case 3:
    57.                                         state = 4;
    58.                                         break;
    59.                                 case 4:        
    60.                                         state = 5;
    61.                                         break;
    62.   //
    63.   //  case 5:
    64.   //   state = 0;
    65.   //   break;
    66.   //  default:
    67.   //   state = 0;
    68.   //   break;
    69.   // }
    70.  
    71.  
    72.   //update the outputs
    73.   switch(state)
    74.   {
    75.    case 0:
    76.     portb = 0x00;
    77.     break;
    78.    case 1:
    79.     portb = 0x01;
    80.     break;
    81.    case 2:
    82.     portb= 0x03;
    83.     break;
    84.    case 3:
    85.     portb = 0x07;
    86.     break;
    87.                         case 4:
    88.                                 portb = 0x15;
    89.                                 break;
    90.                         case 5:
    91.                                 portb = 0x31;
    92.                                 break;
    93.    default:
    94.     portb = 0xff;
    95.     break;
    96.   }
    97.  
    98.   delay_ms(200);
    99.  
    100.   while((porta.0));  //wait for switch to be released
    101.    
    102.     }
    103. }  
    104.  void Initial(void)
    105. {
    106.     trisb = 0x00;
    107.     portb = 0x00; //all outputs off
    108.     trisa = 0xff; //porta all inputs (switch connected to porta.0
    109.    
    110. #ifdef _PIC18F4520_H_
    111.  adcon1 = 0x0f;
    112.  #endif
    113.     portb = 0xff;
    114.    delay_s(1);
    115.    portb = 0x00;
    116.  }
    117.  
     
    Last edited by a moderator: Jul 28, 2014
  6. DerStrom8

    Well-Known Member

    Feb 20, 2011
    2,428
    1,328
    Hi,

    Would you mind trying that again, but post it in between the code tags so that it preserves the formatting? It's much easier to read that way.

    Thanks!
    Matt
     
  7. blacksole15

    Thread Starter New Member

    Jul 27, 2014
    6
    0
    Could anybody help please how to implement interrupt (5 min) WDT
     
  8. blacksole15

    Thread Starter New Member

    Jul 27, 2014
    6
    0
    Filename:- st_mch.c
    Program Description:-
    Simple state machine exmple to control a torch
    with 5 LEDs
    Code ( (Unknown Language)):
    1.  
    2. /****************************************************/
    3. /****************************************************
    4.   Libraries included
    5. *****************************************************/
    6. #include <system.h>
    7.  /***********************************************
    8.   Configuration Selection bits
    9. ************************************************/
    10.  
    11. #ifdef _PIC18F4520_H_
    12.  #pragma config OSC = HS, LVP = OFF, WDT = OFF
    13.  #pragma config PWRT= OFF, BOREN = OFF
    14.  //#pragma config DEBUG = OFF
    15.  #pragma DATA _CONFIG4L, 0x80
    16. #endif
    17.  /************************************************
    18.    Function Prototypes
    19. *************************************************/
    20. void Initial(void);
    21.  
    22. /*****************************************
    23.     Main Function
    24. ******************************************/
    25.  void main ( void )
    26. {
    27.    
    28.     unsigned char state = 0;  //current state value is 0 (possible states 0,1,2,3,4,5)
    29.     //typedef  enum {OFF = 0,ONE,TWO, THREE,FOUR,FIVE} states;
    30.     //states  state;
    31.    
    32.  Initial();
    33.    
    34.     while(1) //superloop
    35.     {
    36.   //state Machine
    37.  
    38.   //wait for event (button rising edge)
    39.   while(!(porta.0));
    40.    
    41.   //update the state
    42.   state = (state+1)% 4;  
    43.  
    44.  
    45.   // switch(state)
    46.   // {
    47.                 case 0:
    48.   //                      state = 1;
    49.   //                      break;
    50.   //             case 1:
    51.   //                      state = 2;
    52.   //                      break;
    53.   //              case 2:
    54.                           state = 3;
    55.                           break;
    56.                    case 3:
    57.                           state = 4;
    58.                           break;
    59.                     case 4:        
    60.                            state = 5;
    61.                            break;
    62.   //
    63.   //               case 5:
    64.   //                       state = 0;
    65.   //                       break;
    66.   //               default:
    67.   //                       state = 0;
    68.   //                       break;
    69.   //    }
    70.  
    71.  
    72.   //update the outputs
    73.   switch(state)
    74.   {
    75.                case 0:
    76.                          portb = 0x00;
    77.                          break;
    78.                case 1:
    79.                          portb = 0x01;
    80.                          break;
    81.                case 2:
    82.                          portb= 0x03;
    83.                          break;
    84.                case 3:
    85.                          portb = 0x07;
    86.                          break;
    87.                case 4:
    88.                          portb = 0x15;
    89.                          break;
    90.                 case 5:
    91.                          portb = 0x31;
    92.                          break;
    93.                default:
    94.                          portb = 0xff;
    95.                          break;
    96.   }
    97.  
    98.   delay_ms(200);
    99.  
    100.   while((porta.0));  //wait for switch to be released
    101.    
    102.     }
    103. }  
    104.  void Initial(void)
    105. {
    106.     trisb = 0x00;
    107.     portb = 0x00; //all outputs off
    108.     trisa = 0xff; //porta all inputs (switch connected to porta.0
    109.    
    110. #ifdef _PIC18F4520_H_
    111.  adcon1 = 0x0f;
    112.  #endif
    113.     portb = 0xff;
    114.    delay_s(1);
    115.    portb = 0x00;
    116.  }
    117.  
     
    Last edited by a moderator: Jul 28, 2014
  9. NorthGuy

    Active Member

    Jun 28, 2014
    604
    121
    You have 6 states, so when you do "%4" you know it's not going to be right. Probably you want something like that:

    Code ( (Unknown Language)):
    1. state++;
    2.  
    3. if (state == 6) {
    4.   state = 0;
    5. }
    Also, may I suggest istead of "switch(state)" do

    Code ( (Unknown Language)):
    1. portb = (1<<state)-1;
    Short code is easier to work with.
     
    blacksole15 likes this.
  10. DerStrom8

    Well-Known Member

    Feb 20, 2011
    2,428
    1,328
    blacksole15, you didn't post it inside code tags.

    Click the "#" symbol at the top of the reply box, and post your code in between the tags that show up.
     
    blacksole15 likes this.
  11. WBahn

    Moderator

    Mar 31, 2012
    17,757
    4,800
    The code you posted, in addition to being effectively undocumented, is also invalid. You have commented out things that are required for things that you didn't comment out. At least try to post code that will successfully compile or, if you can't, then change the focus of the discussion to the code syntax until it does compile.
     
  12. blacksole15

    Thread Starter New Member

    Jul 27, 2014
    6
    0
    Hi could any one can help how to Implement/activate an automatic power down mode as part of the above design. The power down option would happen if the system is in manual mode and the light has been on for 5 minutes. So torch goes off from any state if no activity more than 5 min(WDT) Pic18 Thanks
     
  13. blacksole15

    Thread Starter New Member

    Jul 27, 2014
    6
    0
    Timer 0???
     
  14. WBahn

    Moderator

    Mar 31, 2012
    17,757
    4,800
    You want us to implement a feature in code that is invalid to begin with?

    That aside, your initial description is schizophrenic in that it says that you have a flashlight with a single button and things are supposed to happen each time the button is pressed. But then it talks about something that is supposed to happen when the device is in "manual mode" without ever defining what the modes are or how the system is placed in the different modes.
     
Loading...