1591 views|0 replies

6552

Posts

0

Resources
The OP
 

MSP430G2553 MCU Programming Experience [Copy link]

TI's 430 series focuses on low power consumption. Its technical documents and Dome programs are very detailed, especially the technical documents, which really make people feel like worshipping. There is also a block diagram for each module, which is very helpful for understanding the settings in the module. I specially printed it out at that time. At that time, its User's Guide did not have a Chinese version. If you really can't understand it, you can refer to the F149 series (someone has translated the Chinese version of this one).

Personally, I think the main points to pay attention to when learning the g2553 microcontroller are:

(1) Pay attention to the use of low power consumption and choose different modes.

(2) IO settings. Since there are relatively few IO ports and multiplexing is quite serious, I was always entangled in the problem of IO port REN and OUT, DIR. The following table summarizes the usage of PxDIRx, PxRENx and PxOUTx registers when configuring I/O ports.

PxDIRx PxRENx PxOUTx I/O port configuration
0 0 x Input
0 1 0 Set low
0 1 1 Set high
1 x x Output


As I can’t find the notes I took at the time, I can’t go into detail about the other details.

The following is a more practical G2553 framework program:


#include

//Function declaration
void InitSys();


int main( void )
{

WDTCTL = WDTPW + WDTHOLD; //Turn off the watchdog

InitSys(); //Initialization

start:
//Fill in user code below


LPM3; //Enter low power mode n, n: 0~4. If you do not want to enter low power mode, shield this sentence
goto start;

}

/************************************************************************************
System initialization
**************************************************************************/
void InitSys()
{
unsigned int iq0;

//Use XT2 oscillator
BCSCTL1&=~XT2OFF; //Turn on XT2 oscillator
do
{
IFG1 &= ~OFIFG; // Clear oscillator failure flag
for (iq0 = 0xFF; iq0 > 0; iq0--); // Delay, wait for XT2 to oscillate
}
while ((IFG1 & OFIFG) != 0); // Determine whether XT2 is oscillating

BCSCTL2 =SELM_2+SELS; //Select MCLK and SMCLK for XT2

//The following fills in user code to initialize various modules, interrupts, peripherals, etc.

_EINT(); //Turn on global interrupt control. If you do not need to turn it on, you can shield this sentence
}

/********************************************************************************
Port 2 interrupt function
******************************************************************************/
#pragma vector=PORT2_VECTOR
__interrupt void Port2()
{
//The following is a reference handler. The judgment of the interrupt source should be deleted for unused ports.
if((P2IFG&BIT0) == BIT0)
{
//Handle P2IN.0 interrupt
P2IFG &= ~BIT0; //Clear interrupt flag
//Fill in user code below

}
else if((P2IFG&BIT1) ==BIT1)
{
//Handle P2IN.1 interrupt
P2IFG &= ~BIT1; //Clear interrupt flag
//Fill in user code below

}
else if((P2IFG&BIT2) ==BIT2)
{
//Handle P2IN.2 interrupt
P2IFG &= ~BIT2; //Clear interrupt flag
//Fill in user code below

}
else if((P2IFG&BIT3) ==BIT3)
{
//Handle P2IN.3 interrupt
P2IFG &= ~BIT3; //Clear interrupt flag
//Fill in user code below

}
else if((P2IFG&BIT4) ==BIT4)
{
//Handle P2IN.4 interrupt
P2IFG &= ~BIT4; //Clear interrupt flag
//Fill in user code below

}
else if((P2IFG&BIT5) ==BIT5)
{
//Handle P2IN.5 interrupt
P2IFG &= ~BIT5; //Clear interrupt flag
//The following is filled with user code

}
else if((P2IFG&BIT6) ==BIT6)
{
//Handle P2IN.6 interrupt
P2IFG &= ~BIT6; //Clear interrupt flag
//The following is filled with user code

}
else
{
//Handle P2IN.7 interrupt
P2IFG &= ~BIT7; //Clear interrupt flag
//Fill in user code below

}

LPM3_EXIT; //Exit low power mode after exiting interrupt. If you want to keep low power mode after exiting interrupt, shield this sentence
}

/*********************************************************************************
USART1 send interrupt function
******************************************************************************/
#pragma vector=USART1TX_VECTOR
__interrupt void Usart1Tx()
{
//Fill in user code below


LPM3_EXIT; //Exit low power mode after exiting interrupt. If you want to keep the low power mode after exiting the interrupt, shield this sentence
}

/*********************************************************************************
USART1 receive interrupt function
******************************************************************************/
#pragma vector=USART1RX_VECTOR
__interrupt void Ustra1Rx()
{
//The following is filled with user code


LPM3_EXIT; //Exit low power mode after exiting the interrupt. If you want to keep the low power mode after exiting the interrupt, mask this sentence
}

/********************************************************************************
Port 1 interrupt function
multiple interrupt sources: P1IFG.0~P1IFG7
After entering the interrupt, the interrupt source should be judged first, and the interrupt flag should be cleared before exiting the interrupt, otherwise the interrupt will be triggered again
************************************************************************/
#pragma vector=PORT1_VECTOR
__interrupt void Port1()
{
//The following is a reference handler. The judgment of the interrupt source should be deleted for unused ports.
if((P1IFG&BIT0) == BIT0)
{
//Handle P1IN.0 interrupt
P1IFG &= ~BIT0; //Clear interrupt flag
//Fill in user code below

}
else if((P1IFG&BIT1) ==BIT1)
{
//Handle P1IN.1 interrupt
P1IFG &= ~BIT1; //Clear interrupt flag
//Fill in user code below

}
else if((P1IFG&BIT2) ==BIT2)
{
//Handle P1IN.2 interrupt
P1IFG &= ~BIT2; //Clear interrupt flag
//Fill in user code below

}
else if((P1IFG&BIT3) ==BIT3)
{
//Handle P1IN.3 interrupt
P1IFG &= ~BIT3; //Clear interrupt flag
//Fill in user code below

}
else if((P1IFG&BIT4) ==BIT4)
{
//Handle P1IN.4 interrupt
P1IFG &= ~BIT4; //Clear interrupt flag
//Fill in user code below

}
else if((P1IFG&BIT5) ==BIT5)
{
//Handle P1IN.5 interrupt
P1IFG &= ~BIT5; //Clear interrupt flag
//Fill in user code below

}
else if((P1IFG&BIT6) ==BIT6)
{
//Handle P1IN.6 interrupt
P1IFG &= ~BIT6; //Clear interrupt flag
//Fill in user code below

}
else
{
//Handle P1IN.7 interrupt
P1IFG &= ~BIT7; //Clear interrupt flag
//Fill in user code below

}

LPM3_EXIT; //Exit low power mode after exiting interrupt. If you want to keep the low power mode after exiting the interrupt, mask this sentence
}

/********************************************************************************
Timer A interrupt function
multiple interrupt sources: CC1~2 TA
**************************************************************************/
#pragma vector=TIMERA1_VECTOR
__interrupt void TimerA1()
{
//The following is a reference handler. Unused interrupt sources should be deleted
switch (__even_in_range(TAIV, 10))
{
case 2:
//Capture/Compare 1 interrupt
//The following is filled with user code

break;
case 4:
//Capture/Compare 2 interrupt
//The following is filled with user code

break;
case 10:
//TAIFG timer overflow interrupt
//The following is filled with user code

break;
}


LPM3_EXIT; //Exit low power mode after exiting the interrupt. If you want to keep the low power mode after exiting the interrupt, shield this sentence
}

/********************************************************************************
Timer A interrupt function
Interrupt source: CC0
******************************************************************************/
#pragma vector=TIMERA0_VECTOR
__interrupt void TimerA0()
{
//Fill in user code below


LPM3_EXIT; //Exit low power mode after exiting the interrupt. If you want to keep the low power mode after exiting the interrupt, mask this sentence
}

/********************************************************************************
AD converter interrupt function
multiple interrupt sources: analog 0~7, VeREF+, VREF-/VeREF-, (AVcc-AVss)/2
ADC12TOV and ADC12OV interrupt flags are not processed
************************************************************************************/
#pragma vector=ADC_VECTOR
__interrupt void Adc()
{
//The following is a reference handler. Unused interrupt sources should be deleted
if((ADC12IFG&BIT0)==BIT0)
{
//Channel 0
//The following is filled with user code

}
else if((ADC12IFG&BIT1)==BIT1)
{
//Channel 1
//The following is filled with user code

}
else if((ADC12IFG&BIT2)==BIT2)
{
//Channel 2
//The following is filled with user code

}
else if((ADC12IFG&BIT3)==BIT3)
{
//Channel 3
//The following is filled with user code

}
else if((ADC12IFG&BIT4)==BIT4)
{
//Channel 4
//The following is filled with user code

}
else if((ADC12IFG&BIT5)==BIT5)
{
//Channel 5
//The following is filled with user code

}
else if((ADC12IFG&BIT6)==BIT6)
{
//Channel 6
//The following is filled with user code

}
else if((ADC12IFG&BIT7)==BIT7)
{
//Channel 7
//The following is filled with user code

}
else if((ADC12IFG&BIT8)==BIT8)
{
//VeREF+
//The following is filled with user code

}
else if((ADC12IFG&BIT9)==BIT9)
{
//VREF-/VeREF-
//The following is filled with user code

}
else if((ADC12IFG&BITA)==BITA)
{
//Temperature
//The following is filled with user code

}
else if((ADC12IFG&BITB)==BITB)
{
//(AVcc-AVss)/2
//The following is filled with user code

}

LPM3_EXIT; //Exit low power mode after exiting interrupt. If you want to keep the low power mode after exiting the interrupt, shield this sentence
}

/*****************************************************************************
USART0 send interrupt function
******************************************************************************/
#pragma vector=USART0TX_VECTOR
__interrupt void Usart0Tx()
{
//The following fills in user code


LPM3_EXIT; //Exit low power mode after exiting the interrupt. If you want to keep the low power mode after exiting the interrupt, shield this sentence
}

/*********************************************************************************
USART0 receive interrupt function
**************************************************************************/
#pragma vector=USART0RX_VECTOR
__interrupt void Usart0Rx()
{
//The following fills in user code


LPM3_EXIT; //Exit low power mode after exiting the interrupt. If you want to keep the low power mode after exiting the interrupt, shield this sentence
}

/*********************************************************************************
Watchdog timer interrupt function
**************************************************************************/
#pragma vector=WDT_VECTOR
__interrupt void WatchDog()
{
//The following fills in user code


LPM3_EXIT; //Exit low power mode after exiting the interrupt. If you want to keep the low power mode after exiting the interrupt, shield this sentence
}

/*********************************************************************************
Comparator A interrupt function
**************************************************************************/
#pragma vector=COMPARATORA_VECTOR
__interrupt void ComparatorA()
{
//The following fills in user code


LPM3_EXIT; //Exit low power mode after exiting the interrupt. If you want to keep the low power mode after exiting the interrupt, shield this sentence
}

/****************************************************************************
Timer B interrupt function
multiple interrupt sources: CC1~6 TB
******************************************************************************/
#pragma vector=TIMERB1_VECTOR
__interrupt void TimerB1()
{
//The following is a reference handler. Unused interrupt sources should be deleted
switch (__even_in_range(TBIV, 14))
{
case 2:
//Capture/Compare 1 interrupt
//The following is filled with user code

break;
case 4:
//Capture/Compare 2 interrupt
//The following is filled with user code

break;
case 6:
//Capture/Compare 3 interrupt
//The following is filled with user code

break;
case 8:
//Capture/Compare 4 interrupt
//The following is filled with user code

break;
case 10:
//Capture/Compare 5 interrupt
//The following is filled with user code

break;
case 12:
//Capture/Compare 6 interrupt
//The following is filled with user code

break;
case 14:
//TBIFG timer overflow interrupt
//The following is filled with user code

break;
}

LPM3_EXIT; //Exit low power mode after exiting interrupt. If you want to keep low power mode after exiting interrupt, shield this sentence
}

/********************************************************************************
Timer B interrupt function
interrupt source: CC0
******************************************************************************/
#pragma vector=TIMERB0_VECTOR
__interrupt void TimerB0()
{
//Fill in user code below

LPM3_EXIT; //Exit low power mode after exiting interrupt. If you want to keep the low power mode after exiting the interrupt, mask this sentence
}

/********************************************************************************
Non-maskable interrupt function
**************************************************************************/
#pragma vector=NMI_VECTOR
__interrupt void Nmi()
{
//The following is a reference handler. Unused interrupt sources should be deletedif
((IFG1&OFIFG)==OFIFG)
{
//Oscillator failureIFG1
&= ~OFIFG;
//The following is filled with user code

}
else if((IFG1&NMIIFG)==NMIIFG)
{
//RST/NMI non-maskable interruptIFG1
&= ~NMIIFG;
//The following is filled with user code

}
else //if((FCTL3&ACCVIFG)==ACCVIFG)
{
//Illegal memory accessFCTL3
&= ~ACCVIFG;
//The following is filled with user code

}

LPM3_EXIT; //Exit low power mode after exiting the interrupt. If you want to keep the low power mode after exiting the interrupt, shield this sentence
}

/********************************************************************************
Basic timer interrupt function
**************************************************************************/
#pragma vector=BASICTIMER_VECTOR
__interrupt void BasTimer()
{
//Fill in user code below

LPM3_EXIT; //Exit low power mode after exiting interrupt. If you want to keep low power mode after exiting interrupt, shield this sentence
}
Delay function (pay attention to selecting clock)

//1us delay function
void delay_1us(void) {
asm("nop");
}
//N us delay function
void delay_nus(unsigned int n) {
unsigned int i;
for (i = 0; i < n; i++ )
delay_1us();
}
//1ms delay function
void delay_1ms(void) {
unsigned int i;
for (i = 0; i < 1140; i++)
;
}
//N ms delay function
void delay_nms(unsigned int n) {
unsigned int i = 0;
for (i = 0; i < n; i++)
delay_1ms();
}

This post is from Microcontroller MCU
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list