#include <main.h>
#include <salvo.h>
#include <msp430x12x2.h>
#include "mytypes.h" // My own things...const UINT32 BoardID0 @ 0xE000 = 0x01234567;
const UINT32 BoardID1 @ 0xE004 = 0x89abcdef;
// declarations for functions used from ACB302_1.s43
extern void init_acb302(void);
extern void start_conv_acb302(void);
extern void ACB302_conv(void);
extern void ACB302_vektorbetrag(void);
#define TASK_ACB302_CONVERSION OSTCBP(1) // Task 1 definition
#define TASK_RESULTANT_VECTOR OSTCBP(2) // Task 2 definition
//#define TASK_DISPLAY_VIBRATIONS OSTCBP(3) // Task 3 definition
#define BINSEM1_P OSECBP(1) // Event 1: Binary Semaphore
#define PRIO_ACB302_CONVERSION 1 // Priority of Task 1
#define PRIO_RESULTANT_VECTOR 2 // Priority of Task 2
//#define PRIO_DISPLAY_VIBRATIONS 3 // Priority of Task 3
_OSLabel(acb302_conversion_label) // Task 1 label for context switch
_OSLabel(resultant_vector_label) // Task 2 label for context switch
//_OSLabel(display_vibrations_label) // Task 3 label for context switch
//////////////////////////////////////////////////////////////
// Name: Set_DCO
// Description: Sets the DCO and controls speed using TimerA SW FLL
// Parameter: -
// Returns: -
//////////////////////////////////////////////////////////////
void Set_DCO (void) // Set DCO to selected frequency
{
#define DELTA 977 // target DCO= DELTA*(4096) = 4Mhz
//#define DELTA 900 // target DCO = DELTA*(4096) = 3686400
//#define DELTA 256 // target DCO = DELTA*(4096) = 1048576
//#define DELTA 70 // target DCO = DELTA*(4096) = 286720
unsigned int Compare, Oldcapture = 0;
BCSCTL1 |= DIVA_3; // ACLK= LFXT1CLK/8
CCTL2 = CM_1 + CCIS_1 + CAP; // CAP, ACLK
TACTL = TASSEL_2 + MC_2 + TACLR; // SMCLK, cont-mode, clear
while (1)
{
while (!(CCIFG & CCTL2)); // Wait until capture occured
CCTL2 &= ~CCIFG; // Capture occured, clear flag
Compare = CCR2; // Get current captured SMCLK
Compare = Compare - Oldcapture; // SMCLK difference
Oldcapture = CCR2; // Save current captured SMCLK
if (DELTA == Compare) break; // If equal, leave "while(1)"
else if (DELTA < Compare) // DCO is too fast, slow it down
{
DCOCTL--;
if (DCOCTL == 0xFF)
{
if (!(BCSCTL1 == (XT2OFF + DIVA_3)))
BCSCTL1--; // Did DCO roll under?, Sel lower RSEL
}
}
else
{
DCOCTL++;
if (DCOCTL == 0x00)
{
if (!(BCSCTL1 == (XT2OFF + DIVA_3 + 0x07)))
BCSCTL1++; // Did DCO roll over? Sel higher RSEL
}
}
}
CCTL2 = 0; // Stop CCR2
TACTL = 0; // Stop Timer_A
BCSCTL1 &= ~0x30; // Set ACLK Divider to 0
BCSCTL2 = 0; // MCLK = SMCLK = DCOCLK
}//Set_DCO
void acb302_conversion(void)
{
start_conv_acb302(); // Starts ADC10 Conversion
ACB302_conv(); // Starts conversions
OSSignalBinSem(BINSEM1_P);
OS_Delay(2,acb302_conversion_label); // delays the task by 2*10ms= 20ms(50Hz)
}
void resultant_vector(void)
{
ACB302_vektorbetrag(); // calculates the resultant vector
P1OUT ^= 0x01; // Toggle P1.0 using XOR
OS_WaitBinSem(BINSEM1_P, OSNO_TIMEOUT, resultant_vector_label);
}
//void display_vibrations(void)
//{
//}
//-----------------------------------------------------------------------------------------
void main()
//-----------------------------------------------------------------------------------------
{
Init(); // For Device Initialization
OSInit(); // Initializes all of Salvo's data structures,
// pointers, and counters.
InitPORT(); // Port Initialization
WDTCTL=WDTPW+WDTHOLD; // stop WD
P1OUT = 0x00; // Set P1 to output direction
P1DIR = 0x03;
Set_DCO();
P2DIR |= 0x08; // Turn on Sensor switch
P2OUT |= 0x08;
init_acb302(); // Initializes ACB302 and Enables adc10_irq interrupt function
// which reads x,y and z axes.
// Create Tasks
OSCreateTask(acb302_conversion, TASK_ACB302_CONVERSION, PRIO_ACB302_CONVERSION);
OSCreateTask(resultant_vector, TASK_RESULTANT_VECTOR, PRIO_RESULTANT_VECTOR);
// OSCreateTask(display_vibrations, TASK_DISPLAY_VIBRATIONS,PRIO_DISPLAY_VIBRATIONS);
OSCreateBinSem(BINSEM1_P, 0); // Creates Binary Semaphore
OSEi(); // Enables Interrupts
for(; ;)
OSSched(); // Multitasking
}
// Interrupt Vecator for Timer
// 10ms System Timer at a frequency of 100Hz
#pragma vector=TIMERA0_VECTOR
__interrupt void Timer_A (void)
{
CCR0 += 10000;
OSTimer();
}