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!!