Will look at suggestions AFTER I get this code working.
I have the ADC working in a test code and all is great
Have the candle flicker routine in another code.
Trying to get both to work together but they don't seem to want to play together.
Had compile issues until put the ADC routine in a sub but it doesn't seem to read the ADC on portA.0
the transistor is enabled all the time. Won't shut off.
Am thinking I have the portA.0 addressed wrong??
As for the SLEEP, will add it in AFTER I get parts A and B to play together.
NOTE parta =the candle routine works
partb (ADC) works by itself. Add the two together and it won't turn off with LIGHT on the cds.
{
*****************************************************************************
* Name : UNTITLED.BAS *
* Author : Doug Bezaire *
* Notice : Copyright (c) 2010 [select VIEW...EDITOR OPTIONS] *
* : All Rights Reserved *
* Date : 3/26/2010 *
* Version : 1.0 *
* Notes : 12/22/2016
need to add ADC input *
* : *
*****************************************************************************
}
DEVICE = 18F1320
CLOCK = 8
INCLUDE "InternalOscillator.bas"
INCLUDE "RandGen.bas"
INCLUDE "Utils.bas"
Include "adc.bas"
//FUNCTION SET
function ADCAsVolt() as word
result = (ADC.read(0) + 1)
end function
sub analog_on ()
dim cds as word
dim switch as portA.6 // LOW to turn on transistor/Leds
trisA .0 = 1 //set up portA.0 as input
ADCON1 .7 =1 //analog portA.0
cds = ADCAsVolt
if (cds)<= 818 then
high (switch)
end if
if (cds) > 819 then
low (switch)
endif
end sub
DIM TMR2IE AS PIE1.1, // TMR2 interrupt enable
TMR2IF AS PIR1.1, // TMR2 overflow flag
TMR2ON AS T2CON.2, // Enables TMR2 to begin incrementing
Signal_Pin AS PORTB.7 // Signal output to frequency meter
DIM Red_Pin_1 AS PORTA.2,
Green_Pin_1 AS PORTB.1,
Blue_Pin_1 AS PORTB.0,
Red_Pin_2 AS PORTB.4,
Green_Pin_2 AS PORTB.5,
Blue_Pin_2 AS PORTB.6,
Red_Pin_3 AS PORTA.7,
Green_Pin_3 AS PORTB.2,
Blue_Pin_3 AS PORTB.3,
'cathod_on AS portA.6, // transistor base goes LOW
Red_Duty AS BYTE,
Green_Duty AS BYTE,
Blue_Duty AS BYTE,
Red_DutyVal AS BYTE,
Green_DutyVal AS BYTE,
Blue_DutyVal AS BYTE,
' cds as word,
'switch as portA.6, // LOW to turn on transistor/Leds
'ADCOn1 = $70 // all digital
' portB = %00000000
' TRISA.0 = 1 // set portA as input for porta.0
' dim ADCON1.7 = 1 //set analog on porta.0
RandomVal AS BYTE
'xxxxxxxxxxxxxxxxxxxxx
'DIM led AS PORTB.3 // testing progress
'xxxxxxxxxxxxxxxxxxxxxxxxxxx
DIM uS AS WORD,
mSa AS WORD
'dim duty as byte
// ranval(1)as word
INTERRUPT TMR2_Interrupt()
' HIGH(Signal_Pin)
SAVE(0) // Back up system variables
IF TMR2IF = 1 THEN // Check if the interrupt was from TMR2
TMR2IF = 0 // Clear the TMR2 interrupt flag
uS = uS + 1000
IF uS >= 1000 THEN
uS = uS - 1000
INC(mSa)
ENDIF
INC(Red_DutyVal)
INC(Green_DutyVal)
INC(Blue_DutyVal)
IF Red_DutyVal > Red_Duty OR Red_Duty = 0 THEN
Red_Pin_1 = 0
Red_Pin_2 = 0
Red_Pin_3 = 0
ELSE
Red_Pin_1 = 1
Red_Pin_2 = 1
Red_Pin_3 = 1
ENDIF
IF Green_DutyVal > Green_Duty OR Green_Duty = 0 THEN
Green_Pin_1 = 0
Green_Pin_2 = 0
Green_Pin_3 = 0
ELSE
Green_Pin_1 = 1
Green_Pin_2 = 1
Green_Pin_3 = 1
ENDIF
IF Blue_DutyVal > Blue_Duty OR Blue_Duty = 0 THEN
Blue_Pin_1 = 0
Blue_Pin_2 = 0
Blue_Pin_3 = 0
' led = 0
' delayms(500)
ELSE
Blue_Pin_1 = 1
Blue_Pin_2 = 1
Blue_Pin_3 = 1
' led = 0
'delayms(500)
ENDIF
ENDIF //
RESTORE // Restore system variables
LOW(Signal_Pin)
END INTERRUPT
PRIVATE SUB TMR2_Initialize()
TMR2ON = 0 // Disable TMR2
TMR2IE = 0 // Turn off TMR2 interrupts
PR2 = 149 // TMR2 Period register PR2
T2CON = %00000000 // T2CON 0:1 = Prescale
// 00 = Prescale is 1:1
// 01 = Prescale is 1:4
// 1x = Prescale is 1:16
// 3:6 = Postscale
// 0000 = 1:1 postscale
// 0001 = 1:2 postscale
// 0010 = 1:3 postscale...
// 1111 = 1:16 postscale
TMR2 = 0 // Reset TMR2 Value
TMR2IE = 1 // Enable TMR2 interrupts
TMR2ON = 1 // Enable TMR2 to increment
ENABLE(TMR2_Interrupt)
END SUB
// Start Of Program...
'led = 1
LOW(Red_Pin_1)
LOW(Green_Pin_1)
LOW(Blue_Pin_1)
LOW(Red_Pin_2)
LOW(Green_Pin_2)
LOW(Blue_Pin_2)
LOW(Red_Pin_3)
LOW(Green_Pin_3)
LOW(Blue_Pin_3)
Red_Duty = 10
Green_Duty = 10
Blue_Duty = 10
Red_DutyVal = 0
Green_DutyVal = 10
Blue_DutyVal =10
'cathod_on = 1 //transtor enable
uS = 0
mSa = 0
RandGen.Initialize(128) // Initialize the Random Number generator
TMR2_Initialize // Setup and enable TMR2
trisA = %00000000 // set up ports as outputs
trisb = %00000000
WHILE True
analog_on ()
// Create an infinite loop
RandomVal = RandGen.Rand // Grab a random number from 0 to 255
// Select RandomVal // Find out what colour to increase/decrease
//RED
Red_Duty =RandomVal-15
IF Red_Duty > 80
THEN mSa = 0
REPEAT
UNTIL mSa = 10
INC(Red_Duty)
ENDIF
// GREEN
Green_Duty =RandomVal-25
IF Green_Duty > 100
THEN mSa = 0
REPEAT
UNTIL mSa = 10
INC(Green_Duty)
ENDIF
// BLUE
Blue_Duty = RandomVal-35
IF Blue_Duty > 0
THEN mSa = 0
REPEAT
UNTIL mSa = 10
' DelayMS(5000)
INC(Blue_Duty)
ENDIF
'xxxxxxxxxxxxxxxxxxxxxxx
{
if RandomVal > 100 then
low (Green_Pin)
low(led) ' port B.3 for testing code progress
DelayMS(5000)
high (Green_Pin)
high (led)
delayms(500)
end if
'xxxxxxxxxxxxxxxxxxxxx
}
WEND
END