I have recently bought a PIC programmer and a book on PICs (PIC Your Personal Introductory Course by John Mortom). The only problem is, when I tried to run the first program, it doesn't work. Basically, it should turn on an LED when a button is pressed, but it doesn't. I am really quite confused about this, because it is a _very_ simple program and the compiler gives 0 errors. The PIC I'm using is a PIC16F627 and here is the code: Code: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ; ; Written by: double_c ; ; Date: 23-08-04 ; ; Version: 1.0 ; ; Filename: push button 1 ; ; For device: PIC16F627 ; ; Frequency: 4MHz ; ; ; ; Description: Turns on an LED if a ; ; button is pressed. ; ; ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; list P=16F627 ;=============== ; Declarations: porta equ 05h portb equ 06h portc equ 07h org 1FFh goto Start org 0 ;=============== ; Subroutines: Init clrf porta ;Reset I/O pins clrf portb movlw b'0000' ;RA0: LED, RA1-3: N/C tris porta movlw b'00000001' ;RB0: BUTTON, RB1-7: N/C tris portb retlw 0 ;=============== ;Program Start: Start call Init Main btfss porta,0 ;tests the push button goto LEDoff bsf portb,0 ;turns the LED on goto Main LEDoff bcf portb,0 ;turns the LED off goto Main END Any help on what I'm doing wrong would be appreciated. Thanks, double_c
Just looking at it, your reset vector is all wrong. Try changing this: org 1FFh goto Start org 0 to: org 0 goto Start Probably the easiest way to check that things are working is to step through it line by line in the debugger. If it behaves correctly in the debugger, then move onto programming the chip.
Why are you declaring ports A,B, and C? If you are using MPLAB than you don't have to define them this may be messing with things. Secondly, rather than using the tris command, you can directly address TRISx as a register. So you could "clrf TRISA" and change the second one to "movwf TRISB". Also you aren't changing banks. (maybe why you are using the tris command) but I find it easier to have a subroutine that changes the bank control bits, so you just "call Bank0" to change the bits.But since that is just for initialization you might as well just set and clear RP0 manually.
right..... everything you said there just went right over my head i'll try changing the org0 thing btw, what debugger?
Code should work just fine. Only thing I noticed is that when you press the button, the code branches to turn the led off, then goes right back to main to turn it right back on. You have now created a 50% duty cycle PWM when you press the button. Basically, you never turn the LED off, just give it half the power (and may never notice it).
I notice you've not included your config word. Do you set the configuration bits at any time before programming?
Code: ;=============== ; Declarations: org 0 goto Init org 0x04 RETFIE ;=============== ; Subroutines: Init clrf status clrf intcon clrf porta ;Reset I/O pins clrf portb BSF STATUS, RP0 ; Bank 1 MOVLW 0xFF ; All inputs MOVWF TRISA CLRF TRISB ; ALL ouputs ;=============== ;Program Start: Main btfss porta,0 ;tests the push button goto LEDoff bsf portb,0 ;turns the LED on goto Main LEDoff bcf portb,0 ;turns the LED off goto Main END also try using the include directive to get the chip's definitions, and the config directive to get the correct config words set!
Hmm. You probably want a Code: BCF STATUS, RP0 ; Bank 0 at the end of the Init routine. Otherwise your button check routine will just end up reading the TRIS registers.