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.

using the CCP1 output/input pin 18 on an 18F1320

Status
Not open for further replies.
we have a winner I hope

Got this from DIY (Thanks to Lightspeed) but it works on my test pcb.
Now just combine the candle LED code and this code together and hope
I need to figure out how to enable the B ports using the output from the ADC. This code does it but will it work with my candle code?
NOTE a new pak of modules for SF Swordfish User Module Pack
Code:
Device = 18F1320
Clock = 8

#option AD_RESIST = 800000

Include "InternalOscillator.bas"
Include "ADC.bas"
Include "MinADtime.bas"

Dim LED0 As PORTB.0, LED1 As PORTB.1, LED2 As PORTB.2, LED3 As PORTB.3
Const DarkVal = 512

Function Get_ADC_Sample() As Word       // Function to grab the ADC sample
   Result = ADC.Read(0)                 // Grab an ADC sample from channel 0
End Function

ADCON1.0 = 0                            // Configure AN0 as analogue

While True                              // Create an infinite loop
    If Get_ADC_Sample > DarkVal Then    // Monitor for change
        High(LED0)
        High(LED1)
        High(LED2)
        High(LED3)       
    Else
        Low(LED0)
        Low(LED1)
        Low(LED2)
        Low(LED3)
    EndIf
Wend
 
candle LED routine

here is my candle routine and yes it works.
better than any other routine I have seen that just turn the LEDs off and on
Code:
Device = 18F1320
Clock = 8

Include "IntOSC8.bas" 
Include "RandGen.bas"
Include "Utils.bas" 
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 PORTA.0     // Signal output to frequency meter
 
Dim Red_Pin As PORTB.0,
    Green_Pin As PORTB.1,
    Blue_Pin As PORTB.2,
    Red_Duty As Byte,
    Green_Duty As Byte,
    Blue_Duty As Byte,
    Red_DutyVal As Byte,
    Green_DutyVal As Byte,
    Blue_DutyVal As Byte,
    RandomVal As Byte
   
 
Dim uS As Word,
    mS 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 + 50
        If uS >= 1000 Then
            uS = uS - 1000
            Inc(mS)
        EndIf       
        Inc(Red_DutyVal)
        Inc(Green_DutyVal)
        Inc(Blue_DutyVal)
        If Red_DutyVal > Red_Duty Or Red_Duty = 0 Then
            Red_Pin = 0
        Else
            Red_Pin = 1
        EndIf
        If Green_DutyVal > Green_Duty Or Green_Duty = 0 Then
            Green_Pin = 0
        Else
            Green_Pin = 1
        EndIf
        If Blue_DutyVal > Blue_Duty Or Blue_Duty = 0 Then
            Blue_Pin = 0
        Else
            Blue_Pin = 1
        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 = %00000001         // 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...
Low(Red_Pin)
Low(Green_Pin)
Low(Blue_Pin)
Red_Duty = 30
Green_Duty = 0
Blue_Duty = 0
Red_DutyVal = 0
Green_DutyVal = 0
Blue_DutyVal =0
 
uS = 0
mS = 0

RandGen.Initialize(128)       // Initialize the Random Number generator
TMR2_Initialize               // Setup and enable TMR2
 
While True                    // Create an infinite loop
    RandomVal = RandGen.Rand  // Grab a random number from 0 to 255 
   
             Red_Duty =RandomVal-25 
             
           
            
       
            If Red_Duty > 0
              Then  mS = 0
                Repeat
                Until mS = 19
                Dec(Red_Duty)
            EndIf
      
             Green_Duty =RandomVal-20 
             
            If Green_Duty > 0
              Then  mS = 0
                Repeat
                Until mS = 19
                Dec(Green_Duty)
            EndIf
     
             Blue_Duty = RandomVal-30
             
            If Blue_Duty > 0
              Then  mS = 0
                Repeat
                Until mS = 19
                Dec(Blue_Duty)
            EndIf
            Wend
          
         End
 
not pretty but??

Got the syntax right, now to see if it works??
Got some other work to do (some more glueing on project) before I can test this code but thought I would share
Code:
Device = 18F1320
Clock = 8

//Include "IntOSC8.bas" 
Include "RandGen.bas"
Include "Utils.bas"
//Include "ADC.bas"                   // Include the ADC.bas library
 #option AD_RESIST = 800000 //11XXXXXXXX

 Include "InternalOscillator.bas" //22XXXXXXXXX
 Include "ADC.bas"        //33XXXXXX
 Include "MinADtime.bas"  //44XXXXXXXX

Dim LED As PORTB.3                  // Declare the LED pin turns on transistor
 

//OSCCON = %01110110                 // Setup the internal OSC for 8Mhz
 

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 PORTA.3     // Signal output to frequency meter
 
Dim Red_Pin As PORTB.0,
    Green_Pin As PORTB.1,
    Blue_Pin As PORTB.2,
    Red_Duty As Byte,
    Green_Duty As Byte,
    Blue_Duty As Byte,
    Red_DutyVal As Byte,
    Green_DutyVal As Byte,
    Blue_DutyVal As Byte,
    RandomVal As Byte
   
 
Dim uS As Word,
    mS As Word           
 Const DarkVal = 512 //XXX

 Function Get_ADC_Sample() As Word       //2xx Function to grab the ADC sample
   Result = ADC.Read(0)                 //3xx Grab an ADC sample from channel 0
 End Function                            //4xxxx

 //ADCON1.0 = 0                            //5xxx Configure AN0 as analogue
  
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 + 50
        If uS >= 1000 Then
            uS = uS - 1000
            Inc(mS)
        EndIf       
        Inc(Red_DutyVal)
        Inc(Green_DutyVal)
        Inc(Blue_DutyVal)
        If Red_DutyVal > Red_Duty Or Red_Duty = 0 Then
            Red_Pin = 0
        Else
            Red_Pin = 1
        EndIf
        If Green_DutyVal > Green_Duty Or Green_Duty = 0 Then
            Green_Pin = 0
        Else
            Green_Pin = 1
        EndIf
        If Blue_DutyVal > Blue_Duty Or Blue_Duty = 0 Then
            Blue_Pin = 0
        Else
            Blue_Pin = 1
        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 = %00000001         // T2CON 0:1 = Prescale
                             
 
    TMR2 = 0                  // Reset TMR2 Value   
    TMR2IE = 1                // Enable TMR2 interrupts
    TMR2ON = 1                // Enable TMR2 to increment
    Enable(TMR2_Interrupt)
End Sub
 
// Start Of Program...
Low(Red_Pin)
Low(Green_Pin)
Low(Blue_Pin)
Red_Duty = 30
Green_Duty = 0
Blue_Duty = 0
Red_DutyVal = 0
Green_DutyVal = 0
Blue_DutyVal =0
 
uS = 0
mS = 0
 //Function Get_ADC_Sample() As Word       //2xx Function to grab the ADC sample
 //  Result = ADC.Read(0)                 //3xx Grab an ADC sample from channel 0
 //End Function                            //4xxxx

 ADCON1.0 = 0                            //5xxx Configure AN0 as analogue
RandGen.Initialize(128)       // Initialize the Random Number generator
TMR2_Initialize               // Setup and enable TMR2
 
While True                    // Create an infinite loop
 If Get_ADC_Sample > DarkVal Then    //7xxx Monitor for change
 else 
 end if
    RandomVal = RandGen.Rand  // Grab a random number from 0 to 255 
   
             Red_Duty =RandomVal-25 
             
           
            
       
            If Red_Duty > 0
              Then  mS = 0
                Repeat
                Until mS = 19
                Dec(Red_Duty)
            EndIf
      
             Green_Duty =RandomVal-20 
             
            If Green_Duty > 0
              Then  mS = 0
                Repeat
                Until mS = 19
                Dec(Green_Duty)
            EndIf
     
             Blue_Duty = RandomVal-30
             
            If Blue_Duty > 0
              Then  mS = 0
                Repeat
                Until mS = 19
                Dec(Blue_Duty)
            EndIf
            Wend
          
         End
{
 
couldn't wait besides the shop is not fully heated.
The PWM works but the ADC does not turn off the ADC portion.
tried changing the get sample >darkval to get sample <darkval
still a no go
I should get up to par with mplab??
 
While True // Create an infinite loop
If Get_ADC_Sample > DarkVal Then //7xxx Monitor for change
else
end if
Not a fully formed statement. What happens after If...then, and else? Presumably the "else" and "endif" are artifacts from previous code.
 
It is supposed to execute the code following if the while true statement is true.
If Get_ADC_Sample > DarkVal Then ------if DarkVal is greater than GET sample then execute the rest of the WHILE TRUE statement
ELSE END IF --if it is light then the Get_ADC_Sample > DarkVal is false.
well that's my intention but the "false" never seems to execute. The LEDs continue to light via the PWM code.
end if--if statement is false then END
I am new at this code stuff but learning.
any suggestions?
I tried making the GET SAMPLE < DARKVAL
don't remember what i DID BUT AT ONE POINT i FOUND THAT {oups that cap lock} only port b0 came on. adjusted the pot and then all three came on. couldn't get them all turned off.
 
Got this from DIY (Thanks to Lightspeed) but it works on my test pcb.


Code:
While True                              // Create an infinite loop
    If Get_ADC_Sample > DarkVal Then    // Monitor for change
        High(LED0)
        High(LED1)
        High(LED2)
        High(LED3)       
    Else
        Low(LED0)
        Low(LED1)
        Low(LED2)
        Low(LED3)
    EndIf
Wend
The above code is what I mean as a fully formed If...Then statement. Else is the last condition, on which an If...Then statement acts on, prior to Endif. The Else condition should not be empty if it is specified.

Have you tried removing aforementioned Else and Endif? The Else, Low(LED's), Endif from above code should be added to the end of the random and set duty code prior to Wend.
Code:
 If Get_ADC_Sample > DarkVal Then    //7xxx Monitor for change
[COLOR="Red"] else    //remove
 end if[/COLOR]
    RandomVal = RandGen.Rand  // Grab a random number from 0 to 255 
   
             Red_Duty =RandomVal-25
.....
.....
.....

[COLOR="Red"] Else    //Add
        Low(LED0)
        Low(LED1)
        Low(LED2)
        Low(LED3)
    EndIf[/COLOR]
Wend
 
will try it

I see where your going with this
Code:
While True                    // Create an infinite loop
 If Get_ADC_Sample > DarkVal Then     //7xxx Monitor for change
 
 
 
 
    RandomVal = RandGen.Rand  // Grab a random number from 0 to 255 
   
             Red_Duty =RandomVal-25 
             
           
            
       
            If Red_Duty > 0
              Then  mS = 0
                Repeat
                Until mS = 19
                Dec(Red_Duty)
            EndIf
      
             Green_Duty =RandomVal-20 
             
            If Green_Duty > 0
              Then  mS = 0
                Repeat
                Until mS = 19
                Dec(Green_Duty)
            EndIf
     
             Blue_Duty = RandomVal-30
             
            If Blue_Duty > 0
              Then  mS = 0
                Repeat
                Until mS = 19
                Dec(Blue_Duty)
            EndIf
              Else                                //8xxx
        Low(LED0)
        Low(LED1)
        Low(LED2)
        Low(LED3)
    EndIf    //9xxxxxx
Wend         //10xxxxxx
           
         End
 
All it did was change the PWM speed??
Don't know why but then I am not going to play with this anymore tonight as 3 glasses of red wine have done its magic.
Will try again tomorrow.
Thanks, it LOOKED like it should work??
 
Pardon me, that should of been Red_Pin, Green_Pin, Blue_Pin, as LED0, LED1, LED2 not defined/dimensioned. Good luck tommorrow:=).
 
One gets engrossed on something and can't see the forrest because of the trees.
I DIM the LED0 etc but your suggestion of green_led etc is the correct way.
Thanks.
Will be making changes in the next 10 minutes (waiting for second computer to warm up)
 
we have partial sucess.
Adjusting the 5K pot the LEDs at a certain point just start blinking instead of a random blink .
They start out blinking randomily as they are supposed to. Adjust the pot and they are all the same.
Contemplating trying to see if I can get the extra LED on port B3 to light up when the WHILE TRUE statement is false.
For debugging purposses only.
 
making progress
At a certain point B3 comes on and the three LEDs that are flickering start pulsing all at the same rate.
adjusting the pot seems to adjust the flicker rate but B3 never goes out.
The program is stuck in a loop.
 
Well can't get the code to STOP when the GET SAMPLE > DARKVAL
got B3 to go high.
thinking just go with a transistor to enable the gropund return for the 3 candle LEDs.
might be easier but I am curious how to get the ADC output to stop when requitred.
will play with it later. been trying lots of different code pieces but nothing seems to make it stop before the while true statement is up and running.
 
got it working!!

minor issue is it takes awhile for the LEDs to all shut off when sample < darkval.
also when the code is turned on the B0 port flashes very fast but dim then goes out after about 5 seconds.
Code:
Device = 18F1320
Clock = 8


Include "RandGen.bas"
Include "Utils.bas"
                   
 #option AD_RESIST = 800000 //11XXXXXXXX

 Include "InternalOscillator.bas" //22XXXXXXXXX
 Include "ADC.bas"        //33XXXXXX
 Include "MinADtime.bas"  //44XXXXXXXX

Dim LED3 As PORTB.3                  // indicates when get sample > darkval




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 PORTA.3     // Signal output to frequency meter
 
Dim Red_Pin As PORTB.0,
    Green_Pin As PORTB.1,
    Blue_Pin As PORTB.2,
    Red_Duty As Byte,
    Green_Duty As Byte,
    Blue_Duty As Byte,
    Red_DutyVal As Byte,
    Green_DutyVal As Byte,
    Blue_DutyVal As Byte,
    RandomVal As Byte
   
 
Dim uS As Word,
    mS As Word           
 Const DarkVal = 512 //XXX

 Function Get_ADC_Sample() As Word       //2xx Function to grab the ADC sample
   Result = ADC.Read(0)                 //3xx Grab an ADC sample from channel 0
 End Function                            //4xxxx


Interrupt TMR2_Interrupt()
  
    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 + 50
        If uS >= 1000 Then
            uS = uS - 1000
            Inc(mS)
        EndIf       
        Inc(Red_DutyVal)
        Inc(Green_DutyVal)
        Inc(Blue_DutyVal)
        If Red_DutyVal > Red_Duty Or Red_Duty = 0 Then
            Red_Pin = 0
        Else
            Red_Pin = 1
        EndIf
        If Green_DutyVal > Green_Duty Or Green_Duty = 0 Then
            Green_Pin = 0
        Else
            Green_Pin = 1
        EndIf
        If Blue_DutyVal > Blue_Duty Or Blue_Duty = 0 Then
            Blue_Pin = 0
        Else
            Blue_Pin = 1
        EndIf               
    EndIf                     //
    Restore                   // Restore system variables
    Low(Signal_Pin) //port A3
End Interrupt
 
Private Sub TMR2_Initialize()
    TMR2ON = 0                // Disable TMR2
    TMR2IE = 0                // Turn off TMR2 interrupts   
 
    PR2 = 149                 // TMR2 Period register PR2
    T2CON = %00000001         // T2CON 0:1 = Prescale
                             
 
    TMR2 = 0                  // Reset TMR2 Value   
    TMR2IE = 1                // Enable TMR2 interrupts
    TMR2ON = 1                // Enable TMR2 to increment
    Enable(TMR2_Interrupt)
End Sub

// Start Of Program...
Low(Red_Pin)
Low(Green_Pin)
Low(Blue_Pin)
Red_Duty = 30
Green_Duty = 0
Blue_Duty = 0
Red_DutyVal = 0
Green_DutyVal = 0
Blue_DutyVal =0
 
uS = 0
mS = 0

 ADCON1.0 = 0                            //5xxx Configure AN0 as analogue
RandGen.Initialize(128)       // Initialize the Random Number generator
TMR2_Initialize               // Setup and enable TMR2
  
 
While True                    // Create an infinite loop
 
if Get_ADC_Sample> Darkval then // check for light or dark
                                // if false then end of routine
  
 
 
    RandomVal = RandGen.Rand  // Grab a random number from 0 to 255 
   
             Red_Duty =RandomVal-25 
             
           
            
       
            If Red_Duty > 0
              Then  mS = 0
                Repeat
                Until mS = 19
                Dec(Red_Duty)
            EndIf
      
             Green_Duty =RandomVal-20 
             
            If Green_Duty > 0
              Then  mS = 0
                Repeat
                Until mS = 19
                Dec(Green_Duty)
            EndIf
     
             Blue_Duty = RandomVal-30
             
            If Blue_Duty > 0
              Then  mS = 0
                Repeat
                Until mS = 19
                Dec(Blue_Duty)
            EndIf
             
                                              //8xxx
       
   
else
  toggle (led3)                 // indicate status of sample
  delayms (5000)              //indicate when get sample < darkval
  low (led3)
  toggle (led3)   
Low(Red_Pin)
Low(Green_Pin)
Low(Blue_Pin)
Red_Duty = 0
Green_Duty = 0
Blue_Duty = 0
Red_DutyVal = 0
Green_DutyVal = 0
Blue_DutyVal =0 
end if
Wend         //10xxxxxx           
         End
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top