Pic16f818

Status
Not open for further replies.

uaefame

New Member
Good afternoon,

I am trying to turn an LED on and off every 2 second. I am using PIC 16F818. I am interested in using internal timming and I am setting it to 4MHz.

Here are my codes
Code:
  list      p=16F818             ; list directive to define processor
    #include <p16F818.inc>         ; processor specific variable definitions

__config H'3F10'

	movlw          b'01100000' 
	movwf          OSCCON 

	cblock 0x0c	
	d1,d2,d3
	endc

		bsf		STATUS,5
		movlw	00h
		movwf	TRISA
		bcf		STATUS,5

   
  		
START	movlw	02h
		movwf	PORTA
		call 	Delay2sec
		movlw	00h
		movwf	PORTA
		call 	Delay2sec
		goto	START


Delay2sec
			;1999996 cycles
	movlw	0x11
	movwf	d1
	movlw	0x5D
	movwf	d2
	movlw	0x05
	movwf	d3
Delay2sec_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	$+2
	decfsz	d3, f
	goto	Delay2sec_0

			;4 cycles (including call)
	return
	END

I would like to know why my LED doesn't function as I expect it is the problem in the coding because I was able to turn led on and off using PIC16F84a.

My header file is as follow
Code:
Configuration Bits
			__CONFIG	H'3F10'
					;sets INTRC-A6 is port i/o
					;WDT off
					;PUT on
					;MCLR tied to VDD
					;A5 is I/O
					;BOD off
					;LVP disabled
					;Flash Program Write disabled
					;Background Debugger disabled
					;CCP function on B2
					;Code Protection disabled

Help needed!

Q> Is there a problem with my coding?
Q> How can I set an internal timmer? I am following the datasheet page 38 of PIC16F818?

Thanks in advance
 
hi,
You have the PIC reserved register area at 0x0c
No origin
and the code was not in order.

This modified code now works in my simulator.

Code:
 list      p=16F818             ; list directive to define processor
    #include <p16F818.inc>         ; processor specific variable definitions
 errorlevel -302, -207

  __config H'3F10'

	cblock 0x20;[COLOR="Red"]0c[/COLOR]	
	d1,d2,d3
	endc

	org 0x0000

	movlw          b'01100000' 
	movwf          OSCCON 

	

		bsf		STATUS,5
		movlw	00h
		movwf	TRISA
		bcf		STATUS,5

   
  		
START	movlw	02h
		movwf	PORTA
		call 	Delay2sec
		movlw	00h
		movwf	PORTA
		call 	Delay2sec
		goto	START


Delay2sec
			;1999996 cycles
	movlw	0x11
	movwf	d1
	movlw	0x5D
	movwf	d2
	movlw	0x05
	movwf	d3
Delay2sec_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	$+2
	decfsz	d3, f
	goto	Delay2sec_0

			;4 cycles (including call)
	return
	END
 
Last edited:
Thanks eric

Can you explain these lines

Code:
cblock 0x20;0c

& this one

Code:
org 0x0000

& this one

Code:
errorlevel -302, -207

Sorry of the troubles

Thanks in advance
 

Its no trouble.

Look at the image for the registers.

Always get a datasheet for the PIC you are working on.
 

Attachments

  • esp01 Oct. 21.gif
    11.5 KB · Views: 337
Last edited:
Hmm thanks for the explaination I like it!!!!!!!

BTW, its still not working I will post my circuit i am using
 

Attachments

  • Pic16f818.JPG
    39.1 KB · Views: 416
Last edited:
Hmm thanks for the explaination I like it!!!!!!!

BTW, its still not working I will post my circuit i am using

hi,
Thats because you are switching pin 18 in your program, not 17.!

Code:
               movlw	[COLOR="Red"]02h[/COLOR];;; 01h
		movwf	PORTA
		call 	Delay2sec
		movlw	00h
		movwf	PORTA
 
Last edited:
Still not working!!!

Here is the update of my schematic thanks eric for spotting that out.

Here is what happen When i turn the power supply on the LED lights on and keep lighting on.


BTW, if you can help me thanks again
 

Attachments

  • 16F818_revA,.JPG
    43.2 KB · Views: 289
Still not working!!!

Here is the update of my schematic thanks eric for spotting that out.

Here is what happen When i turn the power supply on the LED lights on and keep lighting on.


BTW, if you can help me thanks again

Check the config.

Rechecking.

EDIT:
bit 5 is low so MCLR is internal, you have a resistor attached.
this shouldnt cause a problem.
 
Last edited:
I print screen my config so you can have a look into it

Thanks for replying
 

Attachments

  • config.JPG
    21.1 KB · Views: 256
I print screen my config so you can have a look into it

Thanks for replying

I have just programmed a 16F818, If I recall correctly dosnt the internal clock frequency default to 32KHz.!!!
Its working the LED but VERY slowly,,,

You have to change it to 4MHZ within the program.
 
hi,
I see what you have done wrong.

The OSCCON setting is on BANK0 it should be BANK1

Move the OSCCON and it works OK.

The LED is flashing at 2secs in a PIC.

Code:
		bsf		STATUS,5
		movlw	00h
		movwf	TRISA
                movlw          b'01100000' 
	        movwf          OSCCON 

		bcf		STATUS,5
 
Last edited:
Amazing eric, I Really should consider you on my robotic team!!

My robotic team consist of All mechanical students. I am the only guy who know few on electrical stuff. But eric if you join us we will rule hehe .

Thanks again
 
Last edited:
General question?

Does all PIC contain PWM? If not does PIC 16F84a or PIC 16F818 contains it?

I am trying to control a servo motor using PWM i need to send a signal of 1ms to reach 0 degree 2ms to reach 180degree and so on.
 
General question?

Does all PIC contain PWM? If not does PIC 16F84a or PIC 16F818 contains it?

I am trying to control a servo motor using PWM i need to send a signal of 1ms to reach 0 degree 2ms to reach 180degree and so on.

Do it in software, it's not asy using the PWM module, although by using interrupts and reseting it continually it can be done.

The 84 doesn't have PWM, no idea on the 818, check the datasheet.
 
General question?

Does all PIC contain PWM? If not does PIC 16F84a or PIC 16F818 contains it?

I am trying to control a servo motor using PWM i need to send a signal of 1ms to reach 0 degree 2ms to reach 180degree and so on.

Yes the 16F818 does have PWM.

Post the program you have written for the PWM, I'll work thru it with you.OK.
 
Does PWM concept work like this?

Code:
    list      p=16F818             ; list directive to define processor
    #include <p16F818.inc>         ; processor specific variable definitions
	errorlevel -302, -207

__config H'3F10'

	cblock 0x20	
	d1,d2,d3
	endc

	org 0x0000


		bsf		STATUS,5
		movlw	00h
		movwf	TRISA	
		movlw   b'01100000' 
     	movwf   OSCCON
		bcf		STATUS,5

   
  		
START	movlw	02h
		movwf	PORTA
		call 	Delay1ms
		movlw	00h
		movwf	PORTA
		call 	Delay1ms
		goto	START

Delay1ms
			;993 cycles
	movlw	0xC6
	movwf	d1
	movlw	0x01
	movwf	d2
Delay1ms_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	Delay1ms_0

			;3 cycles
	goto	$+1
	nop

			;4 cycles (including call)
	return

	END

Or there is a different way of doing it?

If there is please tell me ? Thanks again for offering help !!

Thanks in advance
 
Look at Nigels tutorials for PWM examples.

I thought - WHERE?

Then I remembered, it's NOT for servo control, but just a simple example of how to use the two channel hardware PWM for speed and direction control of a small robot. I added it specifically because I was getting multiple questions a week about how to do this using the PWM modules.
 

hi,
IIRC you did post the 'above' comment some weeks ago, thats why I knew they PWM did exist on your tutorials.!

It just shows you, members do read and remember your posts.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…