brainmsp430
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionLast revisionBoth sides next revision | ||
brainmsp430 [2014/11/02 19:20] – created admin | brainmsp430 [2019/08/06 15:10] – admin | ||
---|---|---|---|
Line 1: | Line 1: | ||
< | < | ||
- | #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 |
- | volatile | + | const unsigned |
- | 15, //1 | + | {1,15, |
- | | + | {1, |
- | | + | {1, |
- | | + | {1,12,14, |
- | | + | {1, |
- | | + | {1, |
- | 9, //7 | + | {1,12,14,11, |
- | | + | {1, |
- | | + | |
- | 6, //10 | + | |
- | 5, //11 | + | |
- | 4, //12 | + | |
- | 7, //13 | + | |
- | 6, //14 | + | |
- | | + | |
- | 4, //16 | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | 13, //28 | + | |
- | 14, //29 | + | |
- | | + | |
}; | }; | ||
- | volatile | + | const unsigned |
- | 20, //1 | + | {0,20, |
- | | + | {1,15,15,5,15, |
- | | + | {2, |
- | | + | {3, |
- | | + | {4,5,15,10,5,10, |
- | | + | {5,5, |
- | | + | {6, |
- | | + | {7, |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | 60, //16 | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | 5, //23 | + | |
- | 5, //24 | + | |
- | 5, //25 | + | |
- | 5, //26 | + | |
- | | + | |
- | | + | |
- | 5, //29 | + | |
- | 25, //30 | + | |
}; | }; | ||
- | volatile | + | const unsigned |
- | volatile int on = 0; | + | volatile |
// Declaration of functions | // Declaration of functions | ||
- | int FreqTime(int Hz); | + | unsigned |
- | int FreqCycle(int Hz , unsigned long speed); | + | unsigned |
unsigned long millis(); | unsigned long millis(); | ||
- | int blinkms(int Millis); | + | unsigned long blinkms(unsigned long Millis); |
- | int CountByTime(int duration, int ArrayLength); | + | unsigned |
// 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; | WDTCTL = WDT_MDLY_0_5; | ||
- | P1DIR |= 0x0F; // Set P1.0 - P1.3 as Output | + | IFG1 &= ~WDTIFG; |
- | | + | 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 | ||
+ | | ||
+ | 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, | TA1CCR0 = 1250; // Count limit for 400Hz predefinitoin, | ||
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 | ||
| | ||
- | j = CountByTime(brainDuration[j], | + | j = CountByTime(brainDuration[ProgNo][j], ArrayLength[ProgNo]); |
- | on = blinkms(FreqTime(brainFq[j])); | + | if (j < ArrayLength[ProgNo]){ |
- | | + | 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){ |
- | | + | for (int i = 0; i < 10; i++){ |
- | | + | // poor mans PWM |
- | } | + | if (i< |
- | } | + | P1OUT |= 0x03; |
+ | } | ||
+ | |||
+ | else{ | ||
+ | P1OUT &= 0xFC; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | else{ | ||
+ | P1OUT & | ||
+ | } | ||
+ | } | ||
+ | else{ | ||
+ | P1DIR = 0xFF; // Set P1.0 - P1.7 as output | ||
+ | P2DIR = 0xFF; // Set P2.0 - P2.7 as output | ||
+ | | ||
+ | | ||
+ | __dint(); | ||
+ | _BIS_SR(LPM4_bits); | ||
+ | } | ||
+ | } | ||
| | ||
} | } | ||
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 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 |
static unsigned long PreviousMillis = 0; | static unsigned long PreviousMillis = 0; | ||
- | static int back = 0; | + | static |
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 v = 1000 / Hz / 2; | + | unsigned |
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 v = speed / Hz / 2; | + | unsigned |
return v; | return v; | ||
} | } | ||
Line 164: | Line 159: | ||
P1OUT ^= 0x08; // Toggle output P1.3 | P1OUT ^= 0x08; // Toggle output P1.3 | ||
} | } | ||
- | |||
- | </ | ||
- |
brainmsp430.txt · Last modified: 2019/08/06 15:11 by admin