Question
ONLY JUST ANSWER THE FOLLOWING QUESTIONS FROM OBSERVATION OF THE CCS CODE I HAVE WRITTEN BELOW. -HOW WAS THE SINE WAVE GENERATED FROM THE CODE
ONLY JUST ANSWER THE FOLLOWING QUESTIONS FROM OBSERVATION OF THE CCS CODE I HAVE WRITTEN BELOW.
-HOW WAS THE SINE WAVE GENERATED FROM THE CODE BELOW?
-WHY DO WE NEED THE SECOND 32 BIT TIMER INTERRUPT AND HOW DOES IT WORK WITH THE DAC?
-WHERE DOES THE CODE PASS THE ADC CONTROL INFORMATION INTO THE DATA THAT IS SENT THROUGH SPI?
-FOR THE SPI MASK LOGIC OPERATION WHY DO WE SET MCP4921_SPI_CTRL_MASK TO 0X0FFF AND SET MCP4921_SPI_CTRL_MASK TO 0X7000?
#include
#include
#include
#include
#include
#include
#define CLOCK_HF 48000000 //48MHz
#define CLOCK_LF 32000 //32kHz
#define TIMER0_FREQ 1 //unit: Hz
#define SAMPLE_FREQ 48e3 //Frequency in Hz, common sampling rates for digital
audio: 8000, 32000, 44100, 48000
#define SIGNAL_FREQ 1000 //Signal frequency in Hz.
#define SIGNAL_LEN ((uint32_t)(SAMPLE_FREQ/SIGNAL_FREQ)) //period of discrete-
time signal; needs to be an integer
#define MCP4921_SPI_BITRATE 20e6 //needs to > SAMPLE_FREQ*16, and < SMCLK/2, max
20M
#define MCP4921_SPI_DATA_MASK (0x0FFF) //See datasheet, MCP4921
#define MCP4921_SPI_CTRL_MASK (0x7000) //See datasheet, MCP4921
#define MATH_PI 3.14159265358979323846
#define RED_LED GPIO_PIN0
#define GREEN_LED GPIO_PIN1
#define BLUE_LED GPIO_PIN2
#define NUM_DISP_TEXT_LINE 4
#define MAX_STR_BUFFER_LEN 100
//Function prototypes
void initDevice_HFXT(void);
void initGPIO(void);
void initTimer(void);
void initUART(void);
void initSPI(void);
void initSignalTable_sine(void);
void initSignalTable_square(void);
void uart0_transmitStr(const char *str);
//Global variables
uint32_t clockMCLK, clockSMCLK;
uint8_t currentLED = RED_LED;
char strBuffer[MAX_STR_BUFFER_LEN];
const char *terminalDisplayText[NUM_DISP_TEXT_LINE] =
{
" ",
"UART and DAC Demo ",
"R: Red, G: Green, B: Blue, H: Help ",
"> "
};
uint16_t signalTable[SIGNAL_LEN], signalIndex=0;
void main(void)
{
uint32_t i;
uint8_t data;
initDevice_HFXT();
initGPIO();
initTimer();
initUART();
initSPI();
initSignalTable_sine();
//initSignalTable_square();
Interrupt_enableMaster();
Timer32_startTimer(TIMER32_0_BASE, false);
Timer32_startTimer(TIMER32_1_BASE, false);
// Initial display on terminal.
for(i=0; i { uart0_transmitStr(terminalDisplayText[i]); } while(1) { if(UART_getInterruptStatus(EUSCI_A0_BASE, EUSCI_A_UART_RECEIVE_INTERRUPT_FLAG)) { data = UART_receiveData(EUSCI_A0_BASE); UART_clearInterruptFlag(EUSCI_A0_BASE, EUSCI_A_UART_RECEIVE_INTERRUPT_FLAG); switch(data) { case 'R': case 'r': currentLED = RED_LED; uart0_transmitStr("Blink red LED. > "); break; case 'G': case 'g': currentLED = GREEN_LED; uart0_transmitStr("Blink green LED. > "); break; case 'B': case 'b': currentLED = BLUE_LED; uart0_transmitStr("Blink blue LED. > "); break; case 'H': case 'h': for(i=0; i { uart0_transmitStr(terminalDisplayText[i]); } break; } } //end of if } //end of while } void initDevice_HFXT(void) { WDT_A_holdTimer(); //Stop watchdog timer //Change VCORE to 1 to support a frequency higher than 24MHz. //See data sheet for Flash wait-state requirement for a given frequency. PCM_setPowerState(PCM_AM_DCDC_VCORE1); FlashCtl_setWaitState(FLASH_BANK0, 1); FlashCtl_setWaitState(FLASH_BANK1, 1); FPU_enableModule(); FPU_enableLazyStacking(); //Required to use FPU within ISR. //Configure PJ.2 and PJ.3 in HFXT mode. //Initialize external clock sources HFXT. GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_PJ, GPIO_PIN2|GPIO_PIN3, GPIO_PRIMARY_MODULE_FUNCTION); CS_setExternalClockSourceFrequency(CLOCK_LF, CLOCK_HF); CS_startHFXT(false); CS_initClockSignal(CS_MCLK, CS_HFXTCLK_SELECT, CS_CLOCK_DIVIDER_1); CS_initClockSignal(CS_HSMCLK, CS_HFXTCLK_SELECT, CS_CLOCK_DIVIDER_16); CS_initClockSignal(CS_SMCLK, CS_HFXTCLK_SELECT, CS_CLOCK_DIVIDER_1); //48MHz to support SPI clockMCLK = CS_getMCLK(); clockSMCLK = CS_getSMCLK(); } void initGPIO(void) { //Configure P2.0, P2.1, P2.2 as output. //P2.0, P2.1, P2.2 are connected to a RGB tri-color LED on LaunchPad. GPIO_setAsOutputPin(GPIO_PORT_P2, GPIO_PIN0|GPIO_PIN1|GPIO_PIN2); } void initTimer(void) { // TIMER32_0 to blink heartbeat LED Timer32_initModule(TIMER32_0_BASE, TIMER32_PRESCALER_1, TIMER32_32BIT, TIMER32_PERIODIC_MODE); Timer32_setCount(TIMER32_0_BASE, clockMCLK/TIMER0_FREQ); Timer32_enableInterrupt(TIMER32_0_BASE); Interrupt_enableInterrupt(INT_T32_INT1); //Enable Timer32_0 interrupt in the interrupt controller. //TIMER32_1 to clock signal out to DAC at sample frequency Timer32_initModule(TIMER32_1_BASE, TIMER32_PRESCALER_1, TIMER32_32BIT, TIMER32_PERIODIC_MODE); Timer32_setCount(TIMER32_1_BASE, clockMCLK/SAMPLE_FREQ); Timer32_enableInterrupt(TIMER32_1_BASE); Interrupt_enableInterrupt(INT_T32_INT2); //Enable Timer32_1 interrupt in the interrupt controller. } void initUART(void) { //Configuration for 48MHz SMCLK, 115200 baud rate. //Calculated using the online calculator that TI provides at: //http://software-dl.ti.com/msp430/msp430_public_sw/mcu/msp430/MSP430BaudRateConver ter/index.html const eUSCI_UART_Config config = { EUSCI_A_UART_CLOCKSOURCE_SMCLK, //SMCLK Clock Source, 48MHz 26, //BRDIV 0, //UCxBRF 111, //UCxBRS EUSCI_A_UART_NO_PARITY, //No Parity EUSCI_A_UART_LSB_FIRST, //MSB First EUSCI_A_UART_ONE_STOP_BIT, //One stop bit EUSCI_A_UART_MODE, //UART mode EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION //Oversampling }; // Configure GPIO pins for UART. RX: P1.2, TX:P1.3. GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P1, GPIO_PIN2|GPIO_PIN3, GPIO_PRIMARY_MODULE_FUNCTION); UART_initModule(EUSCI_A0_BASE, &config); UART_enableModule(EUSCI_A0_BASE); } void initSPI(void) { const eUSCI_SPI_MasterConfig spiMasterConfig = { EUSCI_B_SPI_CLOCKSOURCE_SMCLK, //SMCLK Clock Source clockSMCLK, //Clock source frequency MCP4921_SPI_BITRATE, //desiredSpiClock EUSCI_B_SPI_MSB_FIRST, //MSB First EUSCI_B_SPI_PHASE_DATA_CAPTURED_ONFIRST_CHANGED_ON_NEXT, //Phase EUSCI_B_SPI_CLOCKPOLARITY_INACTIVITY_LOW, //LOW polarity EUSCI_B_SPI_3PIN //3Wire SPI Mode }; //P1.5: SCK, P1.6: SDI, P5.2: CS GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P1, GPIO_PIN5|GPIO_PIN6, GPIO_PRIMARY_MODULE_FUNCTION); GPIO_setAsOutputPin(GPIO_PORT_P5, GPIO_PIN2); GPIO_setOutputHighOnPin(GPIO_PORT_P5, GPIO_PIN2); //Configuring SPI in 3wire master mode SPI_initMaster(EUSCI_B0_BASE, &spiMasterConfig); //Enable SPI module SPI_enableModule(EUSCI_B0_BASE); } //Initialize the lookup table that is used to generate sine wave signal. void initSignalTable_sine(void) { uint32_t i; float w; w = 2*MATH_PI*SIGNAL_FREQ/SAMPLE_FREQ; for(i=0; i { signalTable[i] = (0.5*(1 + sinf(w*i)))*0x0FFF; signalTable[i] = MCP4921_SPI_CTRL_MASK | (MCP4921_SPI_DATA_MASK & signalTable[i]); } } //Initialize the lookup table that is used to generate square wave signal. void initSignalTable_square(void) { uint32_t i; for(i=0; i { if(i signalTable[i] = 0x0000; else signalTable[i] = 0x0FFF; signalTable[i] = MCP4921_SPI_CTRL_MASK | (MCP4921_SPI_DATA_MASK & signalTable[i]); } } //Transmit a string through UART0. void uart0_transmitStr(const char *str) { uint32_t len, i=0; len = strlen(str); while(i < len) { UART_transmitData(EUSCI_A0_BASE, str[i++]); while(!UART_getInterruptStatus(EUSCI_A0_BASE, EUSCI_A_UART_TRANSMIT_COMPLETE_INTERRUPT_FLAG)); UART_clearInterruptFlag(EUSCI_A0_BASE, EUSCI_A_UART_TRANSMIT_COMPLETE_INTERRUPT_FLAG); } } //Timer32_0 ISR void T32_INT1_IRQHandler(void) { Timer32_clearInterruptFlag(TIMER32_0_BASE); if(GPIO_getInputPinValue(GPIO_PORT_P2, GPIO_PIN0|GPIO_PIN1|GPIO_PIN2)) { GPIO_setOutputLowOnPin(GPIO_PORT_P2, GPIO_PIN0|GPIO_PIN1|GPIO_PIN2); } else { GPIO_setOutputHighOnPin(GPIO_PORT_P2, currentLED); } } //Timer32_1 ISR void T32_INT2_IRQHandler(void) { uint32_t i; uint8_t b1=(signalTable[signalIndex]>>8), b0=signalTable[signalIndex]; Timer32_clearInterruptFlag(TIMER32_1_BASE); //SPI is written with register-level operations. P5->OUT &= ~BIT2; //Set CS to LOW on SPI slave. for(i=0; i<2; i++); //delay at least 40ns, t_CSSR. EUSCI_B0->TXBUF = b1; //Transmit Byte 1 while(!(EUSCI_B0->IFG & EUSCI_B_IFG_TXIFG)); //Check if TX buffer is ready, no need to clear the flag TXIFG. EUSCI_B0->TXBUF = b0; //Transmit Byte 0 while(!(EUSCI_B0->IFG & EUSCI_B_IFG_TXIFG)); //Check if TX buffer is ready, no need to clear the flag TXIFG. //LDAC tied to GND, so transfer on rising edge of CS. P5->OUT |= BIT2; //Set CS to HIGH on SPI slave. //Update signalIndex signalIndex++; if(signalIndex >= SIGNAL_LEN) { signalIndex = 0; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started