![]() |
![]() |
![]() |
|
|
|||||||
| Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc. |
|
|
Thread Tools | Display Modes |
|
|
(permalink) |
|
Hi there!
I have written a programme for a line follower robot ( though still not implemented), first I want to try it with LEDs, Here is the code : Code:
list p=16f628 #include<p16f628.inc> __CONFIG _MCLRE_ON & _CP_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT ERRORLEVEL -302 cblock 0x20 temp temp_s endc ;****************************************************************** org 0x00 goto main ;***********************************INTERRUPT LOOP***************** org 0X04 inter: movwf temp swapf STATUS,w clrf STATUS movwf temp_s btfsc PORTB,0x00 goto loop1 loop2: movlw b'00000101' movwf PORTA btfsc PORTB,0x05 goto loop2 goto loop_x loop1: movlw b'10001000' movwf PORTA btfsc PORTB,0x00 goto loop1 loop_x bcf INTCON,0x01 bcf INTCON,0x00 swapf temp_s,w movwf STATUS swapf temp,w retfie main: bsf INTCON,0x07 ;Globel interrupt enable(we are using interr.) bsf INTCON,0x04 bsf INTCON,0x03 ;RB4-RB7 interrupt on change is enabled/ in other words these pins will also work as ;interrupts and interrupt will occur every time any of these pin changes state ; i.e.:HIGH to LOW or LOW to HIGH, we can not set it to be on eather one state change bcf INTCON, 0x01 bcf INTCON, 0x00 ;Clear RB4-RB7 interrupt flag so, that another interr. can occur. ;**********************************SET UP THE PORTS ***************** bsf STATUS,RP0 ;switch to BANK 1 movlw b'00100001' movwf TRISB ;set RB6 & RB5 as input movlw b'00000000' movwf TRISA ;setPORT A all output bcf OPTION_REG,0x07 ; enable weak pull ups on port b/ pull ups mean internal resistors bcf STATUS,RP0 ;back to BANK 0 movlw 0x07 ;turn comparators off, so HIGH will be simple ; HIGH rather than +5V (compared to supply) so with the LOW movwf CMCON Loop: movlw b'00001001' movwf PORTA ;set pin 0 of port a HIGH goto Loop END Now let me explain you what this code supposed to do (which it does very well but only when the PIC is powered by PIC Kit 2): First when the power turned on RA0 and RA3 goes HIGH lighting YELLOW and GREEN LEDs, and when I press switch 1, RB0 goes high and an interrupt occur and YELLOW LED switched off and BLUE LED lights up and same sequance is applicable to RED and GREEN LEDs when I press switch 2 which causes interrupt at RB5 Again let me tell you that the PIC runs this programme very well BUT ONLY WHEN IT IS POWERED BY PIC KIT 2 AND NOT WHEN CONNECTED EXTERNALLY AS SOWN IN CKT. PLEASE, don't check that this code is o.k. for a line follower or not or don't tell me that there is a better code for that I know ther are many, I'm a layman (see how I learned PIC programming on thread titled" setup an interrupt in 16f690") , I JUST WANT TO ASK THAT WHY IT IS NOT WORKING WHILE POWERED EXTERNALLY ? By not working I mean : When I turn on the power YELLOW and GREEN LEDs lights up as it should be but, on either interrupt both LEDs goes off lightning NO OTHER LED, I tried tieing MCLRE high and even LOW but nothing work. PLEASE HELP ME !! Edit: Kindly see the circuit that is exactly how I connect it, nothing more nothing less
__________________
CHRIS Last edited by c36041254; 23rd May 2008 at 03:09 PM. |
|
|
|
|
|
|
(permalink) |
|
You don't have any capacitors on your power supply.
Also, it's not a good idea to drive LEDs straight from a pic without resistors. |
|
|
|
|
|
|
(permalink) | |
|
Quote:
You should have a 0.1uF ceramic capacitor close to the chip from VDD to VSS, and a 10uF electrolytic (or bigger) is a good idea somewhere near the chip from VDD to VSS (observe polarity). As House0Fwax says, put (typically) 330 ohm current limiting resistors in series with each LED. 180 ohm to 470 ohm is the typical range for current limiting resistors on LEDs. Depends on your LEDs and how bright you want them.
__________________
========================= Futz's Microcontrollers & Robotics ========================= Last edited by futz; 23rd May 2008 at 03:33 PM. |
||
|
|
|
|
|
(permalink) | |
|
Quote:
As for the caps 100uF and 0.1uF would be a nice start. |
||
|
|
|
|
|
(permalink) |
|
I understand that I need to couple the AC and so I tried this (see the ckt), but that is not working infact LEDs are now start blinking (red and yellow ). I don't get what do you mean by "...somewhare near the chip....", I mean whether it is near the chip or even at long distance , should not make any difference as far as the caps are across the Vdd and Vss.
__________________
CHRIS Last edited by c36041254; 24th May 2008 at 03:08 AM. |
|
|
|
|
|
|
(permalink) |
|
You definitely need a resistor from MCLR to 5V for the pic to run, if this is missing the pic may run intermittently and produce flashing LEDs. This is normally taken care of by the Pickit2 but once you remove it, you have to supply the resistor.
Also, if your 9V supply is a wall wart then you may need an electrolytic on the 9V side. You should also have a 0.1uF capacitor on both sides of the regulator and a small electrolytic on the 5V side. The 104pF capacitor is probably a 0.1uF anyway - 0.1uF capacitors are marked 104, that is 10 + 4*zeros after = 100,000pF = 0.1uF. It is important that the 0.1uF cap is near to the pic pins, this is to suppress fast switching currents. I'm sure you have checked it anyway but have you checked the obvious things like the voltage on the actual pic chip, many problems here have been solved with a new battery. Mike. |
|
|
|
|
|
|
(permalink) |
|
Another thing is R1 & R2 are pretty strong pull downs, PORTB has weak pullups FYI.
|
|
|
|
|
|
|
(permalink) |
|
Mike I have attached a 10k resistor as pull up for MCLRE but, haven't apply any cap. on 9v side so I'll try that. Thanks for help !
__________________
CHRIS |
|
|
|
|
|
|
(permalink) | |
|
Quote:
bleh_sm.jpg At the bottom left you can see the 33K MCLR pullup resistor right beside the power LED's current limiting resistor.
__________________
========================= Futz's Microcontrollers & Robotics ========================= |
||
|
|
|
|
|
(permalink) |
|
O.K. Ihave done the caps and attached the resistors to LEDs but, still only one switch (RB0 interr.) is working and that too when I have my fingure on the polar acp connected across Vss and Vdd. I have attached a pull down resistor of 10k on RB0, doing the same at RB5 stops the PIC and all LEDs goes off
__________________
CHRIS |
|
|
|
|
|
|
(permalink) |
|
Sounds like a poor connection somewhere, if it's a solderless breadboard they are not uncommon.
|
|
|
|
|
|
|
(permalink) |
|
I spent a whole day on that and found this:
When I ground the RB0 the RB5 interr. works fine and when ground the RB5 the RB0 works fine, with pull down resistors connected to both it should not be necessary to ground the interr. manualy but that's not happening. Is thare any way (except transistors) by which RB0 automatically gets grounded while giving high to RB5 and vice versa. When these interr. will be connected with phototransistors I think this will be resolved. Let me know if there is any other solution to this.
__________________
CHRIS |
|
|
|
|
|
|
(permalink) |
|
I spent an hour or so and found this:
I made some minor corrections to the code, but nothing that changes the fundamentals of how you were doing things: Code:
list p=16f628 #include <p16f628.inc> __CONFIG _MCLRE_ON & _LVP_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT ERRORLEVEL -302 cblock 0x20 w_temp,status_temp endc org 0x00 goto main org 0X04 inter movwf w_temp ;preserve regs swapf STATUS,w bcf STATUS,RP0 movwf status_temp btfsc PORTB,0 ;RB0 switch pressed? goto loop1 ;yes, go handle it loop2 movlw b'00000101' ;RB5 switch pressed, red & yellow LED on movwf PORTA btfsc PORTB,5 ;keep them lit till switch released goto loop2 goto loop_x ;RB5 switch released, go return loop1 movlw b'10001000' ;green & blue LED on movwf PORTA btfsc PORTB,0 ;keep them lit till switch released goto loop1 loop_x bcf INTCON,INTF ;clear interrupt flags bcf INTCON,RBIF swapf status_temp,w ;restore regs & return movwf STATUS swapf w_temp,f swapf w_temp,w retfie main banksel TRISB ;bank 1 clrf TRISA ;PortA all outs movlw b'00100001' ;RB0 & RB5 inputs movwf TRISB banksel PORTA ;bank 0 movlw 0x07 ;comparators off movwf CMCON movlw b'10011000' ;set interrupts movwf INTCON Loop movlw b'00001001' movwf PORTA ;RA0 & RA3 high goto Loop end And here are some pics of the circuit for you to compare with yours. Those are 330 c36041254_001sm.jpgc36041254_002sm.jpgc36041254_003sm.jpgc36041254_004sm.jpg My power supply is a wall wart plugged into one of these, so the capacitors on the breadboard are really redundant and unnecessary for me. I put them there for your benefit. There are a 100uF and 10uF electrolytic and a 0.1uF ceramic on there.
__________________
========================= Futz's Microcontrollers & Robotics ========================= Last edited by futz; 24th May 2008 at 11:04 PM. |
|
|
|
|
|
|
(permalink) |
|
__________________
========================= Futz's Microcontrollers & Robotics ========================= |
|
|
|
|
|
|
(permalink) |
|
Thank you ! futz turning off the WPUs work !
I have just finished my diploma in electronics and telecommunication (Bachlor's running) so, I was wondering that at this stage, is it normal to have this much knowledge (whatever you can make out of this discussion ), I am very nervous about all this, it was just a very simple programme and still took so long for me ! and there was Mike who explained me what was 104pF (104pF = 0.1uF)
__________________
CHRIS |
|
|
|
|
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|
|
|
||||
| Thread | Thread Starter | Forum | Replies | Latest |
| weird garage door problem | lukasz | General Electronics Chat | 33 | 26th January 2007 08:50 PM |
| Weird 74LS90 counter problem | Torben | Electronic Projects Design/Ideas/Reviews | 11 | 8th December 2006 09:07 AM |
| Weird DVD Problem | ThermalRunaway | General Electronics Chat | 15 | 17th August 2005 06:32 PM |
| weird pic code problem | Fletcher | Micro Controllers | 9 | 13th October 2004 02:39 PM |
| wierd problem with 16f628 | eeanma | Micro Controllers | 2 | 1st November 2003 11:42 AM |