-
Notifications
You must be signed in to change notification settings - Fork 0
Cpp
Alexander Bhandari edited this page Jan 31, 2016
·
4 revisions
TI example code is written in C but supports C++. All user code generated will be organized using classes. An example follows.
/* Include Files */
#include "sys_common.h"
/* USER CODE BEGIN (1) */
#include "main.h"
/* External Functions */
extern void sciSend_32bitdata(sciBASE_t *sci, unsigned int data);
extern void adcStartConversion_selChn(adcBASE_t *adc, unsigned channel, unsigned fifo_size, unsigned group);
extern void adcGetSingleData(adcBASE_t *adc, unsigned group, adcData_t *data);
/* USER CODE END */
/** @fn void main(void)
* @brief Application main function
* @note This function is empty by default.
*
* This function is called after startup.
* The user can use this function to implement the application.
*/
/* USER CODE BEGIN (2) */
class ADC {
int width, height;
public:
uint16_t Read_Channel(uint16_t channel);
void Read_All_Channels(uint16_t* output);
int area (void);
} rect;
uint16_t ADC::Read_Channel(uint16_t channel)
{
adcData_t adc_data[16];
adcData_t *adc_data_ptr = &adc_data;
/** - Start Group1 ADC Conversion
* Select Channel
*/
adcStartConversion_selChn(adcREG1, channel, 1, adcGROUP1);
/** - Wait for ADC Group1 conversion to complete */
while(!adcIsConversionComplete(adcREG1, adcGROUP1));
/** - Read the conversion result
* The data contains the Ambient Light sensor data
*/
adcGetSingleData(adcREG1, adcGROUP1, adc_data_ptr);
return(adc_data_ptr->value);
}
void ADC::Read_All_Channels(uint16_t* output) {
for(int i=0;i<NUM_CHANNELS;i++) {
output[i] = Read_Channel(i);
}
}
void ADC::wait(uint32_t time)
{
while(time--);
}
int ADC::sci_printf(const char *_format, ...)
{
char str[128];
int length = -1;
va_list argList;
va_start( argList, _format );
length = vsnprintf(str, sizeof(str), _format, argList);
va_end( argList );
if (length > 0)
{
sciSend(scilinREG, (unsigned)length, (unsigned char*)str);
}
return length;
}
/* USER CODE END */
void main(void)
{
/* USER CODE BEGIN (3) */
int i; int temp;
gioInit();
rect.set_values(5,10);
temp = rect.area();
if(temp == 50)
{
asm(" nop");
//My C++ code works!!
}
else
{
asm(" nop");
// I don't know....:(
}
//Configure GIOA2 as Output Pin
gioSetDirection(gioPORTA, 0x04);
// Loop to Toggle GIOA2 pin
while(1)
{
//Set GIOA2 pin to 1
gioSetBit(gioPORTA, 2, 1);
// Simple Delay
for(i=0;i<1000000;i++);
//Set GIOA2 pin to 0
gioSetBit(gioPORTA, 2, 0);
// Simple Delay
for(i=0;i<1000000;i++);
}
/* USER CODE END */
}