Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Trash to Treasure project

Status
Not open for further replies.
Have a look here for how to use the sleep command in Swordfish. You must do this exactly as shown.
 
Thanks Audioguru.
After progressing with the transistor section I am contemplating putting the pic to SLEEP during daylight but Swordfish has no SLEEP command in its library that I could find.

Can you add assembler/machine code routines to Swordfish? - assuming you can, then it's simple to put it to sleep.

However, it's generally much more important to make sure everything else is 'switched off', and that orientations of transistors etc. mean there's no current used in the off state.

Using sleep can be VERY useful, it's a good idea to have a 32KHz clock crystal running on TMR1 - this uses almost no power, and you can wake the PIC up every second via TMR1 interrupts so it can check if it needs to be running yet. While 1 second sounds like it's wasting a lot of power, running a few microseconds once a second doesn't waste much power.

Another simple power saving technique is to use a lower clock speed, usually PIC's run vastly faster than you need, and you can drastically reduce consumption by running them slower - many modern PIC's allow you to adjust clock speed while it's running, so you can make savings on a dynamic basis. No need to get carried away though, as external devices (LED's etc.) will consume MUCH more than the PIC does.
 
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
 
going grocery shopping w/ wife and will redo something simpler just using delayms.
have laptop, will travel
have a feeling the interupt is messing with the ADC.
 
back at the same old code and determined that the TMR2_interupt is conflicting with the ADCON1.7
more research
wonder if I changed the ADC input to portA.1 seeing how the chip has more ADC inputs
 
Boy what a new day does to the mindset!!
I made a few changes on a similar code that started changing the LED colors so went back to my code in post #24
Asked myself why isn't the sub routine called BEFORE the interrupt??
INTERRUPT TMR2_Interrupt()
analog_on ()
' HIGH(Signal_Pin)
SAVE(0)

after adding this change the ADC works exceptionally well and the candles flicker nicely without changing colors. The ADC is really senesitive to ANY change in light conditions. Need to adjust.
Now to get the tin cans done and mounted for the Pic, cds and ON/OFF switch.
Will post a video when done. /
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top