User Tools

Site Tools


brainmsp430

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
Last revisionBoth sides next revision
brainmsp430 [2014/11/02 19:20] – created adminbrainmsp430 [2019/08/06 15:10] admin
Line 1: Line 1:
 <code> <code>
-#include <msp430g2553.h>+#include <msp430.h>
  
 /* /*
Line 6: Line 6:
  */  */
 volatile unsigned long doublemillis = 0; volatile unsigned long doublemillis = 0;
-volatile int j = 0; +volatile unsigned int j = 0; 
-volatile int brainFq[] = { +const unsigned int brainFq[8][63] = { 
-  15, //1 +{1,15,14,13,12,11,10,9,8,7,6,5,4,7,6,5,4,6,5,4,5,6,7,8,9,10,11,12,13,14,15,}, 
-  14, //2 +{1,14,13,15,12,11,14,11,10,9,11,9,8,7,10,7,6,5,7,6,8,5,8,7,9,11,10,12,}, 
-  13, //3 +{1,15,14,13,12,14,11,10,9,8,11,10,9,8,9,8,9,10,8,10,9,8,9,10,11,12,13,10,15,10,11,10,11,12}, 
-  12, //4 +{1,12,14,11,13,12,10,9,8,7,6,5,4,8,7,6,5,4,7,6,5,4,7,6,5,4,5,6,7,8,9,12,10,11,12,15,12,15,12,}, 
-  11, //5 +{1,12,14,11,13,12,10,9,8,7,6,5,4,8,7,6,5,4,7,6,5,4,7,6,5,4,5,6,7,8,9,12,10,11,12,15,12,15,12,}, 
-  10, //+{1,12,14,11,13,12,10,9,8,7,6,5,4,8,7,6,5,4,7,6,5,4,7,6,5,4,5,6,7,8,9,12,10,11,12,15,12,15,12,}, 
-  9,  //7 +{1,12,14,11,13,12,10,9,8,7,6,5,4,8,7,6,5,4,7,6,5,4,7,6,5,4,5,6,7,8,9,12,10,11,12,15,12,15,12,}, 
-  8,  //8 +{1,10,1,1,1,1,1,1,1,1,1,1,1,1,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,}
-  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[] = { +const unsigned int brainDuration[8][63] = { 
-  20, //1 +{0,20,20,20,20,20,20,20,30,30,30,30,40,5,10,20,40,5,10,30,5,5,5,5,5,5,5,5,5,5,25,}, 
-  20, //2 +{1,15,15,5,15,15,5,15,15,15,5,15,15,15,3,15,10,10,5,10,5,10,5,10,10,5,15,15,}, 
-  20, //3 +{2,15,15,15,15,10,20,20,20,20,5,5,10,10,10,10,10,10,10,5,10,10,10,20,20,20,20,10,15,10,10,10,10,30,}, 
-  20, //4 +{3,5,15,10,5,10,8,10,10,12,5,4,15,5,5,10,10,10,5,5,5,10,15,5,5,5,5,5,5,10,10,10,5,5,15,5,10,5,25,}, 
-  20, //5 +{4,5,15,10,5,10,8,10,10,12,5,4,15,5,5,10,10,10,5,5,5,10,15,5,5,5,5,5,5,10,10,10,5,5,15,5,10,5,25,}, 
-  20, //6 +{5,5,15,10,5,10,8,10,10,12,5,4,15,5,5,10,10,10,5,5,5,10,15,5,5,5,5,5,5,10,10,10,5,5,15,5,10,5,25,}, 
-  20, //7 +{6,5,15,10,5,10,8,10,10,12,5,4,15,5,5,10,10,10,5,5,5,10,15,5,5,5,5,5,5,10,10,10,5,5,15,5,10,5,25,}, 
-  30, //8 +{7,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,},
-  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+const unsigned int ArrayLength[8] {31,28,34,39,39,39,39,39,}
-volatile int on = 0;+volatile unsigned int on = 0;
 // Declaration of functions // Declaration of functions
-int FreqTime(int Hz); +unsigned int FreqTime(unsigned int Hz); 
-int FreqCycle(int Hz , unsigned long speed);+unsigned int FreqCycle(unsigned int Hz , unsigned long speed);
 unsigned long millis(); unsigned long millis();
-int blinkms(int Millis); +unsigned long blinkms(unsigned long Millis); 
-int CountByTime(int duration, int ArrayLength);+unsigned int CountByTime(unsigned long duration, unsigned int ArrayLength);
 // MAIN program // MAIN program
 int main(void) { int main(void) {
  // Set Watchdog as timer with an interrupt every 0.5ms  // 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  WDTCTL = WDT_MDLY_0_5; // (WDTPW+WDTTMSEL+WDTCNTCL+WDTIS1) /* 0.5ms
-    P1DIR |= 0x0F; // Set P1.0 - P1.3 as Output + IFG1 &= ~WDTIFG; 
-  P1OUT &0xF0; // Set P1.0 - P1.3 to 0+ IE1 |= WDTIE; // Enable WDT interrupt 
 + P1SEL = 0x00; // Set all Pins to GPIO Pins, no clock sources, nothing 
 + P1SEL2 = 0x00; // Set all Pins to GPIO Pins, no clock sources, nothing 
 + P2SEL = 0x00; 
 + P2SEL2 = 0x00; 
 + P3SEL = 0x00; 
 + P3SEL2 = 0x00; 
 +    P1DIR = 0xCF; // Set P1.0 - P1.3 as output P1.4 - P1.5 as input and P1.6 - P1.7 as output 
 + P1REN = 0x30; // Set P1.4 - P1.5 pull down resistor 
 +  P1OUT = 0x00; // Set P1.0 - P1.7 to 0 
 + P2DIR = 0xF8; // Set P2.0 - P2.2 as Intput and P2.3 - P2.7 as Output 
 + P2REN = 0x07; // Set P2.0 - P2.2 pull down resistor 
 + P2OUT = 0x00; // Set P2.0 - P2.7 to 0 
 +    P3DIR = 0xFF; 
 +    P3OUT = 0x00;
   // Timer_A   // Timer_A
  TA0CCR0 = 1250; // Count limit for 400Hz at !MHz (1000000 / 400 / 2)  TA0CCR0 = 1250; // Count limit for 400Hz at !MHz (1000000 / 400 / 2)
Line 91: Line 63:
  TA1CCR0 = 1250; // Count limit for 400Hz predefinitoin, will be overwriten later  TA1CCR0 = 1250; // Count limit for 400Hz predefinitoin, will be overwriten later
  TA1CCTL0 = 0x10;  // Enable Timer A0 interrupts, bit 4=1  TA1CCTL0 = 0x10;  // Enable Timer A0 interrupts, bit 4=1
- TA1CTL = TASSEL_2 + MC_1; // Timer A0 with SMCLK, count UP+ TA1CTL = TASSEL_2 + MC_1; // Timer A1 with SMCLK, count UP 
 + _EINT(); //enable interrupts 
 +// convert input of PIN 2.0 - 2.2 to programnumber. Onöy once at startup. So no switching during program 
 +    volatile int ProgNo = P2IN & 0x07;
 // Endless loop // Endless loop
   while(1){   while(1){
-  j = CountByTime(brainDuration[j], ArrayLength); + j = CountByTime(brainDuration[ProgNo][j], ArrayLength[ProgNo]); 
-  on = blinkms(FreqTime(brainFq[j])); +  if (j < ArrayLength[ProgNo]){ 
-  TA1CCR0 = FreqCycle(brainFq[j]+400, 1000000); + on = blinkms(FreqTime(brainFq[ProgNo][j])); 
-  if (on){ + TA1CCR0 = FreqCycle(brainFq[ProgNo][j]+400, 1000000); 
-  P1OUT |= 0x03; + int brightness = ((P1IN & 0x30) >> 4)+5;  // brightness encoded on switches on PIN 1.4 and 1.5 
-  } + if (on){ 
-  else{ +            for (int i = 0; i < 10; i++){ 
-  P1OUT &0xFD+ // poor mans PWM 
-  } + if (i<brightness){ 
-  }+ P1OUT |= 0x03; 
 +
 + 
 + else{ 
 + P1OUT &= 0xFC; 
 +
 + } 
 +
 + else{ 
 + P1OUT &0xFC
 +
 +
 + else{ 
 +            P1DIR = 0xFF; // Set P1.0 - P1.7 as output 
 +            P2DIR = 0xFF; // Set P2.0 - P2.7 as output 
 +  P1OUT = 0x00; 
 +  P2OUT = 0x00; 
 +            __dint(); 
 + _BIS_SR(LPM4_bits); 
 +
 + }
   return 0;   return 0;
 } }
Line 110: Line 105:
  
 // blink in the rhythm of given ms // blink in the rhythm of given ms
-int blinkms(int Millis){+unsigned long blinkms(unsigned long Millis){
  static unsigned long PreviousMillis = 0;  static unsigned long PreviousMillis = 0;
- static int back = 0;+ static unsigned int back = 0;
  unsigned long CurrentMillis = millis();  unsigned long CurrentMillis = millis();
  if (CurrentMillis - PreviousMillis >= Millis){  if (CurrentMillis - PreviousMillis >= Millis){
Line 121: Line 116:
 } }
 // count 1 up, if time in ms done // count 1 up, if time in ms done
-int CountByTime(int duration, int ArrayLength){+unsigned int CountByTime(unsigned long duration, unsigned int ArrayLength){
  static unsigned long PreviousMillis = 0;  static unsigned long PreviousMillis = 0;
- static int back = 0;+ static unsigned int back = 0;
  unsigned long Millis = duration * 1000;  unsigned long Millis = duration * 1000;
  unsigned long CurrentMillis = millis();  unsigned long CurrentMillis = millis();
  if (CurrentMillis - PreviousMillis >= Millis){  if (CurrentMillis - PreviousMillis >= Millis){
  PreviousMillis = CurrentMillis;  PreviousMillis = CurrentMillis;
- if (back < ArrayLength) back++;+ if (back <ArrayLength) back++;
  }  }
  return back;  return back;
Line 134: Line 129:
  
 // Convert frequency to ms // Convert frequency to ms
-int FreqTime(int Hz){ +unsigned int FreqTime(unsigned int Hz){ 
- int v = 1000 / Hz / 2;+ unsigned int v = 1000 / Hz / 2;
  return v;  return v;
 } }
 // Convert frequency to Cycles by given Clockspeed // Convert frequency to Cycles by given Clockspeed
-int FreqCycle(int Hz, unsigned long speed){ +unsigned int FreqCycle(unsigned int Hz, unsigned long speed){ 
- int v = speed / Hz / 2;+ unsigned int v = speed / Hz / 2;
  return v;  return v;
 } }
Line 164: Line 159:
    P1OUT ^= 0x08;                  // Toggle output P1.3    P1OUT ^= 0x08;                  // Toggle output P1.3
 } }
- 
-</code> 
- 
brainmsp430.txt · Last modified: 2019/08/06 15:11 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