PDA

View Full Version : Need help for PIC16C745



christina86
- 12th September 2007, 02:45
Hi everybody,

I'm using the PIC16C745 microchip to do my final year project
the situation is, if switch 1 is pressed, LED will light up and when switch 2 is pressed again, the LED will go off. I am using an assembly language ( sofware MPLab version 7.2) to write the code. I have encounter some problem, when i apply the 5 V to the circuit. The Led must Light up, but it did not .
Here is myCode:

MAIN org 00000030h

Start
bsf STATUS,RP0 ; select bank1
clrf PORTB ; clear PORTB output latch
movlw b'00000110' ; Set all pins PORTB 1,2 as Input
MOVWF TRISB ;

ON
btfsc PORTB,1 ; Has S2 been press? (Normally high,goes low when pressed.)
goto ON ; No, go back check again
movlw b'00000001' ; move it from register 1 to w.
movwf PORTB ; move it to PORTB to light up the LED

OFF
btfsc PORTB,2 ; Has key been press? (Normally high, goes low when pressed.)
goto OFF ; No, check again
movlw b'00000000' ; move it from register 1 to w.
movwf PORTB ; OFF the LED

DebounceA
btfss PORTB,1 ; Has key been released?
goto DebounceA ; No, wait again
goto Start ; Loop again
end ; directive indicates end of code

I use only one port instead to be an input and output. When i build the code inside the PIC16C745, the Led still not turn on. Can you help me to see weather to code i have done correctly. Thank you

Archangel
- 12th September 2007, 06:12
Hi christina86,
Certainly I am not the one to ask about assembly language. That said, it looks to me like your sub routines get locked into a loop and never finish their execution.
example<p>
ON
btfsc PORTB,1 ; Has S2 been press? (Normally high,goes low when pressed.)
goto ON ; No, <font color=red> goto ON and repeats forever</font color>
movlw b'00000001' ; move it from register 1 to w.
movwf PORTB ; move it to PORTB to light up the LED<p>
I think if you wish to recheck the first line you should use whatever assembly equiv. of a for next loop.
JS

paul borgmeier
- 12th September 2007, 06:16
Hi Christina86,

This is a BASIC language forum ... ASM help is harder to get here. A quick look at your program......

See Table 4.2 in the 16F745 datasheet. You will see that PORTB resides in BANK0 and TRISB resides in BANK1.

When you want to work with PORTB, you need to make sure you are in BANK0 (clear RP0 and RP1 in the STATUS register)

When you want to work with TRISB, you need to make sure you are in BANK1 (set RB0 as you did)

If you want to continue on and change PORTB values, you need to switch back to BANK0 before updating PORTB (otherwise you will actually update TRISB even though you told the program to update PORTB)


Start
clrf PORTB ; STATUS defaults to BANK0 - clear PORTB output latch (not really a latch)
bsf STATUS, RP0 ; select bank1
movlw b'00000110' ; Set all pins PORTB 1,2 as Input
MOVWF TRISB ;
bcf STATUS, RP0 ; go back to Bank 0

The 16C745 appears to be an interesting chip – I hope you are using the windowed version.

Being that this is a BASIC forum, I should tell you that what you are trying to do would be trivial with PicBasic or PicBasic Pro

Good Luck

paul borgmeier
- 12th September 2007, 06:20
Hi Joe


btfsc PORTB,1

this command says "bit test file skip if clear" ... it jumps (skips) over the goto is PORTB.1 = 0

paul borgmeier
- 12th September 2007, 06:31
Hey Joe,

I should note that the last time you and I tried to help someone together, we got locked up and whipped ... we better tread carefully here :):)

http://www.picbasic.co.uk/forum/showthread.php?t=6783

christina86
- 13th September 2007, 02:25
Hi Paul and Joe,

Thank you so mush for replying my post. I am totally new to this PIC16C745, i have got the project from my teacher which is totaly new to me, it is nothing related to my course of study. I am in the process of learning. Please give me your guidance.
I have made some changes to the code but it is still not working . Can you help me to check?
The code is :
AIN org 00000030h

Start
bsf STATUS,RP0 ; select bank1
movlw b'00000110' ; Set pins PORTB 1,2 as Input
movwf TRISB ;
bcf STATUS, RP0 ; go back to Bank 0
ON
btfss PORTB,1 ; Has S1 been press? (Normally high,goes low when pressed.)
goto ON ; No, go back check again
movlw b'00000001' ; move it from register 1 to w.
movwf PORTB ; move it to PORTB to light up the LED

OFF
btfss PORTB,2 ; Has key been press? (Normally high, goes low when pressed.)
goto OFF ; No, check again
movlw b'00000000' ; move it from register 1 to w.
movwf PORTB ; OFF the LED

end ; directive indicates end of code
Thank you Paul and Joe so much . I look forward to hearing from you.

paul borgmeier
- 13th September 2007, 02:48
Is this all of your code or is it a snippet (in post above)? I ask because you have no list, includes, or CONFIG at the start. Inside the Microchip folder (from the MPLAB install), you should be able to find a template for the 16c745. I would suggest using this as the outer shell and put your stuff inside. Further, because the PIC you picked is quite "fancy", you should make it blink an LED first to ensure it is even running. Your program is not difficult, but you should start even simpler than you are otherwise you do not know if your problem is a logic issue, a hardware issue, a register issues, etc.

Try to blink your led first

Oh, if your code is complete, you lost the wrap around to return to Start. Your code would just run off into no-where and execute any code left in your PIC (should it be the windowed version)

christina86
- 13th September 2007, 06:59
Hi Paul,

I did include list and # include and CONFIG in my code .
list p=16c745 ; list directive to define processor
#include <p16c745.inc> ; processor specific variable definitions

__CONFIG _CP_OFF & _WDT_OFF & _PWRTE_ON & _E4_OSC

But yet with this, my code is not working.Can you help me. I did like what you said do the simple one first ( blink the LED only ) but it is not working either . Here is my code : for binkling one.
list p=16c745 ; list directive to define processor
#include <p16c745.inc> ; processor specific variable definitions

__CONFIG _CP_OFF & _WDT_OFF & _PWRTE_ON & _E4_OSC


MAIN org 00000030h


bsf STATUS,RP0 ; select bank1
movlw b'00000000' ; Set pins PORTB 1,2 as Input
movwf TRISB ;
bcf STATUS, RP0 ; go back to Bank 0

Start
movlw b'00000001' ; move it from register 1 to w.
movwf PORTB ; move it to PORTB to light up the LED
movlw b'00000000' ; move it from register 1 to w.
movwf PORTB ; OFF the LED
goto Start

Can you help me with the code cause this Fridat is my dateline.

Thank you so much

paul borgmeier
- 13th September 2007, 07:47
>>I did like what you said do the simple one first ( blink the LED only ) but it is not working either
Did the LED not light or did it stay on?

What crystal are you using?

Do you have a schematic?

Archangel
- 13th September 2007, 07:58
Hey Joe,

I should note that the last time you and I tried to help someone together, we got locked up and whipped ... we better tread carefully here :):)

http://www.picbasic.co.uk/forum/showthread.php?t=6783
Yes, We were spanked ! I think this poor kid is going to have to repeat this class, I just looked at the .lst file for a simple "Blinky" and it was 57k file size, I have just downloaded Section 29 from Microchip website.
Christina, here is a forum from Microchip, where they speak assembly, and apparently C as well.:
http://forum.microchip.com/tt.aspx?forumid=-2

christina86
- 13th September 2007, 08:23
Thank you Joe for the link.

HI Paul

I use the 6 MHZ crystal. I did draw the schematic digram but i do not have a sofware to draw the schematic , so i use free hand.

Let me expalain how i connect .
the PIC16C745have 28 pins

connect pin 8 and 19 to the ground
pin 20 to +5 V
pin 21 connnect to a resistor and Led then + 5 V

pin 21 _______(Resistor)____(Led)___ +5V
pin 22 connect reisistor ( also the resistor connect to the ground ) + then the switch and follow by + 5V

(ground)
^
I
I
pin 22.______Resistor__(switch)___(+5V)

pin 23 connect reisistor ( also the resistor connect to the ground ) + then the switch and follow by + 5V


(ground)
^
I
I
pin 23.______Resistor__(switch)___(+5V)

And the cystal have got 4 pins

Pin no 7 to the ground
pin 8 to pin 9 of PIC16C745
pin 14 to the 5V

This schematic is tofor match for :

AIN org 00000030h

Start
bsf STATUS,RP0 ; select bank1
movlw b'00000110' ; Set pins PORTB 1,2 as Input
movwf TRISB ;
bcf STATUS, RP0 ; go back to Bank 0
ON
btfss PORTB,1 ; Has S1 been press? (Normally high,goes low when pressed.)
goto ON ; No, go back check again
movlw b'00000001' ; move it from register 1 to w.
movwf PORTB ; move it to PORTB to light up the LED

OFF
btfsc PORTB,2 ; Has key been press? (Normally high, goes low when pressed.)
goto OFF ; No, check again
movlw b'00000000' ; move it from register 1 to w.
movwf PORTB ; OFF the LED

end

paul borgmeier
- 13th September 2007, 09:06
You did not answer

Did the LED light up in your blink test?

Is your 745 windowed or are you burning new PICs each time?

On the diode, which end (anode or cathode) is connect to 5V?

Do you have an Oscilloscope? I have never used a four pin Xtal so I cannot comment on its correctness. If you have a scope, it would be easy to find out.

you say "btfsc PORTB,2 ; Has key been press? (Normally high, goes low when pressed.)"
but you described the opposite - normally low and high when pressed. If it truly is opposite your program flies past this routine and then off into the ether. You would never see the light blink at 24MHz clock speed. You need to have a way to either send the program back to top (e.g., goto START) or a way to stop the controller from running away (e.g., goto $)

I beg you to make your LED blink first ... the rest is trivial after that.

BobK
- 13th September 2007, 10:47
Hello,

I too am not an assembly language user but shouldn't there be some sort of delay (250ms at least)after the LED is turned on and another delay (250ms) after it is turned off so you can actually see the LED changing states?

BobK

paul borgmeier
- 13th September 2007, 14:14
Hello,

I too am not an assembly language user but shouldn't there be some sort of delay (250ms at least)after the LED is turned on and another delay (250ms) after it is turned off so you can actually see the LED changing states?

BobK

Yes - that is why I asked


Did the LED not light or did it stay on?
without the delays, if the OP's code was working the LED would appear to the eye as stuck on

I have asked twice. :(

GrandPa
- 13th September 2007, 20:46
Just my 2 cents,
Never seen a 4 pins crystal or resonator, maybe she's using an oscillator, not a crystal.
Then we should know if the config is right for that.

BTW Can we use oscillator ???




And the cystal have got 4 pins


Also, maybe someone familiar with this PIC can make a very small program using PBP and send the hex file to Christina to validate the circuit she's using.

J-P

christina86
- 14th September 2007, 03:57
sorry, it is my mistake. I used the 4 pin using an oscillator.

GrandPa
- 14th September 2007, 04:38
sorry, it is my mistake. I used the 4 pin using an oscillator.

HeHe!

I'm happy to know that I helped you find the mistake. The good news is that you learned something you will not forget.I should also tell you that I've seen much worst that that. Ever heard of fried PICs?

BTW Is the program working fine now?

J-P