msp430实用程序汇总

更新时间:2024-07-04 12:27:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

一、基础实验【10个】

1、入门试验:LED闪烁(1个)

2、时钟实验:设置MCLK、ACLK、SMCLK(1个) 3、低功耗实验:设置低功耗模式(1个) 4、IO端口试验: IO端口寄存器设置(1个)

5、定时器:看门狗定时器、TimerA寄存器设置(2个)6、比较器:比较器A寄存器(1个) 7、Flash:flash读写(1个)

8、异步通信:异步通信寄存器设置(1个) 9、ADC:ADC12寄存器设置(1个)

1

二、开发板模块简单程序【56个】

1、LED流水灯实验(红、黄、绿)

(1)LED1:检测开发板

(2)LED2:普通IO控制闪烁 (3)LED3:PWM信号控制闪烁

2、蜂鸣器实验

(1)蜂鸣器1:单频音(步进变音调) (2)蜂鸣器2:奏乐(祝你平安)

3、数码管实验

(1)数码管1(显示123456) (2)数码管2(动态显示0~F) (3)数码管3(流动光圈) (4)数码管4(来回光标)

4、4×1独立按键实验

(1)4×1键盘1:扫描数码管显示

(2)4×1键盘2:中断数码管显示 (3)4×1键盘3:控制LED (4)4×1键盘4:控制蜂鸣器

5、4×4矩阵键盘实验

(1)4×4键盘1:行列扫描数码管显示

(2)4×4键盘2:行列扫描1602液晶显示

2

(3)4×4键盘3:控制LED蜂鸣器

6、1602液晶实验

(1)1602液晶1:动态字符显示 (2)1602液晶2:静态字符显示 (3)1602液晶3:内部时钟显示

7、3.3V-5V电平转换实验

(1)电平转换1:输出5V方波

(2)电平转换2:输出不同占空比的方波 (3)电平转换3:MCLK,SMCLK,ACLK

8、RS232接口实验

(1)RS232接口1:MCU发送数据PC机显示

(2)RS232接口2:按键控制MCU发送数据PC机显示 (3)RS232接口3:PC机发送数据MCU液晶显示 (4)RS232接口4:MCU回发接收到的PC机数据 (5)RS232接口5:RS232控制蜂鸣器

9、RS485接口实验

(1)RS485接口1:发送程序 (2)RS485接口2:接收程序

10、USB接口实验

(1)USB接口1:简单连接测试 (2)USB接口2:USB接收数据 (3)USB接口3:USB发送数据

11、PS2接口实验

(1)PS2接口1: PS2控制1602显示 (2)PS2接口2: PS2控制数码管显示 (3)PS2接口3:PS2控制LED和蜂鸣器

12、12-Bit高精度温度传感器实验

(1)温度传感器1:DS18B20在数码管显示 (2)温度传感器2:DS18B20在液晶显示

13、RTC实时时钟实验

(1)实时时钟1:DS1302测试

3

(2)实时时钟2:DS1302电子钟

14、2k Bit EEPROM实验

(1)EEPROM1:AT24C02测试

(2)EEPROM2:读出数据通过串口在PC机显示

15、12-Bit模数转换器(ADC)接口实验

(1)模数转换器1:ADC在数码管显示 (2)模数转换器2:ADC在1602液晶在显示 (3)模数转换器3:ADC通过串口在PC机显示

16、8-Bit数模转换器(DAC)实验

(1)数模转换器1:DAC控制LED

(2)数模转换器2:DAC输出电压,ADC采样转换并在液晶上显示

17、12864液晶实验(与12864液晶配套)

(1)12864液晶并口1:字符显示 (2)12864液晶并口2:汉字显示 (3)12864液晶并口3:图形显示 (4)12864液晶并口4:综合演示 (5)12864液晶串口5:字符显示 (6)12864液晶串口6:汉字显示 (7)12864液晶串口7:图形显示 (8)12864液晶串口8:综合演示

18、射频模块CC1000实验

(1)射频模块1:发送数据 (2)射频模块2:接收数据

19、ucos移植

注:17、18程序随模块赠送

三、开发板综合程序【30】

1、键盘综合实验

(1)4×4键盘+蜂鸣器+LED+数码管显示 (2)4×4键盘+蜂鸣器+LED+1602液晶显示 (3)4×4键盘+蜂鸣器+LED+PC机显示 (4)PS2键盘+UART+PC机显示

4

(5)PS2键盘+USB+PC机显示

2、接口综合实验

(1)USB?UART (2)UART?USB (3)RS232?RS485 (4)RS485?RS232

3、温度时间综合实验

(1)DS18B20 + DS1302 + 数码管 (2)DS18B20 + DS1302 + USB (3)DS18B20 + DS1302 + UART (4)DS18B20 + DS1302 + 1602

4、AD DA综合实验

(1)ADC + 1602 (2)ADC + UART (3)ADC + USB

(4)DAC + LED + KEY (5)DAC + UART (6)DAC + USB

(7)ADC + UART + DS1302 (8)ADC + DAC + 1602 + KEY (9)ADC + DAC + UART + KEY

5、其他综合实验

(1)AT24C02高级应用(搜索,擦除,读出全部)(2)DS1302高级应用(内部RAM存取数据)

6、12864液晶综合实验

(1)汉字库 (2)图形库

7、3.2寸TFT触摸屏实验

(1)静态图片 (2)动画

5

/*************************************************** 程序功能:BoardConfig.h 头文件 ---------------------------------------------------

***************************************************/ typedef unsigned char uchar; typedef unsigned int uint;

//控制位的宏定义

#define Ctrl_Out P3DIR |= BIT3 + BIT6 + BIT7; #define Ctrl_0 P3OUT &= ~(BIT3 + BIT6 + BIT7) #define SRCLK_1 P3OUT |= BIT7 #define SRCLK_0 P3OUT &= ~BIT7 #define SER_1 P3OUT |= BIT6 #define SER_0 P3OUT &= ~BIT6 #define RCLK_1 P3OUT |= BIT3 #define RCLK_0 P3OUT &= ~BIT3

//板上资源配置函数

void BoardConfig(uchar cmd) {

uchar i;

Ctrl_Out; Ctrl_0;

for(i = 0; i < 8; i++) {

SRCLK_0;

if(cmd & 0x80) SER_1; else SER_0; SRCLK_1; cmd <<= 1; }

RCLK_1; _NOP(); RCLK_0; }

/*************************************************** 程序功能:控制8个LED闪烁,用于测试下载功能是否正常 --------------------------------------------------- 测试说明:观察LED闪烁

***************************************************/ #include #include \

6

/****************主函数****************/ void main(void) {

WDTCTL = WDTPW + WDTHOLD; //关闭看门狗

BoardConfig(0xf0); //关闭数码管和电平转换,打开流水灯

CCTL0 = CCIE; //使能CCR0中断

CCR0 = 2047; //设定周期0.5S

TACTL = TASSEL_1 + ID_3 + MC_1; //定时器A的时钟源选择ACLK,增计数模式 P2DIR = 0xff; //设置P2口方向为输出 P2OUT = 0xff;

_EINT(); //使能全局中断

LPM3; //CPU进入LPM3模式 }

/******************************************* 函数名称:Timer_A

功 能:定时器A的中断服务函数 参 数:无 返回值 :无

********************************************/ #pragma vector = TIMERA0_VECTOR __interrupt void Timer_A (void) {

P2OUT ^= 0xff; //P2口输出取反 }

/*********************************************** 程序功能:实现流水灯以三种流动方式和四种流动速度 的不同组合而进行点亮\流动\------------------------------------------------

测试说明:观察流水灯流动顺序和速度的变化

************************************************/ #include #include \

uint i = 0,j = 0,dir = 0;

uint flag = 0,speed = 0; //flag--灯光流动方式,speed--灯光流动速度

/****************主函数****************/ void main(void) {

WDTCTL = WDTPW + WDTHOLD; //关闭看门狗

7

BoardConfig(0xf0);

CCTL0 = CCIE; //使能CCR0中断 CCR0 = 50000;

TACTL = TASSEL_2 + ID_3 + MC_1; //定时器A的时钟源选择SMCLK,增计数模式 P2DIR = 0xff; //设置P2口方向为输出 P2OUT = 0xff;

_EINT(); //使能全局中断

LPM0; //CPU进入LPM0模式 }

/******************************************* 函数名称:Timer_A

功 能:定时器A的中断服务函数,在这里通过标志 控制流水灯的流动方向和流动速度 参 数:无 返回值 :无

********************************************/ #pragma vector = TIMERA0_VECTOR __interrupt void Timer_A (void) {

if(flag == 0) {

P2OUT = ~(0x80>>(i++)); //灯的点亮顺序D8 -> D1 }

else if(flag == 1) {

P2OUT = ~(0x01<<(i++)); //灯的点亮顺序D1 -> D8 } else {

if(dir) //灯的点亮顺序 D8 -> D1,D1 -> D8,循环绕圈 {

P2OUT = ~(0x80>>(i++)); } else {

P2OUT = ~(0x01<<(i++)); } }

if(i == 8) {

i = 0;

8

dir = ~dir; }

j++;

if(j == 40) {

i = 0; j = 0; flag++;

if(flag == 4) flag = 0; switch(speed) {

case 0:

TACTL &=~ (ID0 + ID1); TACTL |= ID_3; break; case 1:

TACTL &=~ (ID0 + ID1); TACTL |= ID_2; break; case 2:

TACTL &=~ (ID0 + ID1); TACTL |= ID_1; break; case 3:

TACTL &=~ (ID0 + ID1); TACTL |= ID_0; break; default: break; }

if(flag != 3) speed++; if(speed == 4) speed = 0; } }

/******************************************************* 程序功能:用从P2.3和P2.4输出的PWM波形驱动LED闪烁 P2.3口输出方波的占空比为75% P2.4口输出方波的占空比为25% ------------------------------------------------------- 测试说明:观察LED的亮灭的时间长短

*******************************************************/ #include #include \void main(void)

9

{

WDTCTL = WDTPW + WDTHOLD; // 关狗

BoardConfig(0xb0); // 关闭数码管和电平转换,打开流水灯 P2DIR = 0xff; // P2端口设置为输出 P2OUT = 0xff; // 关闭其他LED

P2SEL |= BIT3 + BIT4; // P2.3和P2.4连接内部模块 CCR0 = 4096-1; // PWM周期为1S CCTL1 = OUTMOD_7; // CCR1 reset/set

CCR1 = 3072; // CCR1 PWM duty cycle CCTL2 = OUTMOD_7; // CCR2 reset/set

CCR2 = 1024; // CCR2 PWM duty cycle TACTL = TASSEL_1 + ID_3 + MC_1; // ACLK/8, up mode

_BIS_SR(LPM3_bits); // Enter LPM3 }

//****************************************************************************** // MSP-FET430P140 Demo - Basic Clock, Output Buffered SMCLK, ACLK and MCLK //

// Description: Output buffered MCLK, SMCLK and ACLK.

// ACLK = LFXT1 = 32768, MCLK = DCO Max, SMCLK = XT2 // //* XTAL's REQUIRED - NOT INSTALLED ON FET *//

// //* Min Vcc required varies with MCLK frequency - refer to datasheet *// //

// MSP430F149 // -----------------

// /|\\| XIN|- // | | | 32k // --|RST XOUT|- // | | // | XT2IN|-

// | | XTAL (455k - 8Mhz) // |RST XT2OUT|- // | |

// | P5.4|-->MCLK = DCO Max // | P5.5|-->SMCLK = XT2 // | P5.6|-->ACLK = 32kHz //

// M. Buccini

// Texas Instruments Inc. // Feb 2005

// Built with IAR Embedded Workbench Version: 3.21A

//******************************************************************************

10

#include #include \

void main(void) {

BoardConfig(0xb8);

WDTCTL = WDTPW +WDTHOLD; // Stop Watchdog Timer DCOCTL = DCO0 + DCO1 + DCO2; // Max DCO

BCSCTL1 = RSEL0 + RSEL1 + RSEL2; // XT2on, max RSEL BCSCTL2 |= SELS; // SMCLK = XT2 P5DIR |= 0x70; // P5.6,5,4 outputs P5SEL |= 0x70; // P5.6,5,5 options

while(1) { } }

//****************************************************************************** // MSP-FET430P140 Demo - Basic Clock, LPM3 Using WDT ISR, 32kHz ACLK //

// Description: This program operates MSP430 normally in LPM3, pulsing P3.4 // at 4 second intervals. WDT ISR used to wake-up system. All I/O configured // as low outputs to eliminate floating inputs. Current consumption does

// increase when LED is powered on P3.4. Demo for measuring LPM3 current. // ACLK= LFXT1/4= 32768/4, MCLK= SMCLK= default DCO // //* External watch crystal on XIN XOUT is required for ACLK *// // //

// MSP430F149 // ---------------

// /|\\| XIN|-

// | | | 32kHz // --|RST XOUT|- // | |

// | P3.5|-->LED //

// Dasheng

// LiTian Electronic Inc. // Feb 2008

// Built with IAR Embedded Workbench Version: 3.42A

//******************************************************************************

#include #include \

11

void main(void) {

BoardConfig(0xb8);

BCSCTL1 |= DIVA_2; // ACLK/4

WDTCTL = WDT_ADLY_1000; // WDT 1s/4 interval timer IE1 |= WDTIE; // Enable WDT interrupt

P1DIR = 0xFF; // All P1.x outputs P1OUT = 0; // All P1.x reset P2DIR = 0xFF; // All P2.x outputs P2OUT = 0; // All P2.x reset P3DIR = 0xFF; // All P3.x outputs P3OUT = 0x30; // All P3.x reset P4DIR = 0xFF; // All P4.x outputs P4OUT = 0; // All P4.x reset P5DIR = 0xFF; // All P5.x outputs P5OUT = 0; // All P5.x reset P6DIR = 0xFF; // All P6.x outputs P6OUT = 0x80; // All P6.x reset

while(1) {

uint i;

_BIS_SR(LPM3_bits + GIE); // Enter LPM3

P3OUT &= ~BIT5; // Set P3.5 LED on for (i = 18000; i>0; i--); // Delay

P3OUT |= BIT5; // Clear P3.5 LED off } }

#pragma vector=WDT_VECTOR

__interrupt void watchdog_timer (void) {

_BIC_SR_IRQ(LPM3_bits); // Clear LPM3 bits from 0(SR) }

//******************************************************************************* // MSP-FET430P140 Demo - Software Toggle P3.4 //

// Description: Toggle P3.4 by xor'ing P3.4 inside of a software loop. // ACLK= n/a, MCLK= SMCLK= default DCO ~800k //

// MSP430F149 // -----------------

// /|\\| XIN|-

12

// | | | // --|RST XOUT|- // | |

// | P3.4|-->LED //

// Dasheng

// LiTian Electronic Inc. // Feb 2008

// Built with IAR Embedded Workbench Version: 3.42A

//******************************************************************************

#include #include \

void main(void) {

BoardConfig(0xb8);

WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer P3DIR |= BIT4; // Set P3.4 to output direction

for (;;) {

volatile unsigned int i;

P3OUT ^= BIT4; // Toggle P3.4 using exclusive-OR

i = 50000; // Delay do (i--);

while (i != 0); } }

//****************************************************************************** // MSP-FET430P140 Demo - WDT, Toggle P3.4, Interval Overflow ISR, DCO SMCLK //

// Description: Toggle P3.4 using software timed by the WDT ISR. Toggle rate // is approximately 30ms based on default ~ 800khz DCO/SMCLK clock source // used in this example for the WDT.

// ACLK= n/a, MCLK= SMCLK= default DCO~ 800k // // MSP430F149

// -----------------

// /|\\| XIN|- // | | | // --|RST XOUT|-

13

// | |

// | P3.4|-->LED //

// Dasheng

// LiTian Electronic Inc. // Feb 2008

// Built with IAR Embedded Workbench Version: 3.42A

//****************************************************************************** #include #include \

void main(void) {

BoardConfig(0xbf); //关闭数码管、流水灯和电平转换

WDTCTL = WDT_MDLY_32; // Set Watchdog Timer interval to ~30ms IE1 |= WDTIE; // Enable WDT interrupt P3DIR |= BIT4; // Set P3.4 to output direction

_BIS_SR(LPM0_bits + GIE); // Enter LPM0 w/ interrupt }

// Watchdog Timer interrupt service routine #pragma vector=WDT_VECTOR

__interrupt void watchdog_timer(void) {

P3OUT ^= BIT4; // Toggle P3.4 using exclusive-OR }

//****************************************************************************** // MSP-FET430P140 Demo - WDT, Toggle P3.4, Interval Overflow ISR, 32kHz ACLK //

// Description: Toggle P3.4 using software timed by WDT ISR. Toggle rate is // exactly 250ms based on 32kHz ACLK WDT clock source. In this example the // WDT is configured to divide 32768 watch-crystal(2^15) by 2^13 with an ISR // triggered @ 4Hz.

// ACLK= LFXT1= 32768, MCLK= SMCLK= DCO~ 800kHz

// //* External watch crystal installed on XIN XOUT is required for ACLK *// // // MSP430F149

// -----------------

// /|\\| XIN|-

// | | | 32kHz // --|RST XOUT|- // | |

// | P3.4|-->LED //

14

// Dasheng

// LiTian Electronic Inc. // Feb 2008

// Built with IAR Embedded Workbench Version: 3.42A

//******************************************************************************

#include #include \

void main(void) {

BoardConfig(0xb8);

WDTCTL = WDT_ADLY_250; // WDT 250ms, ACLK, interval timer IE1 |= WDTIE; // Enable WDT interrupt P3DIR |= BIT4; // Set P3.4 to output direction

_BIS_SR(LPM3_bits + GIE); // Enter LPM3 w/interrupt }

// Watchdog Timer interrupt service routine #pragma vector=WDT_VECTOR

__interrupt void watchdog_timer(void) {

P3OUT ^= BIT4; // Toggle P3.4 using exclusive-OR }

//****************************************************************************** // MSP-FET430P140 Demo - Timer_A, Toggle P3.4, CCR0 Cont. Mode ISR, DCO SMCLK //

// Description: Toggle P3.4 using software and TA_0 ISR. Toggles every // 50000 SMCLK cycles. SMCLK provides clock source for TACLK.

// During the TA_0 ISR, P3.4 is toggled and 50000 clock cycles are added to // CCR0. TA_0 ISR is triggered every 50000 cycles. CPU is normally off and // used only during TA_ISR.

// ACLK = n/a, MCLK = SMCLK = TACLK = default DCO ~800kHz //

// MSP430F149 // ---------------

// /|\\| XIN|- // | | | // --|RST XOUT|- // | |

// | P3.4|-->LED //

// Dasheng

// LiTian Electronic Inc.

15

// Feb 2008

// Built with IAR Embedded Workbench Version: 3.42A

//******************************************************************************

#include #include \

void main(void) {

WDTCTL = WDTPW + WDTHOLD; // Stop WDT

BoardConfig(0xb8); //关闭数码管、流水灯和电平转换 P3DIR |= BIT4; // P3.4 output

CCTL0 = CCIE; // CCR0 interrupt enabled CCR0 = 50000;

TACTL = TASSEL_2 + MC_2; // SMCLK, contmode

_BIS_SR(LPM0_bits + GIE); // Enter LPM0 w/ interrupt }

// Timer A0 interrupt service routine #pragma vector=TIMERA0_VECTOR __interrupt void Timer_A (void) {

P3OUT ^= BIT4; // Toggle P3.4

CCR0 += 50000; // Add Offset to CCR0 }

//****************************************************************************** // MSP-FET430P140 Demo - Timer_A, Toggle P3.4, CCR0 Up Mode ISR, DCO SMCLK //

// Description: Toggle P3.4 using software and TA_0 ISR. Timer_A is // configured for up mode, thus the timer overflows when TAR counts // to CCR0. In this example, CCR0 is loaded with 20000.

// ACLK = n/a, MCLK = SMCLK = TACLK = default DCO ~800kHz //

// MSP430F149 // ---------------

// /|\\| XIN|- // | | | // --|RST XOUT|- // | |

// | P3.4|-->LED //

// Dasheng

// LiTian Electronic Inc. // Feb 2008

16

// Built with IAR Embedded Workbench Version: 3.42A

//******************************************************************************

#include #include \void main(void) {

WDTCTL = WDTPW + WDTHOLD; // Stop WDT BoardConfig(0xb8);

P3DIR |= BIT4; // P3.4 output

CCTL0 = CCIE; // CCR0 interrupt enabled CCR0 = 20000;

TACTL = TASSEL_2 + MC_1; // SMCLK, upmode

_BIS_SR(LPM0_bits + GIE); // Enter LPM0 w/ interrupt }

// Timer A0 interrupt service routine #pragma vector=TIMERA0_VECTOR __interrupt void Timer_A (void) {

P3OUT ^= BIT4; // Toggle P3.4 }

//****************************************************************************** // MSP-FET430P140 Demo - Timer_A, Toggle P3.4, Overflow ISR, DCO SMCLK //

// Description: Toggle P3.4 using software and Timer_A overflow ISR. // In this example an ISR triggers when TA overflows. Inside the TA // overflow ISR P3.4 is toggled. Toggle rate is approximatlely 12Hz. // Proper use of the TAIV interrupt vector generator is demonstrated. // ACLK = n/a, MCLK = SMCLK = TACLK = default DCO ~800kHz //

// MSP430F149 // ---------------

// /|\\| XIN|- // | | | // --|RST XOUT|- // | |

// | P3.4|-->LED //

// Dasheng

// LiTian Electronic Inc. // Feb 2008

// Built with IAR Embedded Workbench Version: 3.42A

//******************************************************************************

17

#include #include \void main(void) {

WDTCTL = WDTPW + WDTHOLD; // Stop WDT BoardConfig(0xb8);

P3DIR |= BIT4; // P3.4 output

TACTL = TASSEL_2 + MC_2 + TAIE; // SMCLK, contmode, interrupt

_BIS_SR(LPM0_bits + GIE); // Enter LPM0 w/ interrupt }

// Timer_A3 Interrupt Vector (TAIV) handler #pragma vector=TIMERA1_VECTOR __interrupt void Timer_A(void) {

switch( TAIV ) {

case 2: break; // CCR1 not used case 4: break; // CCR2 not used case 10: P3OUT ^= BIT4; // overflow break; } }

//****************************************************************************** // MSP-FET430P140 Demo - Timer_A, Toggle P3.4, Overflow ISR, 32kHz ACLK //

// Description: Toggle P3.4 using software and the Timer_A overflow ISR. // In this example an ISR triggers when TA overflows. Inside the ISR P3.4 // is toggled. Toggle rate is exactly 0.5Hz. Proper use of the TAIV interrupt // vector generator is demonstrated.

// ACLK = TACLK = 32768Hz, MCLK = SMCLK = default DCO ~800kHz // //* An external watch crystal on XIN XOUT is required for ACLK *// //

// MSP430F149 // ---------------

// /|\\| XIN|-

// | | | 32kHz // --|RST XOUT|- // | |

// | P3.4|-->LED //

// Dasheng

// LiTian Electronic Inc.

18

// Feb 2008

// Built with IAR Embedded Workbench Version: 3.42A

//******************************************************************************

#include #include \

void main(void) {

WDTCTL = WDTPW + WDTHOLD; // Stop WDT BoardConfig(0xb8);

P3DIR |= BIT4; // P3.4 output

TACTL = TASSEL_1 + MC_2 + TAIE; // ACLK, contmode, interrupt

_BIS_SR(LPM3_bits + GIE); // Enter LPM3 w/ interrupt }

// Timer_A3 Interrupt Vector (TAIV) handler #pragma vector=TIMERA1_VECTOR __interrupt void Timer_A(void) {

switch( TAIV ) {

case 2: break; // CCR1 not used case 4: break; // CCR2 not used case 10: P3OUT ^= BIT4; // overflow break; } }

#include #include \void main(void) {

WDTCTL = WDTHOLD + WDTPW; // 关看门狗

BoardConfig(0xb0); //开流水灯,关数码管和电平转换 CACTL1 = CARSEL + CAREF0 + CAON ; // Vcc/4 = - cmp CACTL2 = P2CA0; // 使用 CA0 P2DIR = 0xff; P2OUT = 0xff;

while(1) {

19

if((CACTL2 | 0xfe) ==0xff)

{ // 比较电压是否超过0.25Vcc P2OUT &= ~BIT4;

CACTL1 &= 0xfe; // CAIFG = 0 } else {

P2OUT |= BIT4; } } }

//**************************************************************************** // MSP-FET430P140 Demo - Flash In-System Programming, Copy SegA to SegB //

// Description: This program first erases flash seg A, then it increments all // values in seg A, then it erases seg B, then copies seg A to seg B. // Assumed MCLK 550kHz - 900kHz.

// //* Set Breakpoint on NOP in the Mainloop to avoid Stressing Flash *// //

// MSP430F149 // -----------------

// /|\\| XIN|- // | | | // --|RST XOUT|- // | | //

// M. Mitchell

// Texas Instruments Inc. // Feb 2005

// Built with IAR Embedded Workbench Version: 3.21A

//******************************************************************************

#include #include \

uchar value; // 8-bit value to write to segment A uchar DataBuffer[128];

// Function prototypes

void write_SegA (uchar value); void copy_A2B (void);

void main(void)

20

{

BoardConfig(0xb8);

WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer

FCTL2 = FWKEY + FSSEL0 + FN0; // MCLK/2 for Flash Timing Generator value = 0; // Initialize value

while(1) // Repeat forever {

write_SegA(value++); // Write segment A, increment value copy_A2B(); _NOP(); } }

void write_SegA (uchar value) {

uchar *Flash_ptr; uint i;

Flash_ptr = (uchar *) 0x1080; FCTL1 = FWKEY + ERASE; FCTL3 = FWKEY; *Flash_ptr = 0;

FCTL1 = FWKEY + WRT;

for (i=0; i<128; i++) {

*Flash_ptr++ = value; }

FCTL1 = FWKEY; FCTL3 = FWKEY + LOCK; }

void copy_A2B (void) {

uchar *Flash_ptrA; uchar *Flash_ptrB; uint i;

Flash_ptrA = (uchar *) 0x1080; Flash_ptrB = (uchar *) 0x1000; FCTL1 = FWKEY + ERASE;

// Copy segment A to B

// SET BREAKPOINT HERE // Flash pointer // Initialize Flash pointer // Set Erase bit // Clear Lock bit

// Dummy write to erase Flash segment // Set WRT bit for write operation // Write value to flash // Clear WRT bit // Set LOCK bit // Segment A pointer // Segment B pointer // Initialize Flash segment A pointer // Initialize Flash segment B pointer // Set Erase bit

21

FCTL3 = FWKEY; // Clear Lock bit

*Flash_ptrB = 0; // Dummy write to erase Flash segment B FCTL1 = FWKEY + WRT; // Set WRT bit for write operation

for (i=0; i<128; i++) {

DataBuffer[i] = *Flash_ptrA++;

*Flash_ptrB++ = DataBuffer[i]; // Copy value segment A to segment B }

FCTL1 = FWKEY; // Clear WRT bit FCTL3 = FWKEY + LOCK; // Set LOCK bit }

//****************************************************************************** // MSP-FET430P140 Demo - USART0, Ultra-Low Pwr UART 2400 Echo ISR, 32kHz ACLK //

// Description: Echo a received character, RX ISR used. In the Mainloop UART0 // is made ready to receive one character with interrupt active. The Mainloop // waits in LPM3. The UART0 ISR forces the Mainloop to exit LPM3 after // receiving one character which echo's back the received character.

// ACLK = UCLK0 = LFXT1 = 32768, MCLK = SMCLK = DCO~ 800k

// Baud rate divider with 32768hz XTAL @2400 = 32768Hz/2400 = 13.65 (000Dh) // //* An external watch crystal is required on XIN XOUT for ACLK *// //

// MSP430F149 // -----------------

// /|\\| XIN|-

// | | | 32kHz // --|RST XOUT|- // | |

// | P3.4|-----------> // | | 2400 - 8N1 // | P3.5|<----------- // //

// M. Buccini

// Texas Instruments Inc. // Feb 2005

// Built with IAR Embedded Workbench Version: 3.21A

//******************************************************************************

#include #include \

22

void main(void) {

BoardConfig(0xb8);

WDTCTL = WDTPW + WDTHOLD; // Stop WDT

P3SEL |= 0x30; // P3.4,5 = USART0 TXD/RXD ME1 |= UTXE0 + URXE0; // Enable USART0 TXD/RXD UCTL0 |= CHAR; // 8-bit character UTCTL0 |= SSEL0; // UCLK = ACLK UBR00 = 0x0D; // 32k/2400 - 13.65 UBR10 = 0x00; //

UMCTL0 = 0x6B; // Modulation

UCTL0 &= ~SWRST; // Initialize USART state machine IE1 |= URXIE0; // Enable USART0 RX interrupt

// Mainloop for (;;) {

_BIS_SR(LPM3_bits + GIE); // Enter LPM3 w/interrupt while (!(IFG1 & UTXIFG0)); // USART0 TX buffer ready? TXBUF0 = RXBUF0; // RXBUF0 to TXBUF0 } }

// UART0 RX ISR will for exit from LPM3 in Mainloop #pragma vector=UART0RX_VECTOR __interrupt void usart0_rx (void) {

_BIC_SR_IRQ(LPM3_bits); // Clear LPM3 bits from 0(SR) }

//****************************************************************************** // MSP-FET430P140 Demo - USART0, Ultra-Low Pwr UART 9600 Echo ISR, 32kHz ACLK //

// Description: Echo a received character, RX ISR used. In the Mainloop UART0 // is made ready to receive one character with interrupt active. The Mainloop // waits in LPM3. The UART0 ISR forces the Mainloop to exit LPM3 after // receiving one character which echo's back the received character.

// ACLK = UCLK0 = LFXT1 = 32768, MCLK = SMCLK = DCO~ 800k

// Baud rate divider with 32768hz XTAL @9600 = 32768Hz/9600 = 3.41 (0003h 4Ah ) // //* An external watch crystal is required on XIN XOUT for ACLK *// //

// MSP430F149 // -----------------

// /|\\| XIN|-

// | | | 32kHz // --|RST XOUT|-

23

// | |

// | P3.4|-----------> // | | 9600 - 8N1 // | P3.5|<----------- // //

// M. Buccini

// Texas Instruments Inc. // Feb 2005

// Built with IAR Embedded Workbench Version: 3.21A

//******************************************************************************

#include #include \

void main(void) {

BoardConfig(0xb8);

WDTCTL = WDTPW + WDTHOLD; // Stop WDT

P3SEL |= 0x30; // P3.4,5 = USART0 TXD/RXD ME1 |= UTXE0 + URXE0; // Enable USART0 TXD/RXD UCTL0 |= CHAR; // 8-bit character UTCTL0 |= SSEL0; // UCLK = ACLK UBR00 = 0x03; // 32k/9600 - 3.41 UBR10 = 0x00; //

UMCTL0 = 0x4A; // Modulation

UCTL0 &= ~SWRST; // Initialize USART state machine IE1 |= URXIE0; // Enable USART0 RX interrupt

// Mainloop for (;;) {

_BIS_SR(LPM3_bits + GIE); // Enter LPM3 w/interrupt while (!(IFG1 & UTXIFG0)); // USART0 TX buffer ready? TXBUF0 = RXBUF0; // RXBUF0 to TXBUF0 } }

// UART0 RX ISR will for exit from LPM3 in Mainloop #pragma vector=UART0RX_VECTOR __interrupt void usart0_rx (void) {

_BIC_SR_IRQ(LPM3_bits); // Clear LPM3 bits from 0(SR) }

24

//****************************************************************************** // MSP-FET430P140 Demo - USART0, UART 19200 Echo ISR, XT2 HF XTAL ACLK //

// Description: Echo a received character, RX ISR used. Normal mode is LPM0, // USART0 RX interrupt triggers TX Echo. Though not required, MCLK = XT2. // ACLK = n/a, MCLK = SMCLK = UCLK0 = XT2 = 8MHz

// Baud rate divider with 8Mhz XTAL @19200 = 8MHz/19200 = 416.66 ~ 417 (01A0h) // //* An external 8MHz XTAL on X2IN X2OUT is required for XT2CLK *// // //* Min Vcc required varies with MCLK frequency - refer to datasheet *// // //

// MSP430F149 // -----------------

// /|\\| XT2IN|-

// | | | 8Mhz // --|RST XT2OUT|- // | |

// | P3.4|------------> // | | 19200 - 8N1 // | P3.5|<------------ // //

// M. Buccini

// Texas Instruments Inc. // Feb 2005

// Built with IAR Embedded Workbench Version: 3.21A

//******************************************************************************

#include #include \

void main(void) {

volatile unsigned int i;

BoardConfig(0xb8);

P3SEL |= 0x30; // P3.4,5 = USART0 TXD/RXD WDTCTL = WDTPW + WDTHOLD; // Stop WDT

BCSCTL1 &= ~XT2OFF; // XT2on do {

IFG1 &= ~OFIFG; // Clear OSCFault flag for (i = 0xFF; i > 0; i--); // Time for flag to set

25

}

while ((IFG1 & OFIFG)); // OSCFault flag still set?

BCSCTL2 |= SELM_2 + SELS; // MCLK = SMCLK = XT2 (safe)

ME1 |= UTXE0 + URXE0; // Enable USART0 TXD/RXD UCTL0 |= CHAR; // 8-bit character UTCTL0 |= SSEL1; // UCLK = SMCLK UBR00 = 0xA0; // 8Mhz/19200 ~ 417 UBR10 = 0x01; //

UMCTL0 = 0x00; // no modulation

UCTL0 &= ~SWRST; // Initialize USART state machine IE1 |= URXIE0; // Enable USART0 RX interrupt

_BIS_SR(LPM0_bits + GIE); // Enter LPM0 w/ interrupt }

#pragma vector=UART0RX_VECTOR __interrupt void usart0_rx (void) {

while (!(IFG1 & UTXIFG0)); // USART0 TX buffer ready? TXBUF0 = RXBUF0; // RXBUF0 to TXBUF0 }

//****************************************************************************** // MSP-FET430P140 Demo - USART0, UART 115200 Echo ISR, XT2 HF XTAL ACLK //

// Description: Echo a received character, RX ISR used. Normal mode is LPM0, // USART0 RX interrupt triggers TX Echo. Though not required, MCLK= XT2. // ACLK = n/a, MCLK = SMCLK = UCLK0 = XT2 = 8MHz

// Baud rate divider with 8Mhz XTAL = 8000000/115200 = 0069 (0045h) // //* An external 8MHz XTAL on X2IN X2OUT is required for XT2CLK *// // //* Min Vcc required varies with MCLK frequency - refer to datasheet *// // //

// MSP430F149 // -----------------

// /|\\| XT2IN|-

// | | | 8Mhz // --|RST XT2OUT|- // | |

// | P3.4|------------> // | | 115200 - 8N1 // | P3.5|<------------ // //

26

// M. Buccini

// Texas Instruments Inc. // Feb 2005

// Built with IAR Embedded Workbench Version: 3.21A

//******************************************************************************

#include #include \

void main(void) {

volatile unsigned int i;

BoardConfig(0xb8);

WDTCTL = WDTPW + WDTHOLD; P3SEL |= 0x30;

BCSCTL1 &= ~XT2OFF; do {

IFG1 &= ~OFIFG; for (i = 0xFF; i > 0; i--); }

while ((IFG1 & OFIFG));

BCSCTL2 |= SELM_2 + SELS; ME1 |= UTXE0 + URXE0; UCTL0 |= CHAR; UTCTL0 |= SSEL1; UBR00 = 0x45; UBR10 = 0x00; UMCTL0 = 0x00; UCTL0 &= ~SWRST; IE1 |= URXIE0;

_BIS_SR(LPM0_bits + GIE); }

#pragma vector=UART0RX_VECTOR __interrupt void usart0_rx (void) {

while (!(IFG1 & UTXIFG0)); TXBUF0 = RXBUF0;

// Stop WDT

// P3.4,5 = USART0 TXD/RXD // XT2on // Clear OSCFault flag // Time for flag to set // OSCFault flag still set? // MCLK = SMCLK = XT2 (safe) // Enable USART0 TXD/RXD // 8-bit character // UCLK = SMCLK // 8MHz 115200 // 8MHz 115200

// 8MHz 115200 modulation

// Initialize USART state machine // Enable USART0 RX interrupt // Enter LPM0 w/ interrupt // USART0 TX buffer ready? // RXBUF0 to TXBUF0

27

}

//****************************************************************************** // MSP-FET430P140 Demo - ADC12, Sample A0, Set P3.4 if A0 > 0.5*AVcc //

// Description: A single sample is made on A0 with reference to AVcc. // Software sets ADC10SC to start sample and conversion - ADC12SC

// automatically cleared at EOC. ADC12 internal oscillator times sample (16x)

// and conversion. In Mainloop MSP430 waits in LPM0 to save power until ADC12 // conversion complete, ADC12_ISR will force exit from LPM0 in Mainloop on // reti. If A0 > 0.5*AVcc, P3.4 set, else reset. //

// MSP430F149 // -----------------

// /|\\| XIN|- // | | | // --|RST XOUT|- // | |

// Vin-->|P6.0/A0 P3.4|--> LED //

// Dasheng

// LiTian Electronic Inc. // Feb 2008

// Built with IAR Embedded Workbench Version: 3.42A

//******************************************************************************

#include #include \

void main(void) {

WDTCTL = WDTPW + WDTHOLD; // Stop WDT BoardConfig(0xb8);

ADC12CTL0 = SHT0_2 + ADC12ON; // Set sampling time, turn on ADC12 ADC12CTL1 = SHP; // Use sampling timer ADC12IE = 0x01; // Enable interrupt

ADC12CTL0 |= ENC; // Conversion enabled P6SEL |= 0x01; // P6.0 ADC option select P3DIR |= BIT4; // P3.4 output

for (;;) {

ADC12CTL0 |= ADC12SC; // Sampling open

_BIS_SR(CPUOFF + GIE); // LPM0, ADC12_ISR will force exit }

28

}

// ADC12 interrupt service routine #pragma vector=ADC_VECTOR __interrupt void ADC12_ISR (void) {

if (ADC12MEM0 < 0x7FF)

P3OUT &= ~BIT4; // Clear P3.4 LED off else

P3OUT |= BIT4; // Set P3.4 LED on

_BIC_SR_IRQ(CPUOFF); // Clear CPUOFF bit from 0(SR) }

//****************************************************************************** // MSP-FET430P140 Demo - ADC12, Using the Internal Reference //

// Description: This example shows how to use the internal reference of the ADC12. // It uses the internal 2.5V reference and performs a single conversion

// on channel A0. The conversion results are stored in ADC12MEM0. Test by // applying a voltage to channel A0, then setting and running to a break point // at the \// register window in debugger and view the contents of ADC12MEM0. // //

// MSP430F149 // ---------------

// | | // Vin -->|P6.0/A0 | // | | // //

// M. Mitchell

// Texas Instruments Inc. // Feb 2005

// Built with IAR Embedded Workbench Version: 3.21A

//******************************************************************************

#include #include \

void main(void) {

volatile unsigned int i;

WDTCTL = WDTPW+WDTHOLD; // Stop watchdog timer BoardConfig(0xb8);

P6SEL |= 0x01; // Enable A/D channel A0

29

ADC12CTL0 = ADC12ON+SHT0_2+REFON+REF2_5V; // Turn on and set up ADC12 ADC12CTL1 = SHP; // Use sampling timer ADC12MCTL0 = SREF_1; // Vr+=Vref+

for ( i=0; i<0x3600; i++) // Delay for reference start-up { }

ADC12CTL0 |= ENC; // Enable conversions

while (1) {

ADC12CTL0 |= ADC12SC; // Start conversion while ((ADC12IFG & BIT0)==0);

_NOP(); // SET BREAKPOINT HERE } }

//***************************************************************************** // MSP-FET430P140 Demo - ADC12, Sample A10 Temp, Set P1.0 if Temp ++ ~2C //

// Description: Use ADC12 and the integrated temperature sensor to detect // temperature gradients. The temperature sensor output voltage is sampled // ~ every 80ms and compared with the defined delta values using an ISR. // (ADC12OSC/256)/ determines sample time which needs to be greater than // 30us for temperature sensor.

// ADC12 is operated in repeat-single channel mode with the sample and

// convert trigger sourced from Timer_A CCR1. The ADC12MEM0_IFG at the end // of each converstion will trigger an ISR.

// ACLK = n/a, MCLK = SMCLK = default DCO ~ 800k, ADC12CLK = ADC12OSC //

// MSP430F149 // -----------------

// /|\\| XIN|- // | | | // --|RST XOUT|- // | |

// |A10 P3.4|-->LED //

// A. Dannenberg

// Texas Instruments Inc. // Feb 2005

// Built with IAR Embedded Workbench Version: 3.21A

//******************************************************************************

#include

30

#include \

#define ADCDeltaOn 12 // ~2 Deg C delta

static unsigned int FirstADCVal; // holds 1st ADC result

void main(void) {

WDTCTL = WDTPW + WDTHOLD; // Stop watchdog BoardConfig(0xb8);

P3OUT = 0x00; // Clear P1 P3DIR = BIT4; // P3.4 as output

ADC12CTL1 = SHS_1 + SHP + CONSEQ_2; // TA trig., rpt conv. ADC12MCTL0 = SREF_1 + INCH_10; // Channel A10, Vref+ ADC12IE = 0x01; // Enable ADC12IFG.0

ADC12CTL0 = SHT0_8 + REF2_5V + REFON + ADC12ON + ENC; // Config ADC12 TACCTL1 = OUTMOD_4; // Toggle on EQU1 (TAR = 0) TACTL = TASSEL_2 + MC_2; // SMCLK, cont-mode while (!(0x01 & ADC12IFG)); // First conversion?

FirstADCVal = ADC12MEM0; // Read out 1st ADC value _BIS_SR(LPM0_bits + GIE); // Enter LPM0 w/ interrupt }

#pragma vector=ADC_VECTOR __interrupt void ADC12ISR (void) {

if (ADC12MEM0 <= FirstADCVal + ADCDeltaOn)

P3OUT &= ~BIT4; // LED off else P3OUT |= BIT4; // LED on }

/********************************************************* 程序功能:用固定频率的方波驱动蜂鸣器,共16种音调;在蜂鸣器 发出不同音调的同时,LED发光以二进制数字形式指示 当前音调的编号(1~16)

---------------------------------------------------------- 测试说明:聆听蜂鸣器发声的音调变化。

**********************************************************/ #include #include \

uchar step = 0xff;

/************************主函数************************/ void main( void ) {

31

uchar i;

WDTCTL = WDTPW + WDTHOLD; //关狗

BoardConfig(0xb0); //关数码管、流水灯和电平转换

/*------选择系统主时钟为8MHz-------*/

BCSCTL1 &= ~XT2OFF; //打开XT2高频晶体振荡器 do {

IFG1 &= ~OFIFG; //清除晶振失败标志 for (i = 0xFF; i > 0; i--); //等待8MHz晶体起振 }

while ((IFG1 & OFIFG)); //晶振失效标志仍然存在?

BCSCTL2 |= SELM_2 + SELS; //MCLK和SMCLK选择高频晶振

TACCTL0 |= CCIE; //使能比较中断

TACTL |= TASSEL_2 + ID_3 ; //计数时钟选择SMLK=8MHz,1/8分频后为1MHz

TBCCR0 = 4096*2 - 1; //周期两秒 TBCCTL0 |= CCIE;

TBCTL |= TBSSEL_1 + ID_3 + MC_1; //时钟源ACLK/8,up mode

P6DIR |= BIT7; //蜂鸣器对应IO设置为输出 P2DIR = 0xff; P2OUT = 0xff;

_EINT();

LPM1; }

/******************************************* 函数名称:Timer_A

功 能:定时器A的中断服务函数,在这里驱动 蜂鸣器发声 参 数:无 返回值 :无

********************************************/ #pragma vector=TIMERA0_VECTOR __interrupt void Timer_A (void) {

P6OUT ^= BIT7; // Toggle P6.7 }

/******************************************* 函数名称:Timer_B

功 能:定时器B的中断服务函数,在这里更改

32

蜂鸣器发声频率 参 数:无 返回值 :无

********************************************/ #pragma vector=TIMERB0_VECTOR __interrupt void Timer_B (void) {

if(step == 0xff) //up mode TACTL |= MC_1; step++; switch(step) {

case 0: TACCR0 = 5000; P2OUT = ~1; break; // 100Hz case 1: TACCR0 = 2500; P2OUT = ~2; break; // 200Hz case 2: TACCR0 = 1250; P2OUT = ~3; break; // 400Hz case 3: TACCR0 = 625; P2OUT = ~4; break; // 800Hz case 4: TACCR0 = 500; P2OUT = ~5; break; // 1KHz case 5: TACCR0 = 250; P2OUT = ~6; break; // 2KHz case 6: TACCR0 = 167; P2OUT = ~7; break; // 3KHz case 7: TACCR0 = 125; P2OUT = ~8; break; // 4KHz case 8: TACCR0 = 100; P2OUT = ~9; break; // 5KHz case 9: TACCR0 = 83; P2OUT = ~10; break; // 6KHz case 10: TACCR0 = 71; P2OUT = ~11; break; // 7KHz case 11: TACCR0 = 63; P2OUT = ~12; break; // 8KHz case 12: TACCR0 = 56; P2OUT = ~13; break; // 9KHz case 13: TACCR0 = 50; P2OUT = ~14; break; // 10KHz case 14: TACCR0 = 33; P2OUT = ~15; break; // 15KHz case 15: TACCR0 = 25; P2OUT = ~16; break; // 20KHz case 16: step = 0xff; // 循环播放 } }

//《祝你平安》对应的编码 const unsigned char SONG[]= {

0x26,0x20,0x20,0x20,0x20,0x20,0x26,0x10,0x20, 0x10,0x20,0x80,0x26,0x20,0x30,0x20,0x30,0x20, 0x39,0x10,0x30,0x10,0x30,0x80,0x26,0x0,0x20, 0x20,0x20,0x20,0x1c,0x20,0x20,0x80,0x2b,0x20, 0x26,0x20,0x20,0x20,0x2b,0x10,0x26,0x10,0x2b, 0x80,0x26,0x20,0x30,0x20,0x30,0x20,0x39,0x10, 0x26,0x10,0x26,0x60,0x40,0x10,0x39,0x10,0x26, 0x20,0x30,0x20,0x30,0x20,0x39,0x10,0x6,0x10, 0x26,0x80,0x26,0x20,0x2b,0x10,0x2b,0x10,0x2b, 0x20,0x30,0x10,0x39,0x10,0x26,0x10,0x2b,0x10, 0x2b,0x20,0x2b,0x40,0x40,0x20,0x20,0x10,0x20,

33

0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x18,0x20, 0x18,0x20,0x26,0x20,0x20,0x20,0x20,0x40,0x26, 0x20,0x2b,0x20,0x30,0x20,0x30,0x20,0x1c,0x20, 0x20,0x20,0x20,0x80,0x1c,0x20,0x1c,0x20,0x1c, 0x20,0x30,0x20,0x30,0x60,0x39,0x10,0x30,0x10, 0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x10,0x26, 0x10,0x26,0x10,0x2b,0x10,0x2b,0x80,0x18,0x20, 0x18,0x20,0x26,0x20,0x20,0x20,0x20,0x60,0x26, 0x10,0x2b,0x20,0x30,0x20,0x30,0x20,0x1c,0x20, 0x20,0x20,0x20,0x80,0x26,0x20,0x30,0x10,0x30, 0x10,0x30,0x20,0x39,0x20,0x26,0x10,0x2b,0x10, 0x2b,0x20,0x2b,0x40,0x40,0x10,0x40,0x10,0x20, 0x10,0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80, 0x00 };

/********************************************* 程序功能:MCU控制蜂鸣器演奏歌曲《祝你平安》 ---------------------------------------------- 测试说明:聆听蜂鸣器“唱出”的乐曲

*********************************************/ #include #include \#include \

#define Buzzer BIT7 #define Buzzer_Port P6OUT #define Buzzer_DIR P6DIR

uchar counter;

void Play_Song(void);

/***************主函数****************/ void main(void) {

uchar i;

WDTCTL = WDTPW + WDTHOLD; //关闭看门狗 /*------选择系统主时钟为8MHz-------*/

BCSCTL1 &= ~XT2OFF; // 打开XT2高频晶体振荡器 do {

IFG1 &= ~OFIFG; //清除晶振失败标志 for (i = 0xFF; i > 0; i--); // 等待8MHz晶体起振 }

34

while ((IFG1 & OFIFG)); // 晶振失效标志仍然存在?

BCSCTL2 |= SELM_2 + SELS; //主时钟和从时钟都选择高频晶振

BoardConfig(0xf8); //关闭数码管、流水灯、电平转换

//设置定时器A每10ms中断一次 CCTL0 = CCIE; CCR0 = 10000;

TACTL |= TASSEL_2 + ID_3;

//设置控制蜂鸣器的IO方向为输出 Buzzer_DIR |= Buzzer; //打开全局中断 _EINT();

//循环演奏歌曲 while(1) {

Play_Song(); } }

/******************************************* 函数名称:TimerA_ISR

功 能:定时器A的中断服务函数 参 数:无 返回值 :无

********************************************/ #pragma vector = TIMERA0_VECTOR __interrupt void TimerA_ISR(void) {

counter++; }

/******************************************* 函数名称:Delay_Nms

功 能:延时N个ms的函数 参 数:n--延时长度 返回值 :无

********************************************/ void Delay_Nms(uchar n) {

uchar i,j;

for( i = 0;i < n; i++ ) {

for( j = 0;j < 3;j++ ) _NOP();

35

} }

/******************************************* 函数名称:Play_Song

功 能:播放《祝你平安》的乐曲 参 数:无 返回值 :无

********************************************/ void Play_Song(void) {

uchar Temp1,Temp2; uchar addr = 0;

counter = 0; //中断计数器清0 while(1) {

Temp1 = SONG[addr++];

if ( Temp1 == 0xFF ) //休止符 {

TACTL &=~MC_1; //停止计数 Delay_Nms(100); }

else if ( Temp1 == 0x00 ) //歌曲结束符 {

return; } else {

Temp2 = SONG[addr++];

TACTL |=MC_1; //开始计数 while(1) {

Buzzer_Port ^= Buzzer; Delay_Nms(Temp1); if ( Temp2 == counter ) {

counter = 0; break; } } } } }

36

/*************************************************** 程序功能:用扫描方式读取四个独立式按键的键值,同时将 按键的键值在数码管上显示出来 ----------------------------------------------------

测试说明:按动K1~k4四个按键,观察数码管显示

***************************************************/ #include #include \

#define keyin (P1IN & 0x0f)

//数码管7位段码:0--f

uchar scandata[16] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,

0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};

void delay(void);

/********************主函数********************/ void main( void ) {

uchar temp,keyval = 0;

WDTCTL = WDTPW + WDTHOLD; //关闭看门狗

BoardConfig(0x88); //打开数码管,关闭流水灯和电平转换

P1DIR = BIT7; //设置P1.0~P.3为输入状态,P.7为输出 P1OUT = 0;

P3DIR |= BIT4; //设置P3.4为输出状态 P3OUT |= BIT4; //P3.4输出1 P4DIR = 0xff; P5DIR = 0xff; P4OUT = 0x3f; P5OUT = 0xf7; while(1) {

if(keyin != 0x0f) //如果有键被按下 {

delay(); //延时消抖

if(keyin != 0x0f) //再次检测按键状态 {

temp=keyin;

while(keyin != 0x0f); //等待按键被放开 switch(temp) //转换键值 {

case 0x0e:

37

keyval = 1;break; case 0x0d:

keyval = 2;break; case 0x0b:

keyval = 3;break; case 0x07:

keyval = 4;break; default:

keyval = 0;break; }

P4OUT = scandata[keyval]; //用一位数码管显示

P3OUT &= ~BIT4; //P3.4连接的LED闪烁一下 delay();delay(); P3OUT |= BIT4; } } } }

/******************************************* 函数名称:delay

功 能:用于消抖的延时 参 数:无 返回值 :无

********************************************/ void delay(void) {

uint tmp;

for(tmp = 12000;tmp > 0;tmp--); }

/*************************************************** 程序功能:用中断方式读取四个独立式按键的键值,同时将 按键的键值在数码管上显示出来 ----------------------------------------------------

测试说明:按动K1~k4四个按键,观察数码管显示

***************************************************/ #include #include \

#define keyin (P1IN & 0x0f)

//数码管7位段码:0--f

uchar scandata[16] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,

0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};

38

uchar KeyVal = 0; // 按键的键值

void delay(void);

/********************主函数********************/ void main( void ) {

WDTCTL = WDTPW + WDTHOLD; //关闭看门狗

BoardConfig(0x88); //打开数码管,关闭流水灯和电平转换

P1IES = 0x0f; // P1.0~P1.3选择下降沿中断 P1IE = 0x0f; // 打开中断使能

P1DIR = BIT7; //设置P1.0~P.3为输入状态,P.7为输出 P1OUT = 0; P4DIR = 0xff; P5DIR = 0xff; P4OUT = 0x3f; P5OUT = 0xf7;

_EINT(); //打开全局中断控制位 while(1) {

LPM1;

P4OUT = scandata[KeyVal]; } }

/******************************************* 函数名称:delay

功 能:用于消抖的延时 参 数:无 返回值 :无

********************************************/ void delay(void) {

uint tmp;

for(tmp = 12000;tmp > 0;tmp--); }

/******************************************* 函数名称:PORT1_ISR

功 能:P1端口的中断服务函数 参 数:无 返回值 :无

********************************************/ #pragma vector=PORT1_VECTOR __interrupt void PORT1_ISR(void)

39

{

if(P1IFG & 0x0f) {

switch(P1IFG) {

case 0x01:

if(keyin == 0x0e) //如果是第一个按键被按下 {

delay();

case 0x02:

case 0x04:

case 0x08:

if(keyin == 0x0e) {

while(keyin != 0x0f); //等待按键放开 KeyVal = 1; LPM1_EXIT; P1IFG = 0; return; } } if(keyin == 0x0d) //如果是第二个按键被按下 {

delay();

if(keyin == 0x0d) {

while(keyin != 0x0f); //等待按键放开 KeyVal = 2; LPM1_EXIT; P1IFG = 0; return; } } if(keyin == 0x0b) //如果是第三个按键被按下 {

delay();

if(keyin == 0x0b) {

while(keyin != 0x0f); //等待按键放开 KeyVal = 3; LPM1_EXIT; P1IFG = 0; return; } } 40

本文来源:https://www.bwwdw.com/article/2kl.html

Top