📄 dac8831.c
字号:
/****************************************************************/
/* FILENAME: main.c */
/* DESCRIPTION: This program uses the MSP430F449 to write 256 */
/* samples to the DAC8531 EVM board. */
/* The samples are generated by sine/cosine functions */
/* AUTHOR: T. Hendrick, Data Acquisition Products, */
/* Dallas Texas */
/* CREATED 2005(C) BY TEXAS INSTRUMENTS INCORPORATED. */
/* VERSION: 01 */
/* Compiled on IAR Embedded Workbench V 3.0B; IAR C-Compiler */
/* for MSP430 V2.21B-P1/W32 [Kickstart] */
/****************************************************************/
#include <msp430x44x.h>
#include <math.h>
#include "SBLCDA2.h"
/**************** Function prototypes ***********************************/
/* */
void init_sys(void); /* MSP430 Initialisation routine */
void setupClock(int);
void delay(int); /* Software delay */
void dac_convert(int); /* Do the adc conversion */
void display(void); /* Indicate Program complete */
void InitializeLCD( void );
void clearDisplay( void );
void sortMajor( unsigned int );
void displaySpecial( long int );
void displayMinor( int, int );
void displayHPA449(void);
void displayTest(void);
void InitTables(int);
/************************************************************************/
/* */
/* HPA449 variable declarations */
/* */
/************************************************************************/
#define DAC_CS 0x20 //p3.5
#define Resonator (0)
/************************************************************************/
/* */
/* main() variable declarations */
/* */
/************************************************************************/
#define BLOCK_SZ 256/* size of data buffer */
#define TABLE_SIZE 256
int sinetable[TABLE_SIZE], costable[TABLE_SIZE], byte0, byte1;
int i, y;
/******************************************************************************\
* Function: InitTables()
* Description: Initializes Sine and Cosine Values
\******************************************************************************/
void main(void)
{
setupClock(Resonator);
init_sys(); // Initialise the MSP430
InitializeLCD();
clearDisplay();
displaySpecial(SoftBaugh|AL|AR|AU|AD);
InitTables(4);
displayTest();
displaySpecial(SoftBaugh|AR);
do {
for(i=0; i<BLOCK_SZ; i++)
{
dac_convert(sinetable[i]); // Send sine values to the DAC
}
}
while (1); // Endless Loop
}
void InitTables(int period)
{
int y;
for (y = 0; y < TABLE_SIZE; y++)
{
sinetable[y] = (unsigned int)((sin(2 * 3.141593659 * period * y / TABLE_SIZE) + 1.0) * 32767);
costable[y] = (unsigned int)((cos(2 * 3.141593659 * period * y / TABLE_SIZE) + 1.0) * 32767);
}
}
/************************************************************/
/* Prototype - init_sys */
/* */
/* Description */
/* This prototype initialises the MSP430F149 */
/************************************************************/
void init_sys(void)
{
void setupports(void); /* Local function prototype */
void setupSPI(void); /* Local function prototype */
setupports();
setupSPI();
_EINT(); // Enable interrupts
}
/************************************************************/
/* Prototype - setupClock */
/* */
/* Description */
/* This prototype sets-up the XT2 oscillator and tests */
/* that it has settled before moving on */
/************************************************************/
void setupClock (int RES)
{
switch (RES)
{
case 0:
{ WDTCTL = WDTPW + WDTHOLD;
/* D=2, N=121, no mod */
SCFQCTL=121;
// SCFI0 = 0100 0100
SCFI0=0x44;
// DCOPLUS=1, XTS_FLL=0
// XCAPXPF=00
FLL_CTL0=0x80;
// SMCLK=0, XT2OFF=1, SELMX=00, SELS=0, FLL_DIVX=10
// FLL_CTL1= 0010 0010
FLL_CTL1=0x22;
}
break;
case 1:
{ WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
FLL_CTL0&=XT2OFF; // Switch on the XT2 osc.
FLL_CTL1|=SELM_XT2+SELS; // Select XT2 osc. for
// Test the osc. flag bit
do {
IFG1 &= ~OFIFG; // Clear the OFIFG bit
} while (OFIFG&IFG1); //
}
break;
}
}
/************************************************************/
/* Prototype - setupports */
/* Description */
/* This prototype sets-up the GPIO ports as appropriate */
/************************************************************/
void setupports (void)
{
// SPI port for ADC
P3SEL = BIT3 + BIT2 + BIT1; // Bits 3, 2 & 1 are assigned as SPI specific pins
P3DIR = DAC_CS; // Set the ADC_CS bit as an output
P3OUT = DAC_CS; // De-assert ADC_CS for the adc - HIGH
P4SEL = BIT5 + BIT3 + BIT2;
//SetupInterrupts
P1IFG |= 0x00;
P1IES |= 0x40;
P1IE |= 0x40;
}
/************************************************************/
/* Prototype - setupSPI */
/* */
/* Description */
/* This prototype sets-up the P3 for communication via SPI */
/************************************************************/
void setupSPI (void)
/************************************************************/
/* System definitions */
/************************************************************/
#define SPI0en 0x40
#define SPI1en 0x10
{
ME1 |= USPIE0; // Module Enable - SPI1
U0CTL &= ~SWRST; // Make sure the RESET bit is off
U0CTL |= CHAR + SYNC + MM; // USART0 module operation
// CHAR = 1 => 8-bit data
// SYNC = 1 => SPI selected
// MM = 1 => master mode,
U0TCTL |= CKPH + SSEL0 + SSEL1 + STC; // USART0 Tranmit control register
U0BR0 = 0x02; // Divide SMCLK by 2 => transfer clock
U0BR1 = 0x00; //
U0MCTL = 0x00; // Modulation control - not used. Ensure
// all these bits are reset
}
/************************************************************/
/* Prototype - delay */
/* Description */
/* This prototype gives a delay of around 1 second */
/************************************************************/
void delay(int time)
{
unsigned int i;
for (i = 0; i < time; i++);
}
/************************************************************/
/* Prototype - convert */
/* Description */
/* This prototype does the DAC conversion */
/************************************************************/
void dac_convert (value)
{
byte0 = value >>8;
byte1 = value >>0;
P3OUT &= ~(DAC_CS); // Set DAC /CS low
while ((IFG1 & UTXIFG0) == 0);
U0TXBUF = byte0; // Send clocks to the DAC, this shifts 1st byte
// Wait until all 8 bits have been received.
while ((IFG1 & UTXIFG0) == 0);
U0TXBUF = byte1 ; // Send clocks to the DAC, this shifts 2nd byte
// Wait until all 8 bits have been received.
P3OUT |= (DAC_CS); // Set DAC /CS high
}
/************************************************************/
/* Prototype - displayTest */
/* Description */
/* This function displays "8831" on the LCD Screen */
/************************************************************/
void displayTest(void)
{
displayMinor(5,UL_EIGHT);
displayMinor(4,UL_EIGHT);
displayMinor(3,UR_THREE);
displayMinor(2,UR_ONE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -