一些MSP430G2553简单程序
MSP430G2553捕获程序案例与经验分享
MSP430G2553捕获程序案例与经验分享MSP430G2553单片机定时器A有3个捕获比较寄存器CCR0,CCR1,CCR2.。
MSP430G2553捕获程序应用很广泛,电子工程师可以多加了解。
所谓捕获,就是我们来检测外围的信号跳变时刻(此时信号理解为数字信号,即脉冲),此信号乃为我们捕获的对象,可以测量信号的脉冲宽度,即频率等。
捕获首先需要考虑的初始化工作1.设置BCS模块,确定系统时钟MCLK子系统时钟SMCLK把MCLK设置为8MHZ,SMCLK设置为1MHZ。
2.捕获输入引脚的选择选择IO引脚时应查阅器件的手册,能够快速的查阅PDF资料找到正确的答案是一个程序员的基本素质。
3.程序设计思路根据测频的原理,需要2次捕获才能测量一次输入信号的频率。
因此要定义2个变量保存2次捕获结果。
变量是无符号的整数型变量(与捕获寄存器的字长匹配)。
输入信号与CPU的工作是异步的,所以设计程序的时候是不知道什么时候才有捕获输入。
程序处理何时发生了捕获的方法有2种一是查询的方法,定时器硬件在发生捕获事件后会置捕获中断表示CCIF为1,程序在主循环里不断的查询这个标志即可判断是否有捕获事件发生。
二是定时器中断法,当发生捕获事件时必产生定时器中断,在中断中读取捕获寄存器即可。
查询的方法不是好的程序设计方法,因为查询时要占用CPU,使得CPU不能再做其他任务。
中断的方法对初学者有一定的困难。
即中断程序如何与主程序通信(交换信息)。
理解中断及设计中断服务程序要困难一些。
捕获模式捕获外部输入的信号的上升沿或下降沿或上升沿下降沿都捕捉,当捕捉发生时,把TAR 的值装载到TACCRx中,同时也可以进入中断,执行相应的操作。
这样利用捕捉上升沿或。
基于msp430g2553的红外遥控小车解码控制程序
基于msp430g2553的红外遥控小车解码控制程序//遥控小车最终程序#include#define CPU_F ((double)12000000)//数字控制震荡器1MHZ#define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/12000000.0))//延时X微秒#define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/12000.0))//延时X毫秒char const redled[8]={0x07,0x00,0x01,0x02,0x03,0x04,0x05,0x06};//led测试版对应的八个灯unsigned char receive[2]={0x00,0x00};//数据码,数据反码unsigned char j=0,k=0,f=0,led=0;//中断次数,receive的元素,,找到按键地址数组的第f个元素int flag=1;//***************************主程序********************//void main(void){WDTCTL=WDTPW+WDTHOLD; //关闭看门狗BCSCTL1=CALBC1_1MHZ; //这两句的作用,基本时钟系统控制,数控震荡控制,将时钟校准1MHZDCOCTL=CALDCO_1MHZ;P1DIR|=BIT0+BIT6+BIT2+BIT3+BIT4;//P1端口的P1.0、P1.6设置为输出方向P2DIR|=0x0f; //P2的0,1,2,3设置为输出口P1OUT|=BIT0+BIT6; //P1.0、P1.6输出高电平,次单片机的VCC为3.56VP1IE|=0X02; //P1.1中断使能P1IES|=BIT1; //P1.1中断边沿选择,下降沿触发P1IFG=0; //清P1.1中断标志_BIS_SR(GIE); //开总中断while(1) //{if(receive[0]==0xa2){flag=1;}if(receive[0]==0xe2){flag=-1;}if(flag==1) //正转P1.0{P1OUT&=~BIT3;P1OUT&=~BIT4;switch(receive[0]){case 0x68:{P1OUT&=~BIT0;P1OUT&=~BIT2;break;} //0键case0x30:{{P1OUT|=BIT0;P1OUT|=BIT2;delay_ms(1);P1OUT&=~B IT0;P1OUT&=~BIT2;delay_m s(9);}break;}//1键case0x18:{{P1OUT|=BIT0;P1OUT|=BIT2;delay_ms(2);P1OUT&=~B IT0;P1OUT&=~BIT2;delay_m s(8);}break;}//2键case0x7a:{{P1OUT|=BIT0;P1OUT|=BIT2;delay_ms(3);P1OUT&=~B IT0;P1OUT&=~BIT2;delay_m s(7);}break;}//3键case0x10:{{P1OUT|=BIT0;P1OUT|=BIT2;delay_ms(4);P1OUT&=~BIT0;P1OUT&=~BIT2;delay_m s(6);}break;}//4键case0x38:{{P1OUT|=BIT0;P1OUT|=BIT2;delay_ms(5);P1OUT&=~B IT0;P1OUT&=~BIT2;delay_m s(5);}break;}//5键case0x5a:{{P1OUT|=BIT0;P1OUT|=BIT2;delay_ms(6);P1OUT&=~B IT0;P1OUT&=~BIT2;delay_m s(4);}break;}//6键case0x42:{{P1OUT|=BIT0;P1OUT|=BIT2;delay_ms(7);P1OUT&=~B IT0;P1OUT&=~BIT2;delay_m s(3);}break;}//7键case0x4a:{{P1OUT|=BIT0;P1OUT|=BIT2;delay_ms(8);P1OUT&=~B IT0;P1OUT&=~BIT2;delay_m s(2);}break;}//8键case 0x52:{P1OUT|=BIT0;P1OUT|=BIT2;break;} //9键}}else if(flag==-1) //反转P1.2{P1OUT&=~BIT0;P1OUT&=~BIT2;switch(receive[0]){case 0x68:{P1OUT&=~BIT3;P1OUT&=~BIT4;break;} //0键case0x30:{{P1OUT|=BIT3;P1OUT|=BIT4;delay_ms(1);P1OUT&=~B IT3;P1OUT&=~BIT4;delay_m s(9);}break;}//1键case0x18:{{P1OUT|=BIT3;P1OUT|=BIT4;delay_ms(2);P1OUT&=~B IT3;P1OUT&=~BIT4;delay_m s(8);}break;}//2键case0x7a:{{P1OUT|=BIT3;P1OUT|=BIT4;delay_ms(3);P1OUT&=~B IT3;P1OUT&=~BIT4;delay_m s(7);}break;}//3键case0x10:{{P1OUT|=BIT3;P1OUT|=BIT4;delay_ms(4);P1OUT&=~B IT3;P1OUT&=~BIT4;delay_m s(6);}break;}//4键case0x38:{{P1OUT|=BIT3;P1OUT|=BIT4;delay_ms(5);P1OUT&=~B IT3;P1OUT&=~BIT4;delay_m s(5);}break;}//5键case0x5a:{{P1OUT|=BIT3;P1OUT|=BIT4;delay_ms(6);P1OUT&=~B IT3;P1OUT&=~BIT4;delay_m s(4);}break;}//6键case0x42:{{P1OUT|=BIT3;P1OUT|=BIT4;delay_ms(7);P1OUT&=~B IT3;P1OUT&=~BIT4;delay_m s(3);}break;}//7键case0x4a:{{P1OUT|=BIT3;P1OUT|=BIT4;delay_ms(8);P1OUT&=~B IT3;P1OUT&=~BIT4;delay_m s(2);}break;}//8键case 0x52:{P1OUT|=BIT3;P1OUT|=BIT4;break;} //9键}}}}//*********************红外遥控器中断程序*******************//#pragma vector=PORT1_VECTOR //中断程序的格式:#pragma vector=中断矢量__interrupt void port1(void)//格式:__interrupt void 函数名(void){P1IFG=0X00; //清P1中断标志int count=0; //高电平持续时间计数值while(!(P1IN&BIT1)); //等电平变为高电平while(P1IN&BIT1) //计算高电平持续时间{count++;if(count>8000)return;//如果高电平持续时间过长则推出中断程序}if(j>16) //一体化红外接收头一接收遥控器信号,就会输出32位的脉冲序列波,其中后16位{ //决定遥控器的按键地址,16位由8位数据码和数据反码组成,我们需要将其解码//time[j-17]=count; //将记得的高电平持续时间放入时间数组中if(j==25)k++; //到数据反码的起始位的时候,我让receive数组元素下标+1receive[k]<<=1; //接收数据码左移一位,比如:xxxx xxxx 左移一位后xxxx xxx0if(count>80)receive[k]|=0x01;//高电平持续时间超过80,则将左移一位后的最低位变1,} //结果变为,xxxx xxx1,如果没超过80则保持不变,xxxx xxx0 j++;if(j>32){j=0;k=0; //解码结束,j,k值清零delay_ms(150);}}。
用MSP430G2553单片机控制Ht1621的程序及笔记
用MSP430G2553单片机控制Ht1621的程序及笔记型号:HT1621BS725G01012液晶全部显示时如下图:6位数字、3个小数点和右方电源标志全部点亮函数:/*********全部点亮**************/ void HT1621_all_on(uchar num) { uchar i;uchar addr=0;for(i=0;i<num;i++){Write_1621(addr,0xff);addr+=2;}}屏幕数字笔画对应编码:程序例子:#include "msp430g2553.h"#define uint unsigned int#define uchar unsigned char/***定义1621的端口******/#define DATA1 P2OUT|=BIT0//1621DATA端口#define DATA0 P2OUT&=~BIT0 #define WR1 P2OUT|=BIT1//1621WR0端口#define WR0 P2OUT&=~BIT1#define CS1 P2OUT|=BIT2//1621CS端口#define CS0 P2OUT&=~BIT2/******定义HT1621的命令*******/#define ComMode 0x52 //4COM,1/3bias 1000 010 1001 0#define RCosc 0x30 //内部RC振荡器(上电默认)1000 0011 0000#define LCD_on 0x06 //打开LCD 偏压发生器1000 0000 0 11 0#define LCD_off 0x04 //关闭LCD显示#define Sys_en 0x02 //系统振荡器开 1000 0000 0010 #define CTRl_cmd 0x80 //写控制命令#define Data_cmd 0xa0 //写数据命令char dispnum[6]={0x00,0x00,0x00,0x00,0x00,0x00};//用来存放要显示的数字const char num[]={0x7d,0x60,0x3e,0x7a,0x63,0x5b,0x5f,0x70,0x7f,0x7b};/*0,1,2,3,4,5,6,7,8,9*****//**************延时子程序**********/void delay_1us(void) //1us延时函数{asm("nop");}//N us延时函数void delaynus(uint n){uint i;for(i=0;i<n;i++)asm("nop");}void delay_1ms(void) //1ms延时函数 {unsigned int i;for (i=0;i<1140;i++); }void delay_nms(unsigned int n) //N ms延时函数 {unsigned int i=0;for (i=0;i<n;i++)delay_1ms();}/*****发送数据***********/void SendBit_1621(uchar sdata,uchar cnt) //data 的高cnt 位写入HT1621,高位在前{uchar i;for(i=0;i<cnt;i++){WR0;delaynus(20); //8M必须加,4M可省略if(sdata&0x80) DATA1;else DATA0;delaynus(20);//8M必须加,4M可省略WR1;delaynus(20); //8M必须加,4M可省略sdata<<=1;}delaynus(20);}/******送命令*****/void SendCmd_1621(uchar command){CS0;SendBit_1621(0x80,4); //写入标志码“100”和9 位command 命令,由于SendBit_1621(command,8); //没有使有到更改时钟输出等命令,为了编程方便CS1; //直接将command 的最高位写“0”}/******发送数据和命令**参数说明:addr:要在第几位显示***************************/ void Write_1621(uchar addr,uchar sdata) {addr<<=2;CS0;SendBit_1621(0xa0,3); //写入标志码“101”SendBit_1621(addr,6); //写入addr 的高6位,***6位显示SendBit_1621(sdata,8); //写入data的8位CS1;}/****清除显示********参数说明:num:要清除几位***********************/ void HT1621_all_off(uchar num) {uchar i;uchar addr=0;for(i=0;i<num;i++){Write_1621(addr,0x00);addr+=2;}}/*********全部点亮*******参数说明:num:要点亮几位***************************/ void HT1621_all_on(uchar num) { uchar i;uchar addr=0;for(i=0;i<num;i++){Write_1621(addr,0xff);addr+=2;}}/******全部点亮1621,显示同样的数字******* 参数说明:num:要显示几位****************************/void HT1621_all_on_num(uchar num,uchar xx){uchar i;uchar addr=0;for(i=0;i<num;i++){Write_1621(addr,xx);addr+=2;}}/*********(初始化1621)*************/ void Init_1621(void){SendCmd_1621(Sys_en);//系统振荡器开SendCmd_1621(RCosc); //内部RC振荡器SendCmd_1621(ComMode); //4COM,1/3bias 1000 010 1001 0 SendCmd_1621(LCD_on);//打开LCD 偏压发生器1000 0000 0 11 0}/***********初始化1621) *****************/ void LCDoff(void) {SendCmd_1621(LCD_off); }/*****Name: LCDon(初始化1621)***********/ void LCDon(void){SendCmd_1621(LCD_on); }void displayallnum(unsigned int n) //各位显示所有数码字符函数 { uchar i,j;uchar addr=0;for(i=0;i<n;i++){for(j=0;j<29;j++){Write_1621(addr,num[j]); delay_nms(100) ;}addr+=2;}}//测试第2位数码各位笔画,用于编写num数组 void displaybihua(void) { Write_1621(2,0x01);delay_nms(350) ;Write_1621(2,0x02);delay_nms(350) ;Write_1621(2,0x04);delay_nms(350) ;Write_1621(2,0x08);delay_nms(350) ;Write_1621(2,0x10);delay_nms(350) ;Write_1621(2,0x20);delay_nms(350) ;Write_1621(2,0x40);delay_nms(350) ;Write_1621(2,0x80);delay_nms(350) ; }//逐位显示各位8字+小数点void displayall8(void) {uchar i;HT1621_all_off(6);for(i=0;i<6;i++){Write_1621(2*i,0xff);delay_nms(50) ;}}//显示数组数据void displaydata(long int t,int p) {uchar i;dispnum[4]=num[t/10000];dispnum[3]=num[(t/1000)%10]; dispnum[2]=num[(t/100)%10]; dispnum[1]=num[(t/10)%10]; dispnum[0]=num[t%10];/*switch(p){case 1:sbi(dispnum[0], 4);break;case 2:sbi(dispnum[1], 4);break;case 3:sbi(dispnum[2], 4);break;default:break;}*/for(i=0;i<5;i++){Write_1621(i*2,dispnum[i]); }}void main(){WDTCTL = WDTPW + WDTHOLD; long int t=0;uchar i;P2DIR=0xff;Init_1621() ;//初始化HT1621_all_on(6);//全部点亮delay_nms(200);HT1621_all_off(16);//全部关闭delay_nms(50);for(i=0;i<10;i++){HT1621_all_on_num(6,num[i]);//点亮所有且显示相同数字delay_nms(100) ;}while(1){displaydata(t,2);t++;if(t>99999) t=0;delay_nms(20);}}。
单片机msp430g2553中文教程
24 TSSOP 封
LF,
装
-
1
DCO,
VLO
20 引脚
16 TSSOP 封
装
16
20 引脚 PDIP 封装
24
32 引脚 QFN 封装
28 引脚
24 TSSOP 封
LF,
装
-
1
DCO,
VLO
20 引脚
16 TSSOPyright © 2011–2012, Texas Instruments Incorporated
• 通用串行通信接口 (USCI) – 支持自动波特率检测的增强型通用异步收发器 (UART) – IrDA 编码器和解码器 – 同步 SPI – I2C™
• 用于模拟信号比较功能或者斜率模数 (A/D) 转换的 片载比较器
• 带内部基准、采样与保持以及自动扫描功能的 10 位 200-ksps 模数 (A/D) 转换器(见 表 1)
3
MSP430G2x53 MSP430G2x13
ZHCS178E – APRIL 2011 – REVISED JANUARY 2012
器件引出脚配置、MSP430G2x13 和 MSP430G2x53、20 引脚器件、 TSSOP 和 PDIP 封装
DVCC 1 P1.0/TA0CLK/ACLK/A0/CA0 2 P1.1/TA0.0/UCA0RXD/UCA0SOMI/A1/CA1 3 P1.2/TA0.1/UCA0TXD/PUCA0SIMO/A2/CA2 4 P1.3/ADC10CLK/CAOUT/VREF-/VEREF-/A3/CA3 5 P1.4/SMCLK/UCB0STE/UCA0CLK/VREF+/VEREF+/A4/CA4/TCK 6 P1.5/TA0.0/UCB0CLK/UCA0STE/A5/CA5/TMS 7
MSP430G2553入门
2.实验室要的是能踏下心来做事情的人
3.热烈欢迎真正想来实验室发展的同学
附:实验室基本情况和要求
实验室开门时间:每天在群里发布,请勿水群…… 单片机和学习的书不能带出实验室 爱护实验仪器,遇到问题找管理员同学(514)
MSP430 单片机
走进单片机的世界
——袁程磊
单片机是个微型的电脑主机
电脑有的功能单片机也都有
单片机+液晶屏
电脑有的功能单片机也都有
电脑有的功能单片机也都有
单片机的其他用途
MSP430G2553开发板介绍
仿真器
单片机
IO口 板载开关和LED
MSP430G2553单片机外围电路
附:发光二极管知识
Blink The LED 让LED灯闪烁起来
6.点击Finish,新建完成!
CCS的使用
<3>.出现下面的内容就对了
CCS的使用
<4>.上图是工具栏 点击工具栏里的小斧头,意为编译,用来查语法错误 没有报错的话 点击小绿虫,意为下载,用来将程序下载到单片机里
(如果出现如图所示错误提示,那就耐心等待驱动安装完毕)
<6>右移 >> 右移一位1001 0111 >>1 = 0100 1011
右移两位1001 0111 >>2 = 0010 0101
(低位舍弃,高位补0) 右移一位相当于除以2
C语言基础知识复习
<7>进制 二进制数:0和1 八进制数:0~7 十六进制数:0~f
一位八进制数
可以拆成
三个二进制数
四个二进制数
0x40 位
7 0
6 5 4 3 2 1 0 P1.6 0 相连的 0 LED 0 ?1 0 怎么点亮与
OPT3001 MSP430G2553完整程序
{
return 0;
}
else//否则初始化不成功
{
return 1;
}
}
//OPT3001寄存器配置
//12-15位RN[0,3]:0x1100,设置为Full-Scale Mode
OPT3001IIC_Init();//OPT3001端口初始化
OPT3001Config(); //配置OPT3001并且唤醒OPT3001
delay();
ManufacturerIDNum=GetOPT3001ManufacturerID();
delay();
DeviceIDNum=GetOPT3001DeviceID();
write1();
stop();//停止信号
}
//*********************************************************
//读出BMP085内部数据,连续两个
//*********************************************************
}
BCSCTL1 = CALBC1_1MHZ; // Set DCO
DCOCTL = CALDCO_1MHZ;
}
void Opt3001WriteRegister(unsigned char registerName, unsigned int value)
{
start(); //起始信号
write1byte(0x88); //发送设备地址+写信号
msp430g2553例程大全
MSPG2553 例程1.//************************************************************************* *****// LaunchPad Lab2 - Software Toggle P1.0,//// MSP430G2xx2// -----------------// /|\| XIN|-// | | |// --|RST XOUT|-// | |// | P1.0|-->LED////************************************************************************* *****#include <msp430g2553.h>void main(void){WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timerif (CALBC1_1MHZ == 0xFF || CALDCO_1MHZ == 0xFF){while(1); // If calibration constants erased, trap CPU!!}// Configure Basic ClockBCSCTL1 = CALBC1_1MHZ; // Set rangeDCOCTL = CALDCO_1MHZ; // Set DCO step + modulationBCSCTL3 |= LFXT1S_2; // Set LFXT1P1DIR = BIT6; // P1.6 output (green LED)P1OUT = 0; // LED offIFG1 &= ~OFIFG; // Clear OSCFault flagBCSCTL2 |=SELM_1 + DIVM_0; // Set MCLKfor(;;){P1OUT = BIT6; // P1.6 on (green LED)_delay_cycles(100);P1OUT = 0; // green LED off_delay_cycles(5000);}}2.//************************************************************************* *****// LaunchPad Lab3 - Software Port Interrupt Service//// MSP430G2xx2// -----------------// /|\| XIN|-// | | |// --|RST XOUT|-// /|\ | |// --o--|P1.3 P1.0|-->LED// \|/////************************************************************************* *****#include <msp430g2553.h>void main(void){WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timerP1DIR |= BIT0; // Set P1.0 to output directionP1IES |= BIT3; // P1.3 Hi/lo edgeP1IFG &= ~BIT3; // P1.3 IFG clearedP1IE |= BIT3; // P1.3 interrupt enabled_BIS_SR(LPM4_bits + GIE); // Enter LPM4 w/interrupt }// Port 1 interrupt service routine#pragma vector=PORT1_VECTOR__interrupt void Port_1(void){if (P1IFG & BIT3){P1OUT ^= BIT0; // P1.0 = toggleP1IFG &= ~BIT3; // P1.3 IFG cleared }}3.//************************************************************************* *****// LaunchPad Lab5 - ADC10, Sample A10 Temp and Convert to oC and oF//// MSP430G2452// -----------------// /|\| XIN|-// | | |// --|RST XOUT|-// | |// |A10 |////************************************************************************* *****#include "msp430g2553.h"long temp;long IntDegF;long IntDegC;void main(void){WDTCTL = WDTPW + WDTHOLD; // Stop WDT//Configure ADC10ADC10CTL1 = INCH_10 + ADC10DIV_3; // Choose ADC Channel as Temp SensorADC10CTL0 = SREF_1 + ADC10SHT_3 + REFON + ADC10ON + ADC10IE;//Choose ADC Ref source__enable_interrupt(); // Enable interrupts.TACCR0 = 30; // Delay to allow Ref to settleTACCTL0 |= CCIE; // Compare-mode interrupt.TACTL = TASSEL_2 | MC_1; // TACLK = SMCLK, Up mode.LPM0; // Wait for delay.TACCTL0 &= ~CCIE; // Disable timer Interrupt__disable_interrupt();while(1){ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start__bis_SR_register(LPM0_bits + GIE); // LPM0 with interrupts enabled// oF = ((A10/1024)*1500mV)-923mV)*1/1.97mV = A10*761/1024 - 468temp = ADC10MEM;IntDegF = ((temp - 630) * 761) / 1024;// oC = ((A10/1024)*1500mV)-986mV)*1/3.55mV = A10*423/1024 - 278temp = ADC10MEM;IntDegC = ((temp - 673) * 423) / 1024;__no_operation(); // SET BREAKPOINT HERE}}// ADC10 interrupt service routine#pragma vector=ADC10_VECTOR__interrupt void ADC10_ISR (void){__bic_SR_register_on_exit(LPM0_bits); // Clear CPUOFF bit from 0(SR)}#pragma vector=TIMER0_A0_VECTOR__interrupt void ta0_isr(void){TACTL = 0;__bic_SR_register_on_exit(LPM0_bits); // Clear CPUOFF bit from 0(SR)}4.//************************************************************************* *****// MSP430F20xx Demo - Basic Clock, Output Buffered SMCLK, ACLK and MCLK/10 //// Description: Buffer ACLK on P2.0, default SMCLK(DCO) on P1.4 and MCLK/10 on // P1.5.// ACLK = LFXT1 = VLO, MCLK = SMCLK = default DCO// //* External watch crystal installed on XIN XOUT is required for ACLK *////// MSP430F20xx// -----------------// /|\| XIN|-// | | |// --|RST XOUT|-// | |// | P1.4/SMCLK|-->SMCLK = Default DCO// | P1.5|-->MCLK/10 = DCO/10// | P1.0/ACLK|-->ACLK = VLO//// M. Buccini / L. Westlund// Texas Instruments Inc.// October 2005// Built with IAR Embedded Workbench Version: 3.40A//************************************************************************* *****#include <msp430x20x3.h>unsigned char s;void main(void){WDTCTL = WDTPW +WDTHOLD; // Stop Watchdog TimerBCSCTL3 |= LFXT1S_2; // LFXT1 = VLO//DCOCTL = 0;//BCSCTL1 = CALBC1_16MHZ;//DCOCTL = CALBC1_16MHZ;P1DIR |= 0x31; // P1.0,5 and P1.4 outputsP1SEL |= 0x11; // P1.0,4 ACLK/VLO, SMCLK/DCO output//SMCLK Sub-System Main Clk,ACLK和SMCLK可以通过复用引脚输出,MCLK 不能直接输出体现, MCLK可以配置为VLO或者DCOwhile(1){P1OUT |= 0x20; // P1.5 = 1, 通过开关P1.5来体现MCLK,这两条指令的周期大概为SMCLK的1/10P1OUT &= ~0x20;//20;}}5.//************************************************************************* *****// MSP430xG46x Demo - FLL+, Runs Internal DCO at 8MHz// Description: This program demonstrates setting the internal DCO to run at// 8MHz with auto-calibration by the FLL+.// ACLK = LFXT1 = 32768Hz, MCLK = SMCLK = DCO = (121+1) x 2 x ACLK = 7995392Hz// //* An external watch crystal between XIN & XOUT is required for ACLK *////// MSP430xG461x// -----------------// /|\| XIN|-// | | | 32kHz// --|RST XOUT|-// | |// | P1.1|--> MCLK = 8MHz// | |// | P1.5|--> ACLK = 32kHz// | |//// K. Quiring/ M. Mitchell// Texas Instruments Inc.// October 2006// Built with IAR Embedded Workbench Version: 3.41A//************************************************************************* ****#include <msp430xG46x.h>void main(void){WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timerFLL_CTL0 |= DCOPLUS + XCAP18PF; // DCO+ set, freq = xtal x D x N+1 SCFI0 |= FN_4; // x2 DCO freq, 8MHz nominal DCOSCFQCTL = 121; // (121+1) x 32768 x 2 = 7.99 MHzP1DIR = 0x22; // P1.1 & P1.5 to output directionP1SEL = 0x22; // P1.1 & P1.5 to output MCLK & ACLKwhile(1); // Loop in place}6.//************************************************************************* ***// MSP430xG46x 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 *////// MSP430xG461x// -----------------// /|\| XIN|-// | | |// --|RST XOUT|-// | |//// M. Mitchell// Texas Instruments Inc.// Feb 2005// Built with IAR Embedded Workbench Version: 3.21A//************************************************************************* *****#include <msp430xG46x.h>char value; // 8-bit value to write to segment A// Function prototypesvoid write_SegA (char value);void copy_A2B (void);void main(void){WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timerFCTL2 = FWKEY + FSSEL0 + FN0; // MCLK/2 for Flash Timing Generatorvalue = 0; // Initialize valuewhile(1) // Repeat forever{write_SegA(value++); // Write segment A, increment valuecopy_A2B(); // Copy segment A to B_NOP(); // SET BREAKPOINT HERE}}void write_SegA (char value){char *Flash_ptr; // Flash pointerunsigned int i;Flash_ptr = (char *) 0x1080; // Initialize Flash pointerFCTL1 = FWKEY + ERASE; // Set Erase bitFCTL3 = FWKEY; // Clear Lock bit*Flash_ptr = 0; // Dummy write to erase Flash segmentFCTL1 = FWKEY + WRT; // Set WRT bit for write operationfor (i=0; i<128; i++){*Flash_ptr++ = value; // Write value to flash}FCTL1 = FWKEY; // Clear WRT bitFCTL3 = FWKEY + LOCK; // Set LOCK bit}void copy_A2B (void){char *Flash_ptrA; // Segment A pointerchar *Flash_ptrB; // Segment B pointerunsigned int i;Flash_ptrA = (char *) 0x1080; // Initialize Flash segment A pointerFlash_ptrB = (char *) 0x1000; // Initialize Flash segment B pointerFCTL1 = FWKEY + ERASE; // Set Erase bitFCTL3 = FWKEY; // Clear Lock bit*Flash_ptrB = 0; // Dummy write to erase Flash segment B FCTL1 = FWKEY + WRT; // Set WRT bit for write operationfor (i=0; i<128; i++){*Flash_ptrB++ = *Flash_ptrA++; // Copy value segment A to segment B}FCTL1 = FWKEY; // Clear WRT bitFCTL3 = FWKEY + LOCK; // Set LOCK bit}7.//************************************************************************* *****// MSP430xG46x Demo - Software Port Interrupt on P1.0 from LPM4//// Description: A hi/low transition on P1.0 will trigger P1_ISR which,// toggles P2.1. Normal mode is LPM4 ~ 0.1uA. LPM4 current can be measured// with the LED removed, all unused P1.x/P2.x configured as output or inputs// pulled high or low, and ensure the P2.0 interrupt input does not float.// ACLK = 32.768kHz, MCLK = SMCLK = default DCO//// MSP430xG461x// -----------------// /|\| |// | | |// --|RST |// /|\ | |// --o--|P1.0 P2.1|-->LED// \|///// K. Quiring/ M. Mitchell// Texas Instruments Inc.// October 2006// Built with IAR Embedded Workbench Version: 3.41A//************************************************************************* *****#include <msp430xG46x.h>void main(void){WDTCTL = WDTPW + WDTHOLD; // Stop WDTFLL_CTL0 |= XCAP14PF; // Configure load capsP2DIR = BIT1; // Set P2.1 to output directionP1IES = BIT0; // H-L transitionP1IE = BIT0; // Enable interrupt_BIS_SR(LPM4_bits + GIE); // LPM4, enable interrupts}// Port 1 interrupt service routine#pragma vector=PORT1_VECTOR__interrupt void Port1_ISR (void){unsigned volatile int i;for (i=10000; i>0; i--); // Debounce delayP1IFG &= ~BIT0; // Clear P1IFGif ((P1IN & 0x01) == 0)P2OUT ^= 0x02; // Toggle P2.1 using exclusive-OR}8.//************************************************************************* *****// MSP430xG46x Demo - Software Port Interrupt on P1.0 from LPM4//// Description: A hi/low transition on P1.0 will trigger P1_ISR which,// toggles P2.1. Normal mode is LPM4 ~ 0.1uA. LPM4 current can be measured// with the LED removed, all unused P1.x/P2.x configured as output or inputs// pulled high or low, and ensure the P2.0 interrupt input does not float.// ACLK = 32.768kHz, MCLK = SMCLK = default DCO//// MSP430xG461x// -----------------// /|\| |// | | |// --|RST |// /|\ | |// --o--|P1.0 P2.1|-->LED// \|///// K. Quiring/ M. Mitchell// Texas Instruments Inc.// October 2006// Built with IAR Embedded Workbench Version: 3.41A//************************************************************************* *****#include <msp430xG46x.h>void main(void){WDTCTL = WDTPW + WDTHOLD; // Stop WDTFLL_CTL0 |= XCAP14PF; // Configure load capsP2DIR = BIT1; // Set P2.1 to output directionP1IES = BIT0; // H-L transitionP1IE = BIT0; // Enable interrupt_BIS_SR(LPM4_bits + GIE); // LPM4, enable interrupts}// Port 1 interrupt service routine#pragma vector=PORT1_VECTOR__interrupt void Port1_ISR (void){unsigned volatile int i;for (i=10000; i>0; i--); // Debounce delayP1IFG &= ~BIT0; // Clear P1IFGif ((P1IN & 0x01) == 0)P2OUT ^= 0x02; // Toggle P2.1 using exclusive-OR}9.//************************************************************************* *****// MSP430xG46x Demo - USCI_A0, 115200 UART Echo ISR, DCO SMCLK// (modified code example "msp430xG46x_uscia0_uart_01_115k.c")//// Description: Echo a received character, RX ISR used. Normal mode is LPM0.// USCI_A0 RX interrupt triggers TX Echo.// Baud rate divider with 1048576hz = 1048576/115200 = ~9.1 (009h|01h)// ACLK = LFXT1 = 32768Hz, MCLK = SMCLK = default DCO = 32 x ACLK = 1048576Hz// //* An external watch crystal between XIN & XOUT is required for ACLK *////// MSP430FG4619// -----------------// /|\| XIN|-// | | | 32kHz// --|RST XOUT|-// | |// | P2.5/UCA0RXD|<------------// | | 115200 - 8N1// | P2.4/UCA0TXD|------------>//// Texas Instruments Inc.// October 2006// Built with IAR Embedded Workbench Version: 3.41A//************************************************************************* *****#include "msp430xG46x.h"void main(void){volatile unsigned int i;WDTCTL = WDTPW+WDTHOLD; // Stop WDTFLL_CTL0 |= XCAP14PF; // Configure load capsdo{IFG1 &= ~OFIFG; // Clear OSCFault flagfor (i = 0x47FF; i > 0; i--); // Time for flag to set}while ((IFG1 & OFIFG)); // OSCFault flag still set?P2SEL |= 0x030; // P2.4,5 = USCI_A0 RXD/TXDUCA0CTL1 |= UCSSEL_2; // SMCLKUCA0BR0 = 18;0x09; // 1MHz 115200UCA0BR1 = 0;0x00; // 1MHz 115200UCA0MCTL = 0;0x02; // ModulationUCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**IE2 |= UCA0RXIE; // Enable USCI_A0 RX interrupt_BIS_SR(LPM0_bits + GIE); // Enter LPM0, interrupts enabled}// Echo back RXed character, confirm TX buffer is ready first#pragma vector=USCIAB0RX_VECTOR__interrupt void USCIA0RX_ISR (void){while(!(IFG2&UCA0TXIFG));UCA0TXBUF = UCA0RXBUF; // TX -> RXed character}10./************************************************************************** ***** MSP-EXP430G2-LaunchPad User Experience Application** 1. Device starts up in LPM3 + blinking LED to indicate device is alive* + Upon first button press, device transitions to application mode* 2. Application Mode* + Continuously sample ADC Temp Sensor channel, compare result against* initial value* + Set PWM based on measured ADC offset: Red LED for positive offset, Green* LED for negative offset* + Transmit temperature value via TimerA UART to PC* + Button Press --> Calibrate using current temperature* Send character '� via UART, notifying PC******************************************************************************/ #include "msp430g2553.h"#define LED0 BIT0#define LED1 BIT6#define LED_DIR P1DIR#define LED_OUT P1OUT#define BUTTON BIT3#define BUTTON_OUT P1OUT#define BUTTON_DIR P1DIR#define BUTTON_IN P1IN#define BUTTON_IE P1IE#define BUTTON_IES P1IES#define BUTTON_IFG P1IFG#define BUTTON_REN P1REN#define TXD BIT1 // TXD on P1.1 #define RXD BIT2 // RXD on P1.2#define APP_STANDBY_MODE 0#define APP_APPLICATION_MODE 1#define TIMER_PWM_MODE 0#define TIMER_UART_MODE 1#define TIMER_PWM_PERIOD 2000#define TIMER_PWM_OFFSET 20#define TEMP_SAME 0#define TEMP_HOT 1#define TEMP_COLD 2#define TEMP_THRESHOLD 5// Conditions for 9600/4=2400 Baud SW UART, SMCLK = 1MHz#define Bitime_5 0x05*4 // ~ 0.5 bit length + small adjustment#define Bitime 13*4//0x0D#define UART_UPDA TE_INTERV AL 1000unsigned char BitCnt;unsigned char applicationMode = APP_STANDBY_MODE;unsigned char timerMode = TIMER_PWM_MODE;unsigned char tempMode;unsigned char calibrateUpdate = 0;unsigned char tempPolarity = TEMP_SAME;unsigned int TXByte;/* Using an 8-value moving average filter on sampled ADC values */long tempMeasured[8];unsigned char tempMeasuredPosition=0;long tempAverage;long tempCalibrated, tempDifference;void InitializeLeds(void);void InitializeButton(void);void PreApplicationMode(void); // Blinks LED, waits for button pressvoid ConfigureAdcTempSensor(void);void ConfigureTimerPwm(void);void ConfigureTimerUart(void);void Transmit(void);void InitializeClocks(void);void main(void){unsigned int uartUpdateTimer = UART_UPDATE_INTERV AL;unsigned char i;WDTCTL = WDTPW + WDTHOLD; // Stop WDTInitializeClocks();InitializeButton();InitializeLeds();PreApplicationMode(); // Blinks LEDs, waits for button press/* Application Mode begins */applicationMode = APP_APPLICATION_MODE;ConfigureAdcTempSensor();ConfigureTimerPwm();__enable_interrupt(); // Enable interrupts./* Main Application Loop */while(1){ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start__bis_SR_register(CPUOFF + GIE); // LPM0 with interrupts enabled/* Moving average filter out of 8 values to somewhat stabilize sampled ADC */tempMeasured[tempMeasuredPosition++] = ADC10MEM;if (tempMeasuredPosition == 8)tempMeasuredPosition = 0;tempAverage = 0;for (i = 0; i < 8; i++)tempAverage += tempMeasured[i];tempAverage >>= 3; // Divide by 8 to get averageif ((--uartUpdateTimer == 0) || calibrateUpdate ){ConfigureTimerUart();if (calibrateUpdate){TXByte = 248; // A character with high value, outside of temp rangeTransmit();calibrateUpdate = 0;TXByte = (unsigned char)( ((tempAverage - 630) * 761) / 1024 );Transmit();uartUpdateTimer = UART_UPDATE_INTERV AL;ConfigureTimerPwm();}tempDifference = tempAverage - tempCalibrated;if (tempDifference < -TEMP_THRESHOLD){tempDifference = -tempDifference;tempPolarity = TEMP_COLD;LED_OUT &= ~ LED1;}elseif (tempDifference > TEMP_THRESHOLD){tempPolarity = TEMP_HOT;LED_OUT &= ~ LED0;}else{tempPolarity = TEMP_SAME;TACCTL0 &= ~CCIE;TACCTL1 &= ~CCIE;LED_OUT &= ~(LED0 + LED1);}if (tempPolarity != TEMP_SAME){tempDifference <<= 3;tempDifference += TIMER_PWM_OFFSET;TACCR1 = ( (tempDifference) < (TIMER_PWM_PERIOD-1) ? (tempDifference) : (TIMER_PWM_PERIOD-1) );TACCTL0 |= CCIE;TACCTL1 |= CCIE;}}void PreApplicationMode(void){LED_DIR |= LED0 + LED1;LED_OUT |= LED0; // To enable the LED toggling effect LED_OUT &= ~LED1;BCSCTL1 |= DIV A_1; // ACLK/2BCSCTL3 |= LFXT1S_2; // ACLK = VLOTACCR0 = 1200; //TACTL = TASSEL_1 | MC_1; // TACLK = SMCLK, Up mode. TACCTL1 = CCIE + OUTMOD_3; // TACCTL1 Capture Compare TACCR1 = 600;__bis_SR_register(LPM3_bits + GIE); // LPM0 with interrupts enabled}void ConfigureAdcTempSensor(void){unsigned char i;/* Configure ADC Temp Sensor Channel */ADC10CTL1 = INCH_10 + ADC10DIV_3; // Temp Sensor ADC10CLK/4 ADC10CTL0 = SREF_1 + ADC10SHT_3 + REFON + ADC10ON + ADC10IE;__delay_cycles(1000); // Wait for ADC Ref to settleADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start __bis_SR_register(CPUOFF + GIE); // LPM0 with interrupts enabled tempCalibrated = ADC10MEM;for (i=0; i < 8; i++)tempMeasured[i] = tempCalibrated;tempAverage = tempCalibrated;}void ConfigureTimerPwm(void){timerMode = TIMER_PWM_MODE;TACCR0 = TIMER_PWM_PERIOD; //TACTL = TASSEL_2 | MC_1; // TACLK = SMCLK, Up mode. TACCTL0 = CCIE;TACCTL1 = CCIE + OUTMOD_3; // TACCTL1 Capture Compare TACCR1 = 1;}void ConfigureTimerUart(void){timerMode = TIMER_UART_MODE; // Configure TimerA0 UART TXCCTL0 = OUT; // TXD Idle as MarkTACTL = TASSEL_2 + MC_2 + ID_3; // SMCLK/8, continuous modeP1SEL |= TXD + RXD; //P1DIR |= TXD; //}// Function Transmits Character from TXBytevoid Transmit(){BitCnt = 0xA; // Load Bit counter, 8data + ST/SP while (CCR0 != TAR) // Prevent async captureCCR0 = TAR; // Current state of TA counterCCR0 += Bitime; // Some time till first bitTXByte |= 0x100; // Add mark stop bit to TXByteTXByte = TXByte << 1; // Add space start bitCCTL0 = CCIS0 + OUTMOD0 + CCIE; // TXD = mark = idlewhile ( CCTL0 & CCIE ); // Wait for TX completion}// Timer A0 interrupt service routine#pragma vector=TIMER0_A0_VECTOR__interrupt void Timer_A (void){if (timerMode == TIMER_UART_MODE){CCR0 += Bitime; // Add Offset to CCR0if (CCTL0 & CCIS0) // TX on CCI0B?{if ( BitCnt == 0)CCTL0 &= ~ CCIE; // All bits TXed, disable interrupt else{CCTL0 |= OUTMOD2; // TX Spaceif (TXByte & 0x01)CCTL0 &= ~ OUTMOD2; // TX MarkTXByte = TXByte >> 1;BitCnt --;}}}else{if (tempPolarity == TEMP_HOT)LED_OUT |= LED1;if (tempPolarity == TEMP_COLD)LED_OUT |= LED0;TACCTL0 &= ~CCIFG;}}#pragma vector=TIMER0_A1_VECTOR__interrupt void ta1_isr(void){TACCTL1 &= ~CCIFG;if (applicationMode == APP_APPLICATION_MODE)LED_OUT &= ~(LED0 + LED1);elseLED_OUT ^= (LED0 + LED1);}void InitializeClocks(void){BCSCTL1 = CALBC1_1MHZ; // Set rangeDCOCTL = CALDCO_1MHZ;BCSCTL2 &= ~(DIVS_3); // SMCLK = DCO / 8 = 1MHz }void InitializeButton(void) // Configure Push Button{BUTTON_DIR &= ~BUTTON;BUTTON_OUT |= BUTTON;BUTTON_REN |= BUTTON;BUTTON_IES |= BUTTON;BUTTON_IFG &= ~BUTTON;BUTTON_IE |= BUTTON;}void InitializeLeds(void){LED_DIR |= LED0 + LED1;LED_OUT &= ~(LED0 + LED1);}/* ************************************************************** Port Interrupt for Button Press* 1. During standby mode: to exit and enter application mode* 2. During application mode: to recalibrate temp sensor* *********************************************************** */#pragma vector=PORT1_VECTOR__interrupt void PORT1_ISR(void){BUTTON_IFG = 0;BUTTON_IE &= ~BUTTON; /* Debounce */WDTCTL = WDT_ADL Y_250;IFG1 &= ~WDTIFG; /* clear interrupt flag */IE1 |= WDTIE;if (applicationMode == APP_APPLICATION_MODE){tempCalibrated = tempAverage;calibrateUpdate = 1;}else{applicationMode = APP_APPLICATION_MODE; // Switch from STANDBY to APPLICATION MODE__bic_SR_register_on_exit(LPM3_bits);}}#pragma vector=WDT_VECTOR__interrupt void WDT_ISR(void){IE1 &= ~WDTIE; /* disable interrupt */IFG1 &= ~WDTIFG; /* clear interrupt flag */WDTCTL = WDTPW + WDTHOLD; /* put WDT back in hold state */BUTTON_IE |= BUTTON; /* Debouncing complete */ }// ADC10 interrupt service routine#pragma vector=ADC10_VECTOR__interrupt void ADC10_ISR (void){__bic_SR_register_on_exit(CPUOFF); // Return to active mode}。
使用IAR烧录MSP430G2553程序并硬件仿真
使用IAR Embedded Workbench烧录MSP430G2553
程序并硬件仿真
之前用IAR编写MSP430程序时候,一直以为烧录程序要设置生成.TXT文件然后使用第三方烧录软件,例如Lite FET才能烧录程序的,
今天才知道原来不用第三方烧录软件,直接用IAR也是能烧录程序的,下面阐述下如何用IAR烧录程序并硬件仿真,希望刚刚学习MSP430烧录的朋友看了能有所帮助。
废话少说,下面开始。
如何建立项目这个我就不多说了,我使用的单片机型号是G2553,项目建立完成之后,进行下列一些设置。
1.选择设置项
2.我使用的单片机型号是G2553所以这里选择G2553。
3.用第三方LiteFet烧录程序的话是要生成.txt文件的,这里我们不用生成,
保持下列设置即可,不然会造成烧录失败。
4.这里选择FET Debugger 硬件仿真。
5.Connection我这里选择的是USB-IF,实测能下载成功。
但是网上有的网
友选择的是LPT-IF,而我选择LPT-IF却下载失败,所以,大家可以两个都试一试。
6.点击下载程序并调试图标
正在下载程序……
7.下载成功后进入调试界面….
本人本来是使用CCS(Code Composer Studio v5)的编译环境的,但是发现编译超慢,iar就很快,还有一个重要原因是CCS使用sprintf函数编译不了不然就是格式化不了long格式的变量,设置了CCS的Library Function Assumptions使用full 选项也不行,相同代码却在IAR能编译和运行到想要的sprintf结果,所以毅然决定该用IAR了。
MSP430G2553_
ZeroMemory(&wrOverlapped,sizeof(wrOverlapped)); if (wrOverlapped.hEvent != NULL) { ResetEvent(wrOverlapped.hEvent); wrOverlapped.hEvent = CreateEvent(NULL,TRUE,FALSE,NULL); } PurgeComm(hCom,PURGE_TXCLEAR|PURGE_RXCLEAR); txIndex=0; rxIndex=0; } virtual ~ComPort(void) { if(bComOpened) CloseHandle(hCom); } unsigned char rxIndex; unsigned char lpInBuffer[1024]; int Sync(void){ int i=1000; while(rxIndex!=txIndex && txIndex!=0 && (--i)){ int size=Read(); for(int i=0;i<size;i++) if(((lpInBuffer[i]&0x80)==0) && (lpInBuffer[i]!=0)) rxIndex=lpInBuffer[i]; } if(i<1) return -1; return rxIndex; } int Read(void){ DWORD dwBytesRead=1024; COMSTAT ComStat; DWORD dwErrorFlags; OVERLAPPED m_osRead; memset(&m_osRead,0,sizeof(OVERLAPPED)); m_osRead.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL); ClearCommError(hCom,&dwErrorFlags,&ComStat); dwBytesRead=min(dwBytesRead,(DWORD)ComStat.cbInQue); //clear port buffer
mini12864的MSP430G2553驱动程序
ret_data=ret_data+0;
// P2DIR|=BIT1;
ROMSCK_1;
}
return(ret_data);
}
/*从相关地址(addrHigh:地址高字节,addrMid:地址中字节,addrLow:地址低字节)中连续读出DataLen个字节的数据到 pBuff的地址*/
LCDSID_1;
else
LCDSID_0;
LCDSCLK_1;
data1=data1<<=1;
}
}
/*写数据到LCD模块*/
void transfer_data_lcd(int data1)
{
char i;
LCDRS_1;
for(i=0;i<8;i++)
#define ROMCS_0 P2OUT&=~BIT3
/*写指令到LCD模块*/
void transfer_command_lcd(int data1)
{
char i;
LCDRS_0;
for(i=0;i<8;i++)
{
LCDSCLK_0;
if(data1&0x80)
#define LCDCS_0 P1OUT&=~BIT4
#define ROMIN_1 P2OUT|=BIT0 /*字库IC接口定义:Rom_IN就是字库IC的SI*/
#define ROMIN_0 P2OUT&=~BIT0
#define ROMOUT_1 P2OUT|=BIT1 /*字库IC接口定义:Rom_OUT就是字库IC的SO*/
MSP430G2553制作的自行车测度系统
MSP430G2553制作的自行车测度系统一,功能介绍:本系统主要用于安装在自行车上测量自行车的运行速度和行驶距离。
然后将速度和距离信息显示在显示在LCD显示屏上。
(1) 处理器选用的是TI的MSP430G2553 单片机(2)测速装置选用的是霍尔器件,在车圈上固定一个磁铁,然后430连接霍尔器件,通过输入捕获功能测量车轮的转动输出的脉冲的周期在加上事先计算出车轮的直径就可计算出车子的运行速度和行驶距离(3)显示屏选用的是NOKIA5110 LCD的显示屏,此显示屏的成本比较低,可以满足我的显示要求。
(4)整个系统有两个按键,一个是距离清零按键,按下该按键可以将显示的距离清零,这样方便测量起始地和目的地的距离,另外一个按键是用于打开LCD的背光的,用于夜晚使用。
当夜晚光线比较暗时,按下该按键LCD的背光打开,方便看清楚显示的东西。
在按一下背光熄灭。
(5)整个系统的供电的用到2节7号电池供电,当平常车子没有运行的时候,430处于低功耗状态。
耗电比较少,现在测速系统已将安装在我车子上正常工作3个月了二,系统框图图1是整个系统的框图. LCD显示屏和MSP430G2553的连接采用的SPI接口,霍尔传感器和按键和430之间是IO口连接。
三,系统原理图图2是整个系统的原理图四,程序代码//******************************************************************* ***********#include#include#include#includeunsigned char Count, First_Time;unsigned int REdge1, REdge2, FEdge;unsigned char MST_Data, SLV_Data;#define DC P2OUT = P2OUT|(1<<5)#define SCE P2OUT = P2OUT|(1<<4)#define CLK_H P1OUT=P1OUT|(1<<5)#define CLK_L P1OUT=P1OUT&(~(1<<5))#define Data_H P1OUT=P1OUT|((1<<7))#define Data_L P1OUT=P1OUT&(~(1<<7))#define u8 unsigned char#define u16 unsigned inttypedef struct{u8 x;u8 y;}point;/*------5110屏幕尺寸和功能宏定义------*/#define DATA 1 //数据#define CMD 0 //命令#define LCD_X 84 //液晶屏横坐标宽度#define LCD_ROW LCD_X //液晶屏列宽度void LCD_write_byte(unsigned char dat, unsigned char command); void LCD_init(void);void LCD_init(void);void LCD_set_XY(unsigned char X, unsigned char Y);void LCD_clear(void);void LCD_write_char(unsigned char c);void LCD_write_String(unsigned char X,unsigned char Y,unsigned char *s); void LCD_write_byte(unsigned char dat, unsigned char command){unsigned int i;// LCD_SCE = 0; //5110片选有效,允许输入数据P2OUT &=~BIT4;if (command == 0) //写命令// LCD_DC = 0;P2OUT &=~BIT5;else // LCD_DC = 1;P2OUT |=BIT5;//写数据for (i=0;i<8;i++){CLK_L;//spi_clk=0;if((dat & 0x80)==0x80){Data_H;}else{Data_L;}CLK_H; // spi_clk=1;dat=(dat<<1);}P2OUT |=BIT4;}void LCD_init(void){P2OUT &=~BIT4;P1OUT &= ~BIT4;P1OUT |= BIT4;LCD_write_byte(0x21, 0); // LCD模式设置:芯片活动,水平寻址,使用扩展指令LCD_write_byte(0xc8, 0); // 设置液晶偏置电压LCD_write_byte(0x06, 0); // 温度校正LCD_write_byte(0x13, 0); // 1:48LCD_write_byte(0x20, 0); // 使用基本命令,V=0,水平寻址LCD_clear(); // 清屏LCD_write_byte(0x0c, 0); // 设定显示模式,正常显示P2OUT |=BIT4;}void LCD_set_XY(unsigned char X, unsigned char Y){LCD_write_byte(0x80 | X, 0); // X 行(横坐标)LCD_write_byte(0x40 | Y, 0); // column 列(纵坐标)}/*------------------------------------------//LCD_clear: LCD清屏函数--------------------------------------------*/void LCD_clear(void){unsigned char t;unsigned char k;LCD_set_XY(0,0);for(t=0;t<6;t++){for(k=0;k<84;k++){LCD_write_byte(0x00,1);}}}void LCD_write_char(unsigned char c){unsigned char i;c-=0x20; //ASCII码减去 0x20for (i=0; i<6; i++)LCD_write_byte(ASCII_6_8[c], 1);}void LCD_write_String(unsigned char X,unsigned char Y,unsigned char *s){LCD_set_XY(X,Y);while (*s) //等效*s!='\0'{LCD_write_char(*s);s++;}}static float speed;static unsigned char display[10];static unsigned char display2[10];static unsigned int temp ;static unsigned int distance=0;static float Period;static float Distance_km;int main(void){volatile unsigned int i;unsigned char lcd_buf[6][84];WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timerP1DIR |= BIT0; // P1.0/LED OutputP1OUT &= ~BIT0; // LED offif (CALBC1_8MHZ==0xFF) // If calibration constant erased {while(1); // do not load, trap CPU!!}DCOCTL = 0; // Select lowest DCOx and MODx settingsBCSCTL1 = CALBC1_8MHZ; // Set DCO to 8MHzDCOCTL = CALDCO_8MHZ;P1DIR &= ~BIT2;P1SEL |= BIT2;P2DIR &= ~BIT2;P2DIR &= ~BIT1;P2IES |=BIT2+BIT1;P2IE |=BIT2+BIT1;P1DIR |= BIT0;P1DIR|=BIT4|BIT5|BIT7;TA0CCTL1 = CAP + CM_3 + CCIE + SCS + CCIS_0;TA0CTL |= TASSEL_1 + MC_2 + TACLR; // ACLK, Cont Mode; start timer Count = 0x0;First_Time = 0x01;P2OUT = 0x00;P2DIR |=BIT5+BIT4 ;P1DIR |=BIT4 ;_EINT();P1OUT &= ~BIT4; // Now with SPI signals initialized,P1OUT |= BIT4; // reset slaveLCD_init(); //初始化LCD模块LCD_clear(); //清屏幕LCD_write_String(0,1,"Speed:");LCD_write_String(60,1,"km/h");LCD_write_String(0,3,"Dista:");while(1){__bis_SR_register(LPM0_bits+GIE); // Enter LPM0 Period = REdge2 - REdge1; // Calculate Period speed = 1.0362*(32768/Period)*3.6;memset(display,0,sizeof(display));sprintf((char*)display,"%4.1f",speed);LCD_write_String(36,1," ");LCD_write_String(36,1,display);LCD_write_String(60,1,"km/h");LCD_write_String(50,3," ");Distance_km=(float)distance*2.073656/1000.0;if(Distance_km<1){LCD_write_String(34,3," ");Distance_km=Distance_km*1000;sprintf((char*)display2,"%d",(int)Distance_km); LCD_write_String(44,3,display2);}else{LCD_write_String(36,3," ");sprintf((char*)display2,"%6.3f",Distance_km); LCD_write_String(34,3,display2);LCD_write_String(72,3,"km");}}}// TA0_A1 Interrupt vector#pragma vector = TIMER0_A1_VECTOR__interrupt void TIMER0_A1_ISR (void){switch(__even_in_range(TA0IV,0x0A)){case TA0IV_NONE: break; // Vector 0: No interrupt case TA0IV_TACCR1: // Vector 2: TACCR1 CCIFG// distance++;if (TA0CCTL1 & CCI) // Capture Input Pin Status {distance++;if (!Count){REdge1 = TA0CCR1;Count++;}else{REdge2 = TA0CCR1;Count=0x0;__bic_SR_register_on_exit(LPM0_bits +GIE); // Exit LPM0 on return to main }if (First_Time)First_Time = 0x0;}else{if(!First_Time){FEdge = TA0CCR1;}}break;default: break;}}#pragma vector=PORT2_VECTOR__interrupt void PORT2 (void){unsigned int i;for(i=0;i<20000;i++);if(P2IFG&BIT1){distance=0;while(!(P2IN&BIT1));}if(P2IFG&BIT2){P1OUT=P1OUT^BIT0;while(!(P2IN&BIT2));}P2IFG=0x00;__bic_SR_register_on_exit(LPM0_bits +GIE); // Exit LPM0 on return to main }#define LCD_X 84 //液晶屏横坐标宽度#define LCD_ROW LCD_X //液晶屏列宽度。
一些MSP430G2553简单程序讲解
MSP430基础时钟模块包含以下3个时钟输入源。
一、4个时钟振荡源1、LFXT1CLK: 外部晶振或时钟1 低频时钟源低频模式:32768Hz 高频模式:(400KHz-16MHz)2、XT2CLK: 外部晶振或时钟2 高频时钟源(400KHz-16MHz)3、DCOCLK: 内部数字RC振荡器,复位值1.1MHz4、VLOCLK: 内部低功耗振荡器12KHz注:MSP430x20xx: LFXT1 不支持HF 模式, XT2 不支持, ROSC 不支持.(1)LFXT1CLK低频时钟源:由LFXT1振荡器产生(如图2所示)。
通过软件将状态寄存器中OSCOff 复位后,LFXT1开始工作,即系统采用低频工作。
如果LFXT1CLK没有用作SMCLK或MCLK信号,则可以用软件将OSCOff置位,禁止LFXT1工作。
(2)XT2CLK高频时钟源:由XT2振荡器产生。
它产生时钟信号XT2CLK,其工作特性与LFXT1振荡器工作在高频模式时类似。
可简单地通过软件设置XT2振荡器是否工作,当XT2CLK没有用作SMCLK或MCLK信号时,关闭XT2,选择其他时钟源。
3)DCOCLK数字控制RC振荡器。
由集成在时钟模块中的DCO振荡器产生。
DCO振荡器是一个RC 振荡器,频率可以通过软件调节,其控制逻辑如图3所示。
当振荡器LFXT1、XT2被禁止或失效时,DCO振荡器被自动选作MCLK的时钟源。
因此由振荡器失效引起的系统中断请求可以得到响应,甚至在CPU关闭的情况下也能得到处理。
由基础时钟模块可以提供系统所需的3种时钟信号,即:ACLK、MCLK、SMCLK。
其中辅助时钟ACLK是LFXT1CLK信号经1、2、4、8分频后得到的。
ACLK可由软件选作各个外围模块的时钟信号,一般用于低速外设;系统主时钟MCLK可由软件选择来自LFXT1CLK、XT2CLK、DCOCLK三者之一,然后经1、2、4、8分频得到。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
CCR1=0; //确保灯暗
delay_nms(600); //暗0.6S增强效果
}
}
IAR环境下的程序!
矩阵按键在单片机设计中经常见到,下面给大家几个程序!如果有需求可以看看!
矩阵按键,扫描
下面程序是淘来的!
#include<msp430g2553.h>
/***************全局变量***************/
//即输出电平在TAR的值等于CCR1时复位为0,当TAR的值等于CCR0时置位为1,改变CCR1,从而产生PWM。其实模式2也可以
TACTL = TASSEL_2 +MC_1; //设置TIMERA的时钟源为SMCLK,计数模式为up,到CCR0再自动从0开始计数
while(1)
{
CCR1=0;//确保开始是暗的
#include "msp430g2553.h"
// *************************************************************
//延时函数,对于1M主频,约延时1ms
// *************************************************************
// \|/
//
//******************************************************************************
#include <msp430g2553.h>
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
P1DIR |= BIT0; // Set P1.0 to output direction
P1IES |= BIT3; // P1.3 Interrupt Edge Select下降沿触发
P1IFG &= ~BIT3; // P1.3 IFG cleared
P1IE |= BIT3; // P1.3 interrupt enabled允许P1.3产生中断
//将CCR0设置为1500来定义PWM的周期,利用循环不断改变CCR1的值,
//实现利用改变PWM的占空比来改变LED亮度.
// ACLK = na, SMCLK = MCLK = TACLK = default DCO
//
// MSP430G2553
// -----------------
// /|\| XIN|-
DCOCTL = CALDCO_1MHZ;
P1DIR |=BIT6; //设置P1.6为输出
P1SEL |=BIT6; //设置P1.6为TA0.1输出
TACCR0 = PWMPeriod; //设置PWM周期
TACCTL1 = OUTMOD_7; //设置PWM输出模式为:7 - PWM复位/置位模式,
void delay_1ms(void)
{
unsigned int k;
for (k=0;k<195;k++);
}
// *************************************************************
//延时函数,延时n*ms
// *************************************************************
}
///////////////////////////////////////////////////////////////////////////////////////////
呼吸灯程序
//MSP430G2553呼吸灯演示程序-使用Timer_A,Up Mode, DCO SMCLK
//
//介绍:该程序利用TIMER A的UP模式在P1.6脚产生PWM输出
__interrupt void Port_1(void)
{
int i;
i=10000;
if (P1IFG & BIT3)
{ while(i--);
P1OUT ^= BIT0; // P1.0 = toggle
while(i--);
P1IFG &= ~BIT3; // P1.3 IFG cleared
unsigned char Key_Val; //存放键值
void CtrlKey(unsigned char sw); //控制键盘开关//sw=0关sw=1开
/*******************************************
函数名称:Init_Keypad
功能:初始化扫描键盘的IO端口
由基础时钟模块可以提供系统所需的3种时钟信号,即:ACLK、MCLK、SMCLK。其中辅助时钟ACLK是LFXT1CLK信号经1、2、4、8分频后得到的。ACLK可由软件选作各个外围模块的时钟信号,一般用于低速外设;系统主时钟MCLK可由软件选择来自LFXT1CLK、XT2CLK、DCOCLK三者之一,然后经1、2、4、8分频得到。MCLK主要用于CPU和系统。子系统时钟SMCLK可由软件选择来自LFXT1CLK和DCOCLK,或者XT2CLK和DCOCLK,然后经1、2、4、8分频得到,主要用于高速外设模块。
//渐亮过程:不断设置CCR1的值,使翻转的时间变长,改变PWM的占空比
//由于只有1个定时器,编程有些你困难,故仍引入delay函数
for(i=0;i<PWMPeriod;i+=1)
{
CCR1=i;
delay_nms(4-(i/500)); //占空比变化的延时,调整延迟时间可改变呼吸灯变暗的速度
(2)XT2CLK高频时钟源:由XT2振荡器产生。它产生时钟信号XT2CLK,其工作特性与LFXT1振荡器工作在高频模式时类似。可简单地通过软件设置XT2振荡器是否工作,当XT2CLK没有用作SMCLK或MCLK信号时,关闭XT2,选择其他时钟源。
3)DCOCLK数字控制RC振荡器。由集成在时钟模块中的DCO振荡器产生。DCO振荡器是一个RC振荡器,频率可以通过软件调节,其控制逻辑如图3所示。当振荡器LFXT1、XT2被禁止或失效时,DCO振荡器被自动选作MCLK的时钟源。因此由振荡器失效引起的系统中断请求可以得到响应,甚至在CPU关闭的情况下也能得到处理。
4、VLOCLK: 内部低功耗振荡器 12KHz
注:MSP430x20xx: LFXT1 不支持 HF 模式, XT2 不支持, ROSC 不支持.
(1)LFXT1CLK低频时钟源:由LFXT1振荡器产生(如图2所示)。通过软件将状态寄存器中OSCOff复位后,LFXT1开始工作,即系统采用低频工作。如果LFXT1CLK没有用作SMCLK或MCLK信号,则可以用软件将OSCOff置位,禁止LFXT1工作。
// | | |
// --|RST XOUT|-
// | |
// | P1.6/TA1|--> CCR1 - PWM
//
//使用MSP430 LaunchPad , MSP430G2553芯片
//在IAR Embedded Workbench for MSP430 5.40下编译测试通过
//******************************************************************************
void main(void)
{
unsigned const PWMPeriod = 1500; //设置PWMPeriod参数
volatile unsigned int i; //变量
WDTCTL = WDTPW + WDTHOLD; //关闭看门狗
BCSCTL1= CALBC1_1MHZ; //1M内部时钟,其他也可以
中断小程序
////////////////////////////////////////////////////////////////////////////////////////////////////
P1口中断
//******************************************************************************
void delayBiblioteka nms(unsigned int n)
{
unsigned int j;
for (j=0;j<n;j++)
{
delay_1ms();
}
}
// *************************************************************
//主程序
// *************************************************************
// LaunchPad Lab3 - Software Port Interrupt Service
//
// MSP430G2xx2
// -----------------
// /|\| XIN|-
// | | |
// --|RST XOUT|-
// /|\ | |
// --o--|P1.3 P1.0|-->LED
P1DIR|=0x01;
_BIS_SR(LPM3_bits+GIE);//_EINT();
/* for(;;)
{
_BIS_SR(CPUOFF);
_NOP();
}