From DEW - Daniel's Electronic Wiki
Jump to: navigation, search

MSP430G2553 Configuration - library

I would like to present here my configuration procedure for MSP430G2553
There is few registers which should be properly configured, otherwise we
can trouble with unexpected issues.

Description

Usage

To use this library you need to copy source provided below to your project.
And then call this 2 functions in your main function:

C code
1
2
3
4
void main(void) {
        setupClock();
        ConfigPins();
}

Functionality

  • _system_pre_init() - function that it is called on the very beginning, it guarantees to stop watch dog before any thing else is done by MCU.
  • setupClock() - function that sets proper configuration of timings and frequency, e.g. for proper flash write/erase.
  • ConfigPins() - function that sets all pins to outputs and to logic 0.
  • __interrupt void ISR_trap() - this is interrupt trap which should be set for all unused ISR vectors.

Source code

C code
1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
34
3536
37
38
39
4041
42
43
44
4546
47
48
49
5051
52
53
54
5556
57
58
59
6061
62
63
64
6566
67
68
69
7071
72
73
74
7576
77
78
79
8081
82
83
84
8586
87
88
89
9091
92
93
94
9596
97
98
99
100101
102
103
104
/*----------------------------------------------------------------------------*/
// Description:
//              Disable watch dog in pre-initialization state
/*----------------------------------------------------------------------------*/
int _system_pre_init(void) {        WDTCTL = WDTPW + WDTHOLD;                               // stop WDT
        return 1;
}
 
//------------------------------------------------------------------------------// Conditions for SMCLK = 16MHz
//------------------------------------------------------------------------------
#define CLOCK_MHZ       16
#define CLOCK_HZ        CLOCK_MHZ *1000000
 /*----------------------------------------------------------------------------*/
// Description:
//              Configure MSP430 clock
/*----------------------------------------------------------------------------*/
void setupClock() {        char clock;
        clock = CLOCK_MHZ;
        switch (clock) {
        case 16:
            if (CALBC1_16MHZ==0xFF)                             // If calibration constant erased                while(1);                                       // do not load, trap CPU!!
            DCOCTL = 0;                                         // Select lowest DCOx and MODx settings
            BCSCTL1 = CALBC1_16MHZ;                             // Set DCOCLK to 16MHz
            DCOCTL = CALDCO_16MHZ;
            BCSCTL3 |= XCAP_3;                                  // Set oscillator caps 12.5pF            // For correct flash write/read clock must be in range (257; 476) [kHz] in this case:
//              FCTL2 = FWKEY + FSSEL0 + FN1 + FN5;             // MCLK/35 for Flash Timing Generator (457 [kHz]) - errors
//              FCTL2 = FWKEY + FSSEL0 + FN3 + FN5;             // MCLK/41 for Flash Timing Generator (390 [kHz]) - errors
//              FCTL2 = FWKEY + FSSEL0 + FN1 + FN3 + FN5;       // MCLK/43 for Flash Timing Generator (372 [kHz]) - errors
                FCTL2 = FWKEY + FSSEL0 + FN4 + FN5;             // MCLK/49 for Flash Timing Generator (326 [kHz]) - until now looks OK                break;
        case 12:
            if (CALBC1_12MHZ==0xFF)                             // If calibration constant erased
                while(1);                                       // do not load, trap CPU!!
            DCOCTL = 0;                                         // Select lowest DCOx and MODx settings            BCSCTL1 = CALBC1_12MHZ;                             // Set DCOCLK to 12MHz
            DCOCTL = CALDCO_12MHZ;
            BCSCTL3 |= XCAP_3;                                  // Set oscillator caps 12.5pF
            // For correct flash write/read clock must be in range (257; 476) [kHz] in this case 444 [kHz]
                FCTL2 = FWKEY + FSSEL0 + FN1 + FN3 + FN4;       // MCLK/27 for Flash Timing Generator                break;
        case 8:
            if (CALBC1_8MHZ==0xFF)                              // If calibration constant erased
                while(1);                                       // do not load, trap CPU!!
            DCOCTL = 0;                                         // Select lowest DCOx and MODx settings            BCSCTL1 = CALBC1_8MHZ;                              // Set DCOCLK to 8MHz
            DCOCTL = CALDCO_8MHZ;
            BCSCTL3 |= XCAP_3;                                  // Set oscillator caps 12.5pF
            // For correct flash write/read clock must be in range (257; 476) [kHz] in this case 421 [kHz]
                FCTL2 = FWKEY + FSSEL0 + FN1 + FN4;             // MCLK/19 for Flash Timing Generator                break;
        default:
            if (CALBC1_1MHZ==0xFF)                              // If calibration constant erased
                while(1);                                       // do not load, trap CPU!!
            DCOCTL = 0;                                         // Select lowest DCOx and MODx settings            BCSCTL1 = CALBC1_1MHZ;                              // Set DCOCLK to 1MHz
            DCOCTL = CALDCO_1MHZ;
            BCSCTL3 |= XCAP_3;                                  // Set oscillator caps 12.5pF+
            // For correct flash write/read clock must be in range (257; 476) [kHz] in this case 333 [kHz]
                FCTL2 = FWKEY + FSSEL0 + FN1;                   // MCLK/3 for Flash Timing Generator        }
}
 
/*----------------------------------------------------------------------------*/
// Description://              Configure MSP430 pins.
/*----------------------------------------------------------------------------*/
void ConfigPins() {
        P1DIR = 0xFF;                                           // Set P1 to output direction
        P2DIR = 0xFF;                                           // Set P2 to output direction        P3DIR = 0xFF;                                           // Set P3 to output direction
 
        P1OUT = 0x00;                                           // Set P1  outputs to logic 0
        P2OUT = 0x00;                                           // Set P1  outputs to logic 0
        P3OUT = 0x00;                                           // Set P1  outputs to logic 0 
        P2SEL &= ~BIT6; P2SEL &= ~BIT7;                         // reset port configuration to use like GPIO
}
 
/*----------------------------------------------------------------------------*/// Description:
//              Not used Interrupt Handlers
/*----------------------------------------------------------------------------*/
#pragma vector= PORT1_VECTOR,\
                PORT2_VECTOR,\                ADC10_VECTOR,\
                USCIAB0RX_VECTOR,\
                USCIAB0TX_VECTOR,\
                TIMER0_A1_VECTOR,\
                TIMER0_A0_VECTOR,\                WDT_VECTOR,\
                COMPARATORA_VECTOR,\
                TIMER1_A1_VECTOR,\
                TIMER1_A0_VECTOR,\
                NMI_VECTOR__interrupt void ISR_trap(void) {
//      while (1);                                                                                              // For debugging purposes (trap the CPU)
        WDTCTL = 0;                                                                                             // Write to WDT with a wrong password (software reset)
}

Bugs

XX.XX.2015 - __________

Nothing reported yet.

Changelog

10.01.2015 - Version 1.0

First version released.


You must be logged in to add comments