通过点灯入门430.doc_第1页
通过点灯入门430.doc_第2页
通过点灯入门430.doc_第3页
通过点灯入门430.doc_第4页
通过点灯入门430.doc_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

开始学习430了,本以为有了51的基础学起来会飞快,谁知430的资源太过丰富,一时眼花缭乱,各种寄存器就让我纠结的要死了,开始几天就看例程,在头文件里查阅各种寄存器的功能,然后运用点亮LED灯的方法写程序,学习IO口的设定,中断,定时器。现在还是菜鸟一枚,小小总结一下入门的经验,新手可以参考一下。首先写了这个程序,最简单了,点亮一个灯#includevoid main() WDTCTL = WDTPW + WDTHOLD;/停止开门狗 P2DIR = 0X01; P2OUT = 0xfe; 然后试着写了一个独立按键开关灯的程序,调了一天,还是没有得到满意的结果,还是把代码贴下来,希望能有人批评指正。/*实验目的:K1控制D8,使用中断*/#include/unsigned char f = 0;void main() WDTCTL = WDTPW + WDTHOLD; P2DIR = 0X01;/设置P2.0引脚为输出模式 P2OUT = 0Xef;/刚开始灯全不亮 P1IES = BIT0;/P1.1下降沿触发中断 P1IFG = 0x00;/中断标志位清零 P1IE = BIT0;/允许P1.0位中端 _EINT(); while(1) #pragma vector = PORT1_VECTOR_interrupt void P1_Interrupt(void) if(P1IFG = 0X02)/检测到P1.0触发了中断 P1IFG = 0X00;/中断标志位清零 P2OUT = 0XFE; 接下来是用看门狗作为定时器来控制灯一秒闪烁一次以下两程序,大概的意思是_BIS_SR();/开启括号内的内容_BIS_SR_IRQ();关闭括号内的内容#define LPM3 _BIC_SR (LPM3_bits)#define LPM3_EXIT _BIC_SR_IRQ(LPM3_bits)_BIS_SR(LPM3_bits + GIE);的意思是,开启低功耗,打开中断,但我写两行,LPM3;_EINT();/开启中断函数代替就不行,也不知道为什么/用定时器控制D8一秒闪一下#includevoid P_init()/端口初始化函数/ WDTCTL = WDTPW + WDTHOLD; BCSCTL1 |= DIVA_1; WDTCTL = WDT_ADLY_1000;/定时1秒 IE1 |= WDTIE;/ P2DIR = BIT0;/P2.0设置为输出 P2OUT =0XFE;/P2.0输出低电平 void main()/ unsigned int i;/LPMS_bits低功耗 P_init(); while(1) _BIS_SR(LPM3_bits + GIE);/打开低功耗模式,打开通用中断/还可以直接LPM3; /再单独打开总中断 _EINT();/开启总中断 P2OUT = 0x01; #pragma vector = WDT_VECTOR_interrupt void WDT_Interrupt() _BIC_SR_IRQ(LPM3_bits);/关闭低功耗/LPM3_EXIT;看门狗作为定时器来实现流水灯#includevoid P_init() WDTCTL = WDT_ADLY_1000;/定时一秒 IE1 |= WDTIE; P2DIR = 0XFF; P2OUT = 0XFE;void main() P_init(); while(1) _BIS_SR(LPM3_bits + GIE); P2OUT = 1; P2OUT +=1; if(P2OUT = 0XFF) P2OUT = 0XFE; #pragma vector = WDT_VECTOR_interrupt void WDT_Interrupt() LPM3_EXIT;/ _BIC_SR_IRQ(LPM3_bits);因为框架大致和上面差不多,所以没有注释,只是用来练手的。接下来就开始用定时器A了。#includevoid P_init() WDTCTL = WDTPW + WDTHOLD;/关狗 CCTL0 = CCIE; CCR0 = 10000; TACTL = TASSEL_2 + ID_3 + MC_1; /选择SMCLK时钟源(800kKZ),8分频,增计数模式 P2DIR = 0X01; P2OUT = 0Xff; _EINT();void main() P_init(); while(1) / _BIC_SR(LPM0_bits);/ P2OUT = 0X01; unsigned char i = 0;#pragma vector = TIMERA0_VECTOR_interrupt void Timer_A(void)/LPM0_EXIT; if(i 10) P2OUT = 0X01; i = 0; i+;很奇怪,在看门狗中使用的高低功耗切换的方法不管用了,只能在中断里切换模式。用那种办法,灯会亮很久然后灭掉。就算我给灯设的初始状态时是灭的他也会先亮。先不追究了,以后用的时候注意,尽量避免这种写法。在此算是入门了,接下来要开始模块的学习。最后强烈推荐新手多研究一下msp430x14x.h等几个头文件,430的寄存器都在里面宏定义,注释的蛮清楚的。/* Alternate register names */ by msp430x14x.h#define CCTL0 TACCTL0 /* Timer A Capture/Compare Control 0 */定时器捕获/比较控制0#define CCTL1 TACCTL1 /* Timer A Capture/Compare Control 1 */#define CCTL2 TACCTL2 /* Timer A Capture/Compare Control 2 */#define CCR0 TACCR0 /* Timer A Capture/Compare 0 */#define CCR1 TACCR1 /* Timer A Capture/Compare 1 */#define CCR2 TACCR2 /* Timer A Capture/Compare 2 */#define CCTL0_ TACCTL0_ /* Timer A Capture/Compare Control 0 */#define CCTL1_ TACCTL1_ /* Timer A Capture/Compare Control 1 */#define CCTL2_ TACCTL2_ /* Timer A Capture/Compare Control 2 */#define CCR0_ TACCR0_ /* Timer A Capture/Compare 0 */#define CCR1_ TACCR1_ /* Timer A Capture/Compare 1 */#define CCR2_ TACCR2_ /* Timer A Capture/Compare 2 */#define TASSEL1 (0x0200u) /* Timer A clock source select 0 */定时器的时钟源选择0#define TASSEL0 (0x0100u) /* Timer A clock source select 1 */#define ID1 (0x0080u) /* Timer A clock input divider 1 */#define ID0 (0x0040u) /* Timer A clock input divider 0 */#define MC1 (0x0020u) /* Timer A mode control 1 */#define MC0 (0x0010u) /* Timer A mode control 0 */#define TACLR (0x0004u) /* Timer A counter clear */#define TAIE (0x0002u) /* Timer A counter interrupt enable */#define TAIFG (0x0001u) /* Timer A counter interrupt flag */#define MC_0 (0*0x10u) /* Timer A mode control: 0 - Stop */#define MC_1 (1*0x10u) /* Timer A mode control: 1 - Up to CCR0 */#define MC_2 (2*0x10u) /* Timer A mode control: 2 - Continous up */#define MC_3 (3*0x10u) /* Timer A mode control: 3 - Up/Down */#define ID_0 (0*0x40u) /* Timer A input divider: 0 - /1 */#define ID_1 (1*0x40u) /* Timer A input divider: 1 - /2 */#define ID_2 (2*0x40u) /* Timer A input divider: 2 - /4 */#define ID_3 (3*0x40u) /* Timer A input divider: 3 - /8 */#define TASSEL_0 (0*0x100u) /* Timer A clock source select: 0 - TACLK */#define TASSEL_1 (1*0x100u) /* Timer A clock source select: 1 - ACLK */#define TASSEL_2 (2*0x100u) /* Timer A clock source select: 2 - SMCLK */#define TASSEL_3 (3*0x100u) /* Timer A clock source select: 3 - INCLK */#define CM1 (0x8000u) /* Capture mode 1 */#define CM0 (0x4000u) /* Capture mode 0 */#define CCIS1 (0x2000u) /* Capture input select 1 */#define CCIS0 (0x1000u) /* Capture input select 0 */#define SCS (0x0800u) /* Capture sychronize */#define SCCI (0x0400u) /* Latched capture signal (read) */#define CAP (0x0100u) /* Capture mode: 1 /Compare mode : 0 */#define OUTMOD2 (0x0080u) /* Output mode 2 */#define OUTMOD1 (0x0040u) /* Output mode 1 */#define OUTMOD0 (0x0020u) /* Output mode 0 */#define CCIE (0x0010u) /* Capture/compare interrupt enable */#define CCI (0x0008u) /* Capture input signal (read) */#define OUT (0x0004u) /* PWM Output signal if output mode 0 */#define COV (0x0002u) /* Capture/compare overflow flag */#define CCIFG (0x0001u) /* Capture/compare interrupt flag */#define OUTMOD_0 (0*0x20u) /* PWM output mode: 0 - output only */#define OUTMOD_1 (1*0x20u) /* PWM output mode: 1 - set */#define OUTMOD_2 (2*0x20u) /* PWM output mode: 2 - PWM toggle/reset */#define OUTMOD_3 (3*0x20u) /* PWM output mode: 3 - PWM set/reset */#define OUTMOD_4 (4*0x20u) /* PWM output mode: 4 - toggle */#define OUTMOD_5 (5*0x20u) /* PWM output mode: 5 - Reset */#define OUTMOD_6 (6*0x20u) /* PWM outp

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论