PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
mcp41xxx_spi_pot.c
Go to the documentation of this file.
1 /*
2  * "Copyright (c) 2008 Robert B. Reese, Bryan A. Jones, J. W. Bruce ("AUTHORS")"
3  * All rights reserved.
4  * (R. Reese, reese_AT_ece.msstate.edu, Mississippi State University)
5  * (B. A. Jones, bjones_AT_ece.msstate.edu, Mississippi State University)
6  * (J. W. Bruce, jwbruce_AT_ece.msstate.edu, Mississippi State University)
7  *
8  * Permission to use, copy, modify, and distribute this software and its
9  * documentation for any purpose, without fee, and without written agreement is
10  * hereby granted, provided that the above copyright notice, the following
11  * two paragraphs and the authors appear in all copies of this software.
12  *
13  * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR
14  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
15  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS"
16  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17  *
18  * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
23  *
24  * Please maintain this header in its entirety when copying/modifying
25  * these files.
26  *
27  *
28  */
29 
30 #include "pic24_all.h"
31 #include <stdio.h>
32 
33 /** \file
34  * SPI example: PIC24 uC in Master mode to Microchip MCP41xxx Digital Potentiometer
35 */
36 
37 #define CONFIG_SLAVE_ENABLE() CONFIG_RB2_AS_DIG_OUTPUT()
38 #define SLAVE_ENABLE() _LATB2 = 0 //low true assertion
39 #define SLAVE_DISABLE() _LATB2 = 1
40 
41 
42 void configSPI1(void) {
43  //spi clock = 40MHz/1*4 = 40MHz/4 = 10MHz
44  SPI1CON1 = SEC_PRESCAL_1_1 | //1:1 secondary prescale
45  PRI_PRESCAL_4_1 | //4:1 primary prescale
46  CLK_POL_ACTIVE_HIGH | //clock active high (CKP = 0)
47  SPI_CKE_ON | //out changes active to inactive (CKE=1)
48  SPI_MODE8_ON | //8-bit mode
49  MASTER_ENABLE_ON; //master mode
50  SPI1STATbits.SPIEN = 1; //enable SPI mode
51  //configure pins. Only need SDO, SCLK since POT is output only
52 #if (defined(__dsPIC33E__) || defined(__PIC24E__))
53  //nothing to do here. On this family, the SPI1 port uses dedicated
54  //pins for higher speed. The SPI2 port can be used with remappable pins.
55  //you may need to add code to disable analog functionality if the SPI ports
56  //are on analog-capable pins.
57 #else
58  //all other families (PIC24H/PIC24F/dsPIC33F)
59  CONFIG_SDO1_TO_RP(6); //use RP6 for SDO
60  CONFIG_RP6_AS_DIG_PIN(); //ensure that analog is disabled
61  CONFIG_SCK1OUT_TO_RP(7); //use RP7 for SCLK
62  CONFIG_RP7_AS_DIG_PIN(); //ensure that analog is disabled
63 #endif
64  CONFIG_SLAVE_ENABLE(); //chip select for MCP41xxx
65  SLAVE_DISABLE(); //disable the chip select
66 }
67 
68 void setPotWiper(uint8_t u8_i) {
69  SLAVE_ENABLE(); //assert MCP41xxx chipselect
70  ioMasterSPI1(0x11); //command byte to select wiper register
71  ioMasterSPI1(u8_i);
72  SLAVE_DISABLE(); //negate MCP41xxx chipselect
73 }
74 
75 #define BUFSIZE 15
76 char sz_1[BUFSIZE+1];
77 
78 int main (void) {
79  uint16_t u16_pv;
80  configBasic(HELLO_MSG);
81  configSPI1();
82  while (1) {
83  outString("Input decimal value (0-255): ");
84  inString(sz_1,BUFSIZE);
85  sscanf(sz_1,"%d", (int *) &u16_pv);
86  printf("\nSending %d to pot.\n",u16_pv);
87  setPotWiper(u16_pv & 0x00FF);
88  }
89 }