• AVR Freaks

AnsweredPIC16F1823 Digital Potentiometer SPI MCP4131 Setup with MCC

Author
btommo
Junior Member
  • Total Posts : 112
  • Reward points : 0
  • Joined: 2017/07/07 02:59:33
  • Location: 0
  • Status: offline
2019/11/07 07:18:08 (permalink)
0

PIC16F1823 Digital Potentiometer SPI MCP4131 Setup with MCC

Hi,
 
I'm looking into SPI communication with a microchip potentiometer, I'm having issues with the SPI part as I haven't really used it before, I understand the concept, pull chip select low, delay, send address byte, send command byte and them send data byte.
 
I've connected a PICKIT 3 to a PIC16F1823 which is connected to a MCP4131 digital potentiometer with a 4K7 connected between MOSI connection as the MCP4131 has one pin for SDI/SDO. it's set up on a breadboard with the PICKIT 3 providing Vdd.
 
Below is the code i've tried to write;
 
/**
  Generated Main Source File

  Company:
    Microchip Technology Inc.

  File Name:
    main.c

  Summary:
    This is the main file generated using PIC10 / PIC12 / PIC16 / PIC18 MCUs

  Description:
    This header file provides implementations for driver APIs for all modules selected in the GUI.
    Generation Information :
        Product Revision : PIC10 / PIC12 / PIC16 / PIC18 MCUs - 1.77
        Device : PIC16F1823
        Driver Version : 2.00
*/

/*
    (c) 2018 Microchip Technology Inc. and its subsidiaries.
    
    Subject to your compliance with these terms, you may use Microchip software and any
    derivatives exclusively with Microchip products. It is your responsibility to comply with third party
    license terms applicable to your use of third party software (including open source software) that
    may accompany Microchip software.
    
    THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
    EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY
    IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS
    FOR A PARTICULAR PURPOSE.
    
    IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
    INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
    WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP
    HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO
    THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL
    CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT
    OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS
    SOFTWARE.
*/

#include "mcc_generated_files/mcc.h"

/*
                         Main application
 */
void main(void)
{
    // initialize the device
    SYSTEM_Initialize();

    // When using interrupts, you need to set the Global and Peripheral Interrupt Enable bits
    // Use the following macros to:

    // Enable the Global Interrupts
    //INTERRUPT_GlobalInterruptEnable();

    // Enable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptEnable();

    // Disable the Global Interrupts
    //INTERRUPT_GlobalInterruptDisable();

    // Disable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptDisable();

    while (1)
    {
        for( uint8_t x = 0; x < 129; x++) //increment resistance
        {
            POTCS_SetLow();
            __delay_ms(50);
            spi_writeByte(0b0000);
            spi_writeByte(0b0000);
            spi_writeByte(0b0100); //increment command 01
            spi_writeByte(0b0000);
            POTCS_SetHigh();
            __delay_ms(50);
        }
        for( uint8_t x = 0; x < 129; x++)//decrement resistance
        {
            POTCS_SetLow();
            __delay_ms(50);
            spi_writeByte(0b0000);
            spi_writeByte(0b0000);
            spi_writeByte(0b1000); //decrement command 10
            spi_writeByte(0b0000);
            POTCS_SetHigh();
            __delay_ms(50);
        }
        
        // Add your application code
    }
}
/**
 End of File
*/

 
 
 
The SPI was set up in MCC with the MSSP foundation services library and using the provided spi_writeByte function.
 
I would upload datasheet but I seem to be unable to. Anyone used these POTs or similar before?
 
I forgot to mention the issue, the 10K POT continuously shows 5K049 on the wiper so I'm unsure if SPI is set up correctly.
 
Checking the SCK pin with a scope, there's no waveform, I also checked the nCS pin, it shows a 10Hz square wave as expected so the SPI is not functioning.
post edited by btommo - 2019/11/07 08:18:43
#1
ric
Super Member
  • Total Posts : 24202
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: PIC16F1823 Digital Potentiometer SPI MCP4131 Setup with MCC 2019/11/07 12:44:37 (permalink)
+1 (1)
You don't mention which actual pins you connected to on the PIC, so we have to just trust you did it correctly.
SCK on RC0 (pin#10 on 14 pin parts)
SDO  on RC2 (pin#8) via resistor
SDI on RC1 (pin#9) direct to SDI/SDO on pot

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#2
btommo
Junior Member
  • Total Posts : 112
  • Reward points : 0
  • Joined: 2017/07/07 02:59:33
  • Location: 0
  • Status: offline
Re: PIC16F1823 Digital Potentiometer SPI MCP4131 Setup with MCC 2019/11/08 01:55:19 (permalink)
0
Sorry, Pin 10 of PIC to pin 2(SCK) of MCP4131, Pin 9 of PIC to pin 3(SDI/SDO) of MCP4131, Pin 8 of PIC connected through a 4K7 resistor to Pin 3(SDI/SDO) of MCP4131. RC0 SCK, RC1 SDI, RC2 SDO. I have noticed though that on MCC the pins aren't configured as output on MCC Pin module, may be a silly question but does this need to be done?
#3
ric
Super Member
  • Total Posts : 24202
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: PIC16F1823 Digital Potentiometer SPI MCP4131 Setup with MCC 2019/11/08 02:02:10 (permalink)
+1 (1)
btommo
....I have noticed though that on MCC the pins aren't configured as output on MCC Pin module, may be a silly question but does this need to be done?

I don't use MCC so can't be sure, but I would expect they do need to be set as outputs. It can't hurt to try.

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#4
btommo
Junior Member
  • Total Posts : 112
  • Reward points : 0
  • Joined: 2017/07/07 02:59:33
  • Location: 0
  • Status: offline
Re: PIC16F1823 Digital Potentiometer SPI MCP4131 Setup with MCC 2019/11/08 03:16:12 (permalink)
0
Didn't make any difference, SCK is constantly low still, I checked the POTs nCS pin which showed the 10Hz waveform still so PIC is running.
#5
ric
Super Member
  • Total Posts : 24202
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: PIC16F1823 Digital Potentiometer SPI MCP4131 Setup with MCC 2019/11/08 03:24:01 (permalink)
+1 (1)
Sorry, can't comment if the SPI is set up correctly without seeing what code is inside your SYSTEM_Initialize() function.

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#6
btommo
Junior Member
  • Total Posts : 112
  • Reward points : 0
  • Joined: 2017/07/07 02:59:33
  • Location: 0
  • Status: offline
Re: PIC16F1823 Digital Potentiometer SPI MCP4131 Setup with MCC 2019/11/08 05:36:42 (permalink)
0
Progress! I got rid of the foundation one as it didn't seem to set it up properly and used the other one, MSSP(PIC10/PIC12/PIC16/PIC18...) etc which has a set up in system initialize;
 

 
 
 
void SPI_Initialize(void)
{
// Set the SPI module to the options selected in the User Interface

// R_nW write_noTX; P stopbit_notdetected; S startbit_notdetected; BF RCinprocess_TXcomplete; SMP Middle; UA dontupdate; CKE Idle to Active; D_nA lastbyte_address;
SSP1STAT = 0x00;

// SSPEN enabled; WCOL no_collision; CKP Idle:Low, Active:High; SSPM FOSC/4; SSPOV no_overflow;
SSP1CON1 = 0x20;

// SSPADD 0;
SSP1ADD = 0x00;
}
 
 
 

 
I undated program to use the below function generated;
 

 
 
 
uint8_t SPI_Exchange8bit(uint8_t data)
{
// Clear the Write Collision flag, to allow writing
SSP1CON1bits.WCOL = 0;
 
 
 
SSP1BUF = data;
 
 
 
while(SSP1STATbits.BF == SPI_RX_IN_PROGRESS)
{
}
 
 
 
return (SSP1BUF);
}
 
 
 

 
Updated main file below;
 

 
 
 
/**
Generated Main Source File
 
 
 
Company:
Microchip Technology Inc.
 
 
 
File Name:
main.c
 
 
 
Summary:
This is the main file generated using PIC10 / PIC12 / PIC16 / PIC18 MCUs
 
 
 
Description:
This header file provides implementations for driver APIs for all modules selected in the GUI.
Generation Information :
Product Revision : PIC10 / PIC12 / PIC16 / PIC18 MCUs - 1.77
Device : PIC16F1823
Driver Version : 2.00
*/
 
 
 
/*
(c) 2018 Microchip Technology Inc. and its subsidiaries.

Subject to your compliance with these terms, you may use Microchip software and any
derivatives exclusively with Microchip products. It is your responsibility to comply with third party
license terms applicable to your use of third party software (including open source software) that
may accompany Microchip software.

THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY
IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS
FOR A PARTICULAR PURPOSE.

IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP
HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO
THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL
CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT
OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS
SOFTWARE.
*/
 
 
 
#include "mcc_generated_files/mcc.h"
 
 
 
/*
Main application
*/
void main(void)
{
// initialize the device
SYSTEM_Initialize();
 
 
 
// When using interrupts, you need to set the Global and Peripheral Interrupt Enable bits
// Use the following macros to:
 
 
 
// Enable the Global Interrupts
//INTERRUPT_GlobalInterruptEnable();
 
 
 
// Enable the Peripheral Interrupts
//INTERRUPT_PeripheralInterruptEnable();
 
 
 
// Disable the Global Interrupts
//INTERRUPT_GlobalInterruptDisable();
 
 
 
// Disable the Peripheral Interrupts
//INTERRUPT_PeripheralInterruptDisable();
 
 
 
while (1)
{

for( uint8_t x = 0; x < 129; x++) //increment resistance
{
POTCS_SetLow();
__delay_ms(50);
SPI_Exchange8bit(0b0000);
SPI_Exchange8bit(0b0000);
SPI_Exchange8bit(0b0100); //increment command 01
SPI_Exchange8bit(0b0000);
POTCS_SetHigh();
__delay_ms(50);
}
for( uint8_t x = 0; x < 129; x++)//decrement resistance
{
POTCS_SetLow();
__delay_ms(50);
SPI_Exchange8bit(0b0000);
SPI_Exchange8bit(0b0000);
SPI_Exchange8bit(0b1000); //decrement command 10
SPI_Exchange8bit(0b0000);
POTCS_SetHigh();
__delay_ms(50);
}

// Add your application code
}
}
/**
End of File
*/
 
 
 

 
I get the attached result on the scope, newfile1 is SCK and newfile2 is SDO.
 
The result I get from the POT is that it changes from 189 Ohm straight to 113 Ohm rather than incrementing over time to a higher value (129 steps for POT) and decrementing over time.
 
 
post edited by btommo - 2019/11/08 05:44:53

Attached Image(s)

#7
davea
Senior Member
  • Total Posts : 157
  • Reward points : 0
  • Joined: 2016/01/28 13:12:13
  • Location: 0
  • Status: offline
Re: PIC16F1823 Digital Potentiometer SPI MCP4131 Setup with MCC 2019/11/08 18:37:01 (permalink) ☄ Helpfulby btommo 2019/11/13 06:32:02
+2 (2)
#8
ric
Super Member
  • Total Posts : 24202
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: PIC16F1823 Digital Potentiometer SPI MCP4131 Setup with MCC 2019/11/08 23:50:55 (permalink) ☄ Helpfulby btommo 2019/11/13 06:31:58
+2 (2)
Indeed.
You are sending 32 bit commands, when increment and decrement are just 8 bit commands.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#9
btommo
Junior Member
  • Total Posts : 112
  • Reward points : 0
  • Joined: 2017/07/07 02:59:33
  • Location: 0
  • Status: offline
Re: PIC16F1823 Digital Potentiometer SPI MCP4131 Setup with MCC 2019/11/11 01:41:18 (permalink)
0
Ah my mistake, I thought I needed the four bits at the beginning and end as well. I'll try it this morning and let you know the outcome.
 
/**
  Generated Main Source File

  Company:
    Microchip Technology Inc.

  File Name:
    main.c

  Summary:
    This is the main file generated using PIC10 / PIC12 / PIC16 / PIC18 MCUs

  Description:
    This header file provides implementations for driver APIs for all modules selected in the GUI.
    Generation Information :
        Product Revision : PIC10 / PIC12 / PIC16 / PIC18 MCUs - 1.77
        Device : PIC16F1823
        Driver Version : 2.00
*/

/*
    (c) 2018 Microchip Technology Inc. and its subsidiaries.
    
    Subject to your compliance with these terms, you may use Microchip software and any
    derivatives exclusively with Microchip products. It is your responsibility to comply with third party
    license terms applicable to your use of third party software (including open source software) that
    may accompany Microchip software.
    
    THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
    EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY
    IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS
    FOR A PARTICULAR PURPOSE.
    
    IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
    INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
    WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP
    HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO
    THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL
    CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT
    OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS
    SOFTWARE.
*/

#include "mcc_generated_files/mcc.h"

/*
                         Main application
 */
void main(void)
{
    // initialize the device
    SYSTEM_Initialize();

    // When using interrupts, you need to set the Global and Peripheral Interrupt Enable bits
    // Use the following macros to:

    // Enable the Global Interrupts
    //INTERRUPT_GlobalInterruptEnable();

    // Enable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptEnable();

    // Disable the Global Interrupts
    //INTERRUPT_GlobalInterruptDisable();

    // Disable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptDisable();

    while (1)
    {
        
        for( uint8_t x = 0; x < 129; x++) //increment resistance
        {
            POTCS_SetLow();
            __delay_ms(50);
            SPI_Exchange8bit(0b0000);
            SPI_Exchange8bit(0b0100); //increment command 01
            POTCS_SetHigh();
            __delay_ms(50);
        }
        for( uint8_t x = 0; x < 129; x++)//decrement resistance
        {
            POTCS_SetLow();
            __delay_ms(50);
            SPI_Exchange8bit(0b0000);
            SPI_Exchange8bit(0b1000); //decrement command 10
            POTCS_SetHigh();
            __delay_ms(50);
        }
        
        // Add your application code
    }
}
/**
 End of File
*/

 
The result is that the resistance across P0B to P0W shows 266-267 Ohm on startup and switches to 420 Ohm after a period of time then switches back and so on.
post edited by btommo - 2019/11/11 02:05:41
#10
ric
Super Member
  • Total Posts : 24202
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: PIC16F1823 Digital Potentiometer SPI MCP4131 Setup with MCC 2019/11/11 02:32:53 (permalink) ☼ Best Answerby btommo 2019/11/13 05:52:31
+2 (2)
You are still sending 16 bits, not 8.
EACH call to SPI_Exchange8bit() sends 8 bits.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#11
btommo
Junior Member
  • Total Posts : 112
  • Reward points : 0
  • Joined: 2017/07/07 02:59:33
  • Location: 0
  • Status: offline
Re: PIC16F1823 Digital Potentiometer SPI MCP4131 Setup with MCC 2019/11/11 03:05:05 (permalink)
0
My silly mistake, don't know why I thought of sending 4 bits each time, I have changed it and programmed the MCU it increments from from 5K05 to 9K99, it seems to have a delay before it begins incrementing after a delay.
 
This delay was due to the increment command being incorrect, for some reason "0000 0100" didn't work and it starting incrementing on the decrement command "0000 1000" and the value didn't decrement so I tried "0001 0000" and now it is both incrementing and decrementing. I beleived the first 4 bits was the address "0000" for wiper 0 so I'm a bit confused.
#12
Jump to:
© 2019 APG vNext Commercial Version 4.5