Thanks for the book, I didn't put a comment on it, may be because of "html" setting on y email.. On 30/06/2013 2:36 PM, Tamas Rudnai wrote: > I am just keep saying that probably you would lear this faster if you > follow these excellent sources, because I believe the foundation of > programming PIC is missing. > > This is an excellent book about PIC micros and it is free of charge to re= ad > online (take a look at the Timer chapter and the Example 4, you just need > to copy+paste the code from there and you are done. Having said that I > strongly recommend to start from the beginning): > http://www.mikroe.com/products/view/11/book-pic-microcontrollers/ > > Here is the checklist with links to tutorials: > http://www.piclist.com/techref/piclist/begin.htm > > Previously I have pointed out the code generator here at PIClist servers. > That can generate a normal Delay routine for you, you just type the > parameters in (clock speed and desired delay) and then copy+paste the > generated code into your source. > > BTW: Your code looks like commented out so I do not think if that would > ever compiled. Anyway, if you want to do that with Timer, you have couple > of options: > > 1. You setup Timer to run right at the initialisation and then never touc= h > that again, the Timer runs freely. Then when you need to wait for a certa= in > time, sample the Timer, and in a closed loop you subtract this value form > the current one. When it reaches your desired value, you break out of the > loop. > > 2. You clear the Timer each time you need to wait, and then you do the > comparison just like previously, however, you do not need to subtract the > sample Timer before that > > 3. You set the Timer to a certain value each time you need to wait, so wh= en > the counter overflows you will know your time is up > > 4. You set the Timer to a certain value each time you need to wait and se= t > the interrupt, so when the counter overflows you will receive an interrup= t > > There can be many other options out there, this is only the basic ones. > Mikroelektronika's book contains the no.4 - Copy+past that code and play > with that. Good thing with that is that once you fired up the Timer you d= o > not need to check that, when time os up you get that interrupt anyway, an= d > then you can decide what to do with that. In the meanwhile you can have > other tasks to do. > > Tamas > > > > On 29 June 2013 22:05, Electronic Consultation < > electronic.consultation.au@gmail.com> wrote: > >> I put prescaler to 1:64 here's the init >> >> /; Set up timer interrupt// >> // movlw b'10010110' ;weak portb pullups disabled - >> 1ms interrupt// >> // movwf OPTION_REG ;select tmr0, 64d prescal, clock >> source// >> //// >> // bcf STATUS, RP0 ;Select bank 0// >> // >> // bcf INTCON,RBIE ; port B interrupt on change >> disable// >> // >> // ;movlw b'1010000' ;enable global/timer0 interrupt/= / >> // movlw b'10100000' ;enable global/timer0 interrupt/= / >> // movwf INTCON ;/ >> >> my issue is, how can I create 1ms counter for it on : >> >> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D >> / bcf STATUS, RP0 ;Select Bank 0// >> // bcf STATUS, RP1 // >> // >> // ;....... 1ms timer counter here..... // >> //// >> ////* clrf TMR0 ;Clear TMR0 and prescaler*//* >> *//* banksel TMR0*//* >> *//* movlw 0xB2 ;Count=3DB2 *//* >> *//* movwf TMR0*// >> //// >> ////* timer1ms*//* >> *//* movf LedTimer,f*//* >> *//* btfsc STATUS,Z*//* >> *//* decf LedTimer,Z*//* >> *//* return*// >> //// >> // ;....................................// >> // >> // incf COUNT, f// >> // movlw .10 ;Compare count to 10 decimal// >> // subwf COUNT, W ;Store the result back in // >> // btfss STATUS, C ;Is the result 0, skip if no// >> // goto exittimer// >> // >> // clrf COUNT ;Clear count register// >> //// >> //;*********************************************************************= *// >> // ; 10ms timer function calls// >> //;*********************************************************************= *// >> // ;....... 10ms timer counter here..... // >> // timer10ms// >> // movf 0x0A// >> // btfsc STATUS,Z// >> // goto timer10ms// >> //// >> //// >> // ;....................................// >> // >> // incf COUNT1, f// >> // movlw .10 ;Compare count to 100 decimal// >> // subwf COUNT1, W ;Store the result back in // >> // btfss STATUS, C ;Is the result 0, skip if no// >> // goto exittimer// >> // clrf COUNT1// >> //;*********************************************************************= *// >> // ; 100ms timer function calls// >> //;*********************************************************************= *// >> // ;....... 100ms timer counter here..... // >> // timer100ms// >> // movf 0x64// >> // btfsc STATUS,Z// >> // goto timer100ms// >> // ;....................................// >> //// >> // >> //exittimer // >> // movlw .217 ;count 14// >> // movwf TMR0// >> //// >> //// >> // >> //// >> //;-------------- Insert no new code below here >> ---------------------------------// >> // bcf INTCON, T0IF ;Clear timer overflow flag// >> //; bsf INTCON, T0IE ;Enable interrupts again// >> // return// >> >> //;---------------------------------------------------------------------= -------------// >> >> //;*********************************************************************= *************// >> //initTimer// >> // Global initTimer// >> // GLOBAL COUNT// >> // clrf COUNT// >> // clrf COUNT1// >> //* call timer1ms*// >> // >> //// >> // return// >> // >> // banksel TMR0// >> // movlw 0xB2 ;Timer0=3DB2 // >> // movwf TMR0/ >> >> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D >> >> does this one make sense to you guys ? >> >> / ;....... 1ms timer counter here..... // >> //// >> ////* clrf TMR0 ;Clear TMR0 and prescaler*//* >> *//* banksel TMR0*//* >> *//* movlw 0xB2 ;Count=3DB2 *//* >> *//* movwf TMR0*// >> //// >> ////* timer1ms*//* >> *//* movf LedTimer,f*//* >> *//* btfsc STATUS,Z*//* >> *//* decf LedTimer,Z*//* >> *//* return*// >> //// >> // ;....................................// >> / >> /; 100ms timer function calls// >> //;*********************************************************************= *// >> // ;....... 100ms timer counter here..... // >> // timer100ms// >> // movf 0x64// >> // btfsc STATUS,Z// >> // goto timer100ms// >> return >> // ;....................................// >> . >> . >> . >> . >> . >> / >> /*call timer1ms*// >> *call timer100ms*/ >> >> thank you >> >> >> On 30/06/2013 12:35 PM, Bob Blick wrote: >>> If you use timer0 as an interrupt source and add to it within the >>> interrupt, you can get "round numbers" more easily. >>> In other words, instead of 51.2 us natural interrupt you can get 50 us. >>> Two thousand of those and you have the 100 ms you asked for. >>> >>> Remember the timer stops for two cycles while you modify it, so you'll >>> need to add two more than the raw calculation would provide. So instead >>> of adding 6, add 8. >>> >>> If you use the prescaler I forget how it reacts to modification, but >>> there is a section in the data sheet that addresses that specific case. >>> >>> Cheerful regards, >>> >>> Bob >>> >>> >>> On Sat, Jun 29, 2013, at 09:19 PM, Electronic Consultation wrote: >>>> Timer0 has prescaler, I can set it on OPTION_REG >>>> >>>> Because other timers is already used. >>>> >>>> So you don't have idea ? >>>> anyway thank you for replying... >>>> >>>> >>>> On 30/06/2013 12:11 PM, Byron Jeff wrote: >>>>> On Sun, Jun 30, 2013 at 11:18:38AM +0800, Electronic Consultation >> wrote: >>>>>> Dear Byron, >>>>>> >>>>>> The issue is, I need to use timer0 to achieve >>>>> Why? >>>>> >>>>>> Have you seen my code ? >>>>> No. After your original code couting in a variable, I took the tack t= o >>>>> discussing how to use a hardware timer. >>>>> >>>>> Timer 0 isn't going to cut it because it doesn't have the features of >> Timer >>>>> 2. Specifically it is only 8 bits, there is no prescaler or >> postscaler, nor >>>>> can it autorollover. >>>>> >>>>> Trust me you want to use the right tool to do the job, and TMR0 isn't >> it. >>>>>> How can I use COUNT variable as a delay, put counter on it ? >>>>> Again unless you have TMR2 dedicated to another task (such as PWM or >> as the >>>>> bit rate generator for the UART), it is the best tool to implement a >> timer >>>>> tick on a PIC. >>>>> >>>>> BAJ >>>>> >>>>>> Thanks >>>>>> On 30/06/2013 9:10 AM, Byron Jeff wrote: >>>>>>> On Sun, Jun 30, 2013 at 07:44:05AM +0800, Electronic Consultation >> wrote: >>>>>>>> I answer the question : >>>>>>>> 1. 16F648A >>>>>>>> 2. 20Mhz >>>>>>>> 3. So far nothing I only need to set the interrupt properly on 1ms= , >>>>>>>> I set the timer0 to B2, but not sure on the code for the counter f= or >>>>>>>> getting 1ms, 10ms and 100ms... >>>>>>>> >>>>>>>> Anyone can help ? >>>>>>> OK this helps a bit. The 16F648A has a Timer 2. This timer can >> handle the >>>>>>> job of creating an autorollover 1ms tick. You can then use this to >> create the >>>>>>> others. >>>>>>> >>>>>>> Timer 2 counts Fosc/4 (the PIC instruction clock). The first task i= s >> to >>>>>>> figure out how many counts are required to get 1 ms clock. I do thi= s >> by >>>>>>> figuring about how much time an instruction clock tick takes, then >> divide >>>>>>> that into the time length desired. The instruction time is 4/20Mhz >> which is >>>>>>> 200 nS. 1mS/200nS is 5000. So it takes 5000 ticks into Timer 2 to >> get 1mS >>>>>>> out. >>>>>>> >>>>>>> Now Timer 2 is a 8 bit timer, On its face it would seem that the ma= x >> count >>>>>>> would be 256. But the Timer has both a prescaler, which predivides >> the >>>>>>> number of counts going into the 8 bit timer by 1,4, or 16, and a >> postscaler, which >>>>>>> divides the rollovers of the counter by a value between 1 and 16. >>>>>>> >>>>>>> So we need to set up the timer in 3 parts so that the 3 factors >> combined is >>>>>>> 5000. The prescaler is the easiest to do. Pick the largest divisor >> that >>>>>>> goes evenly into the desired count. We get: >>>>>>> >>>>>>> 5000/16 =3D 312.5 >>>>>>> 5000/4 =3D 1250 >>>>>>> 5000/1 =3D 5000 >>>>>>> >>>>>>> We cannot use the 16 prescale because it does not evenly divide. Th= e >> divide >>>>>>> by 4 works. So we set it. Now all we have to do is count the >> remaining 1250 >>>>>>> with the other two parts. >>>>>>> >>>>>>> Next we set the rollover value of the 8 bit main counter. Again we >> pick the >>>>>>> largest value that is less than 256 that goes evenly into 1250. Thi= s >> value >>>>>>> is 250 because 1250/250 =3D 5. It should be obvious that the >> postscaler is >>>>>>> set to the remaining count of 5. >>>>>>> >>>>>>> The prescaler and postscaler values are set in the T2CON register. >> The >>>>>>> rollover value is placed in the PR2 register. Once these are set an= d >> the >>>>>>> timer is turned on (TMR2ON bit in the T2CON register), the timer >> will count >>>>>>> 5000 instruction clock ticks, raise the TMR2IF in the PIR1 register= , >> and >>>>>>> automatically reset the timer for the next cycle. >>>>>>> >>>>>>> At 20 Mhz Timer 2 will not give clean mS values beyond 1 mS. So to >> get 10 >>>>>>> mS the easiest thing to do is to set up the timer for 1 mS, then >> count the >>>>>>> number of times it rolls over. Once it has rolled over 10 times >>>>>>> (remembering to clear the TMR2IF flag after each count), you know >> that 10 >>>>>>> mS has elapsed. >>>>>>> >>>>>>> I hope this helps. >>>>>>> >>>>>>> BAJ >>>>>>> >>>>>>>> Thanks in advance >>>>>>>> >>>>>>>> On 29/06/2013 10:41 PM, Byron Jeff wrote: >>>>>>>>> On Sat, Jun 29, 2013 at 06:43:54PM +0800, Electronic Consultation >> wrote: >>>>>>>>>> Is my counter will give me the right 10ms ? >>>>>>>>> I think we may want to step back a bit and examine what it is tha= t >> you are >>>>>>>>> really trying to get done. Some questions: >>>>>>>>> >>>>>>>>> 1) What chip are you using? >>>>>>>>> 2) What speed are you running the chip? >>>>>>>>> 3) What other activities need to be done by the chip? Also do any >> of those >>>>>>>>> other activities need to be done while this delay is going on? >>>>>>>>> >>>>>>>>> For virtually any modern PIC, this is best done using one of the >> hardware >>>>>>>>> timers. It's pretty much set and forget. The timer will >> independently track >>>>>>>>> elapsed time and raise a flag when it rolls over. You code will >> only need >>>>>>>>> to track the flag, and reset it when it rolls over. >>>>>>>>> >>>>>>>>> Just as an aside, this is one of the problems with the absolute >> tons of >>>>>>>>> material out on the Internet about software timers. Most of this >> was >>>>>>>>> written years ago when the PIC 16F84 was the hot chip. It only ha= d >> one 8 >>>>>>>>> bit hardware timer, so it was a precious and not very useful >> resource. >>>>>>>>> Modern PICs have between 3 and 5 timers. Timers 2,4,6 in many >> updated chips >>>>>>>>> are designed to autorollover. With this feature not only will the= y >> raise a >>>>>>>>> flag (or generate an interrupt) at a precise time, but will also >>>>>>>>> automatically start the next precise cycle without software >> intervention. >>>>>>>>> These timers are designed for use with PWM and the CCP. However, >> they can >>>>>>>>> be used standalone in the same manner. >>>>>>>>> >>>>>>>>> But without answers for the questions above, it's tough to know i= f >> you have >>>>>>>>> the resources to use the hardware timers, or if the timing is >> correct on >>>>>>>>> the software timers. >>>>>>>>> >>>>>>>>> BAJ >>>>>>>>> >> ;********************************************************************** >>>>>>>>>> ; 10ms timer function calls >>>>>>>>>> >> ;********************************************************************** >>>>>>>>>> ;....... 10ms timer counter here..... >>>>>>>>>> timer10ms >>>>>>>>>> movf 0x0A >>>>>>>>>> btfsc STATUS,Z >>>>>>>>>> goto timer10ms >>>>>>>>>> >>>>>>>>>> >>>>>>>>>> ;.................................... >>>>>>>>>> >>>>>>>>>> incf COUNT1, f >>>>>>>>>> movlw .10 ;Compare count to 100 decimal >>>>>>>>>> subwf COUNT1, W ;Store the result back in >>>>>>>>>> btfss STATUS, C ;Is the result 0, skip if n= o >>>>>>>>>> goto exittimer >>>>>>>>>> clrf COUNT1 >>>>>>>>>> On 29/06/2013 1:45 PM, eCHIP wrote: >>>>>>>>>>> If you need a 100 mS delay with a 4 MHz PICmicro, below is the >> code. >>>>>>>>>>> delay1mS movlw .250 >>>>>>>>>>> movwf temp1 >>>>>>>>>>> clrwdt >>>>>>>>>>> decfsz temp1,f >>>>>>>>>>> goto $-2 >>>>>>>>>>> retlw 00 >>>>>>>>>>> >>>>>>>>>>> >>>>>>>>>>> delay100ms movlw .100 >>>>>>>>>>> movwf temp >>>>>>>>>>> call delay1ms >>>>>>>>>>> decfsz temp,f >>>>>>>>>>> goto $-2 >>>>>>>>>>> retlw 00 >>>>>>>>>>> >>>>>>>>>>> Cheers >>>>>>>>>>> >>>>>>>>>>> Ravi >>>>>>>>>>> >>>>>>>>>>> >>>>>>>>>>>> Hi, when you post to the Piclist you need to put an appropriat= e >> topic >>>>>>>>>>>> tag in the subject line. This time I have added one to your >> message. Bob >>>>>>>>>>>> On Fri, Jun 28, 2013, at 10:04 PM, Electronic Consultation >> wrote: >>>>>>>>>>>>> Guys, >>>>>>>>>>>>> >>>>>>>>>>>>> Am I creating a right counter for PIC ? >>>>>>>>>>>>> >>>>>>>>>>>>> timer100ms >>>>>>>>>>>>> movlw 0x64 >>>>>>>>>>>>> movwf COUNT1 >>>>>>>>>>>>> decfsz COUNT1 >>>>>>>>>>>>> return >>>>>>>>>>>>> >>>>>>>>>>>>> thanks >>>>>>>>>> -- >>>>>>>>>> http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive >>>>>>>>>> View/change your membership options at >>>>>>>>>> http://mailman.mit.edu/mailman/listinfo/piclist >>>>>>>> -- >>>>>>>> http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive >>>>>>>> View/change your membership options at >>>>>>>> http://mailman.mit.edu/mailman/listinfo/piclist >>>>>> -- >>>>>> http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive >>>>>> View/change your membership options at >>>>>> http://mailman.mit.edu/mailman/listinfo/piclist >>>> -- >>>> http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive >>>> View/change your membership options at >>>> http://mailman.mit.edu/mailman/listinfo/piclist >> -- >> http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive >> View/change your membership options at >> http://mailman.mit.edu/mailman/listinfo/piclist >> > > -- http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .