![]() | ![]() | ![]() |
| | |||||||
| Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc. |
| | LinkBack | Thread Tools | Display Modes |
| | (permalink) |
| Can you post up your program? futz's program should work. Or you can first try the fixed duty cycle.
__________________ Superman returns.. | |
| |
| | (permalink) | |
| Quote:
Code: ;=======pwm.ASM=================================15/11/06== ; standard crystal 4.0MHz XT ;------------------------------------------------------------ ; configure programmer LIST P=16F628;f=inhx8m #include "P16F628.INC" ; Include header file __CONFIG _PWRTE_ON & _WDT_OFF & _HS_OSC & _BODEN_ON & _LVP_OFF & _CP_OFF & _MCLRE_OFF ; http://sandiding.tripod.com/Bertys.html ;------------------------------------------------------------ cblock 0x20 ; Beginn General Purpose-Register ;-------------------------- counters count1 count2 count3 ;-------------------------- endc ;-------------------------- #DEFINE pwmu PORTB,3 ;--- Reset -------------------------------------------------- org h'00' goto init ; reset -> init ;--- Interrupt ---------------------------------------------- org h'04' ;-------------------------- init clrf PORTA clrf PORTB movlw 0x07 ; Turn comparators off and enable pins for I/O movwf CMCON bcf STATUS,RP1 call usi ; setari porturi call pause movlw 0xFF movwf PORTB call pause call set_timer ;-------------------------- asteapta puls de 1ms uu call pause movlw 0x02 movwf CCPR1L call pause movlw 0x10 movwf CCPR1L call pause movlw 0x40 movwf CCPR1L call pause movlw 0x80 movwf CCPR1L call pause movlw 0xFF movwf CCPR1L call pause movlw 0x80 movwf CCPR1L call pause movlw 0x40 movwf CCPR1L call pause movlw 0x10 movwf CCPR1L goto uu ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ set_timer clrf T2CON clrf TMR2 clrf INTCON bsf STATUS,RP0 clrf PIE1 bcf STATUS,RP0 clrf PIR1 bsf STATUS,RP0 movlw 0xFF movwf PR2 ; compare with 255 bcf STATUS,RP0 movlw 0x02 movwf CCPR1L movlw 0x03 movwf T2CON ; prescaler 1:16 and postscaler 1:1 movlw 0x3C movwf CCP1CON bsf T2CON,TMR2ON return ;************************************************************************ ; Subrutine de intarziere * ;************************************************************************ pause movlw 0x02 ; movwf count3 d3 movlw 0x3F movwf count1 d1 movlw 0xFA movwf count2 d2 decfsz count2,F goto d2 decfsz count1,F goto d1 decfsz count3,F goto d3 retlw 0x00 ;============================================================ usi bsf STATUS,RP0 ; Bank 1 movlw 0xFF ; all input movwf TRISA movlw 0x00 movwf TRISB ; all output bcf STATUS,RP0 ; Bank 0 return ;============================================================ end
__________________ This message transmitted on 100% recycled electrons | ||
| |
| | (permalink) |
| I'm not sure how much of a difference this makes but your header says 16f628 and you said that you are using a 16f628a. I'll post an alternative code in a little while.
__________________ jeremy | |
| |
| | (permalink) | |
| Quote:
If you're using a PIC16F628A you have to make little changes: the list directive and the include file. I've played with futz's code and it's ok. I've chosen the internal oscillator. The original program requires a high speed crystal. | ||
| |
| | (permalink) | |
| Quote:
__________________ If you don't have a planet, what good are gold bars? want to contact me directly? gmail gordonthree check out my project website: http://projects.dimension-x.net Favorite numbers: 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0 | ||
| |
| | (permalink) |
| Here's a code that you can put a led with a resistor on any of the PORTB bits. I put it on bit3 like you are already using. In the code there is a place that says movlw 0x44 movwf runn If you change this value the time it takes to fade changes. As is it fades out a few times a second. Something I noticed when writing this code is that leds do not light up in a linear fashion. You have to have an exponential increase in the PWM to get an even fade. This code if really long for what it does, but I hope it's easy to follow and does what you want. I also put a circuit that shows how to put the led. The short leg/the flat side is the - side. Code: ;andy fade some lights list P=16f628a #include <p16f628a.inc> __config _INTRC_OSC_NOCLKOUT & _BODEN_OFF & _WDT_OFF & _PWRTE_ON & _MCLRE_OFF & _CP_OFF & _CP_OFF ERRORLEVEL -302 cblock 20h BH,BL,wait,del_clk1,del_clk2,run,runn endc bsf STATUS,RP0 ;bank 1 movlw 0x00 movwf TRISB ;make all outputs bcf STATUS,RP0 movlw 0x07 ;turn off comparitors movwf CMCON movlw 0x44 ;adjustment for speed of fade movwf runn start call firstt call secondd call thirdd call fourthh call fifthh call sixthh call seventhh call eighthh call ninthh call tenthh call ninthh call eighthh call seventhh call sixthh call fifthh call fourthh call thirdd call secondd goto start firstt movf runn,0 movwf run first movlw 0xff movwf PORTB ;turn on leds movlw 0x01 ;set on time of duty cycle movwf BH loop decfsz BH,1 ;count down the on time goto loop movlw 0x00 ;turn leds off movwf PORTB movlw 0x01 ;set off time of duty cycle movwf BH loop2 incfsz BH,1 ;count up to the off time goto loop2 decfsz run,1 ;run the PWM for a moment goto first return secondd movf runn,0 movwf run second movlw 0xff movwf PORTB ;turn on leds movlw 0x03 ;set on time of duty cycle movwf BH loopa decfsz BH,1 ;count down the on time goto loopa movlw 0x00 ;turn leds off movwf PORTB movlw 0x03 ;set off time of duty cycle movwf BH loop2a incfsz BH,1 ;count up to the off time goto loop2a decfsz run,1 ;run the PWM for a moment goto second return thirdd movf runn,0 movwf run third movlw 0xff movwf PORTB ;turn on leds movlw 0x05 ;set on time of duty cycle movwf BH loopb decfsz BH,1 ;count down the on time goto loopb movlw 0x00 ;turn leds off movwf PORTB movlw 0x05 ;set off time of duty cycle movwf BH loop2b incfsz BH,1 ;count up to the off time goto loop2b decfsz run,1 ;run the PWM for a moment goto third return fourthh movf runn,0 movwf run fourth movlw 0xff movwf PORTB ;turn on leds movlw 0x07 ;set on time of duty cycle movwf BH loopc decfsz BH,1 ;count down the on time goto loopc movlw 0x00 ;turn leds off movwf PORTB movlw 0x07 ;set off time of duty cycle movwf BH loop2c incfsz BH,1 ;count up to the off time goto loop2c decfsz run,1 ;run the PWM for a moment goto fourth return fifthh movf runn,0 movwf run fifth movlw 0xff movwf PORTB ;turn on leds movlw 0x09 ;set on time of duty cycle movwf BH loopd decfsz BH,1 ;count down the on time goto loopd movlw 0x00 ;turn leds off movwf PORTB movlw 0x09 ;set off time of duty cycle movwf BH loop2d incfsz BH,1 ;count up to the off time goto loop2d decfsz run,1 ;run the PWM for a moment goto fifth return sixthh movf runn,0 movwf run sixth movlw 0xff movwf PORTB ;turn on leds movlw 0x11 ;set on time of duty cycle movwf BH loope decfsz BH,1 ;count down the on time goto loope movlw 0x00 ;turn leds off movwf PORTB movlw 0x11 ;set off time of duty cycle movwf BH loop2e incfsz BH,1 ;count up to the off time goto loop2e decfsz run,1 ;run the PWM for a moment goto sixth return seventhh movf runn,0 movwf run seventh movlw 0xff movwf PORTB movlw 0x17 ;set on time of duty cycle movwf BH loopf decfsz BH,1 ;count down the on time goto loopf movlw 0x00 ;turn leds off movwf PORTB movlw 0x17 ;set off time of duty cycle movwf BH loop2f incfsz BH,1 ;count up to the off time goto loop2f decfsz run,1 ;run the PWM for a moment goto seventh return eighthh movf runn,0 movwf run eighth movlw 0xff movwf PORTB movlw 0x25 ;set on time of duty cycle movwf BH loopg decfsz BH,1 ;count down the on time goto loopg movlw 0x00 ;turn leds off movwf PORTB movlw 0x25 ;set off time of duty cycle movwf BH loop2g incfsz BH,1 ;count up to the off time goto loop2g decfsz run,1 ;run the PWM for a moment goto eighth return ninthh movf runn,0 movwf run ninth movlw 0xff movwf PORTB movlw 0x35 ;set on time of duty cycle movwf BH looph decfsz BH,1 ;count down the on time goto looph movlw 0x00 ;turn leds off movwf PORTB movlw 0x35 ;set off time of duty cycle movwf BH loop2h incfsz BH,1 ;count up to the off time goto loop2h decfsz run,1 ;run the PWM for a moment goto ninth return tenthh movf runn,0 movwf run tenth movlw 0xff movwf PORTB movlw 0x50 ;set on time of duty cycle movwf BH loopi decfsz BH,1 ;count down the on time goto loopi movlw 0x00 ;turn leds off movwf PORTB movlw 0x50 ;set off time of duty cycle movwf BH loop2i incfsz BH,1 ;count up to the off time goto loop2i decfsz run,1 ;run the PWM for a moment goto tenth return end
__________________ jeremy | |
| |
| | (permalink) |
| Hi, Why isn't it working? Is the LED turned on? Have you seen anything on RB3 using oscilloscope?
__________________ Superman returns.. | |
| |
| | (permalink) | |
| Quote:
What needs to be changed in order to use a 16F628 code on a 16F628A? I thought the two were interchagable to a point? bananasiong, I never bothered putting the scope on RB3. I guess i could try it though.
__________________ This message transmitted on 100% recycled electrons | ||
| |
| | (permalink) |
| Hi andy whats your plan for smaller pics i'm using crude delay loops.working well.But the code gets bigger to do a smooth fade BTW if you have problem with PIC's to do a PWM until you learn why not try with a simple opamp. http://hw.metku.net/colorfade/index_eng.html | |
| |
| | (permalink) | |
| Quote:
The line posted earlier be Jeremy has a slight mistake, Code: __config _INTRC_OSC_NOCLKOUT & _BODEN_OFF & _WDT_OFF & _PWRTE_ON & _MCLRE_OFF & _DATA_CP_OFF & _CP_OFF Also, the config above has both the internal oscillator and MCLR off. This is not recommended and you should probably turn MCLR on to prevent programming problems. Mike. Edit, I just noticed you posted the code above and your config is fine the way it is. | ||
| |
| | (permalink) |
| It is good to know assembler and the details of the hardware but maybe it is better to start with a high leve language like C. For a person who does not know how to program the task of learning hardware + assembler + logic of the task at hand, may be too much. If you find that the C code I posted earlier is understandable, I could provide a working example for the 18F1320 (Microchip C18 compiler). | |
| |
| | (permalink) |
| Andy the code to fade the lights doesn't fade the PWM all the way down to zero but that would be easy to fix. Change the "duty cycle values" The closer to zero the darker it gets. Remember when you change them that the leds light up in an exponential fashion. The header of the code I used was taken from another code. When I got errors from MPLAB I changed them until they stopped showing errors. (pretty scientific huh) I'm still learning about how to write code properly. Actually I wanted the internal oscillator turned on but I don't understand the data sheet so well. I tried to fix the header. Hopefully someone who knows will double check it. Really I don't know the secret to get the code from one pic to the other is. What I did was steal a header from a code that I know works and then I copied it into my code. In the rest of the code and circuit I use the datasheet to make sure the ports and bits are all assigned properly and wired correctly. Here is a new code with two switches. They change the speed of the fade when you turn them on (close the switch). Also I put a revised circuit. I hope this helps. Code: ;andy fade some lights list P=16f628a #include <p16f628a.inc> __config _INTOSC_OSC_NOCLKOUT & _BODEN_OFF & _WDT_OFF & _PWRTE_ON & _MCLRE_ON & _DATA_CP_OFF & _CP_OFF ERRORLEVEL -302 cblock 20h BH,BL,wait,del_clk1,del_clk2,run,runn endc bsf STATUS,RP0 ;bank 1 movlw 0x00 movwf TRISB ;make all PORTB outputs movlw 0xff movwf TRISA ;make all PORTA inputs bcf STATUS,RP0 movlw 0x07 ;turn off comparitors movwf CMCON start movlw 0x44 ;adjustment for speed of fade movwf runn btfss PORTA,0 ;call to see if low, if low make fade slower call slower btfss PORTA,1 ;call to see if low, if low make fade faster call faster call firstt call secondd call thirdd call fourthh call fifthh call sixthh call seventhh call eighthh call ninthh call tenthh call ninthh call eighthh call seventhh call sixthh call fifthh call fourthh call thirdd call secondd goto start firstt movf runn,0 movwf run first movlw 0xff movwf PORTB ;turn on leds movlw 0x01 ;set on time of duty cycle movwf BH loop decfsz BH,1 ;count down the on time goto loop movlw 0x00 ;turn leds off movwf PORTB movlw 0x01 ;set off time of duty cycle movwf BH loop2 incfsz BH,1 ;count up to the off time goto loop2 decfsz run,1 ;run the PWM for a moment goto first return secondd movf runn,0 movwf run second movlw 0xff movwf PORTB ;turn on leds movlw 0x03 ;set on time of duty cycle movwf BH loopa decfsz BH,1 ;count down the on time goto loopa movlw 0x00 ;turn leds off movwf PORTB movlw 0x03 ;set off time of duty cycle movwf BH loop2a incfsz BH,1 ;count up to the off time goto loop2a decfsz run,1 ;run the PWM for a moment goto second return thirdd movf runn,0 movwf run third movlw 0xff movwf PORTB ;turn on leds movlw 0x05 ;set on time of duty cycle movwf BH loopb decfsz BH,1 ;count down the on time goto loopb movlw 0x00 ;turn leds off movwf PORTB movlw 0x05 ;set off time of duty cycle movwf BH loop2b incfsz BH,1 ;count up to the off time goto loop2b decfsz run,1 ;run the PWM for a moment goto third return fourthh movf runn,0 movwf run fourth movlw 0xff movwf PORTB ;turn on leds movlw 0x07 ;set on time of duty cycle movwf BH loopc decfsz BH,1 ;count down the on time goto loopc movlw 0x00 ;turn leds off movwf PORTB movlw 0x07 ;set off time of duty cycle movwf BH loop2c incfsz BH,1 ;count up to the off time goto loop2c decfsz run,1 ;run the PWM for a moment goto fourth return fifthh movf runn,0 movwf run fifth movlw 0xff movwf PORTB ;turn on leds movlw 0x09 ;set on time of duty cycle movwf BH loopd decfsz BH,1 ;count down the on time goto loopd movlw 0x00 ;turn leds off movwf PORTB movlw 0x09 ;set off time of duty cycle movwf BH loop2d incfsz BH,1 ;count up to the off time goto loop2d decfsz run,1 ;run the PWM for a moment goto fifth return sixthh movf runn,0 movwf run sixth movlw 0xff movwf PORTB ;turn on leds movlw 0x11 ;set on time of duty cycle movwf BH loope decfsz BH,1 ;count down the on time goto loope movlw 0x00 ;turn leds off movwf PORTB movlw 0x11 ;set off time of duty cycle movwf BH loop2e incfsz BH,1 ;count up to the off time goto loop2e decfsz run,1 ;run the PWM for a moment goto sixth return seventhh movf runn,0 movwf run seventh movlw 0xff movwf PORTB movlw 0x17 ;set on time of duty cycle movwf BH loopf decfsz BH,1 ;count down the on time goto loopf movlw 0x00 ;turn leds off movwf PORTB movlw 0x17 ;set off time of duty cycle movwf BH loop2f incfsz BH,1 ;count up to the off time goto loop2f decfsz run,1 ;run the PWM for a moment goto seventh return eighthh movf runn,0 movwf run eighth movlw 0xff movwf PORTB movlw 0x25 ;set on time of duty cycle movwf BH loopg decfsz BH,1 ;count down the on time goto loopg movlw 0x00 ;turn leds off movwf PORTB movlw 0x25 ;set off time of duty cycle movwf BH loop2g incfsz BH,1 ;count up to the off time goto loop2g decfsz run,1 ;run the PWM for a moment goto eighth return ninthh movf runn,0 movwf run ninth movlw 0xff movwf PORTB movlw 0x35 ;set on time of duty cycle movwf BH looph decfsz BH,1 ;count down the on time goto looph movlw 0x00 ;turn leds off movwf PORTB movlw 0x35 ;set off time of duty cycle movwf BH loop2h incfsz BH,1 ;count up to the off time goto loop2h decfsz run,1 ;run the PWM for a moment goto ninth return tenthh movf runn,0 movwf run tenth movlw 0xff movwf PORTB movlw 0x50 ;set on time of duty cycle movwf BH loopi decfsz BH,1 ;count down the on time goto loopi movlw 0x00 ;turn leds off movwf PORTB movlw 0x50 ;set off time of duty cycle movwf BH loop2i incfsz BH,1 ;count up to the off time goto loop2i decfsz run,1 ;run the PWM for a moment goto tenth return slower movlw 0x88 ;puts higher value in speed adjustment movwf runn return faster movlw 0x11 ;puts lower value in speed adjustment movwf runn return end
__________________ jeremy | |
| |
| | (permalink) | |
| Quote:
Once you've got a handle on that, only then should you start using higher level languages (if you even want to at that point). I believe higher level languages tend to insulate you from the details somewhat, making understanding more difficult, especially for newbs. Not only that, but they make porky, fat code that eats up memory and runs slow. | ||
| |
| | (permalink) | |
| Quote:
In a more perfect world preople would learn a high level language first on a PC. Ater that we could have them learn assembly and processor details. But programing on the PC has lost the luster that programming uC's still have. You have to start where the interest is and currently that is uC's. Start them with a high level language. Let them get good at blinking LEDs and such. You hold their hand, hide the details, and they get hooked. Over time you introduce more complex hardware and how it work. At this point you can teach assembler. You teach assembler not because it is the worlds greatest programming language. You teach it because it is the best way to expose them to the nuts and bolts that you have been hidding. The fact that compiled code takes more memory is not a big concern here. The inexpensive 18F family has memory to spare for learning. Last edited by 3v0; 29th November 2007 at 06:53 PM. | ||
| |
| | (permalink) | |
| Quote:
Do learning assembler first, you then have the hardware understanding required if you move to a high level language. Where this advantage is starting to blur is the 18F series, simply because there's a free compiler available, and the application notes are mainly provided written in C. So in that case, I would suggest learning a little assembler on a 16F (so you understand PIC hardware), and probably go directly to C on the 18F. Incidently, in Universities in the past you weren't allowed to use high level languages until you had mastered machine code, never mind assembler! | ||
| |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
| |
| | ||||
| Title | Starter | Forum | Replies | Latest |
| Quik PIC Programming kit | Krumlink | General Electronics Chat | 5 | 28th January 2008 12:27 AM |
| Problems switchin relay with PIC | Andy1845c | General Electronics Chat | 5 | 17th November 2007 07:14 PM |
| High ADC sampling rate PIC, 18F needed? | bananasiong | Micro Controllers | 24 | 28th October 2007 01:13 PM |
| Four PIC with One LCD.. | meera83 | Micro Controllers | 13 | 20th September 2007 07:40 AM |
| Circuit design for keyfob receiver's TTL outputs for current | Guerrero | General Electronics Chat | 6 | 18th April 2005 03:12 AM |