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.

PWM examples in Assembly

Status
Not open for further replies.

bryan1

Well-Known Member
Hiya Guy's,
I'm on playing around with a 6 wire steppermotor and a 5804 translator/driver chip and this will be the first time I've used pwm. I'm thinking of using a 16f88 and after looking on the net for over 3 hours I'm still yet to find some examples in assembly to help me get started. I looked at the piclist but that site is so hard to find anything so if anyone has some routines to setup and run on a 16f chip I'll appreciate it as I'm a bit confused just using the datasheet.

Cheers Bryan :D
 
I'm not sure if this is what you are looking for or not.

This is part of a PID servo on a 16F819.

The PWM setup is
Code:
;	Set PWM Period by writing to PR2
;	With 8MHz clock this will give a frequency of 31kHz
                movlw   3fh
                bsf     STATUS,RP0
                movwf   PR2
                bcf     STATUS,RP0

;	set PWM value to middle - 50/50 duty
                movlw   1fh
                movwf   CCPR1L

;	Enable timer 2 with prescaler 1
                movlw   B'00000100'
                movwf   T2CON

;	Turn on PWM
                movlw   B'00001111'
                movwf   CCP1CON

The PWM value was set with the following.

Code:
                movwf   PWMVal
                movlw   B'00001111'
                bcf     STATUS,C
                rrf     PWMVal,F;      Get lowest bit
                btfsc   STATUS,C
                iorlw   B'00010000';   Put in bit 4 of CCP1CON
                bcf     STATUS,C
                rrf     PWMVal,F;      and bit 1 to bit 5
                btfsc   STATUS,C
                iorlw   B'00100000'
                movwf   CCP1CON
                movfw   PWMVal;		remaining 6 bits to CCPR1L
                movwf   CCPR1L

The values above only give 8 bit resolution of the PWM. The lower bit are in CCP1CON and so mid point is 31 (1Fh).

This might get you started.

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top