User Tools

Site Tools


brainmsp430

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
brainmsp430 [2016/01/07 11:27] adminbrainmsp430 [2019/08/06 15:11] (current) admin
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,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,}
-12, +{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,}
-14, +{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}
-11, +{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, +{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, +{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,}, 
-7, +{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,}
-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[] = { +const unsigned int brainDuration[8][63] = { 
-10, +{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,}
-5, +{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,}
-15, +{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,}
-10, +{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,}
-10, +{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,}
-10, +{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,}
-10, +{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,}
-12, +{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,},
-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+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) {
Line 86: Line 42:
  WDTCTL = WDT_MDLY_0_5; // (WDTPW+WDTTMSEL+WDTCNTCL+WDTIS1) /* 0.5ms  WDTCTL = WDT_MDLY_0_5; // (WDTPW+WDTTMSEL+WDTCNTCL+WDTIS1) /* 0.5ms
  IFG1 &= ~WDTIFG;  IFG1 &= ~WDTIFG;
- IE1 |= WDTIE; // Enable WDT interrupt   + IE1 |= WDTIE; // Enable WDT interrupt 
- P1DIR |0x0F; // Set P1.0 - P1.3 as Output + P1SEL = 0x00; // Set all Pins to GPIO Pins, no clock sources, nothing 
-  P1OUT &0xF0; // Set P1.0 - P1.3 to 0+ 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 97: Line 65:
  TA1CTL = TASSEL_2 + MC_1; // Timer A1 with SMCLK, count UP  TA1CTL = TASSEL_2 + MC_1; // Timer A1 with SMCLK, count UP
  _EINT(); //enable interrupts  _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]); 
-  if (j < ArrayLength){ +  if (j < ArrayLength[ProgNo]){ 
- on = blinkms(FreqTime(brainFq[j])); + on = blinkms(FreqTime(brainFq[ProgNo][j])); 
- TA1CCR0 = FreqCycle(brainFq[j]+400, 1000000);+ TA1CCR0 = FreqCycle(brainFq[ProgNo][j]+400, 1000000); 
 + int brightness = ((P1IN & 0x30) >> 4)+5;  // brightness encoded on switches on PIN 1.4 and 1.5
  if (on){  if (on){
-  P1OUT |= 0x03;+            for (int i = 0; i < 10; i++){ 
 + // poor mans PWM 
 + if (i<brightness){ 
 + P1OUT |= 0x03; 
 +
 + 
 + else{ 
 + P1OUT &= 0xFC; 
 +
 + }
  }  }
  else{  else{
-  P1OUT &= 0xFC;+ P1OUT &= 0xFC;
  }  }
  }  }
  else{  else{
-  P1OUT &= 0x00; +            P1DIR = 0xFF; // Set P1.0 - P1.7 as output 
-  P1OUT &= 0x00; +            P2DIR = 0xFF; // Set P2.0 - P2.7 as output 
- _BIS_SR(LPM3_bits); +  P1OUT = 0x00; 
- } +  P2OUT = 0x00
 +            __dint()
 + _BIS_SR(LPM4_bits); 
 + }
  }  }
   return 0;   return 0;
Line 122: 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 133: 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();
Line 146: 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 176: Line 159:
    P1OUT ^= 0x08;                  // Toggle output P1.3    P1OUT ^= 0x08;                  // Toggle output P1.3
 } }
- 
 </code> </code>
brainmsp430.1452166076.txt.gz · Last modified: 2016/01/07 11:27 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