Could anybody an help to write C code

Thread Starter

blacksole15

Joined Jul 27, 2014
6
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
 

DerStrom8

Joined Feb 20, 2011
2,390
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.
 

Thread Starter

blacksole15

Joined Jul 27, 2014
6
Filename:- st_mch.c
Program Description:-
Simple state machine exmple to control a torch
with 5 LEDs
Rich (BB code):
 /****************************************************/
 /****************************************************
  Libraries included
*****************************************************/
#include <system.h>
 /***********************************************
  Configuration Selection bits
************************************************/
 
#ifdef _PIC18F4520_H_
 #pragma config OSC = HS, LVP = OFF, WDT = OFF
 #pragma config PWRT= OFF, BOREN = OFF
 //#pragma config DEBUG = OFF
 #pragma DATA _CONFIG4L, 0x80
#endif
 /************************************************
   Function Prototypes
*************************************************/
void Initial(void);
 
/*****************************************
    Main Function
******************************************/
 void main ( void ) 
{
   
    unsigned char state = 0;  //current state value is 0 (possible states 0,1,2,3,4,5)
    //typedef  enum {OFF = 0,ONE,TWO, THREE,FOUR,FIVE} states;
    //states  state;
    
 Initial();
    
    while(1) //superloop
    {
  //state Machine
  
  //wait for event (button rising edge)
  while(!(porta.0));
    
  //update the state
  state = (state+1)% 4;  
  
  
  // switch(state)
  // {
  //  case 0:
  //   state = 1;
  //   break;
  //              case 1:
  //   state = 2;
  //   break;
  //              case 2:
                                        state = 3;
                                        break;
                                case 3:
                                        state = 4;
                                        break;
                                case 4:         
                                        state = 5;
                                        break;
  //
  //  case 5:
  //   state = 0;
  //   break;
  //  default:
  //   state = 0;
  //   break;
  // }
  
  
  //update the outputs
  switch(state) 
  {
   case 0: 
    portb = 0x00;
    break;
   case 1: 
    portb = 0x01;
    break;
   case 2: 
    portb= 0x03;
    break;
   case 3: 
    portb = 0x07;
    break;
                        case 4: 
                                portb = 0x15;
                                break;
                        case 5:
                                portb = 0x31;
                                break;
   default: 
    portb = 0xff;
    break;
  }
  
  delay_ms(200); 
  
  while((porta.0));  //wait for switch to be released
    
    }
}   
 void Initial(void)
{
    trisb = 0x00;
    portb = 0x00; //all outputs off
    trisa = 0xff; //porta all inputs (switch connected to porta.0
    
#ifdef _PIC18F4520_H_
 adcon1 = 0x0f;
 #endif
    portb = 0xff;
   delay_s(1);
   portb = 0x00;
 }
 
Last edited by a moderator:

DerStrom8

Joined Feb 20, 2011
2,390
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
 

Thread Starter

blacksole15

Joined Jul 27, 2014
6
Filename:- st_mch.c
Program Description:-
Simple state machine exmple to control a torch
with 5 LEDs
Rich (BB code):
/****************************************************/
/****************************************************
  Libraries included
*****************************************************/
#include <system.h>
 /***********************************************
  Configuration Selection bits
************************************************/
 
#ifdef _PIC18F4520_H_
 #pragma config OSC = HS, LVP = OFF, WDT = OFF
 #pragma config PWRT= OFF, BOREN = OFF
 //#pragma config DEBUG = OFF
 #pragma DATA _CONFIG4L, 0x80
#endif
 /************************************************
   Function Prototypes
*************************************************/
void Initial(void);
 
/*****************************************
    Main Function
******************************************/
 void main ( void ) 
{
   
    unsigned char state = 0;  //current state value is 0 (possible states 0,1,2,3,4,5)
    //typedef  enum {OFF = 0,ONE,TWO, THREE,FOUR,FIVE} states;
    //states  state;
    
 Initial();
    
    while(1) //superloop
    {
  //state Machine
  
  //wait for event (button rising edge)
  while(!(porta.0));
    
  //update the state
  state = (state+1)% 4;  
  
  
  // switch(state)
  // {
                case 0:
  //                      state = 1;
  //                      break;
  //             case 1:
  //                      state = 2;
  //                      break;
  //              case 2:
                          state = 3;
                          break;
                   case 3:
                          state = 4;
                          break;
                    case 4:         
                           state = 5;
                           break;
  //
  //               case 5:
  //                       state = 0;
  //                       break;
  //               default:
  //                       state = 0;
  //                       break;
  //    }
  
  
  //update the outputs
  switch(state) 
  {
               case 0: 
                         portb = 0x00;
                         break;
               case 1: 
                         portb = 0x01;
                         break;
               case 2: 
                         portb= 0x03;
                         break;
               case 3: 
                         portb = 0x07;
                         break;
               case 4: 
                         portb = 0x15;
                         break;
                case 5:
                         portb = 0x31;
                         break;
               default: 
                         portb = 0xff;
                         break;
  }
  
  delay_ms(200); 
  
  while((porta.0));  //wait for switch to be released
    
    }
}   
 void Initial(void)
{
    trisb = 0x00;
    portb = 0x00; //all outputs off
    trisa = 0xff; //porta all inputs (switch connected to porta.0
    
#ifdef _PIC18F4520_H_
 adcon1 = 0x0f;
 #endif
    portb = 0xff;
   delay_s(1);
   portb = 0x00;
 }
 
Last edited by a moderator:

NorthGuy

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

Rich (BB code):
state++;

if (state == 6) {
  state = 0;
}
Also, may I suggest istead of "switch(state)" do

Rich (BB code):
portb = (1<<state)-1;
Short code is easier to work with.
 

WBahn

Joined Mar 31, 2012
30,083
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.
 

Thread Starter

blacksole15

Joined Jul 27, 2014
6
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
 

WBahn

Joined Mar 31, 2012
30,083
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.
 
Top