给你一个MSP430驱动数字电位器MAX5484的代码(调试已经通过)
#include "msp430x13x.h"
#define SCLK_HIGH P4OUT |= 0x02
#define SDA_HIGH P4OUT |= 0x04
#define SCLK_LOW P4OUT &= ~0x02
#define SDA_LOW P4OUT &= ~0x04
#define CS_HIGH P4OUT |= 0x20
#define CS_LOW P4OUT &= ~0x20
void usDelay(int microeconds)
{
do
{
_NOP();
_NOP();
}while (--microeconds > 0);
}
static void InitOsc(void)
{
WDTCTL = WDTPW + WDTHOLD; // stop watchdog timer
DCOCTL = DCO0 + DCO1 + DCO2; /* */
BCSCTL1 = RSEL2 + RSEL1 + RSEL0;
BCSCTL2 = SELM1+SELS; // Aclk = 1/1 XT1 urn on XT1 oscillator
// FCTL2 = FWKEY + FSSEL0 + FN1; /* select MCLK/3 for flash timing */
__bic_SR_register(OSCOFF); // turn on XT1 oscillator
do
{
IFG1 &= ~OFIFG; // Clear OFIFG
usDelay(130); // Wait ~130us
} while (IFG1 & OFIFG); // Test oscillator fault flag
}
static void InitPort(void)
{
P1DIR = 0x3f;
P2DIR = 0x3f;
P3DIR = 0;
P4DIR = 0x3f;
P5DIR = 0x3f;
P6DIR = 0;
P1OUT = 0;
P2OUT = 0;
P4OUT = 0x32;
P5OUT = 0x00;
}
void spi_write(long int data)
{
int i;
CS_LOW;
/* for(i=0;i<8;i++)
{
SDA_LOW;
SCLK_LOW;
usDelay(20);
SCLK_HIGH;
}
SCLK_LOW;*/
data <<= 6;
for(i=24;i>0;i--)
{
SCLK_LOW;
if(data & 0x800000)
SDA_HIGH;
else
SDA_LOW;
data <<= 1;
SCLK_HIGH;
usDelay(1);
}
SCLK_LOW;
usDelay(1);
CS_HIGH;
}
void rsave(void)
{
int i,j;
SCLK_LOW;
CS_LOW;
j = 0x20;
for(i=8;i>0;i--)
{
if(j & 0x80)
SDA_HIGH;
else
SDA_LOW;
j <<= 1;
SCLK_LOW;
SCLK_HIGH;
}
SCLK_LOW;
usDelay(1);
CS_HIGH;
usDelay(12000);
}
void r_read(void)
{
int i,j;
SCLK_LOW;
CS_LOW;
j = 0x30;
for(i=8;i>0;i--)
{
if(j & 0x80)
SDA_HIGH;
else
SDA_LOW;
j <<= 1;
SCLK_LOW;
SCLK_HIGH;
}
SCLK_LOW;
CS_HIGH;
usDelay(12000);
}
int main( void )
{
WDTCTL = WDTPW + WDTHOLD;// Stop watchdog timer to prevent time out reset
InitOsc();
InitPort();
spi_write(800);
rsave();
//r_read();
while(1);
}
给你一个MSP430驱动数字电位器MAX5484的代码(调试已经通过)
#include "msp430x13x.h"
#define SCLK_HIGH P4OUT |= 0x02
#define SDA_HIGH P4OUT |= 0x04
#define SCLK_LOW P4OUT &= ~0x02
#define SDA_LOW P4OUT &= ~0x04
#define CS_HIGH P4OUT |= 0x20
#define CS_LOW P4OUT &= ~0x20
void usDelay(int microeconds)
{
do
{
_NOP();
_NOP();
}while (--microeconds > 0);
}
static void InitOsc(void)
{
WDTCTL = WDTPW + WDTHOLD; // stop watchdog timer
DCOCTL = DCO0 + DCO1 + DCO2; /* */
BCSCTL1 = RSEL2 + RSEL1 + RSEL0;
BCSCTL2 = SELM1+SELS; // Aclk = 1/1 XT1 urn on XT1 oscillator
// FCTL2 = FWKEY + FSSEL0 + FN1; /* select MCLK/3 for flash timing */
__bic_SR_register(OSCOFF); // turn on XT1 oscillator
do
{
IFG1 &= ~OFIFG; // Clear OFIFG
usDelay(130); // Wait ~130us
} while (IFG1 & OFIFG); // Test oscillator fault flag
}
static void InitPort(void)
{
P1DIR = 0x3f;
P2DIR = 0x3f;
P3DIR = 0;
P4DIR = 0x3f;
P5DIR = 0x3f;
P6DIR = 0;
P1OUT = 0;
P2OUT = 0;
P4OUT = 0x32;
P5OUT = 0x00;
}
void spi_write(long int data)
{
int i;
CS_LOW;
/* for(i=0;i<8;i++)
{
SDA_LOW;
SCLK_LOW;
usDelay(20);
SCLK_HIGH;
}
SCLK_LOW;*/
data <<= 6;
for(i=24;i>0;i--)
{
SCLK_LOW;
if(data & 0x800000)
SDA_HIGH;
else
SDA_LOW;
data <<= 1;
SCLK_HIGH;
usDelay(1);
}
SCLK_LOW;
usDelay(1);
CS_HIGH;
}
void rsave(void)
{
int i,j;
SCLK_LOW;
CS_LOW;
j = 0x20;
for(i=8;i>0;i--)
{
if(j & 0x80)
SDA_HIGH;
else
SDA_LOW;
j <<= 1;
SCLK_LOW;
SCLK_HIGH;
}
SCLK_LOW;
usDelay(1);
CS_HIGH;
usDelay(12000);
}
void r_read(void)
{
int i,j;
SCLK_LOW;
CS_LOW;
j = 0x30;
for(i=8;i>0;i--)
{
if(j & 0x80)
SDA_HIGH;
else
SDA_LOW;
j <<= 1;
SCLK_LOW;
SCLK_HIGH;
}
SCLK_LOW;
CS_HIGH;
usDelay(12000);
}
int main( void )
{
WDTCTL = WDTPW + WDTHOLD;// Stop watchdog timer to prevent time out reset
InitOsc();
InitPort();
spi_write(800);
rsave();
//r_read();
while(1);
}
举报