00001 /* 00002 * "Copyright (c) 2008 Robert B. Reese, Bryan A. Jones, J. W. Bruce ("AUTHORS")" 00003 * All rights reserved. 00004 * (R. Reese, reese_AT_ece.msstate.edu, Mississippi State University) 00005 * (B. A. Jones, bjones_AT_ece.msstate.edu, Mississippi State University) 00006 * (J. W. Bruce, jwbruce_AT_ece.msstate.edu, Mississippi State University) 00007 * 00008 * Permission to use, copy, modify, and distribute this software and its 00009 * documentation for any purpose, without fee, and without written agreement is 00010 * hereby granted, provided that the above copyright notice, the following 00011 * two paragraphs and the authors appear in all copies of this software. 00012 * 00013 * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR 00014 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT 00015 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS" 00016 * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00017 * 00018 * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES, 00019 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 00020 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS 00021 * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO 00022 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS." 00023 * 00024 * Please maintain this header in its entirety when copying/modifying 00025 * these files. 00026 * 00027 * 00028 */ 00029 00030 #include "pic24_all.h" 00031 #include <stdio.h> 00032 00037 #define CONFIG_SLAVE_ENABLE() CONFIG_RB2_AS_DIG_OUTPUT() 00038 #define SLAVE_ENABLE() _LATB2 = 0 //low true assertion 00039 #define SLAVE_DISABLE() _LATB2 = 1 00040 00041 00042 void configSPI1(void) { 00043 //spi clock = 40MHz/1*4 = 40MHz/4 = 10MHz 00044 SPI1CON1 = SEC_PRESCAL_1_1 | //1:1 secondary prescale 00045 PRI_PRESCAL_4_1 | //4:1 primary prescale 00046 CLK_POL_ACTIVE_HIGH | //clock active high (CKP = 0) 00047 SPI_CKE_ON | //out changes active to inactive (CKE=1) 00048 SPI_MODE8_ON | //8-bit mode 00049 MASTER_ENABLE_ON; //master mode 00050 SPI1STATbits.SPIEN = 1; //enable SPI mode 00051 //configure pins. Only need SDO, SCLK since POT is output only 00052 CONFIG_SDO1_TO_RP(6); //use RP6 for SDO 00053 CONFIG_RP6_AS_DIG_PIN(); //ensure that analog is disabled 00054 CONFIG_SCK1OUT_TO_RP(7); //use RP7 for SCLK 00055 CONFIG_RP7_AS_DIG_PIN(); //ensure that analog is disabled 00056 CONFIG_SLAVE_ENABLE(); //chip select for MCP41xxx 00057 SLAVE_DISABLE(); //disable the chip select 00058 } 00059 00060 void setPotWiper(uint8 u8_i) { 00061 SLAVE_ENABLE(); //assert MCP41xxx chipselect 00062 ioMasterSPI1(0x11); //command byte to select wiper register 00063 ioMasterSPI1(u8_i); 00064 SLAVE_DISABLE(); //negate MCP41xxx chipselect 00065 } 00066 00067 #define BUFSIZE 15 00068 char sz_1[BUFSIZE+1]; 00069 00070 int main (void) { 00071 uint16 u16_pv; 00072 configBasic(HELLO_MSG); 00073 configSPI1(); 00074 while (1) { 00075 outString("Input decimal value (0-255): "); 00076 inString(sz_1,BUFSIZE); 00077 sscanf(sz_1,"%d", (int *) &u16_pv); 00078 printf("\nSending %d to pot.\n",u16_pv); 00079 setPotWiper(u16_pv & 0x00FF); 00080 } 00081 }