PDA

View Full Version : pulsin again!anyone plz help!



engineer7
- 25th May 2011, 06:30
:mad: here is my code:


'************************************************* ***************
'* Name : UNTITLED.BAS *
'* Author : [select VIEW...EDITOR OPTIONS] *
'* Notice : Copyright (c) 2011 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 5/23/2011 *
'* Version : 1.0 *
'* Notes : *
'* : *
'************************************************* ***************
@ DEVICE pic16F877a, WDT_OFF ' Watchdog Timer
@ DEVICE pic16F877a, PWRT_OFF ' Power-On Timer
@ DEVICE pic16F877a, BOD_OFF ' Brown-Out Detect
@ DEVICE pic16F877a, LVP_OFF ' Low-Voltage Programming
@ DEVICE pic16F877a, CPD_OFF ' Data Memory Code Protect
@ DEVICE pic16F877a, PROTECT_OFF ' Program Code Protection
@ DEVICE pic16F877a, HS_OSC
Define OSC 20
Header var word
Body var word
i var byte
tmp var Byte
irButton var byte
irDevice var byte
irIN var PortB.2
SonyLED var PortB.5

'TrisA = %00000100
TrisB = %00000100
Start:
LOW SONYLED
IRBUTTON=255: IRDEVICE=255

Pulsin irin,0,header
if header > 1000 or header < 1350 then goto Start

for i =0 to 11
pulsin irin,0,tmp
if tmp >= 480 then
Body.0[i]=1 'Sony Logic 1 is 120
else
Body.0[i]=0 ' Sony Logic 0 is 60
endif
next
IRBUTTON = Body & %01111111 'Mask 7 bit
IRDEVICE = (Body >>7) & %00011111 'Mask 5 bit
If IRDEVICE=255 then goto start

if IRBUTTON >=0 and IRBUTTON < 255 then
high Sonyled
pause 300
low Sonyled
endif
pause 100
goto start



plz can anyone find any problem with it.im using 20MHZ oscillator and pic16f877a and pic basic pro!!! :(:confused: plz plz can any one point out the probelm in this.it is not showing any output on portb.5...

Bruce
- 25th May 2011, 14:41
What value will header need to have to NOT return to Start?

if header > 1000 or header < 1350 then goto Start

engineer7
- 25th May 2011, 17:16
1200!!!!
as im using 20mhz crystal which has a resolution of 2microseconds.
and the sony header is of 2400milliseconds so you get 1200 milliseconds...

engineer7
- 25th May 2011, 17:34
sorry its a 2400 microsecond header so divide by 2 microsecond get 1200!!!!

engineer7
- 25th May 2011, 17:51
oh yes the header should be
header<1000
n header >1350

but i have tried wid this..
its still not working.....

bogdan
- 26th May 2011, 01:16
-Turn off the Analog to Digital converter
-Comparators Off
-Set a maximum count for Pulsin: DEFINE PULSIN_MAX 1350

engineer7
- 26th May 2011, 06:10
sir i dont think that there would be any point of turning adc off.because i am using portb for input and it is not an analog port in pic 16f877a.in this pic only porta is the analog one!!!

Archangel
- 26th May 2011, 06:51
Hello,
I have to ask what exactly is your circuit doing or not doing? Does it do anything ?
Does it flash the LED ? This question next is not designed to offend you . . . Have you checked all of your connections for errors and continuity? Breadboard errors trip me up regularly as do poor connections caused by internal corrosion in the breadboards. You buy one, and unbeknownst to you, it sat on the dealer's shelf for ten years . . . and all the contacts are corroded. I have disassembled some that were really scuzzy.
One more thing, I think portB inputs default to weak pull ups on, turn them off in the option register.
EDIT: Nope default off. Easy to check with a hi impedance voltmeter though.

Last question: are you measuring a high pulse or a low pulse? State 0, indicates a low pulse, is that correct?

engineer7
- 26th May 2011, 08:15
thanku archangel sir for replying.

now the basic purpose of my project is to turn a relay on or off using sirc(sony protocol).

for this i need to decode just two keys:one for on and one for off.or maybe just one key that if a user presses a button 5v appear on output pin and if it presses the same button then it turns off!!this is one point...

the next point is that the above code is working for


if IRBUTTON >=0 and IRBUTTON < 255 then


but not working for
if irbutton=16 then high sony led '16 is for channel up



it is not working for this the above command
this is the second point...


and the third point is that i dont understand this
One more thing, I think portB inputs default to weak pull ups on, turn them off in the option register.
EDIT: Nope default off. Easy to check with a hi impedance voltmeter though... cant understand this :(

Archangel
- 27th May 2011, 00:55
OK, RE: I Dont understand . . .
Just a small argument with myself . . .
Let me explain my thought process, If the weak pullups were turned on, then applying voltage to that pin might go unnoticed by the MCU because voltage is already present . . .
Let me ask you this, can you output the value of pulsin to an LCD display or to your computer to find out its value, or flash an LED the number stored therein ?
That might give you some insight as to what is going on.
EDIT:


but not working for
if irbutton=16 then high sony led '16 is for channel up

NOT IN POSTED CODE ! Maybe it never gets there, please post
your code, use code tags as shown in the edit explanation at
the bottom of your edited last post

engineer7
- 27th May 2011, 04:51
'include "ALLDIGITAL.pbp"
@ DEVICE pic16F877a, WDT_OFF ' Watchdog Timer
@ DEVICE pic16F877a, PWRT_OFF ' Power-On Timer
@ DEVICE pic16F877a, BOD_OFF ' Brown-Out Detect

@ DEVICE pic16F877a, LVP_OFF ' Low-Voltage Programming
@ DEVICE pic16F877a, CPD_OFF ' Data Memory Code Protect
@ DEVICE pic16F877a, PROTECT_OFF ' Program Code Protection
@ DEVICE pic16F877a, HS_OSC

Define OSC 20

Header var word
Body var word
i var byte
tmp var Byte
irButton var byte
irDevice var byte

irIN var PortB.2
SonyLED var PortB.5


'TrisA = %00000100
TrisB = %00000100

Start:
LOW SONYLED
IRBUTTON=255: IRDEVICE=255


Pulsin irin,0,header
if header < 1000 or header > 1350 then goto Start


for i =0 to 11
pulsin irin,0,tmp
if tmp >= 480 then
Body.0[i]=1 'Sony Logic 1 is 120
else
Body.0[i]=0 ' Sony Logic 0 is 60
endif
next

IRBUTTON = Body & %01111111 'Mask 7 bit
IRDEVICE = (Body >>7) & %00011111 'Mask 5 bit

If IRDEVICE=255 then goto start


if IRBUTTON >=0 and IRBUTTON < 255 then
high Sonyled
pause 300
low Sonyled
endif

pause 100
goto start







the above code works....
but it is for all infrared buttons on the remote.i only want one button to do on/off of relay.if i replace this



if IRBUTTON >=0 and IRBUTTON < 255 then


by this




if IRBUTTON =16 then 'for channel up




it is not working :(.

mackrackit
- 29th May 2011, 15:36
Might be a good idea to display the value of .IRBUTTON on a terminal or LCD to see what is going on.

engineer7
- 30th May 2011, 06:15
sir how can i output the value of a button?? im using a sony tv remote...
so i guess no way to output the value....

mister_e
- 30th May 2011, 06:40
Dave meant the value of the IRBUTTON variable.

When developing software, sometimes you want to monitor what's happening INSIDE the PIC. Like Variables, Register, etc. The debatable lazy way is to use Simulator, but it doesn't solve any real life problem. So what many here do, is to send/display variables value on a LCD... or send it serially to your PC using Hyperterminal, MicroCode Studio Serial Communicator OR else so called "Terminal". If you have a PICKIT 2, you can use the built-in terminal utility.

To me, when you do PIC stuff, you should have at least the following tools:
1) Serial adapter To send/receive data to/from your PC AND/OR LCD (serial or Parallel)
2) Logic Analyzer
3) Scope

For IR stuff, using a logic analyzer to record the incoming data make the whole thing a real piece of cake. You could also record the data with your PC soundcard with any decent Wav/Audio editor (SoundForge, Audacity, etc). Once you know what kind of signal comes in (or to be expected), it's easier to create/debug your code.

HTH

engineer7
- 30th May 2011, 13:48
oh right thanks let me try it out!

engineer7
- 1st June 2011, 07:02
ok i have another question.my code is working the below one.
as i am very new to pic basic pro i just have one more question and i know that it is a piece of cake for u guys. ok in my code the sensor is on portb.2
and i am showing the output on portb.5.ok
the portb.5 is being turned high infinitly when it detects an incoming signal.i want to turn it back low (infinetly)when it detects the signal again and again back high when detects the signal again. can it be done???
maybe using an ISR?can anyone guide me or help me out???





'************************************************* ***************
'* Name : UNTITLED.BAS *
'* Author : [select VIEW...EDITOR OPTIONS] *
'* Notice : Copyright (c) 2011 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 5/23/2011 *
'* Version : 1.0 *
'* Notes : *
'* : *
'************************************************* ***************
'include "ALLDIGITAL.pbp"
@ DEVICE pic16F877a, WDT_OFF ' Watchdog Timer
@ DEVICE pic16F877a, PWRT_OFF ' Power-On Timer
@ DEVICE pic16F877a, BOD_OFF ' Brown-Out Detect
@ DEVICE pic16F877a, LVP_OFF ' Low-Voltage Programming
@ DEVICE pic16F877a, CPD_OFF ' Data Memory Code Protect
@ DEVICE pic16F877a, PROTECT_OFF ' Program Code Protection
@ DEVICE pic16F877a, HS_OSC
Define OSC 20
cmcon=2
Header var word
Body var word
i var byte
tmp var Byte
irButton var byte
irDevice var byte
irIN var PortB.2
SonyLED var PortB.5

'TrisA = %00000100
TrisB = %00000100
Start:
LOW SONYLED
IRBUTTON=255: IRDEVICE=255

Pulsin irin,0,header
if header < 1000 or header > 1350 then goto Start

check:
for i =0 to 11
pulsin irin,0,tmp
if tmp >= 500 then
Body.0[i]=1 'Sony Logic 1 is 120
else
Body.0[i]=0 ' Sony Logic 0 is 60
endif
next
IRBUTTON = Body & %01111111
if IRBUTTON <=9 then
loop:
high Sonyled
goto loop

endif
pause 100
goto start

Archangel
- 1st June 2011, 07:36
You could use the INT INT on RB0, I think if you used RBC interrupts the other B pins would interfere, but RB0 has a seperate INT INT interrupt which would be unaffected, but yes and I would use Darrel's instant interrupts


INCLUDE "DT_INTS-14.bas"

ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler INT_INT, _YourINTHandler, ASM, no
endm
INT_CREATE ; Creates the interrupt processor
ENDASM

@ INT_ENABLE INT_INT ; enable external (INT) interrupts


YourINTHandler:
put your code here . . .
@ INT_RETURN

cncmachineguy
- 1st June 2011, 11:54
No need for interrupts here. First thing to know, outputs will stay in the state you make them until something makes them change. So no need for this:


loop:
High sonyled
Goto loop

all you need is
High sonyled. That will keep the led on forever if nothing tells it to turn off. Further in your code, you have trapped the program to never leave the loop.

Next, if you want to "toggle" the led everytime you get the valid pulse, use
Toggle sonyled instead of high sonyled.

Hth.

engineer7
- 4th June 2011, 08:23
@ cncmachineguy
sir it doesnt work because
the led is high only when IRbutton is detected.... i want to keep it on forever ntil the next command without pressing the button...... is there any way?? maybe a function etc...???

@archangel
sir its difficult for me to understand it..the interrupt portion maybe if u could elaborate a little...
or could point me out to an exmaple program....

mackrackit
- 4th June 2011, 09:35
sir it doesnt work because
the led is high only when IRbutton is detected....

Lets see you non working code based on Bert's advice.

engineer7
- 4th June 2011, 14:18
here you go:





'************************************************* ***************
'* Name : UNTITLED.BAS *
'* Author : [select VIEW...EDITOR OPTIONS] *
'* Notice : Copyright (c) 2011 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 5/23/2011 *
'* Version : 1.0 *
'* Notes : *
'* : *
'************************************************* ***************
'include "ALLDIGITAL.pbp"
@ DEVICE pic16F877a, WDT_OFF ' Watchdog Timer
@ DEVICE pic16F877a, PWRT_OFF ' Power-On Timer
@ DEVICE pic16F877a, BOD_OFF ' Brown-Out Detect
@ DEVICE pic16F877a, LVP_OFF ' Low-Voltage Programming
@ DEVICE pic16F877a, CPD_OFF ' Data Memory Code Protect
@ DEVICE pic16F877a, PROTECT_OFF ' Program Code Protection
@ DEVICE pic16F877a, HS_OSC
Define OSC 20
cmcon=2
Header var word
Body var word
i var byte
tmp var Byte
irButton var byte
irDevice var byte
irIN var PortB.2
SonyLED var PortB.5
'TrisA = %00000100
TrisB = %00000100
Start:
LOW SONYLED
IRBUTTON=255: IRDEVICE=255

Pulsin irin,0,header
if header < 1000 or header > 1350 then goto Start

check:
for i =0 to 11
pulsin irin,0,tmp
if tmp >= 500 then
Body.0[i]=1 'Sony Logic 1 is 120
else
Body.0[i]=0 ' Sony Logic 0 is 60
endif
next
IRBUTTON = Body & %01111111
if IRBUTTON <=9 then
loop:
toggle Sonyled '<----------------------change!

endif
pause 100
goto start

cncmachineguy
- 4th June 2011, 14:34
Irbutton will never be less then 9 because you and it with 127. So your if will always be false.

engineer7
- 4th June 2011, 14:55
so u r saying that i should and it with 65???
BTW it works with less than 9....

cncmachineguy
- 4th June 2011, 15:05
I'm sorry, I am completely wrong. Need more coffee. I was thinking OR.

engineer7
- 4th June 2011, 15:25
np.you had me thinking there for a moment :)
but my problem is the same just a pulse spike of 5 volts on button press i want to keep it permantently off or on when io press the button only once...:mad:

Demon
- 4th June 2011, 15:56
...but my problem is the same just a pulse spike of 5 volts on button press...

I'm not very familiar with AND/OR and masks yet, but I wouldn't be surprised if your program did just what you told it to do.

IF TMP>=500 says to turn on during pulse and then off as soon as pulse stops.

mackrackit
- 4th June 2011, 17:21
if tmp >= 500 then
Will never be as tmp is BYTE size

cncmachineguy
- 4th June 2011, 21:37
Every iteration of your loop turns the led off right after start. There's you problem. :)

engineer7
- 5th June 2011, 13:01
thanks for reply everyone..
@demon and mackaraket guys the above code is working there is nth wrong with it.i just want to add an additional functionality of on/off...

@cncmachineguy i didnt understand your reply can u elaborate a little.... plz

cncmachineguy
- 5th June 2011, 13:24
Start:
LOW SONYLED <-------------------------Here you turn off the LED
IRBUTTON=255: IRDEVICE=255

Pulsin irin,0,header
if header < 1000 or header > 1350 then goto Start

check:
for i =0 to 11
pulsin irin,0,tmp
if tmp >= 500 then
Body.0[i]=1 'Sony Logic 1 is 120
else
Body.0[i]=0 ' Sony Logic 0 is 60
endif
next
IRBUTTON = Body & %01111111
if IRBUTTON <=9 then
loop:
toggle Sonyled '<----------------------change!

endif
pause 100
goto start <--------sends program back to the beginning, this will turn off the LED

Demon
- 5th June 2011, 18:52
if tmp >= 500 then
Will never be as tmp is BYTE size

Very good, a byte can count from 0 to 255.

That could give unpredictable results (except for the gurus that can most likely figure exactly how it will go screwy :D ); it may work, it may not.

cncmachineguy
- 5th June 2011, 19:46
@ Demon, I do not at all to be a guru who could figure out how it will go screwy, I am fairly certain IRBUTTON is ALWAYS = 0 after his for loop. Since there is NO way to cram a number bigger then %11111111,$FF,255 in a byte, it will always do the else setting all 11 bits to zero.

So I would certainly question the OP's statement that it works, I think it prolly does in fact receive the signal, and detect the header. but I think from there it will fail as soon as it is tested for specific button presses.

We shall wait and see :)

bogdan
- 6th June 2011, 01:03
without to check your code i think you should consider the frames (codes) of the ir Sony protocol... because Sony remotes all appear to repeat each frame a minimum of 3 times...and also i think you toggle the led by pressing too long time the button on the remote ..... add some some long pause for test

engineer7
- 6th June 2011, 13:52
@cncmachineguy thanx a lot for pointing me the problem.i ve been a fool n i admit... :( the toggle function works fine... thanx v much again:D your approach has been dead on...g8 advice for beginners!!:)
@demon u r right as well.the code is working for all the buttons of my remote(unexpected results)... not for just button <9 .problem wid temp most probably... i'll declare it a WORD n c how it goes...!

@bogden thanks for your input as well. i have put a delay of 100...

engineer7
- 7th June 2011, 06:29
my final code it is toggling on button 6 of your sony remote.n the good thing is no interrupts or timers simple code





@ DEVICE pic16F877a, WDT_OFF ' Watchdog Timer
@ DEVICE pic16F877a, PWRT_OFF ' Power-On Timer
@ DEVICE pic16F877a, BOD_OFF ' Brown-Out Detect
@ DEVICE pic16F877a, LVP_OFF ' Low-Voltage Programming
@ DEVICE pic16F877a, CPD_OFF ' Data Memory Code Protect
@ DEVICE pic16F877a, PROTECT_OFF ' Program Code Protection
@ DEVICE pic16F877a, HS_OSC

Define OSC 20
cmcon=2
Header var word
Body var word
i var byte
tmp var word
irButton var byte
irDevice var byte
irIN var PortB.2

SonyLED var PortB.5

TrisB = %00000100
Start:
'LOW SONYLED
IRBUTTON=255: IRDEVICE=255

Pulsin irin,0,header
if header < 1000 or header > 1350 then goto Start

check:
for i =0 to 11
pulsin irin,0,tmp
if tmp >= 500 then
Body.0[i]=1 'Sony Logic 1 is 120
else
Body.0[i]=0 ' Sony Logic 0 is 60
endif
next
IRBUTTON = Body & %01111111

if IRBUTTON=5 then
loop:
toggle Sonyled

endif
pause 100
goto start






copy/paste n njoy love PBP!!!!

Demon
- 7th June 2011, 07:01
Insignificant, but I would change this:



if IRBUTTON=5 then
loop:
toggle Sonyled

endif



To this:



if IRBUTTON=5 then toggle Sonyled