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.

bacjk to this PWM candle

Status
Not open for further replies.

MrDEB

Well-Known Member
I thought I had it working BUT changing the pot (voltage on Port A.0 ADC) seems to slow down the PWM code but can't get it to stop the PWM (LED candles go out in LIGHT and code enables the LEDs on B ports when A.0 is dark

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 PORTA.0                  // 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
 
going with a 741 op amp compatator

hopefully this circuit will work.
It should??
R12 is a photo transistor aprox 150k in light and 1Meg in dark
 

Attachments

  • tina comparator.PNG
    tina comparator.PNG
    28.8 KB · Views: 172
Status
Not open for further replies.

Latest threads

Back
Top