msp430g2553基本程序实例.docx
《msp430g2553基本程序实例.docx》由会员分享,可在线阅读,更多相关《msp430g2553基本程序实例.docx(25页珍藏版)》请在冰豆网上搜索。
![msp430g2553基本程序实例.docx](https://file1.bdocx.com/fileroot1/2023-1/21/38d1707c-534f-43c7-b7f0-1fe9aad65ea0/38d1707c-534f-43c7-b7f0-1fe9aad65ea01.gif)
msp430g2553基本程序实例
LaunchPad官方例程(无修改)
1.
//******************************************************************************
//LaunchPadLab2-SoftwareToggleP1.0,
//
//MSP430G2xx2
//-----------------
///|\|XIN|-
//|||
//--|RSTXOUT|-
//||
//|P1.0|-->LED
//
//******************************************************************************
#include
voidmain(void)
{
WDTCTL=WDTPW+WDTHOLD;//Stopwatchdogtimer
if(CALBC1_1MHZ==0xFF||CALDCO_1MHZ==0xFF)
{
while
(1);//Ifcalibrationconstantserased,trapCPU!
!
}
//ConfigureBasicClock
BCSCTL1=CALBC1_1MHZ;//Setrange
DCOCTL=CALDCO_1MHZ;//SetDCOstep+modulation
BCSCTL3|=LFXT1S_2;//SetLFXT1
P1DIR=BIT6;//P1.6output(greenLED)
P1OUT=0;//LEDoff
IFG1&=~OFIFG;//ClearOSCFaultflag
BCSCTL2|=SELM_1+DIVM_0;//SetMCLK
for(;;)
{
P1OUT=BIT6;//P1.6on(greenLED)
_delay_cycles(100);
P1OUT=0;//greenLEDoff
_delay_cycles(5000);
}
}
2.
//******************************************************************************
//LaunchPadLab3-SoftwarePortInterruptService
//
//MSP430G2xx2
//-----------------
///|\|XIN|-
//|||
//--|RSTXOUT|-
///|\||
//--o--|P1.3P1.0|-->LED
//\|/
//
//******************************************************************************
#include
voidmain(void)
{
WDTCTL=WDTPW+WDTHOLD;//Stopwatchdogtimer
P1DIR|=BIT0;//SetP1.0tooutputdirection
P1IES|=BIT3;//P1.3Hi/loedge
P1IFG&=~BIT3;//P1.3IFGcleared
P1IE|=BIT3;//P1.3interruptenabled
_BIS_SR(LPM4_bits+GIE);//EnterLPM4w/interrupt
}
//Port1interruptserviceroutine
#pragmavector=PORT1_VECTOR
__interruptvoidPort_1(void)
{
if(P1IFG&BIT3)
{
P1OUT^=BIT0;//P1.0=toggle
P1IFG&=~BIT3;//P1.3IFGcleared
}
}
3.
//******************************************************************************
//LaunchPadLab5-ADC10,SampleA10TempandConverttooCandoF
//
//MSP430G2452
//-----------------
///|\|XIN|-
//|||
//--|RSTXOUT|-
//||
//|A10|
//
//******************************************************************************
#include"msp430g2553.h"
longtemp;
longIntDegF;
longIntDegC;
voidmain(void)
{
WDTCTL=WDTPW+WDTHOLD;//StopWDT
//ConfigureADC10
ADC10CTL1=INCH_10+ADC10DIV_3;//ChooseADCChannelasTempSensor
ADC10CTL0=SREF_1+ADC10SHT_3+REFON+ADC10ON+ADC10IE;//ChooseADCRefsource
__enable_interrupt();//Enableinterrupts.
TACCR0=30;//DelaytoallowReftosettle
TACCTL0|=CCIE;//Compare-modeinterrupt.
TACTL=TASSEL_2|MC_1;//TACLK=SMCLK,Upmode.
LPM0;//Waitfordelay.
TACCTL0&=~CCIE;//DisabletimerInterrupt
__disable_interrupt();
while
(1)
{
ADC10CTL0|=ENC+ADC10SC;//Samplingandconversionstart
__bis_SR_register(LPM0_bits+GIE);//LPM0withinterruptsenabled
//oF=((A10/1024)*1500mV)-923mV)*1/1.97mV=A10*761/1024-468
temp=ADC10MEM;
IntDegF=((temp-630)*761)/1024;
//oC=((A10/1024)*1500mV)-986mV)*1/3.55mV=A10*423/1024-278
temp=ADC10MEM;
IntDegC=((temp-673)*423)/1024;
__no_operation();//SETBREAKPOINTHERE
}
}
//ADC10interruptserviceroutine
#pragmavector=ADC10_VECTOR
__interruptvoidADC10_ISR(void)
{
__bic_SR_register_on_exit(LPM0_bits);//ClearCPUOFFbitfrom0(SR)
}
#pragmavector=TIMER0_A0_VECTOR
__interruptvoidta0_isr(void)
{
TACTL=0;
__bic_SR_register_on_exit(LPM0_bits);//ClearCPUOFFbitfrom0(SR)
}
4.
//******************************************************************************
//MSP430F20xxDemo-BasicClock,OutputBufferedSMCLK,ACLKandMCLK/10
//
//Description:
BufferACLKonP2.0,defaultSMCLK(DCO)onP1.4andMCLK/10on
//P1.5.
//ACLK=LFXT1=VLO,MCLK=SMCLK=defaultDCO
////*ExternalwatchcrystalinstalledonXINXOUTisrequiredforACLK*//
//
//MSP430F20xx
//-----------------
///|\|XIN|-
//|||
//--|RSTXOUT|-
//||
//|P1.4/SMCLK|-->SMCLK=DefaultDCO
//|P1.5|-->MCLK/10=DCO/10
//|P1.0/ACLK|-->ACLK=VLO
//
//M.Buccini/L.Westlund
//TexasInstrumentsInc.
//October2005
//BuiltwithIAREmbeddedWorkbenchVersion:
3.40A
//******************************************************************************
#include
unsignedchars;
voidmain(void)
{
WDTCTL=WDTPW+WDTHOLD;//StopWatchdogTimer
BCSCTL3|=LFXT1S_2;//LFXT1=VLO
//DCOCTL=0;
//BCSCTL1=CALBC1_16MHZ;
//DCOCTL=CALBC1_16MHZ;
P1DIR|=0x31;//P1.0,5andP1.4outputs
P1SEL|=0x11;//P1.0,4ACLK/VLO,SMCLK/DCOoutput
//SMCLKSub-SystemMainClk,ACLK和SMCLK可以通过复用引脚输出,MCLK不能直接输出体现,MCLK可以配置为VLO或者DCO
while
(1)
{
P1OUT|=0x20;//P1.5=1,通过开关P1.5来体现MCLK,这两条指令的周期大概为SMCLK的1/10
P1OUT&=~0x20;//20;
}
}
5.
//******************************************************************************
//MSP430xG46xDemo-FLL+,RunsInternalDCOat8MHz
//
//Description:
ThisprogramdemonstratessettingtheinternalDCOtorunat
//8MHzwithauto-calibrationbytheFLL+.
//ACLK=LFXT1=32768Hz,MCLK=SMCLK=DCO=(121+1)x2xACLK=7995392Hz
////*AnexternalwatchcrystalbetweenXIN&XOUTisrequiredforACLK*//
//
//MSP430xG461x
//-----------------
///|\|XIN|-
//|||32kHz
//--|RSTXOUT|-
//||
//|P1.1|-->MCLK=8MHz
//||
//|P1.5|-->ACLK=32kHz
//||
//
//K.Quiring/M.Mitchell
//TexasInstrumentsInc.
//October2006
//BuiltwithIAREmbeddedWorkbenchVersion:
3.41A
//*****************************************************************************
#include
voidmain(void)
{
WDTCTL=WDTPW+WDTHOLD;//Stopwatchdogtimer
FLL_CTL0|=DCOPLUS+XCAP18PF;//DCO+set,freq=xtalxDxN+1
SCFI0|=FN_4;//x2DCOfreq,8MHznominalDCO
SCFQCTL=121;//(121+1)x32768x2=7.99MHz
P1DIR=0x22;//P1.1&P1.5tooutputdirection
P1SEL=0x22;//P1.1&P1.5tooutputMCLK&ACLK
while
(1);//Loopinplace
}
6.
//****************************************************************************
//MSP430xG46xDemo-FlashIn-SystemProgramming,CopySegAtoSegB
//
//Description:
ThisprogramfirsterasesflashsegA,thenitincrementsall
//valuesinsegA,theniterasessegB,thencopiessegAtosegB.
//AssumedMCLK550kHz-900kHz.
////*SetBreakpointonNOPintheMainlooptoavoidStressingFlash*//
//
//MSP430xG461x
//-----------------
///|\|XIN|-
//|||
//--|RSTXOUT|-
//||
//
//M.Mitchell
//TexasInstrumentsInc.
//Feb2005
//BuiltwithIAREmbeddedWorkbenchVersion:
3.21A
//******************************************************************************
#include
charvalue;//8-bitvaluetowritetosegmentA
//Functionprototypes
voidwrite_SegA(charvalue);
voidcopy_A2B(void);
voidmain(void)
{
WDTCTL=WDTPW+WDTHOLD;//Stopwatchdogtimer
FCTL2=FWKEY+FSSEL0+FN0;//MCLK/2forFlashTimingGenerator
value=0;//Initializevalue
while
(1)//Repeatforever
{
write_SegA(value++);//WritesegmentA,incrementvalue
copy_A2B();//CopysegmentAtoB
_NOP();//SETBREAKPOINTHERE
}
}
voidwrite_SegA(charvalue)
{
char*Flash_ptr;//Flashpointer
unsignedinti;
Flash_ptr=(char*)0x1080;//InitializeFlashpointer
FCTL1=FWKEY+ERASE;//SetErasebit
FCTL3=FWKEY;//ClearLockbit
*Flash_ptr=0;//DummywritetoeraseFlashsegment
FCTL1=FWKEY+WRT;//SetWRTbitforwriteoperation
for(i=0;i<128;i++)
{
*Flash_ptr++=value;//Writevaluetoflash
}
FCTL1=FWKEY;//ClearWRTbit
FCTL3=FWKEY+LOCK;//SetLOCKbit
}
voidcopy_A2B(void)
{
char*Flash_ptrA;//SegmentApointer
char*Flash_ptrB;//SegmentBpointer
unsignedinti;
Flash_ptrA=(char*)0x1080;//InitializeFlashsegmentApointer
Flash_ptrB=(char*)0x1000;//InitializeFlashsegmentBpointer
FCTL1=FWKEY+ERASE;//SetErasebit
FCTL3=FWKEY;//ClearLockbit
*Flash_ptrB=0;//DummywritetoeraseFlashsegmentB
FCTL1=FWKEY+WRT;//SetWRTbitforwriteoperation
for(i=0;i<128;i++)
{
*Flash_ptrB++=*Flash_ptrA++;//CopyvaluesegmentAtosegmentB
}
FCTL1=FWKEY;//ClearWRTbit
FCTL3=FWKEY+LOCK;//SetLOCKbit
}
7.
//******************************************************************************
//MSP430xG46xDemo-SoftwarePortInterruptonP1.0fromLPM4
//
//Description:
Ahi/lowtransitiononP1.0willtriggerP1_ISRwhich,
//togglesP2.1.NormalmodeisLPM4~0.1uA.LPM4currentcanbemeasured
//withtheLEDremoved,allunusedP1.x/P2.xconfiguredasoutputorinputs
//pulledhighorlow,andensuretheP2.0interruptinputdoesnotfloat.
//ACLK=32.768kHz,MCLK=SMCLK=defaultDCO
//
//MSP430xG461x
//-----------------
///|\||
//|||
//--|RST|
///|\||
//--o--|P1.0P2.1|-->LED
//\|/
//
//K.Quiring/M.Mitchell
//TexasInstrumentsInc.
//October2006
//BuiltwithIAREmbeddedWorkbenchVersion:
3.41A
//******************************************************************************
#include
voidmain(void)
{
WDTCTL=WDTPW+WDTHOLD;//StopWDT
FLL_CTL0|=XCAP14PF;//Configureloadcaps
P2DIR=BIT1;//SetP2.1tooutputdirection
P1IES=BIT0;//H-Ltransition
P1IE=BIT0;//Enableinterrupt
_BIS_SR(LPM4_bits+GIE);//LPM4,enableinterrupts
}
//Port1interruptserviceroutine
#pragmavector=PORT1_VECTOR
__interruptvoidPort1_ISR(void)
{
unsignedvolatileinti;
for(i=10000;i>0;i--);//Debouncedelay
P1IFG&=~BIT0;//ClearP1IFG
if((P1IN&0x01)==0)
P2OUT^=0x02;//ToggleP2.1usingexclusive-OR
}
8.
//************************