Programming timer0 in PIC 16f628

Status
Not open for further replies.

mshemyalnisar

New Member
I need some help in a project of mine, for the part regarding timer0 and I am using PIC 16f628.

the statement of the question goes:

"Write a peripheral program to generate PRECISE 1 ms delay."

I need this help urgently..... somebody plzzzzzzzzzzzzz.......
 

Read this and you should be able to do it:
 
this link is not pic 18f

can anyone give me the timer0 code for a pic 16f628 plzzzzzzz which would generate 1 ms delay......
 
MPLAB is free. It has a stopwatch in the simulator, so you can easily test delays. PICLIST has lots of examples of delay.

You'll learn a lot by doing it yourself.
 
I just want to get done with this project of the course i never even wanted to do..........

so plzzzzzzzzz is there anyone who can help me here???
 
I'll give you a starter, if you have a 4MHz crystal then your pic will run at 1MHz. Set the prescaler to 4 and preload timer0 with 6 (256-6 = 250, 250*4=1000). Timer0 interrupt flag (T0IF) will become set after 1mS.

HTH.

Mike.
 
You have assumed that the option register starts out as zero. It defaults to 0xff.

Mike.
 
Yes, that would set the prescaler to 4.

Next, write 250 (0xfa) to TMR0, clear bit T0IF, and 1mS later T0IF will be set again.

Mike.
 
ok i think i have done it............

i have tried to write the following code where the next LED would flash every 1 ms. meaning the rotate function is used......

 
If you were using a higher language, it would be pretty simple

Written for a 16F628A with a 4Mhz crystal. It creates a loop that delays for 1mS and then repeats.

Written with **broken link removed**

Code:
Device = 16F628A
Xtal = 4
   		   
Dim uS as Word
Dim mS as Word

Symbol GIE = INTCON.7				' Global Interrupt Enable Bit
Symbol TMR0_uS = 512		  		' Set time interval of TMR0
Symbol TMR0_Enable = INTCON.5			' This option enables TMR0 interrupts
Symbol TMR0_Overflow = INTCON.2			' This is the TMR0 overflow flag
			
ON_INTERRUPT Int_Sub

Goto Initialization			
			
Int_Sub:

	GIE = 0
	If TMR0_Overflow = 1 And TMR0_Enable = 1 Then
	   TMR0_Overflow = 0
	   uS = uS + TMR0_uS
	   If uS >= 1000 Then
	   	  uS = uS - 1000
	  	  mS = mS + 1
	   EndIf
	EndIf
	
	GIE = 1
	
	Context Restore
	
Initialization:
	
	TMR0_Enable = 0	   	   	 	' Disable TMR0 interrupts
	
	uS = 0		  			' Clear timer registers
	mS = 0		  			' 

	OPTION_REG.0 = 0   			'000	-	1 : 2
	OPTION_REG.1 = 0   			'
	OPTION_REG.2 = 0			
	OPTION_REG.5 = 0			' Select Internal Clock Source

	TMR0 = 0	   			' Clear the TMR0 register
	
	TMR0_Enable = 1				' Enable TMR0 Interrupts
	
	GIE = 1		  			' Enable Global Interrupts
		
Main:

	Repeat 					' Create a loop
	
	Until mS = 1 				' Wait for 1 mS

	mS = 0		 	 		' Clear the mS register
	
	Goto Main				' Loop for ever
 
the task that i am trying to implement is use a timer0 to generate a delay of 1ms and after every 1 ms the next led on portB would light up and and previous one would turn off. this task is to be done without using ISR......

the program that i made is:

 
Your code is close to working. See below,

Code:
		org	0x00 
;goto main;

PORTB		equ	0x06		;
PORTA		equ	0x05		;
TRISA		equ	0x85		;
TRISB		equ	0x86		;
STATUS		equ	0X03		;
INPUT		equ	0X07		;

		bsf	STATUS,5
		movlw	00H	
		movwf	TRISB	
		movlw	B'1111'	
		movwf	TRISA	
		bcf	STATUS,5
		movlw	00H	
		movwf	PORTB	
		movlw	b'11000001'
		movwf	option_reg
	;;	bsf	intcon,7  <<<------remove this

func
		bcf	intcon,2
		bcf	STATUS,C	;required so only 1 bit set
		rrf	PORTB,1	
		movfw	PORTB		;this is required
		btfsc	STATUS,Z	;otherwise port B
		bsf	PORTB,7		;will end up zero
		movlw	0xfa	
		movwf	TMR0	
		goto	check	
return;


main
		movlw	01H	;\
		movwf	PORTB	; | This is never
		movlw	0xfa	; | executed
		movwf	TMR0	;/
check		btfs[COLOR="Red"]c[/COLOR]	intcon,2   ;<-- changed
		goto	func
		goto	check
end;

You had your test the wrong way around.

You also need config data for it to work in a chip.

Mike.
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…