include f675_4i -- Includes jpic675 in def -- (available at http://www.geocities.com/vsurducan/electro/PIC/lib.html) include nibsBPs -- Available at http://www.wahoo-wa.net/PICprojects -- PS+ - 1 | | 8 - PS- -- 2 | | 7 - LEDR -- 3 | | 6 - LEDG -- PS+ -/\/\/- 4 | | 5 - LEDB -- | -- / | -- PS- --* *- -- ---------------------------------------------------------------------- -- Initialization -- ---------------------------------------------------------------------- osc_calibrate disable_ad gpio_direction = all_output gp3_direction = input -- ---------------------------------------------------------------------- -- ---------------------------------------------------------------------- -- Declare Variables -- ---------------------------------------------------------------------- var volatile bit ledRed is gp0 = off var volatile bit ledGreen is gp1 = off var volatile bit ledBlue is gp2 = off var volatile bit pinButton is gp3 var volatile byte valRed = 0 var volatile byte valGreen = 0 var volatile byte valBlue = 0 var volatile byte valRedTarget = 0 var volatile byte valGreenTarget = 0 var volatile byte valBlueTarget = 0 var volatile byte thePeriod = 0 var volatile byte theCount = 0 var volatile byte tempAddress = 0 var volatile byte tempData = 0 var volatile byte numOfPatterns = 0 var volatile byte currentPattern = 0 var volatile byte patStart = 0 -- ---------------------------------------------------------------------- -- ---------------------------------------------------------------------- -- Display EEPROM Data -- ---------------------------------------------------------------------- pragma eedata 3 -- Number of Patterns pragma eedata 4 -- Start address of Pattern 1 pragma eedata 11 -- Start address of Pattern 2 pragma eedata 24 -- Start address of Pattern 3 -- Data Format: -- pragma eedata 0b(period in cycles * 8, max 16)_(ledRed pwm value, max 16) -- pragma eedata 0b(ledGreen pwm value, max 16)_(ledBlue pwm value, max 16) pragma eedata 0b0100_1111 -- EEPROM address 4 pragma eedata 0b0000_0000 pragma eedata 0b0100_0000 pragma eedata 0b1111_0000 pragma eedata 0b0100_0000 pragma eedata 0b0000_1111 pragma eedata 0b0000_0000 -- End of Pattern 1 pragma eedata 0b0110_0000 -- EEPROM address 11 pragma eedata 0b0000_0000 pragma eedata 0b0110_1111 pragma eedata 0b0000_0000 pragma eedata 0b0110_0000 pragma eedata 0b0000_0000 pragma eedata 0b0110_0000 pragma eedata 0b1111_0000 pragma eedata 0b0110_0000 pragma eedata 0b0000_0000 pragma eedata 0b0110_0000 pragma eedata 0b0000_1111 pragma eedata 0b0000_0000 -- End of Pattern 2 pragma eedata 0b0010_0000 -- EEPROM address 24 pragma eedata 0b0000_0000 pragma eedata 0b0010_1111 pragma eedata 0b1111_1111 pragma eedata 0b0000_0000 -- End of Pattern 3 -- ---------------------------------------------------------------------- procedure checkForButton is if ! pinButton then -- If pinButton is pulled low while ! pinButton loop -- Wait until returns to a high state end loop delay_20ms -- Give time to settle... while ! pinButton loop -- Wait again for debounce end loop currentPattern = currentPattern + 1 -- Set current pattern if currentPattern > numOfPatterns then -- If it is too high though... currentPattern = 1 -- Set currentPattern back to 1 end if eeprom_get( currentPattern, patStart ) -- Get start address of pattern tempAddress = patStart -- Set the main loop address variable to the start end if end procedure -- ---------------------------------------------------------------------- -- Set up program -- ---------------------------------------------------------------------- tempAddress = 0 eeprom_get( tempAddress, numOfPatterns ) -- Get number of patterns currentPattern = 1 -- Set current pattern eeprom_get( currentPattern, patStart ) -- Get start address of pattern tempAddress = patStart -- Set the main loop address variable to the start -- ---------------------------------------------------------------------- -- ---------------------------------------------------------------------- -- Main Progam Loop -- ---------------------------------------------------------------------- forever loop checkForButton -- Check to see if pinButton is pulled low -- Check if the current values are equal to the target values if valRed == valRedTarget & valGreen == valGreenTarget & valBlue == valBlueTarget then -- If so, get next data! eeprom_get( tempAddress, tempData ) -- Get next byte of data tempAddress = tempAddress + 1 byteToNibs (tempData, thePeriod, valRedTarget ) -- Split into nibbles if thePeriod != 0 then -- If thePeriod != 0 use the data eeprom_get( tempAddress, tempData ) tempAddress = tempAddress + 1 byteToNibs (tempData, valGreenTarget, valBlueTarget ) else -- thePeriod = 0 tempAddress = patStart -- Go back to beginning of pattern eeprom_get( tempAddress, tempData ) tempAddress = tempAddress + 1 byteToNibs (tempData, thePeriod, valRedTarget ) eeprom_get( tempAddress, tempData ) tempAddress = tempAddress + 1 byteToNibs (tempData, valGreenTarget, valBlueTarget ) end if thePeriod = thePeriod * 8 -- Multiply thePeriod to make it longer end if if valRed > valRedTarget then -- Check if current value is greater than target valRed = valRed - 1 -- If so decrement elsif valRed < valRedTarget then -- Check if current value is less than target valRed = valRed + 1 -- If so increment end if if valGreen > valGreenTarget then -- ^^ valGreen = valGreen - 1 -- elsif valGreen < valGreenTarget then -- valGreen = valGreen + 1 -- end if if valBlue > valBlueTarget then -- ^^ valBlue = valBlue - 1 -- elsif valBlue < valBlueTarget then -- valBlue = valBlue + 1 -- end if for thePeriod loop -- Loop for period length theCount = 0 -- Set count = 0 for PWM for 16 loop -- Loop for PWM ledRed = ( valRed > theCount ) -- If current value is > theCount then display ledGreen = ( valGreen > theCount ) -- ^^ ledBlue = ( valBlue > theCount ) -- ^^ theCount = theCount + 1 -- Increment theCount end loop end loop end loop -- ----------------------------------------------------------------------