Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
}
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
Dim LED0 As PORTB.0, LED1 As PORTB.1, LED2 As PORTB.2, LED3 As PORTB.3//55XXX
//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
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
I have a PWM circuit and code for a 18F1320 BUT a smaller PIC would be more cost effective.
How does one convert from swordfish to assembly or ?? using a smaller cheaper PIC.
need 3 PWM outputs.
You mean $0.04-.05 on the smt resistors.The 1320 @ Mouser is $3.24 in an ssop package. Its smaller than the 18 pin dip. The SMD resistors are .50+ per.
Consider a version 2 if response goes well to version 1?Is a total rewrite worth the trouble?
If you think there's any chance at all that you might be doin' things with the 8 or 14 pin devices with a 14-bit core then you owe it to yourself to start learning one of the C languages.
Swordfish BASIC is by far the best BASIC language I've come across but (1) it limits you to 18F' devices, and (2) it's BASIC. I don't have anything against BASIC but unfortunately it's not considered "mainstream".
You shouldn't have too much trouble converting BASIC program statements into C statements though there will be a learning curve. Why not download the free/lite versions of BoostC and check it out?
Kind regards, Mike