Thread: PIC contoller script... more conseptuall than technical

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    9

    PIC contoller script... more conseptuall than technical

    ok so posted below is my current script

    Code:
    #include <p18cxxx.h>
    #include <timers.h>
    
    //These compiler directives set the compiler options to work with the RE-SBC100.
    //They should be included in every RE-SBC100 project.
    
    #pragma config OSC = HS			//This selects the High Speed oscillator, using the
    								//external crystal. 
    #pragma config FCMEN = OFF		//This turns off the Fail-Safe Clock Monitor, which
    								//would switch from the external crystal oscillator
    								//circuit to the internal RC oscillator circuit,
    								//should the HS oscillator fail.
    #pragma config PWRT = OFF		//This turns off the Power-up Timer, which holds the
    								//chip in reset mode for about 65ms, making sure 
    								//supply voltage is stable before executing
    								//instructions. Turning this on will cause an "Unable
    								//to enter debug mode" error, on the PICKit 2.
    #pragma config BOREN = SBORDIS	//This enables Brown-Out Reset, which resets the chip
    								//if the supply voltage gets too low.
    #pragma config BORV = 3			//This sets the Brown-Out voltage to the minimum
    								//setting. 
    #pragma config WDT = OFF		//This turns off the WatchDog Timer.
    #pragma config MCLRE = ON		//This disables the RE3 input put and enables MCLR
    								//functionality.
    #pragma config LPT1OSC = OFF	//Timer1 configured for high power operation (no
    								//external 32 KHz crystal).
    #pragma config PBADEN = OFF		//PORTB<4:0> pins configured as DIO on reset, instead
    								//of analog inputs.
    #pragma config CCP2MX = PORTBE 	//CCP2 Module multiplexed with RB3, instead ofRC1.
    #pragma config STVREN = ON		//This causes the processor to reset in the event of
    								//a stack overflow or underflow.          
    #pragma config LVP = OFF		//This disables single supply ICSP disabled.
    #pragma config XINST = OFF		//Instruction set extension and indexed addressing
    								//mode disabled.
    #pragma config DEBUG = ON		//Enable background debug mode on pins RB6 and RB7.
    #pragma config CP0 = OFF		//Code Block 0 protection disabled.
    #pragma config CP1 = OFF		//Code Block 1 protection disabled.
    #pragma config CPB = OFF		//Code Boot Block protection disabled.
    #pragma config WRT0 = OFF		//Block 0 write protection disabled.
    #pragma config WRT1 = OFF		//Block 1 write protection disabled.
    #pragma config WRTB = OFF		//Boot Block write protection disabled.
    #pragma config WRTC = OFF		//Configuration register write protection disabled.
    #pragma config EBTR0 = OFF		//Block 0 table read protection disabled.
    #pragma config EBTR1 = OFF		//Block 1 table read protection disabled.
    #pragma config EBTRB = OFF		//Boot Block table read protection disabled.
    
    unsigned char Red = 0;
    unsigned char Green = 0;		
    unsigned char Blue = 0;	
    unsigned char count = 0;
    unsigned int MilliSeconds = 8;
    
    
    void WaitMilliSeconds(unsigned int MilliSeconds);
    void Timer_ISR(void);
    
    
    //This code sets the interrupt vector to point to the Timer_ISR function.
    #pragma code low_vector=0x18
    void interrupt_at_low_vector(void){
    //this is an inline assembly code statement
    _asm GOTO Timer_ISR _endasm
    }
    #pragma code
    
    //This is the Timer Interupt Service Routine
    #pragma interrupt Timer_ISR
    
    void Timer_ISR(void){
    	
    	PIR1 &= ~0x02;					//clear interrupt for Timer2
    
    	if (count < 255){
    		if (count == Red){
    			PORTCbits.RC0 = 0;		//turn Red LED OFF
    			}//if
    		if (count == Green){
    			PORTCbits.RC1 = 0;		//turn Green LED OFF
    			}//if
    		if (count == Blue){
    			PORTCbits.RC2 = 0;		//turn Blue LED OFF
    			}//if
    	}//if
    	else if (count == 255){
    		count = 0;					//reset count to 0
    		PORTCbits.RC0 = 1;			//turn Red LED ON
    		PORTCbits.RC1 = 1;			//turn Green LED ON
    		PORTCbits.RC2 = 1;			//turn Blue LED ON
    	}//else if
    	
    	count ++;						//increment count
    
    }//Timer_ISR
    
    int main (void){
    unsigned char intensity = 0;
    
    	TRISC = 0x00;		//configure PORTC as output for RC0, RC1, and RC2
    
    	//Timer2 triggers the Timer_ISR interrupt after 255 counts
    	PR2 = 0xFF;
    
    	//Configure and start Timer 2 / Interupt ON / Prescale 1:1 / Postscale 1:1
    	OpenTimer2(TIMER_INT_ON & T2_PS_1_1 & T2_POST_1_1);
    
    	//Enable interrupts
    	INTCONbits.GIEH = 1;		//needed if using Timer2
    	INTCONbits.GIEL = 1;        //needed if using Timer2
    
    	while (1){					//continue forever
    
    	if(PORTBbits.RB5 == 0)
    		state1();
    	else if(PORTBbits.RB4 == 0)
    		state2();
    	else if(PORTBbits.RB3 == 0)
    		state3();
    
    	}
    
    void state1 (void){
    	while(1){
    		for(intensity=0;intensity!=255;intensity++){
    			Red=(255-intensity);
    			Blue=(255-intensity);
    			WaitMilliSeconds(MilliSeconds);
    		}//for
    		Red=0;
    		Blue=0;
    
    		for(intensity=0;intensity!=255;intensity++){
    			Red=intensity;
    			Blue=intensity;
    			WaitMilliSeconds(MilliSeconds);
    		}//for
    		Red=0;
    		Blue=0;
    
    	if(PORTBbits.RB5 == 0)
    		state1();
    	else if(PORTBbits.RB4 == 0)
    		state2();
    	else if(PORTBbits.RB3 == 0)
    		state3();
    	}
    }
    
    void state2 (void){
    	
    	while (1){
    		for(intensity=0;intensity!=255;intensity++){
    			Red=(255-intensity);
    			WaitMilliSeconds(MilliSeconds);
    		}//for
    		Red=0;
    
    		for(intensity=0;intensity!=255;intensity++){
    			Red=intensity;
    			WaitMilliSeconds(MilliSeconds);
    		}//for
    		Red=0;
    
    	if(PORTBbits.RB5 == 0)
    		state1();
    	else if(PORTBbits.RB4 == 0)
    		state2();
    	else if(PORTBbits.RB3 == 0)
    		state3();
    	}
    }
    void state3 (void){
    	while(1){
    		for(intensity=0;intensity!=255;intensity++){
    			Green=(255-intensity);
    			Blue=(255-intensity);
    			WaitMilliSeconds(MilliSeconds);
    		}//for
    		Green=0;
    		Blue=0;
    			
    		for(intensity=0;intensity!=255;intensity++){
    			Green=intensity;
    			Blue=intensity;
    			WaitMilliSeconds(MilliSeconds);
    		}//for
    		Green=0;
    		Blue=0;
    
    	if(PORTBbits.RB5 == 0)
    		state1();
    	else if(PORTBbits.RB4 == 0)
    		state2();
    	else if(PORTBbits.RB3 == 0)
    		state3();
    	}
    }
    }
    	
    
    
    //this function delays the program for the specified milliseconds
    void WaitMilliSeconds(unsigned int MilliSeconds){
    	unsigned int i, j;
    	for(i = 0; i < MilliSeconds; i++){
    		for(j = 0; j < 255; j++);
    	}//for
    }//WaitMilliSeconds
    The problem is that you cant put a function inside another function. I would like to call a function when PORTBbits.RB5=0 (button is pressed) however some of the stuff defined in the var main(var) is needed to run the code in the state# functions.

    I hope I was somewhat Clear. I am fairly new to programing in C and I mostly was cutting and pasting from another code which did something similar only it didnt have any buttons.

    THANKS!!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So pass that stuff into the function. (Just like you pass MilliSeconds into the WaitMilliSeconds function....)

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You can do this....

    Code:
    	if(PORTBbits.RB5 == 0)
    	     {  state1();
                             // insert second call here  
                           } 
    	else if(PORTBbits.RB4 == 0)
    		state2();
    	else if(PORTBbits.RB3 == 0)
    		state3();

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    9
    So pass that stuff into the function
    ok here is what I did....

    Code:
    void main (void){
    unsigned char intensity = 0;
    
    	TRISC = 0x00;		//configure PORTC as output for RC0, RC1, and RC2
    
    	//Timer2 triggers the Timer_ISR interrupt after 255 counts
    	PR2 = 0xFF;
    
    	//Configure and start Timer 2 / Interupt ON / Prescale 1:1 / Postscale 1:1
    	OpenTimer2(TIMER_INT_ON & T2_PS_1_1 & T2_POST_1_1);
    
    	//Enable interrupts
    	INTCONbits.GIEH = 1;		//needed if using Timer2
    	INTCONbits.GIEL = 1;        //needed if using Timer2
    
    	while (1){					//continue forever
    
    	if(PORTBbits.RB5 == 0)
    		state1();                           /*ERROR1/*
    	else if(PORTBbits.RB4 == 0)
    		state2();                            /*ERROR2/*
    	else if(PORTBbits.RB3 == 0)
    		state3();                             /*ERROR3/*
    
    	}
    }
    
    void state1 (char intensity){             /*ERROR4/*
    	while(1){
    		for(intensity=0;intensity!=255;intensity++){
    			Red=(255-intensity);
    			Blue=(255-intensity);
    			WaitMilliSeconds(MilliSeconds);
    		}//for
    		Red=0;
    		Blue=0;
    
    		for(intensity=0;intensity!=255;intensity++){
    			Red=intensity;
    			Blue=intensity;
    			WaitMilliSeconds(MilliSeconds);
    		}//for
    		Red=0;
    		Blue=0;
    
    	if(PORTBbits.RB5 == 0)
    		state1();                      
    	else if(PORTBbits.RB4 == 0)
    		state2();                       /*ERROR5/*
    	else if(PORTBbits.RB3 == 0)
    		state3();                       /*ERROR6/*
    	}
    }
    
    void state2 (char intensity){          /*ERROR7/*
    	
    	while (1){
    		for(intensity=0;intensity!=255;intensity++){
    			Red=(255-intensity);
    			WaitMilliSeconds(MilliSeconds);
    		}//for
    		Red=0;
    
    		for(intensity=0;intensity!=255;intensity++){
    			Red=intensity;
    			WaitMilliSeconds(MilliSeconds);
    		}//for
    		Red=0;
    
    	if(PORTBbits.RB5 == 0)
    		state1();
    	else if(PORTBbits.RB4 == 0)
    		state2();                        
    	else if(PORTBbits.RB3 == 0)
    		state3();                          /*ERROR8/*       
    	}
    }
    void state3 (char intensity){           /*ERROR9/*
    	while(1){
    		for(intensity=0;intensity!=255;intensity++){
    			Green=(255-intensity);
    			Blue=(255-intensity);
    			WaitMilliSeconds(MilliSeconds);
    		}//for
    		Green=0;
    		Blue=0;
    			
    		for(intensity=0;intensity!=255;intensity++){
    			Green=intensity;
    			Blue=intensity;
    			WaitMilliSeconds(MilliSeconds);
    		}//for
    		Green=0;
    		Blue=0;
    
    	if(PORTBbits.RB5 == 0)
    		state1();
    	else if(PORTBbits.RB4 == 0)
    		state2();
    	else if(PORTBbits.RB3 == 0)
    		state3();
    	}
    }

    Here are the Errors I got when trying to compile.... I labeled where each error occurred in the code above

    Code:
    128:Warning [2058] call of function without prototype
    130:Warning [2058] call of function without prototype
    132:Warning [2058] call of function without prototype
    137:Error [1109] type mismatch in redeclaration of 'state1'
    158:Warning [2058] call of function without prototype
    160:Warning [2058] call of function without prototype
    164:Error [1109] type mismatch in redeclaration of 'state2'
    184:Warning [2058] call of function without prototype
    187:Error [1109] type mismatch in redeclaration of 'state3
    What am I doing wrong??

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You need prototypes of functions. Prototypes should look like the function definition, but without the actual function body itself. For example, the last one would be
    Code:
    void state3 (char intensity);
    Prototypes go at the top.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. deallocating a struct containing arrays
    By maxtothemax in forum C Programming
    Replies: 5
    Last Post: 09-28-2009, 07:09 PM
  2. Script in games
    By Shakti in forum Game Programming
    Replies: 7
    Last Post: 09-27-2006, 12:27 AM
  3. From script to C to C++
    By VirtualAce in forum Game Programming
    Replies: 2
    Last Post: 09-11-2006, 04:35 AM
  4. Passing arguments to script.....
    By suwie in forum C Programming
    Replies: 5
    Last Post: 09-25-2004, 11:10 PM
  5. Game structure, any thoughts?
    By Vorok in forum Game Programming
    Replies: 2
    Last Post: 06-07-2003, 01:47 PM