PicBasic Pro code to drive MCP4291 DAC?


Closed Thread
Results 1 to 18 of 18

Hybrid View

  1. #1
    Join Date
    Apr 2011
    Location
    NSW, Australia
    Posts
    39


    Did you find this post helpful? Yes | No

    Default Re: PicBasic Pro code to drive MCP4291 DAC?

    Mark_s

    What speed are you looking for?
    Mine was running at about 9 Hz on an 18F14K50. By setting Config1 to $F100 I now get around 70hZ which is what I was after.

    aajgss

  2. #2


    Did you find this post helpful? Yes | No

    Default Re: PicBasic Pro code to drive MCP4291 DAC?

    aajgss,
    I was getting similar frequencies as you, at 20mhz and 40mhz clock settings. My lookup table has 256
    values, so it takes long time to cycle. I going to try the MSSP tonight and see what is gained. I know a dspic running at 80mhz can output about 4khz with the same dac and MSSP. So I am trying for 1 to 2khz. Maybe a pipe dream? I'll post the code if it works.

    regards

  3. #3
    Join Date
    Apr 2011
    Location
    NSW, Australia
    Posts
    39


    Did you find this post helpful? Yes | No

    Default Re: PicBasic Pro code to drive MCP4291 DAC?

    Mark_s

    I'll be interested in how you go. I did try the MSSP and actually got slower. I know it was the code I had written, because its just logical it should be faster and was going to continue with it to see if I could find my error(s), but the change to the CONFIG1 gave me the frequency I wanted. If I could get it faster and use 2 degree steps instead of 4 degree steps it would be better.

    regards
    aajgss

  4. #4


    Did you find this post helpful? Yes | No

    Default Re: PicBasic Pro code to drive MCP4291 DAC?

    Okay I hammered out a routine to use the MSSP module. It gave me
    a 6x speed increase over shiftout. I'm just a hack so there maybe a
    better way to do it.
    Results:
    Shiftout MSSP
    10mhz 10hz 66hz
    20mhz 21hz 117hz
    40mhz 42hz 2 63hz
    80mhz 83hz 526hz
    The 80mhz is 20mhz crystal 4xpll, overclocking probably not reliable? but worked!
    Code:
    '****************************************************************
    '*  Name    : DAC_12BIT_MSSP                                    *
    '*  Author  : MARK S                                            *
    '*  Date    : 6/9/2011
    '* SINE WAVE GENERATOR USING MCP4921 12BIT DAC                  *
    '* USES MSSP MODULE vs SHIFTOUT                                 *
    '* PIC18F4680, PBP2.6 MPASM                                     *
    '*                                                              *
    '****************************************************************
    DEFINE  OSC 20
    'REGISTER SET
    INTCON = %00000000
    INTCON2.7 = 0       'weak pullups portB
    ADCON1 =%00001111   'ALL DIGITAL
    CMCON  =%00000111   'COMP OFF
    T3CON = 0
    CCP1CON = 0 'ECCP OFF
    ECCP1AS = 0
    ECCP1DEL = 0
    SSPSTAT = %00000000   'TX IDLE LOW,SAMPLE MIDDLE
    SSPCON1 = %00100000 ' ENABLE MSSP,CLK IDLE LOW,FOSC/4
    '-----------------------------------------------------
    'PORTS
     TRISA = %00000000  ' PORTA ALL OUTPUT        ' 
     TRISB = %00000000  ' PORTB  
     PORTB = %00000000  ' CLEAR PORTB 
     TRISC = %00000000  ' PortC 
     TRISD = %00000000   
        TRISE = %00000000
    '----------------------------------------------------
    'DAC CONNECTIONS  
    'VREF     PIN6 MCP4921 TIED TO +5V
    'CLOCK    PIN3 MCP4921 TO PORTC.3  "SCK"
    'DATA     PIN4 MCP4921 TO PORTC.5  "SDO"
    'CS       PIN2 MCP4921 TO PORTD.2
    'LOAD     PIN5 MCP4921 TO PORTD.3   CONNECT THIS TO GROUND TO SAVE A PORT PIN
    '--------------------------------------------------
    'VARIABLES 
    CS_DAC      VAR PORTD.2
    LOAD_DAC    VAR PORTD.3    'KEEP LOW
    WAVE_INDEX  VAR WORD
    DAC_OUT     VAR WORD       
    DAC_OUT1    VAR WORD       
    SSPIF       VAR PIR1.3     ' SPI interrupt flag
    '----------------------------------------------------
    'START    
    SSPBUF = 0          ' CLEAR SPI BUFFER
    SSPIF = 0           ' CLEAR SPI INTERUPT FLAG.
    LOW LOAD_DAC
    '===================================================================   
    MAIN:
    HIGH CS_DAC
    FOR WAVE_INDEX = 0 TO 255
              
    GOSUB SINE_TABLE
        LOW CS_DAC 
          DAC_OUT1 =$3000 | DAC_OUT   ' Combine table value with dac reg control
                SSPBUF = DAC_OUT1.HIGHBYTE  'SEND HIGH 
                While SSPIF = 0            ' WAIT FOR TX TO COMPLETE
                    Wend 
                    SSPIF = 0               'RESET INT FLAG
                SSPBUF = DAC_OUT1.LOWBYTE   'SEND LOW
                While SSPIF = 0            ' WAIT FOR TX TO COMPLETE   
                    Wend
                    SSPIF = 0               'RESET INT FLAG
        HIGH CS_DAC
    NEXT WAVE_INDEX
    GOTO MAIN
       
    SINE_TABLE:
                       
    lookup2 WAVE_INDEX, [0,1,2,6,10,15,22,30,39,50,61,74,88,103,120,137,156,_
            176, 197,219,242,266,291,318,345,374,403,433,465,497,_
            531,565,600,636,673,710,749,788,828,869,910,952,995,_
            1039,1083,1127,1172,1218,1264,1311,1358,1406,1453,1502,1550,1599,_
            1648,1698,1747,1797,1847,1897,1948,1998,2048,2098,2148,2199,2249,_
            2299,2349,2398,2448,2497,2546,2594,2643,2690,2738,2785,2832,2878,_
            2924,2969,3013,3057,3101,3144,3186,3227,3268,3308,3347,3386,3423,_
            3460,3496,3531,3565,3599,3631,3663,3693,3722,3751,3778,3805,3830,_
            3854,3877,3899,3920,3940,3959,3976,3993,4008,4022,4035,4046,4057,_
            4066,4074,4081,4086,4090,4094,4095,4095,4095,4094,4090,4086,4081,_
            4074,4066,4057,4046,4035,4022,4008,3993,3976,3959,3940,3920,3899,_
            3877,3854,3830,3805,3778,3751,3722,3693,3663,3631,3599,3565,3531,_
            3496,3460,3423,3386,3347,3308,3268,3227,3186,3144,3101,3057,3013,_
            2969,2924,2878,2832,2785,2738,2690,2643,2594,2546,2497,2448,2398,_
            2349,2299,2249,2199,2148,2098,2048,1998,1948,1897,1847,1797,1747,_
            1698,1648,1599,1550,1502,1453,1406,1358,1311,1264,1218,1172,1127,_
            1083,1039,995,952,910,869,828,788,749,710,673,636,600,_
            565,531,497,465,433,403,374,345,318,291,266,242,219,_
            197,176,156,137,120,103,88,74,61,50,39,30,22,15,10,6,_
            2,1],DAC_OUT
            
     RETURN
     END
    Last edited by mark_s; - 10th June 2011 at 15:27.

  5. #5
    Join Date
    May 2007
    Posts
    604


    Did you find this post helpful? Yes | No

    Default Re: PicBasic Pro code to drive MCP4291 DAC?

    I don't know what the OP's application is but using a PIC and a serial DAC (MCP4921) to generate sine waves is extremely inefficient. It consumes a tremendous amount of CPU resources and gives very poor performance - limited frequency range and resolution. Using a DDS chip such as the AD8937 (which incidentally costs about the same as then MCP4921) allows you to generate sine (and triangle and square) waveforms from 0-16MHz with 0.06Hz (28-bit) resolution. For low frequency applications (such as VFDs) you can use a lower speed clock - with a 1MHz clock you get 0-1MHz and 0.004Hz resolution. It uses the same SPI interface as the MCP4921 and the slowest 4MHz PIC will still be able to generate up to a 16MHz sine wave.
    Why pay for overpriced toys when you can have
    professional grade tools for FREE!!!

  6. #6
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240


    Did you find this post helpful? Yes | No

    Default Re: PicBasic Pro code to drive MCP4291 DAC?

    Quote Originally Posted by rmteo View Post
    I don't know what the OP's application is but using a PIC and a serial DAC (MCP4921) to generate sine waves is extremely inefficient. It consumes a tremendous amount of CPU resources and gives very poor performance - limited frequency range and resolution. Using a DDS chip such as the AD8937 (which incidentally costs about the same as then MCP4921) allows you to generate sine (and triangle and square) waveforms from 0-16MHz with 0.06Hz (28-bit) resolution. For low frequency applications (such as VFDs) you can use a lower speed clock - with a 1MHz clock you get 0-1MHz and 0.004Hz resolution. It uses the same SPI interface as the MCP4921 and the slowest 4MHz PIC will still be able to generate up to a 16MHz sine wave.
    rmteo, isn't AD9837?
    Thanks and Regards;
    Gadelhas

  7. #7
    Join Date
    May 2007
    Posts
    604


    Did you find this post helpful? Yes | No

    Default Re: PicBasic Pro code to drive MCP4291 DAC?

    Yes, it is AD9837. Sorry for the typo.
    Why pay for overpriced toys when you can have
    professional grade tools for FREE!!!

Members who have read this thread : 0

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