PDA

View Full Version : 16LF1824 strange problem



midali
- 17th August 2014, 22:44
Hi to all
I have some trouble with a code but I don't understand what is wrong : the code or a trouble from PBP3 compilation

1. First time if I turn ON the PIC and then RC with "varb <140 or varb > 160", the code go directly to BEEP
2. if I turn ON first time RC with "varb <140 or varb > 160" and then turn ON the PIC, the code wait in the INIT loop until "140 < varb < 160" and then go to BEEP

Can somebody help me with this trouble ?

1000 x thx in advance !



The code:
#CONFIG
__CONFIG _CONFIG1, _FCMEN_OFF & _FOSC_INTOSC & _WDTE_OFF & _MCLRE_OFF & _CP_ON & _IESO_OFF & _BOREN_OFF & _PWRTE_OFF
__CONFIG _CONFIG2, _PLLEN_OFF & _STVREN_OFF & _LVP_OFF
#ENDCONFIG

Define OSC 4
OSCCON = %01101010 'sets the internal oscillator to 4Mhz

ADCON0 = %00000000
ADCON1 = %00000000
FVRCON = %11000010 'turn fixed voltage reference on & set it to 2.048V
CM1CON0 = 0 ' COMPARATORS OFF
CM1CON1 = 0
CM2CON0 = 0
CM2CON1 = 0
CPSCON0 = 0 'CAPACTIVE SENSE MODULE OFF
RCSTA.7 = 0 'SERIAL PORT OFF

TRISA = %00000000
TRISC = %00000100
ANSELA = %00000000
ANSELC = %00000000

OPTION_REG.7 = 0
WPUA = %00000000
porta = 0
portc = 0

varb var byte

init:
pulsin portc.2 , 1, varb
pause 20
if varb < 140 or varb > 160 then init
if varb > 140 and varb < 160 then goto beep
goto init

beep:
...code....
goto beep
end

jamie_s
- 18th August 2014, 07:59
ive seen similar issues when reading ADC's and not managed to figure it out

EarlyBird2
- 18th August 2014, 09:38
On start up the output of the RC will be unpredictable. So add a start delay before init: begin with "pause 1000" and adjust by testing the result. This also applies to ADC all sensors I have used require a start delay for the reading to stabilise.

Acetronics2
- 18th August 2014, 10:35
looks everything perfectly obvious ...

1) @ first pass, varb value is not initialized ... so, it may be 255, 0 or whatever you want ... not really important here, but ...
2) your receiver may output its fail-safe value signal ( 1500µs ? ) until the Tx signal is captured .... so, a false detection may occur.

so, remember to ALWAYS initialize your variables ... here, 0 will make it !!!

one other little thing ...
pause 20 is not so good, as you can miss one pulse if signal incomes @ more than 46 Hz ( Graupner/JR is 45 Hz ... tadaaaaaa ) ...
as PULSIN waits for the pulse to occur, there's no special need for this delay ... other than pacing the detection loop.


and also think if the Tx is off, the Pic returns 0 as a PULSIN measure ...

Alain

midali
- 18th August 2014, 10:37
Thx for your reply ...I tried with delay between 500ms and 3000 ms...the same. Also i tried with 16F1503 ...the same.

midali
- 18th August 2014, 10:53
Thx Alain !

My Rx have not a fail-safe. When signal is missing the pulse is 0 .
I use for test usually RC , not Graupner or Futaba JR

I changed my code, but the same:

varb var byte
varb = 0
pause 200

init:
pulsin portc.2 , 1, varb
if varb < 140 or varb > 160 then init
if varb > 140 and varb < 160 then goto beep
goto init

midali
- 18th August 2014, 11:22
Also I tried a simple way :

varb = 0
pause 1000

init:
pulsin portc2, 1, varb
if varb < 50 then init
if varb > 50 then goto beep
goto init

beep:
...code...
goto beep



So ,really varb = 0 but INIT loop is missing in my code.

EarlyBird2
- 18th August 2014, 11:32
Have you a way to display varb? Using debug for example. I am thinking that your code is not the problem.

Acetronics2
- 18th August 2014, 12:05
'************************************************* ******************************
'************************************************* ******************************
' Twinair Lights 3
'
' Lights and gear driver for Robbe Twinair
'
' Input is a 3 positions switch, Gear servo signal.

' Outputs 2 positions servo signal 1.1 and 1.9 ms and 8 digital outputs to drive
' position lights, Cabin Lights, Landing lights, Flashes ( 3 sep. ) and Beacons ( 2 Sep. ).

' short push on button ( park pos. ) enables system to restart without power OFF
' long push ( park pos. ) enables gear to go up and lock for transportation.

' 16F1825 or equ. processor with two+ Hard PWM module.
' Use ICD2/3 or PicKit3 as a programmer !!!

' OK On September 8th 2013
' 16F1825 ... 1977 Lines
'
'************************************************* ******************************
'************************************************* ******************************
' Defines

#DEFINE BEC_First 1 ' Wait for signal @ Power ON

DEFINE OSC 8
DEFINE PULSIN_MAX 6500

'************************************************* ******************************
'Config

#CONFIG
__CONFIG _CONFIG1, _FOSC_INTOSC & _WDTE_ON & _PWRTE_ON & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOREN_ON & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF
__CONFIG _CONFIG2, _WRT_ALL & _PLLEN_OFF & _STVREN_OFF & _BORV_HI & _LVP_OFF
#ENDCONFIG

OSCCON = %01110000 ' Internal RC @ 8Mhz
OSCTUNE = %00000000 ' Internal osc Adjustment

INTCON = 0
PIE1 = 0
PIE2 = 0

CM1CON0.7 = 0
CM2CON0.7 = 0
CPSCON0 = 0
CPSCON1 = 0
DACCON0 = 0
FVRCON = 0

CCP1CON = %00001100
CCP2CON = %00001100
CCP3CON = %00000000
CCP4CON = %00000000

'************************************************* ******************************
'ADC parameters

ADCON0 = 0 ' all digital. A/D disabled
ADCON1 = %10000000
ANSELA = %00000000
ANSELC = %00000000

'************************************************* ******************************
' Port parameters & Aliases

OPTION_REG.7 = 0 ' Global Pull Up Enable

PORTA = %00000000
PORTC = %00000000
WPUA = %00000000

TRISA = %00101000
TRISC = %00010000
WPUC = %00010000 ' Enable Weak Pull Up for Manual Gear drive


Poslight_ VAR PORTA.0 ' Position Lights
CabinLight_ VAR PORTA.1 ' Cabin Lights
Landlight_ VAR PORTA.2 ' Landing Lights
Signal_ VAR PORTA.3 ' Gear Signal Input
Gearout_ VAR PORTA.4 ' Gear Signal Output
Batt_ VAR PORTA.5 ' Battery level Input ( 4.3v / 4.7v )

Flashlight1_ VAR PORTC.0 ' FlashLight #1
Flashlight2_ VAR PORTC.1 ' FlashLight #2
Flashlight3_ VAR PORTC.2 ' FlashLight #3
Beacon1_ VAR PORTC.3 ' Beacon #1 need CCP2 Output !
Manual_ VAR PORTC.4 ' Manual Gear retract input
Beacon2_ VAR PORTC.5 ' Beacon #2 need CCP1 Output !

'************************************************* ******************************
' Variables

Servoval VAR WORD
ServovalOld VAR WORD
Servomes VAR WORD
Gearval VAR WORD

Sample VAR BYTE
I VAR BYTE
J VAR BYTE
Bad VAR BYTE
Delay VAR BYTE
HPWMVal1 VAR BYTE
HPWMVal2 VAR BYTE
HPWMVal3 VAR BYTE
HPWMValue1 VAR BYTE
HPWMValue2 VAR BYTE

Battlo VAR BIT
Beacons VAR BIT
FlashLight VAR BIT
FirstPass VAR BIT

'************************************************* ******************************
' Constants

Beacon1 CON 1 ' Alias for HPWM ch.1
Beacon2 CON 2 ' Alias for HPWM ch.2
Stabsample CON 10 ' Nb of samples before command being accepted
GearDown CON 220 ' Down Gear Pulse ( x5 ms )
GearUp CON 380 ' Up Gear Pulse ( x5 ms )
Losig CON 21400 ' 22500 - Geardown*5
Upsig CON 20600 ' 22500 - GearUp*5

'************************************************* ******************************

CLEAR
PORTA = %00100000 ' Batt prepos. to 1 ( Ok level )

'************************************************* ******************************
'************************************************* ******************************
First: ' Wait for Signal and Verify Voltage: lock if low

#IF BEC_First ' Wait for R/C Signal if Power ON First

PULSIN Signal_,1,servomes
IF ( servomes < 160 ) OR ( servomes > 440 ) THEN First

#ENDIF

PAUSE 100 ' Voltage settling time ...

IF !Batt_ OR Battlo THEN

Battlo = 1
LOW Gearout_
GOTO FIRST

ENDIF

'************************************************* ******************************
' is there Someone alive here ???

Alive:
PULSIN Signal_,1,servomes
IF ( servomes < 160 ) OR ( servomes > 440 ) THEN First ' just to be sure ...

IF Servomes <= 330 THEN ' No ... Gear Pulse is NOT High
' do Place Gear switch to HIGH Gear
I = (I + 1)// 25
IF !I THEN ' Flash LandLights until High Gear
HIGH LandLight_
ELSE
LOW LandLight_
ENDIF
GOTO Alive

ENDIF

LOW LandLight_ ' Yes, Switch in the good position let's go on
I = 0

'************************************************* ******************************
' Send GearUP signal to unlock gear

FOR I = 1 to 10 ' Blink PosLights for 6.8 sec
HIGH PosLight_
FOR J = 1 to 15 ' 22.5 ms*15
LOW GearOut_
PULSOUT GearOut_, GearUp
PAUSEUS Upsig
NEXT J

LOW PosLight_
FOR J = 1 to 15 ' 22.5 ms*15
LOW GearOut_
PULSOUT GearOut_, GearUp
PAUSEUS Upsig
NEXT J
NEXT I
Gearval = GearUp ' initialize Gearval

'************************************************* ******************************
'Switch is in UP position and Gear is UP and unlocked ...
' We can place it down now ... without any unwanted movement.

'************************************************* ******************************
'************************************************* ******************************
Main:

WHILE !Manual_
WEND

FirstPass = 1
WHILE 1

'************************************************* ******************************
start:
'************************************************* ******************************

IF !Batt_ OR Battlo THEN
Battlo = 1
GOTO Nosignal ' Endless loop if Batt LOW
ENDIF

Start1:
PULSIN Signal_,1,servomes

Sample = sample + 1

IF ( servomes < 160 ) OR ( servomes > 440 ) THEN

Bad = ( Bad + 1 ) MIN 16 ' stop if Bad.4 = 1

IF Bad.4 THEN
GOTO nosignal ' 16 false pulses ... warning !!!
ELSE
GOTO Start ' Check closer for EACH frame ...
ENDIF
' before processing.
ENDIF

IF Sample = 0 THEN Bad = 0 ' Clear Bad only if 256 good frames (~ 5-6 secs )
' for better glitch catching !!!
Servoval = Servomes ' Signal is then good ...

IF ABS(Servomes - ServovalOld) < 3 AND I => Stabsample THEN Fast

' But we need 10 samples to avoid switching on transients !
' Let's allow +/- 10 µs jitter

IF I = 0 THEN ServovalOld = Servoval ' let's initialize Refvalue

IF ABS( Servoval - ServovalOld ) > 2 THEN ' Too different ... let's restart sampling
I = 0
LOW Gearout_
PULSOUT Gearout_, Gearval ' Continue sending last servo pulses
GOTO Start
ENDIF

I = I + 1
IF I < Stabsample THEN ' Not enough samples !
LOW GearOut_
PULSOUT Gearout_, Gearval ' Continue sending last servo pulses
GOTO Start
ENDIF

'************************************************* ******************************
' We have 10 "equal" Samples: We can continue and process the command
Fast:
SELECT CASE Servoval

Case is > 330 ' Hi pos = Flight

IF FirstPass THEN ' Save lights Batt on ground
HIGH CabinLight_ ' Just CabinLights ON
GOTO Powersave
ENDIF

Beacons = 1 ' Beacons ON
FlashLight = 1 ' FlashLight ON
HIGH Poslight_ ' Position Lights ON
LOW CabinLight_ ' Cabins Lights OFF
LOW Landlight_ ' Landing Lights OFF
Powersave:
Gearval = GearUp ' Gear UP

Case is => 270 ' Mid Pos @ 300 = TakeOff
FirstPass = 0 ' Enable Lights
Beacons = 1 ' Beacons ON
FlashLight = 1 ' FlashLight ON
HIGH Poslight_ ' Position Lights ON
HIGH Landlight_ ' Landing Lights ON
LOW CabinLight_ ' CabinsLights OFF
Gearval = GearDown ' Gear Down

Case else ' Lo Pos = Parking
FirstPass = 0 ' Enable Lights
Beacons = 0 ' Beacons OFF
FlashLight = 0 ' Flashlights OFF
LOW Poslight_ ' POSITION Lights OFF
HIGH CabinLight_ ' CabinsLights ON
LOW Landlight_ ' Landing Lights OFF
Gearval = GearDown ' Gear LOW

' Check for Manual Gear UP
Delay = 0
BUTTON Manual_, 0, 255, 0, Delay, 1, GearUP_

END SELECT

Not_so_sure:

LOW Gearout_
PULSOUT Gearout_, Gearval

IF Beacons THEN ' Let's run the Beacons @ +/- 1Hz ( 45 x 22.5 ms )

LOOKUP HPWMVal1,[0,0,0,0,0,0,0,0,0,1,2,5,8,10,15,40,70,100,130,160, 195,230,237,255 _
,237,230,195,160,130,100,70,40,15,10,8,5,2,1,0,0,0 ,0,0,0,0],HPWMValue1

LOOKUP HPWMVal2,[0,0,0,0,0,0,0,0,0,1,2,5,8,10,15,40,70,100,130,160, 195,230,237,255 _
,237,230,195,160,130,100,70,40,15,10,8,5,2,1,0,0,0 ,0,0,0,0],HPWMValue2

HPWMVal1 = ( HPWMVal1 + 1 )//45
HPWMVal2 = ( HPWMVal2 + 1 )//43
HPWMVal3 = ( HPWMVal1 + 1 )//42

HPWM Beacon1 ,HPWMValue1,5000 ' Beacon1 Control
HPWM Beacon2 ,HPWMValue2,5000 ' Beacon2 Control

ELSE
HPWMVal1 = 0
HPWMVal2 = 0
HPWMVal3 = 0

HPWM Beacon1,0,5000
HPWM Beacon2,0,5000
ENDIF


IF FlashLight THEN ' Let's run the 3 double Flashlights ** @ +/- 1Hz

IF ( HPWMVal1 => 15 AND HPWMVal1 < 20 ) OR ( HPWMVal1 => 27 AND HPWMVal1 < 32 ) THEN
HIGH Flashlight1_
ELSE
LOW Flashlight1_
ENDIF

IF ( HPWMVal2 => 19 AND HPWMVal2 < 24 ) OR ( HPWMVal2 => 31 AND HPWMVal2 < 36 ) THEN
HIGH Flashlight2_
ELSE
LOW Flashlight2_
ENDIF

IF ( HPWMVal3 => 25 AND HPWMVal3 < 30 ) OR ( HPWMVal3 => 37 AND HPWMVal3 < 42 ) THEN
HIGH Flashlight3_
ELSE
LOW Flashlight3_
ENDIF

ELSE
LOW Flashlight1_
LOW Flashlight2_
LOW Flashlight3_

ENDIF


WEND

END

'************************************************* ******************************
nosignal: ' Land lights Blink @ 2.5 Hz , Gear Down , others OFF
'************************************************* ******************************

LOW PosLight_
LOW CabinLight_
LOW Flashlight1_
LOW Flashlight2_
LOW Flashlight3_

IF FirstPass THEN next3 ' If signal cut Gear stays UP

HPWM 1,0,500
HPWM 2,0,500

LOW Beacon1_
LOW Beacon2_

HPWMVal1 = 0
HPWMVal2 = 0
HPWMVal3 = 0

WHILE 1 ' Endless loop

HIGH LandLight_

FOR I = 1 to 10 ' ~ 200 ms
LOW Gearout_
PULSOUT Gearout_, Geardown ' Gear Down !
PAUSEUS losig
NEXT I

LOW LandLight_

FOR I = 1 to 10 ' ~ 200 ms
LOW Gearout_
PULSOUT Gearout_, Geardown ' Gear Down !
PAUSEUS losig
NEXT I

WEND

'************************************************* ******************************
GearUP_: ' Button has to be pressed for more than 7 seconds to place gear UP

LOW Cabinlight_ ' Just PosLights !

' let's verify if we are really sure ...

FOR I = 1 to 3 ' Blink Poslights for 2 sec
HIGH PosLight_
FOR J = 1 to 15
LOW GearOut_
PULSOUT GearOut_, GearDown
PAUSEUS Losig
NEXT J

LOW PosLight_
FOR J = 1 to 15
LOW GearOut_
PULSOUT GearOut_, GearDown
PAUSEUS Losig
NEXT J
NEXT I

Delay = 0
BUTTON Manual_, 0, 255, 0, Delay, 1, pushed ' Test if Button still pushed

released: ' Let Gear down and wait

WHILE 1 ' send DownGear Signal Forever
LOW Gearout_
PULSOUT Gearout_, GearDown ' Let Gear Down !
PAUSEUS Losig

' Rearm system w/Button, no Power OFF needed... or let Gear Down if power OFF !
'

IF !Manual_ THEN ' Wait for Button release before continue
PAUSE 1000 ' Time to release button
GOTO Main
ENDIF
WEND ' NO we Continue Looping.

pushed:
FOR I = 1 to 7 ' Blink Poslights for 4.8 sec
HIGH PosLight_
FOR J = 1 to 15
LOW GearOut_
PULSOUT GearOut_, GearDown
PAUSEUS Losig
NEXT J

LOW PosLight_
FOR J = 1 to 15
LOW GearOut_
PULSOUT GearOut_, GearDown
PAUSEUS Losig
NEXT J

Delay = 0
BUTTON Manual_, 0, 255, 0, Delay, 1, nextI ' Test Button
I = 0 ' IF YES ... jump to nextI
GOTO Not_so_sure ' IF NO ... return
nextI:
NEXT I

next1:' is Button still pushed ???
Delay = 0
BUTTON Manual_, 0, 255, 0, Delay, 1, next2 ' YES ... jump to next2
GOTO Not_so_sure ' NO ... return
next2:' still pushed ??? ... you really sure ???
Delay = 0
BUTTON Manual_, 0, 255, 0, Delay, 1, next3 ' YES ... we proceed !!!
GOTO Not_so_sure ' NO ... return
next3:

' Switch OFF all Lights

'LOW PosLight_ ' already being done !!!
'LOW Cabinlight_ ' already being done !!!

LOW LandLight_
LOW Flashlight1_
LOW Flashlight2_
LOW Flashlight3_

HPWM Beacon1,0,5000
HPWM Beacon2,0,5000
LOW Beacon1_
LOW Beacon2_

HPWMVal1 = 0
HPWMVal2 = 0
HPWMVal3 = 0


WHILE 1 ' send UpGear Signal Forever
LOW Gearout_
PULSOUT Gearout_, GearUP ' Gear UP !
PAUSEUS Upsig
WEND

'************************************************* ******************************

END


But this works more than fine ...

Alain

midali
- 18th August 2014, 12:15
Tomorrow I'll have a little oscilloscope. Something its strange, because I verified with a simple way the level pulse: I put in parallel with portc.2 a simple device with 10F202 that turn ON a LED when signal is missing (< 0,5 ms) and turn OFF LED when signal is different to 0 (>0,5ms) and work perfectly.
Sometime , 1 from 15 tries, my code work good. Between tests I wait few minutes and I discharged the 10uF capacitor from Vcc line.

Amoque
- 18th August 2014, 12:57
The manual describes using parenthesis in IF/ THEN statements as so:

IF (X) AND (Y) THEN
IF (X) OR (Y) THEN

This forces evaluation of the conditions prior to making the comparison. It does not appear, from the code posted, that you are doing so.

midali
- 18th August 2014, 14:10
@ Amoque - Also, I tried with () but no effect

@ Alain - 1000X thx for your example. I'm a begginer and I see in my eyes more fog :) ....but was a inspiration for me and I solved . I think that is a unusually code, but work !

Code :

init:
pulsin portc.2 , 1, varb
for i = 1 to 50
pulsin portc.2 , 1, varb
varb = varb +1
next i
if varb > 60 then
goto init2
else
goto init
endif
goto init

init2:
pulsin portc.2 , 1, varb
if varb > 160 or varb <140 then goto init
if varb > 140 and varb < 160 then goto beep
goto init2

beep:
...code...
goto beep

Thx to all for help!

EarlyBird2
- 18th August 2014, 22:31
@ Amoque - Also, I tried with () but no effect

In your code () will have no effect because the simple comparisons work as expected. But using () makes the code easier to read and it is good practice to always use them.


@ Alain - 1000X thx for your example.

Alain's code is almost the same as yours


varb var byte
varb = 0
pause 200

init:
pulsin portc.2 , 1, varb
if varb < 140 or varb > 160 then init
if varb > 140 and varb < 160 then goto beep
goto init

Except he added a pause 100 and a retry just to make sure


varb var byte
varb = 0
pause 200

init:
pulsin portc.2 , 1, varb
if (varb < 140) or (varb > 160) then init
pause 100
pulsin portc.2 , 1, varb
if (varb < 140) or (varb > 160) then init
if (varb > 140) and (varb < 160) then goto beep
goto init

Your code that works is adding delays in a complicated way and also has a retry.



init:
pulsin portc.2 , 1, varb
for i = 1 to 50
pulsin portc.2 , 1, varb
varb = varb +1
next i
if varb > 60 then
goto init2
else
goto init
endif
goto init

init2:
pulsin portc.2 , 1, varb
if varb > 160 or varb <140 then goto init
if varb > 140 and varb < 160 then goto beep
goto init2

beep:
...code...
goto beep


It looks to me that on starting the RC a "false signal" is received that satisfies the "if varb > 140 and varb < 160 then goto beep" condition and by adding the delay and retry these "false signals" are trapped. Can you try your modified code above, the one with the added pause 100 and retry in red?

midali
- 19th August 2014, 10:32
I tried your code...the same. But is something what I don't understand ... I used 2 different variables and work great :) . It don't work if I use the same variable ! Today I'll try initialisation from Alain's code. In this moment my code work good, but I want to understand whats happen inside.

Code:
varb var byte
vara var byte
varb = 0
vara = 0
pause 200

init:
pulsin portc.2 , 1, vara
if vara < 140 or vara > 160 then init
pause 100
pulsin portc.2 , 1, vara
if vara < 140 or vara > 160 then init
if vara > 140 and vara < 160 then goto beep
goto init

bepp:
pulsin portc.2 , 1, varb
....code....
goto beep

1000 x THX

Acetronics2
- 19th August 2014, 12:13
bepp:
pulsin portc.2 , 1, varb
....code....
goto beep
1000 x THX

something rings your bell ???

midali
- 19th August 2014, 15:45
wow ...you see quickly :smile:

Yeap..I edit bepp...does not matter.

EarlyBird2
- 20th August 2014, 07:56
I tried your code...the same. But is something what I don't understand ... I used 2 different variables and work great :) . It don't work if I use the same variable ! Today I'll try initialisation from Alain's code. In this moment my code work good, but I want to understand whats happen inside.



varb var byte
vara var byte
varb = 0
vara = 0
pause 200

init:
pulsin portc.2 , 1, vara
if vara < 140 or vara > 160 then init
pause 100
pulsin portc.2 , 1, vara
if vara < 140 or vara > 160 then init
if vara > 140 and vara < 160 then goto beep
goto init

beep:
pulsin portc.2 , 1, varb
....code....
goto beep1000 x THX

Are you saying the init routine works but beep does not work if vara is used but does work if varb is used?
If so I am puzzled.
Can you post the code in beep?

midali
- 20th August 2014, 10:14
Its very strange! If I use the same variable in init and in main(another loop), no work properly. If I use different variables, work great. Totally mistake!

Code:

varb var byte
vara var byte
x var byte
divf var byte
divf = 0
varb = 0
vara = 0
x = 0
pause 200

init:
pulsin portc.2 , 1, vara
if vara < 140 or vara > 160 then init
pause 100
pulsin portc.2 , 1, vara
if vara < 140 or vara > 160 then init
if vara > 140 and vara < 160 then goto beep
goto init

beep:
hpwm 1 , 10, 3000
high front
pause 80
low front
pause 200
hpwm 1 , 5, 1200
high front
pause 80
low front
pause 200
hpwm 1 , 10, 3000
high front
pause 80
low front

main:
pulsin portc.2 , 1, varb

hpwm 1 , x, 1954

'========================miss signal===========================
if varb < 50 then
varb = 150
x=0
low front
endif
'================================================= =============

if varb> 160 then
high front
divf=255*(varb-160)/30
if varb > 190 then varb = 190
if x > 255 then x = 255
if divf > x then x=x+2
if divf < x then x=x-6
if x < 15 then x=15
endif

if varb < 160 and varb > 140 then
low front
x = 0
endif
goto main
end

richard
- 20th August 2014, 10:54
would it be possible to post the actual code you are using (in code tags preferably) not some edited portion.what are your config settings ? a schematic sketch could be in order. without the full story any solutions are just speculation
whats "front" ? where is that defined


this sort of code seems pretty awkward and may not really address the problem


init:
pulsin portc.2 , 1, vara
if vara < 140 or vara > 160 then init
pause 100
pulsin portc.2 , 1, vara
if vara < 140 or vara > 160 then init
if vara > 140 and vara < 160 then goto beep
goto init

have a go at something like this




DEFINE PULSIN_MAX 500 ; why wait longer if its already out of limits (may need adjustment)
init_ok var bit

init_ok=0


init:

while portc.2 ; wait till portc.2 goes low
wend
while ! init_ok ; or init_ok=0 if you like
pulsin portc.2 , 1, vara
if abs(vara-150)<=10 then init_ok=1
wend

richard
- 20th August 2014, 11:49
or 5 good pulses in a row


DEFINE PULSIN_MAX 500 ; why wait longer if its already out of limits (may need adjustment)
init_ok var byte
init_ok=5

init:

while portc.2 ; wait till portc.2 goes low
wend
while init_ok ; or init_ok>0 if you like
pulsin portc.2 , 1, vara
if abs(vara-150)<=10 then
init_ok=init_ok -1
else
init_ok=5
endif
wend

midali
- 20th August 2014, 14:04
Something is strange. Now it work with the same variable in init and in main loops. Yesterday I tried a many time and don't worked.
@ richard : thx for your suggestions
Here is working code :


#CONFIG
__CONFIG _CONFIG1, _FCMEN_OFF & _FOSC_INTOSC & _WDTE_OFF & _MCLRE_OFF & _CP_ON & _IESO_OFF & _BOREN_OFF & _PWRTE_OFF
__CONFIG _CONFIG2, _PLLEN_OFF & _STVREN_OFF & _LVP_OFF
#ENDCONFIG

DEfine OSC 4
OSCCON = %01101010 'sets the internal oscillator to 4Mhz



ADCON0 = %00000000
ADCON1 = %00000000
FVRCON = %11000010 'turn fixed voltage reference on & set it to 2.048V (last two bits set the voltage ...01 =1.024V,10=2.048
CM1CON0 = 0 ' COMPARATORS OFF
CM1CON1 = 0
CM2CON0 = 0 ' COMPARATORS OFF
CM2CON1 = 0
CPSCON0 = 0 'CAPACTIVE SENSE MODULE OFF
RCSTA.7 = 0 'SERIAL PORT OFF

TRISA = %00000000
TRISC = %00000100
ANSELA = %00000000
ANSELC = %00000000

OPTION_REG.7 = 0
WPUA = %00000000

PORTA = 0
PORTC = 0

Symbol front = PORTA.4


x var byte
varb var byte
divf var byte

x = 0
varb = 0
divf = 0

pause 100

init:
pulsin portc.2 , 1, varb
if (varb < 140) or (varb > 160) then init
pause 100
pulsin portc.2 , 1, varb
if (varb < 140) or (varb > 160) then init
if (varb > 140) and (varb < 160) then goto beep
goto init

beep:
hpwm 1 , 10, 3000
high front
pause 80
low front
pause 200
hpwm 1 , 5, 1200
high front
pause 80
low front
pause 200
hpwm 1 , 10, 3000
high front
pause 80
low front


'************************************************* ***********
main:
pulsin portc.2 , 1, varb
hpwm 1 , x, 1954

'=======================miss signal=========================
if varb < 50 then
varb = 150
x=0
low front
endif
'================================================= ===========

'========================front==================== ===========
if varb> 160 then
high front
if varb > 190 then varb = 190
divf=255*(varb-160)/30
if x > 255 then x = 255
if divf > 255 then divf = 255
if divf >x then x=x+2
if divf <x then x=x-6
endif
'================================================= ===========

'==================== stay ==================================
if varb < 160 and varb > 140 then
low front
x= 0
endif
'================================================= ============

'==========================light================== ============
if varb < 140 and varb > 50 then
high PORTA.0
else
low PORTA.0
endif
goto main
end

EarlyBird2
- 21st August 2014, 10:37
Well done you have made a lot of progress. Just a couple of small points


DEfine OSC 4should be

DEFINE OSC 4

if only to make it more readable.


OSCCON = %01101010 'sets the internal oscillator to 4Mhz
not that it matters in this case and simply to further understanding.

_FOSC_INTOSC directs the internal oscillator to be used
OSCCON = %01101000 sets the oscillator defined by FOSC to 4Mhz

OSCCON = %01101010 switches to the internal oscillator and sets it to 4Mhz

So what difference does it make? It looks like there are two methods for achieving the same thing.

Dave
- 21st August 2014, 11:53
"not that it matters in this case and simply to further understanding." I beg your pardon? When using the internal oscillator, "OSCCON" register is the only way to select the internal oscillator speed. Unless it is set it will default to 500Khz.

EarlyBird2
- 21st August 2014, 21:03
"not that it matters in this case and simply to further understanding." I beg your pardon? When using the internal oscillator, "OSCCON" register is the only way to select the internal oscillator speed. Unless it is set it will default to 500Khz.


I am asking a question not stating anything why say "I beg your pardon?" did I say something wrong.

You are correct Dave, about the speed did I say otherwise?

The question is why are there two methods to select the internal oscillator and what is the difference. In the code FOSC is used to select the internal oscillator and also OSCCON is used to select the internal oscillator.

mackrackit
- 21st August 2014, 23:49
FOSC in the configs tells the type of oscillator used. In this case internal.

OSCCON sets the speed of the internal with bits 6-3.

Bit 1-0 of OSCCON is for two-speed start-up. If internal is selected in the config, two-speed start-up is disabled.

EarlyBird2
- 22nd August 2014, 07:36
From the datasheet.


5.4.1 TWO-SPEED START-UP MODE
CONFIGURATION
Two-Speed Start-up mode is configured by the
following settings:
• IESO (of the Configuration Word 1) = 1; Internal/
External Switchover bit (Two-Speed Start-up
mode enabled).
• SCS (of the OSCCON register) = 00.
• FOSC<2:0> bits in the Configuration Word 1
configured for LP, XT or HS mode.

Which I take to mean that SCS has to be 00 for Two-Speed Start-up to work. I am not certain this is the same as



Bit 1-0 of OSCCON is for two-speed start-up. If internal is selected in the config, two-speed start-up is disabled.

This section of the datasheet is the source of the issue



5.2.2.1 HFINTOSC
The High-Frequency Internal Oscillator (HFINTOSC) is
a factory calibrated 16 MHz internal clock source. The
frequency of the HFINTOSC can be altered via
software using the OSCTUNE register (Register 5-3).
The output of the HFINTOSC connects to a postscaler
and multiplexer (see Figure 5-1). One of nine
frequencies derived from the HFINTOSC can be
selected via software using the IRCF<3:0> bits of the
OSCCON register. See Section 5.2.2.7 “Internal
Oscillator Clock Switch Timing” for more information.
The HFINTOSC is enabled by:
• Configure the IRCF<3:0> bits of the OSCCON
register for the desired HF frequency, and
• FOSC<2:0> = 100, or
• Set the System Clock Source (SCS) bits of the
OSCCON register to ‘1x’.

Which as I read it states either set FOSC<2:0> = 100 or SCS to '1x' but not both. Yet in the code


#CONFIG
__CONFIG _CONFIG1, _FCMEN_OFF & _FOSC_INTOSC & _WDTE_OFF & _MCLRE_OFF & _CP_ON & _IESO_OFF & _BOREN_OFF & _PWRTE_OFF
__CONFIG _CONFIG2, _PLLEN_OFF & _STVREN_OFF & _LVP_OFF
#ENDCONFIG

DEfine OSC 4
OSCCON = %01101010 'sets the internal oscillator to 4Mhz

Both are set.

Obviously the code works and setting SCS to '1x' is having no detrimental effect in this case. Am I interpreting the datasheet wrong?

midali
- 22nd August 2014, 08:16
Are informations are welcome.
From yours discussions and from datasheet I understand:
- in Config , _FOSC_INTOSC set osc to internal ; default speed is set to 500KHz
- OSCCON or DEFINE OSC set speed

richard
- 22nd August 2014, 08:19
best way to view it
from the data sheet


bit 1-0 SCS<1:0>: System Clock Select bits
1x = Internal oscillator block
01 = Timer1 oscillator
00 = Clock determined by FOSC<2:0> in Configuration Word 1.

when FOSC<2:0> = 100 is equivalent to 1x = Internal oscillator block ie they both set the same option (logical or )

but watch out if you want the pll engaged

EarlyBird2
- 22nd August 2014, 08:21
OSCCON sets the speed and DEFINE from the manual


Note that "DEFINE OSC" doesn't set or change the actual clock frequency. It only tells PBP what to expect.

midali
- 22nd August 2014, 10:06
Thx for patience!
Now its clear in my mind. This was the 6th writed code in my life, so I have not experience, but step by step I hope that I'll understand this field.

EarlyBird2
- 23rd August 2014, 06:49
Thx for patience!
Now its clear in my mind. This was the 6th writed code in my life, so I have not experience, but step by step I hope that I'll understand this field.

No patience required on my part.

I was in your position 40 years ago and learned step by step usually by trial and error as there was no internet and obviously no forum.

If you want more help just ask.

Demon
- 26th December 2014, 22:09
http://www.picbasic.co.uk/forum/showthread.php?t=19648

Post moved to a new thread

Robert