System Implementation
When you have finished designing your schematic and have compiled your project, you are ready to translate your design into microcontroller code, following these basic steps:
- Obtain parameters and addresses from SigmaStudio (export the .params, .hex, and .h files).
- Parse the file with parameter information.
- Incorporate (integrate) into microcontroller code.
To access the parameters and addresses from SigmaStudio, you must have already compiled your project and exported the parameter files. After doing that, you'll have two parameter files:
- yourfilename.hex - hex values of parameters to be written to DSP.
- yourfilename.params - detailed file with addresses and values of all parameters.
You can view sample *.params and *.hex files in the Export Program and Parameters topic.
Following is an example of how you can integrate the parameters into microcontroller code (AD1940). The highlighted functions in the main function are also defined:






~~~~ Click here to view the above source code as plain text |
#include "prog_data.h"
#include "param_data.h"
#include "Design_IC 1.h" // *** This file is generated by SigmaStudio ***
#include "Design_IC 1_REG.h" // *** This file is generated by SigmaStudio ***
#include "comms.h"
#define byte unsigned char
// Microcontroller Code MAIN program
int main(void)
{
// Setup SPI port this is hardware-specific
SpiInit();
Mute();
// Write a program to DSP
SpiWrite(PROG_START_ADR, PROGRAM_DATA, PROG_LENGTH*6);
// Wait some time
Wait(...);
// Write a parameter file
SpiWrite(PARAM_START_ADR, PARAM_DATA, PARAM_LENGTH*4);
Wait(...);
UnMute();
// Write a single value (0.242) to specified address
// The address, MODULE_Main_Single1_VALUE, is defined in the
// SigmaStudio generated header file (Design_IC 1.h).
ParamWrite(MODULE_Main_Single1_VALUE, 0.242);
// Example safeload of single parameter, will safeload 1.999 in 5.23
// format to address 2
// This function includes the initiate safeload to parameter RAM
SafeloadSingleParamWrite(2, 1.999);
// Example of a multiple parameter safeload (i.e. filter coefficients)
// - loads the data to each of the five safeload locations,
// - then initiates the safeload
SafeloadMultipleParamWrite(MODULE_Main_MidEQ1_Alg0_Stage0_B0_ADDRESS, 1.999, 0);
SafeloadMultipleParamWrite(MODULE_Main_MidEQ1_Alg0_Stage0_B1_ADDRESS, -.988, 1);
SafeloadMultipleParamWrite(MODULE_Main_MidEQ1_Alg0_Stage0_B2_ADDRESS, 1.999, 2);
SafeloadMultipleParamWrite(MODULE_Main_MidEQ1_Alg0_Stage0_A1_ADDRESS, -.999888, 3);
SafeloadMultipleParamWrite(MODULE_Main_MidEQ1_Alg0_Stage0_A2_ADDRESS, 1.999, 4);
InitSafeloadParam();
return 0;
}
// Microcontroller Code Wait function
void Wait(float time) //time to wait in milliseconds
{
long cycles;
cycles = time / 1.422e-3; //assuming 45 MHz uC core clock speed
while (cycles != 0)
{
--cycles;
}
}
// Microcontroller Code Mute function
void Mute()
{
byte corecontrol[2] = {0x00, 0x00};
//Write core control register
SpiWrite(CORE_CONTROL_REG, corecontrol, 2);
//Wait 20us
Wait(20);
}
// Microcontroller Code UnMute function
void UnMute()
{
byte corecontrol[2] = {0x02, 0x00};
//Write core control register
SpiWrite(CORE_CONTROL_REG, corecontrol, 2);
//Wait 20us
Wait(20);
}
// Microcontroller SpiWrite function
// The function takes the start address to be written to, an array of
// data, and the length of the array (in bytes).
void SpiWrite(short address, unsigned char* data, int length)
{
int i = 0;
byte address_hi = 0; // register/RAM address high byte
byte address_lo = 0; // register/RAM address lo byte
address_lo = (byte)address; // get low byte of register/RAM address
address_hi = (byte)(address>>8); // get high byte of address shift left by 8 bits
// Fill in your write function here
SPI_TX_REG = 0x00; // Write to DSP at chip address 0
SPI_TX_REG = address_hi; // Write high address byte
SPI_TX_REG = address_lo; // Write lo address byte
for (i=0; i>8);
//Fill in you write function here
SPI_RX_REG = 0x01; //Write to DSP and set RW bit to 1
SPI_RX_REG = address_hi; //Write high address byte
SPI_RX_REG = address_lo; //Write lo address byte
for (i=0; i>8);
byte param_addr_lo;
byte param_addr_hi;
//get low byte of parameter address
param_addr_lo=(byte)param_addr;
//get high byte of address shift left by 8 bits
param_addr_hi=(byte)(param_addr>>8);
SPI_TX_REG = 0x00; // Write to DSP at chip address 0
SPI_TX_REG = safe_addr_hi; //Write high byte of safeload address
SPI_TX_REG = safe_addr_lo; //Write low byte of safeload address
SPI_TX_REG = param_addr_hi; //Write high byte of parameter address
SPI_TX_REG = param_addr_lo; //Write low byte of parameter address
}
//Microcontroller Code - functions, Safeload: SafeParam
void SafeParam(short safe_param, unsigned char* param_data)
{
byte safe_param_lo;
byte safe_param_hi;
//get low byte of register/RAM address
safe_param_lo=(byte)safe_param;
//get high byte of address shift left by 8 bits
safe_param_hi=(byte)(safe_param>>8);
SPI_TX_REG = 0x00; //Write to DSP at chip address 0
SPI_TX_REG = safe_param_hi; //Write high byte of Safeload address
SPI_TX_REG = safe_param_lo; //Write low byte of Safeload address
SPI_TX_REG = param_data[4]; //Write parameter byte 4 (MSBs)
SPI_TX_REG = param_data[3]; //Write parameter byte 3
SPI_TX_REG = param_data[2]; //Write parameter byte 2
SPI_TX_REG = param_data[1]; //Write parameter byte 1
SPI_TX_REG = param_data[0]; //Write parameter byte 1 (LSBs)
}
// Note: SafeParam and SafeAddr must be executed together.
// Microcontroller code - functions: convert a float number to 5.23 format
void To523(float param_dec, byte* param_hex)
{
long param223;
long param227;
//multiply decimal number by 2^23
param223 = param_dec * (1 << 23);
//convert to positive binary
param227 = param223 + (1 << 27);
param_hex[3]=(byte)param227; //get byte 3 (LSBs) of parameter value
param_hex[2]=(byte)(param227>>8); //get byte 2 of parameter value
param_hex[1]=(byte)(param227>>16); //get byte 1 of parameter value
//get byte 0 (MSBs) of parameter value
param_hex[0]=(byte)(param227>>24);
//invert sign bit to get correct sign
param_hex[0] = param_hex[0] ^ 0x08;
}
~~~~