16F684 adcin and hpwm


Closed Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    Nov 2008
    Posts
    41

    Default 16F684 adcin and hpwm

    I'm having a bit of trouble trying to get an adjustable pwm signal out of a 16F684. My version of MPLAB is 8.0 and MPASM is 5.14. The code was pieced together from posts on this forum and a few other sites as well. What I'm hoping to do is get an adjustable 6khz pwm signal with a 0-80% duty cycle. Pin 12 is connected to the wiper of a 10k pot. Looking at the output it seems the duty cycle is changing very slightly (1-2%) over the entire travel. Anything look out of place?


    INCLUDE "modedefs.bas" 'Includes supoprt for PicBasic language

    OSCCON = %01110001
    PAUSE 100 ' start-up delay

    '/////////////////////////
    '// PIN configuration //
    '/////////////////////////

    'DEFINE CCP1_REG PORTC 'Define output port of pulses out
    'DEFINE CCP1_BIT 3 'Define output port of pulses out

    Define ADC_BITS 8 ' Set number of bits in result
    Define ADC_CLOCK 3 ' Set clock source (3=rc)
    Define ADC_SAMPLEUS 100 ' Set sampling time in uSec

    CMCON0 = 7 ' Disable analog comparator
    ANSEL = %00000010 ' set AN1 (RA1) as analog, others to digital
    ADCON1 = %00000000 ' Left justified results in 8 bits
    ADCON0 = %10000001 ' Configure and turn on A/D Module

    TRISC = %00000000 ' Set PORTB to all output
    TRISA = %11111111 ' Set PORTA to all input

    '///////////////////////////////////////////////
    '// Variable Declaration and initialization //
    '///////////////////////////////////////////////

    DutyCycle var byte 'Create adval to store result
    LastDutyCycle var byte 'hold last time around

    '//////////////////////////
    '// Program starts here //
    '//////////////////////////

    Mainloop:
    ADCON0.2 = 1 'Start Conversion

    ADCIN 1, DutyCycle 'analog pin 1 (RA1) get the 8 bit result
    pause 50

    If dutycycle <> lastdutycycle then
    lastdutycycle = dutycycle
    HPWM 1,DutyCycle,6000
    Endif

    GOTO Mainloop
    end

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    You don't need to start the conversion, ADCIN handle it for you.

    Usually you want to use Right Justified results.

    Make sure you set your config fuses to use the internal OSC.

    Not sure if HPWM work as expected on this PIC as it has this ECCP module... might worth a try here if you still have some problem using it.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3
    Join Date
    Nov 2008
    Posts
    41


    Did you find this post helpful? Yes | No

    Default

    What I'm trying to do is rewrite this ASM code into Basic

    http://hades.mech.northwestern.edu/w...M_Motor_Driver

    It works as described but I need to add more options and conditions. The hardest part is getting this pwm part working.

  4. #4
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    NO NEED FOR HPWM then

    Code:
    INIT_PWM
    	BANK0
    	MOVLW	B'10001100'			;Dual-Output, Half-Bridge Mode
    	MOVWF		CCP1CON			;P1A,P1C active-high, P1B,P1D active-high
    	MOVLW	B'00000100'			;Timer2 enabled with prescale 1
    	MOVWF		T2CON
    	BANK1
    	MOVLW	0xFF
    	MOVWF		PR2				;Set frequency to 19.53 kHz
    	BANK0
    	MOVLW	B'01111111'			;50% duty cycle
    	MOVWF		CCPR1L	
    	RETURN
    Code:
    UPDATE_PWM
    	BANK0
    	MOVFW	ADRESH			; copy the 8 MSbs to the CCPR1L register
    	MOVWF	CCPR1L
    	BTFSC	ADRESL,7		; is ADC bit 1 set?
    	BCF		CCP1CON,5		; if not, clear the PWM bit 1
    	BSF		CCP1CON,5		; if so, set the PWM bit 1
    	BTFSC	ADRESL,6		; same thing, for bit 0
    	BCF		CCP1CON,4
    	BSF		CCP1CON,4	
    
    	RETURN
    Think about it a little bit
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  5. #5
    Join Date
    Nov 2008
    Posts
    41


    Did you find this post helpful? Yes | No

    Default

    Got it working. Heres the code:

    DEFINE FOSC 100
    DEFINE ADC_BITS 10 ' Set number of bits in result
    DEFINE ADC_CLOCK 3 ' Set clock source (3=rc)
    DEFINE ADC_SAMPLEUS 50 ' Set sampling time in uS
    OSCCON = %01100001 '4 Mhz

    TRISA = %11111111 ' Set PORTA to all input
    ADCON1 = %10000010 ' Set PORTA analog and right justify
    output portc.5

    adcVar VAR word
    dutyCycle var byte


    main:
    ADCIN 0, adcVar
    dutyCycle = adcVar/256
    hpwm 1,dutyCycle,6000
    goto main

  6. #6
    Join Date
    Nov 2008
    Posts
    19


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by astouffer View Post
    Got it working. Heres the code:

    DEFINE FOSC 100
    DEFINE ADC_BITS 10 ' Set number of bits in result
    DEFINE ADC_CLOCK 3 ' Set clock source (3=rc)
    DEFINE ADC_SAMPLEUS 50 ' Set sampling time in uS
    OSCCON = %01100001 '4 Mhz

    TRISA = %11111111 ' Set PORTA to all input
    ADCON1 = %10000010 ' Set PORTA analog and right justify
    output portc.5

    adcVar VAR word
    dutyCycle var byte


    main:
    ADCIN 0, adcVar
    dutyCycle = adcVar/256
    hpwm 1,dutyCycle,6000
    goto main
    word filled by 10bit, result divided by 256?

  7. #7
    Join Date
    Nov 2008
    Posts
    41


    Did you find this post helpful? Yes | No

    Default

    word filled by 10bit, result divided by 256?
    Would it make more sense to sample 8 bits? Without dividing by 256 one full turn of the pot repeats 0-100% duty cycle many times. I admit being new to the world of programming and working with the internals of microprocessors.

  8. #8
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Well yes and no. Yes if you're using HPWM, no if you set the CCP register yourself as in the ASM code.

    One thing is sure, 10 bits/256, will not give you a 8 bit results... if you divide it by 4... yes.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

Similar Threads

  1. ADCIN > HPWM but with min/max limits - i failed maths
    By jamie_s in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 29th November 2009, 02:02
  2. Portc.3 latching?
    By Bronurstomp in forum mel PIC BASIC
    Replies: 4
    Last Post: - 10th November 2008, 17:47
  3. adcin and HPWM
    By lerameur in forum mel PIC BASIC Pro
    Replies: 24
    Last Post: - 20th September 2008, 16:49
  4. PIC16F819 HPWM CCP1 clariffication
    By earltyso in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 20th March 2008, 18:43
  5. HPWM Problem
    By Mark Scotford in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 8th September 2004, 10:40

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts