User Tools

Site Tools


brainmsp430

This is an old revision of the document!


#include <msp430g2553.h>

/*
 * main.c
 */
volatile unsigned long doublemillis = 0;
volatile int j = 0;
volatile int brainFq[] = {
  15, //1
  14, //2
  13, //3
  12, //4
  11, //5
  10, //6
  9,  //7
  8,  //8
  7,  //9
  6, //10
  5, //11
  4, //12
  7, //13
  6, //14
  5, //15
  4, //16
  6, //17
  5, //18
  4, //19
  5, //20
  6, //21
  7, //22
  8, //23
  9, //24
  10, //25
  11, //26
  12, //27
  13, //28
  14, //29
  15, //30
};
volatile int brainDuration[] = {
  20, //1
  20, //2
  20, //3
  20, //4
  20, //5
  20, //6
  20, //7
  30, //8
  30, //9
  30, //10
  30, //11
  40, //12
  5, //13
  10, //14
  20, //15
  60, //16
  5, //17
  10, //18
  30, //19
  5, //20
  5, //21
  5, //22
  5, //23
  5, //24
  5, //25
  5, //26
  5, //27
  5, //28
  5, //29
  25, //30
};
volatile int ArrayLength = 30;
volatile int on = 0;
// Declaration of functions
int FreqTime(int Hz);
int FreqCycle(int Hz , unsigned long speed);
unsigned long millis();
int blinkms(int Millis);
int CountByTime(int duration, int ArrayLength);
// MAIN program
int main(void) {
	// Set Watchdog as timer with an interrupt every 0.5ms
	WDTCTL = WDT_MDLY_0_5; // (WDTPW+WDTTMSEL+WDTCNTCL+WDTIS1) /* 0.5ms
    P1DIR |= 0x0F; // Set P1.0 - P1.3 as Output
 	P1OUT &= 0xF0; // Set P1.0 - P1.3 to 0
 	// Timer_A
	TA0CCR0 = 1250; // Count limit for 400Hz at !MHz (1000000 / 400 / 2)
	TA0CCTL0 = 0x10;  // Enable Timer A0 interrupts, bit 4=1
	TA0CTL = TASSEL_2 + MC_1; // Timer A0 with SMCLK, count UP
	TA1CCR0 = 1250; // Count limit for 400Hz predefinitoin, will be overwriten later
	TA1CCTL0 = 0x10;  // Enable Timer A0 interrupts, bit 4=1
	TA1CTL = TASSEL_2 + MC_1; // Timer A0 with SMCLK, count UP
// Endless loop
 	while(1){
 		j = CountByTime(brainDuration[j], ArrayLength);
 		on = blinkms(FreqTime(brainFq[j]));
 		TA1CCR0 = FreqCycle(brainFq[j]+400, 1000000);
 		if (on){
 			P1OUT |= 0x03;
 		}
 		else{
 			P1OUT &= 0xFD;
 		}
 	}
 	return 0;
}

// Functions

// blink in the rhythm of given ms
int blinkms(int Millis){
	static unsigned long PreviousMillis = 0;
	static int back = 0;
	unsigned long CurrentMillis = millis();
	if (CurrentMillis - PreviousMillis >= Millis){
		PreviousMillis = CurrentMillis;
		back ^= 0x01;
	}
	return back;
}
// count 1 up, if time in ms done
int CountByTime(int duration, int ArrayLength){
	static unsigned long PreviousMillis = 0;
	static int back = 0;
	unsigned long Millis = duration * 1000;
	unsigned long CurrentMillis = millis();
	if (CurrentMillis - PreviousMillis >= Millis){
		PreviousMillis = CurrentMillis;
		if (back < ArrayLength) back++;
	}
	return back;
}

// Convert frequency to ms
int FreqTime(int Hz){
	int v = 1000 / Hz / 2;
	return v;
}
// Convert frequency to Cycles by given Clockspeed
int FreqCycle(int Hz, unsigned long speed){
	int v = speed / Hz / 2;
	return v;
}

// Function that is reuturning ms since startup
unsigned long millis(){
	return doublemillis >> 1; // Double Millisecondy divided by 2
}

// -------------------------- Interrupts ------------------------------------------
#pragma vector=WDT_VECTOR
__interrupt void WDT(void){
		doublemillis++;
}

#pragma vector=TIMER0_A0_VECTOR    // Timer0 A0 interrupt service routine
__interrupt void Timer0_A0 (void) {
   P1OUT ^= 0x04;                  // Toggle output P1.2
}

#pragma vector=TIMER0_A1_VECTOR    // Timer1 A0 interrupt service routine
__interrupt void Timer1_A0 (void) {
   P1OUT ^= 0x08;                  // Toggle output P1.3
}
brainmsp430.1414956057.txt.gz · Last modified: 2014/11/02 19:20 by admin

Except where otherwise noted, content on this wiki is licensed under the following license: CC0 1.0 Universal
CC0 1.0 Universal Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki