brainmsp430
This is an old revision of the document!
#include <msp430.h> /* * main.c */ volatile unsigned long doublemillis = 0; volatile int j = 0; volatile int brainFq[] = { 15, 12, 14, 11, 10, 9, 8, 7, 6, 5, 4, 7, 6, 5, 4, 7, 6, 5, 4, 5, 6, 7, 8, 9, 12, 10, 11, 12, 15, 12, }; volatile int brainDuration[] = { 10, 5, 15, 10, 10, 10, 10, 12, 5, 4, 15, 5, 5, 5, 10, 15, 5, 5, 5, 5, 5, 5, 10, 10, 10, 5, 5, 15, 5, 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 BCSCTL1 = CALBC1_1MHZ; DCOCTL = CALDCO_1MHZ; WDTCTL = WDT_MDLY_0_5; // (WDTPW+WDTTMSEL+WDTCNTCL+WDTIS1) /* 0.5ms IFG1 &= ~WDTIFG; IE1 |= WDTIE; // Enable WDT interrupt 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 A1 with SMCLK, count UP _EINT(); //enable interrupts // Endless loop while(1){ j = CountByTime(brainDuration[j], ArrayLength); if (j < ArrayLength){ on = blinkms(FreqTime(brainFq[j])); TA1CCR0 = FreqCycle(brainFq[j]+400, 1000000); if (on){ P1OUT |= 0x03; } else{ P1OUT &= 0xFC; } } else{ P1OUT &= 0x00; P1OUT &= 0x00; _BIS_SR(LPM3_bits); } } 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.1452166076.txt.gz · Last modified: 2016/01/07 11:27 by admin