Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

How to start PIC16F676 ...??

Status
Not open for further replies.
Can't we do this in assembly??
You can if you want.... I gave you this to show how I would do it.
and please explain this..

8 bit PWM.. The timer0 has an 8 bit register.. if you make a pulse width of

64 counts high with 192 counts low... 64 + 192 = 256........... 25%
128 counts high with 128 counts low... 128 +128 = 256......... 50%
192 counts high with 64 counts low... 192 + 64 = 256........... 75%

The amount of bits used defines the accuracy.

You could have a 7 bit running at 4khz.. but you will only have 128 bits precision.
 
Last edited:
It's not difficult!!! It's very nearly the same.

Code:
	list p = 16f676
	#include<p16f676.inc>
	__config(0x314)

Tog	equ	20		; didn't use
Pulse	equ	21		; ADC result
OnPulse	equ	22		; High time
OfPulse	equ	23		; Low time
d1	equ	24		; delay var
d2	equ	25		; delay var
d3	equ	26		; delay var

	org	0
	goto	init		; bypass int vector
	org	4
	goto	ISR		; Interrupt vector

init
	banksel TRISC
	clrf	TRISC		; Outputs
	movlw	0xFF
	movwf	TRISA		; Inputs
	movlw	0x01
	movwf	ANSEL		; RA0 for pot
	movlw	0x50
	movwf	ADCON1		; Fosc/16
	clrf	OPTION_REG	; TMR0 pre-scaler 1:2
	banksel	PORTC
	movlw	0x07
	movwf	CMCON		; No comparators
	clrf	ADCON0		; ADC left justify, ADC off
	clrf	INTCON		; no int's
	bsf	INTCON,T0IE	; set TMR0 interrupt only
	bsf	INTCON,GIE	; set global int's
	
loop
	call	readadc		; get pulse width from RA0
	movf	Pulse,w		
	sublw	.255		; 
	movwf	OfPulse		; the off pulse could easily
	movf	Pulse,w		; have been the comp of the pulse
	movwf	OnPulse
	goto	loop	

ISR
	btfsc	PORTC,0		; First we toggle RC0
	goto	t1
	bsf	PORTC,0		; On
	movf	OnPulse,w	; (set tmr0 high time)
	goto 	t2	
t1	bcf	PORTC,0		; or off
	movf	OfPulse,w	;  (set tmr0 low time)
t2	movwf	TMR0
	bcf	INTCON,T0IF	; clear the interrupt
	retfie			; all done

readadc
	bsf	ADCON0,ADON	; switch on
	call	delay40		; have to wait for module
	bsf	ADCON0,GO	; start conversion
	call	delay40		; TAD	
	btfsc	ADCON0,GO	; wait for ADC
	goto	$-1		;
	movf	ADRESH,w	; 8 bit result
	movwf	Pulse		; in pulse
	return

delay40				; about 38mS
	clrf	d1		; 256 iterations
	movlw	30
	movwf	d2		; 30 iterations
	decfsz	d1
	goto	$ -1
	decfsz	d2
	goto	$ -3
	return	

	end

If I keep doing things for you... you'll never learn.....
 
Hi,

What is the function of this instruction??

hi Ritesh,
ALL the registers and instructions are fully explained in this link, read all the Chapters.

**broken link removed**

Chapter 2 , covers the SFR's
 
Last edited:
The code are were fine and when i couple this uC to my TX and Rx of 433MHz with HT12E/D to one of bit of data the motor work slightly fine at full speed but when i slow down the speed it doesn't run continuously (with few mS break and run)!!
at what freq this PWM is generated???
 
Around 2khz... Like I said before, You can lift the frequency by using 7 bit to around 4khz and if you use an external xtal you'll get higher values...

What frequency does the motor need?
 
As i said the motor does not work well with RX and Tx coupled with Holtek encoder and decoder at low speed, the motor is not running continuously at low speed?
and why the PWM is at 2KHZ as i have listen it should be around 20Khz
 
Is software pwm going to work with serial running at the same time. When you receive data
your not going to update the pwm pin why that happen's. It will be on hold till the data is received

You really need hardware PWM to make this work

Or some really tricky code that can time slice without loss packets
 
Last edited:
Is software pwm going to work with serial running at the same time. When you receive data
your not going to update the pwm pin why that happen's. It will be on hold till the data is received

You really need hardware PWM to make this work

Or some really tricky code that can time slice without loss packets

Dear, I am using external rx and tx of my last wireless bot project, the output of PWM is given to data pin of HT 12E.
 
As i said the motor does not work well with RX and Tx coupled with Holtek encoder and decoder at low speed, the motor is not running continuously at low speed?
and why the PWM is at 2KHZ as i have listen it should be around 20Khz

You wont get 20khz from a software PWM You'll need a hardware solution as Burt said.... If you are sending the PWM over the air it would be difficult to contol as you would need to send a signal VERY frequently There would be less time to produce the PWM signal....

Are you sending a voltage signal to the pic...THEN controlling the motor or is the PWM signal coming out of the decoder?

If its the decode, you could have a pic16f676 at either end.. one to send the pulse and one to control the motor.
 
Are you sending a voltage signal to the pic...THEN controlling the motor or is the PWM signal coming out of the decoder?

I am controlling the ADC with pot. then the output signal is feed to encoder of TX....then this signal is decoded with HT 12D and then given as it is to H-BRIDGES!

You wont get 20khz from a software PWM You'll need a hardware solution

so, at what maximum freq can i get PWM from software?
 
Assume the timer can do 1:1... 1mhz / 256 = roughly 4khz (7bit will be 8khz). But I set the pre-scaler to 1:2 so we're at half that.. 2khz but to create the software version the interrupt routine takes a few cycles .. so its smaller that that.

With an external 20mhz.. using the wdt pre-scale of 1:1 5mhz / 256 = nearly 20khz if you use a 7 bit PWM you may get 39khz.

But there will be some errors transmitting the signal continuously...
 
There is no way that what RITESH KAKKAR wants to do will work the chip your using could do software PWM but your also doing software Uart. You'll be using the timer to do the PWM
which would take most of the clock cycles at even 20 Mhz if your wanting 20Khz output.

You then would have to stop PWM to receive data to update the PWM.

You really need a chip with hardware PWM and UART . I was thinking this chip had hardware UART

Your PWM would have to be updated and let you receive data so it would have to be slower then the Uart so it could happen in sync
 
I am controlling the ADC with pot. then the output signal is feed to encoder of TX....then this signal is decoded with HT 12D and then given as it is to H-BRIDGES!

I suggest you read the datasheet for the HT12D, it'sd a crude remote control chip, NOT a data chip and comnpletelymuseless for what you're trying to do.

However, your entire scheme is completely flawed - you're not going to send 20KHz PWM over a licence free data link, which would be completely the wrong way to do it anyway. You send a couple of data bytes telling the receiver what to do - a processor at the receiver end then genetrates the PWM signals in accordance with those instructions.

You either do the coding/encoding in software (as per my tutorials), or use a 'proper' encode/decoder chip (not crude remore chips).

PWM for feeding two motors via H-bridges is covered in another of my tutorials, but you NEED hardware PWM, and you don't need (or want) anything like 20KHz.
 
I suggest you read the datasheet for the HT12D, it'sd a crude remote control chip, NOT a data chip and comnpletelymuseless for what you're trying to do.

However, your entire scheme is completely flawed - you're not going to send 20KHz PWM over a licence free data link, which would be completely the wrong way to do it anyway. You send a couple of data bytes telling the receiver what to do - a processor at the receiver end then genetrates the PWM signals in accordance with those instructions.

You either do the coding/encoding in software (as per my tutorials), or use a 'proper' encode/decoder chip (not crude remore chips).

PWM for feeding two motors via H-bridges is covered in another of my tutorials, but you NEED hardware PWM, and you don't need (or want) anything like 20KHz.


Exactly.... two pic's as I said in post 112.... There is also the pic16f677 which DOES have a PWM module (and more memory).
 
It's not difficult!!! It's very nearly the same.

Code:
	list p = 16f676
	#include<p16f676.inc>
	__config(0x314)

Tog	equ	20		; didn't use
Pulse	equ	21		; ADC result
OnPulse	equ	22		; High time
OfPulse	equ	23		; Low time
d1	equ	24		; delay var
d2	equ	25		; delay var
d3	equ	26		; delay var

	org	0
	goto	init		; bypass int vector
	org	4
	goto	ISR		; Interrupt vector

init
	banksel TRISC
	clrf	TRISC		; Outputs
	movlw	0xFF
	movwf	TRISA		; Inputs
	movlw	0x01
	movwf	ANSEL		; RA0 for pot
	movlw	0x50
	movwf	ADCON1		; Fosc/16
	clrf	OPTION_REG	; TMR0 pre-scaler 1:2
	banksel	PORTC
	movlw	0x07
	movwf	CMCON		; No comparators
	clrf	ADCON0		; ADC left justify, ADC off
	clrf	INTCON		; no int's
	bsf	INTCON,T0IE	; set TMR0 interrupt only
	bsf	INTCON,GIE	; set global int's
	
loop
	call	readadc		; get pulse width from RA0
	movf	Pulse,w		
	sublw	.255		; 
	movwf	OfPulse		; the off pulse could easily
	movf	Pulse,w		; have been the comp of the pulse
	movwf	OnPulse
	goto	loop	

ISR
	btfsc	PORTC,0		; First we toggle RC0
	goto	t1
	bsf	PORTC,0		; On
	movf	OnPulse,w	; (set tmr0 high time)
	goto 	t2	
t1	bcf	PORTC,0		; or off
	movf	OfPulse,w	;  (set tmr0 low time)
t2	movwf	TMR0
	bcf	INTCON,T0IF	; clear the interrupt
	retfie			; all done

readadc
	bsf	ADCON0,ADON	; switch on
	call	delay40		; have to wait for module
	bsf	ADCON0,GO	; start conversion
	call	delay40		; TAD	
	btfsc	ADCON0,GO	; wait for ADC
	goto	$-1		;
	movf	ADRESH,w	; 8 bit result
	movwf	Pulse		; in pulse
	return

delay40				; about 38mS
	clrf	d1		; 256 iterations
	movlw	30
	movwf	d2		; 30 iterations
	decfsz	d1
	goto	$ -1
	decfsz	d2
	goto	$ -3
	return	

	end

If I keep doing things for you... you'll never learn.....

ASM is not difcult more then C ???
if posibol can u sher it with C ..
 
Hi again,

What is CONVERSION CLOCK and why it is used in this program??


movlw 0x50
movwf ADCON1 ; Fosc/16
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top