《MSP430单片机串口控制5路PWM频率变化程序.docx》由会员分享,可在线阅读,更多相关《MSP430单片机串口控制5路PWM频率变化程序.docx(28页珍藏版)》请在优知文库上搜索。
1、下面介绍一段MSP430单片机通过232串口发送OXOo,0x0LOXO2,0x03,0x04控制IO端口P1,P2,P4,P5,P6输出6路PWM波形变化的程序,单片机发送005JO端口PLP2,P4,P5,P6输出PWM为Oe单片机的电路如下图程序如下:链接:https:/pan.baidu.eom/s/l-vcBVWfkaSu8C81LeTFqmw;提取码:18fo/*程序功能:实现流水灯以三种流动方式和四种流动速度的不同组合而进行点亮“流动通过232串口发送0x00,001,002,0x03,0x04调节P1,P2,P4,P5,P6的6个管脚输出PWM波形的频率由小变大通过232串口发
2、送0x05调节P1,P2,P4,P5,P6的6个管脚输出PWM波形为零程序功能:MCU通过串口向PC机发送4X4键盘的键值通信格式:N.8.1,9600拨码开关设置:将LED位拨至ON,其余拨至OFF测试说明:观察流水灯流动顺序和速度的变化*#include/#includeflash.htypedefunsignedintuint;typedefunsignedcharuchar;/voidwrite_flash_char();voidwriteflashchar(unsignedintaddr,char*array,intcount);voidwriteflashint(unsignedi
3、ntaddr,int*array,intcount);voidDelay400ms();voiddelayO;voidDelay5ms();uinti=Oj=0,dir=0;uintflag=0,speed=0;/flag-T,c3fijC,SPeed-灯光速度uinttmp;uintm;/uinttmpl=12000;/uinttmp2=1200;/uinttmp3=12000;/uinttmp4=1200;/uinttmp5=12000;ucharshuju;intshujul;voidmain(void)(*下面六行程序关闭所有的IO口*/PlDIR=OXFF;P10UT=OXFF;P2
4、DIR=0XFF;P2OUT=OXFF;P3DIR=0XFF;P3OUT=OXFF;P4DIR=0XFF;P4OUT=OXFF;P5DIR=0XFF;P5OUT=OXFF;P6DIR=0XFF;P6OUT=0X00;WDTCTL=WDTPW+WDTHOLD;关闭看门狗P6DIR=BIT2;P6OUT=B2;关闭电平转换P50T&=BIT7;CCTLO = CCIE;使能CCRO中断CCRO=50000;TACTL=TASSEL_2+ID_3+MJ1;定时器A的时钟源选择SMCLK,增计数模式P2DIR = Oxff;设置P2 口方向为输出P2OUT=Oxff;PlDIR = Oxff;设置P2
5、 口方向为输出PlOUT=Oxff;P4DIR = Oxff;设置P2 口方向为输出P4OT=Oxff;P5DIR = Oxff;设置P2 口方向为输出P5OUT=Oxff;P6DIR = Oxff;设置P2 口方向为输出P6OT=Oxff;/tmp=12000;/LPMO;CPU进入LPMO模式/P3SELI=0x30;/选择P3.4和P3.5做UART通信端口MEl=UTXEO+URXEO;使能USARTo的发送和接受UCTLOI=CHAR;选择8位字符UTCTLOI=SSELO;/UCLK=ACLKUBROO=0x03;波特率9600UBRlO=0x00;/UMCTLO=04A;/Mod
6、ulationUCTLO&=-SWRST;初始化UART状态机IElI=URXIEO;使能USARTO的接收中断-EINT();使能全局中断/LPMl;while(l)(一ElNT();打开全局中断进入LPMI模式等待以前的字符发送完毕将收到的字符发送出去/while(!(IFGl&TXIFG0);/TXBUFO=RXBUFO;/write_flash_char(0xef,*shuju,4);/writeJlashjnt(0bf,shujul,2);/voidwriteflashchar(Off,001,2);/char*Flash_ptr;/Flashpointer;/inti;/FCTL1
7、=FWKEY+ERASE;/seterasebit/FCTL3=FWKEY;/ClearLockbit/*Flash_ptr=O;/DummywritetoeraseFlashsegment/FCTL1=FWKEY+WRT;/SetWRTbitforwriteoperation/Flash-ptr=01001;/initializeflashpointer/for(i=0;i(i+);灯的点亮Jl酹D8-Dl/ P2OT =(0x804);/ while (!QFGl & TXIFGO);/TXBUFO = RXBUFO;等待以前的字符发送完毕将收到的字符发送出去/P20UT&=-001;P2
8、0UT&=0x3F;/P20T=(0x3F);/delay(tmp);Delay400ms(m);/delay(12000);/P20T=(0x00l);P2OUT=(0x00);/delay(12000);/delay(tmp);Delay400ms(m);PlOUT=(0x3F);/delay(12000);/delay(tmp);Delay400ms(m);PlOUT=(0x00);/delay(12000);/delay(tmp);Delay400ms(m);/delay(12000);P4OT=(0x3F);/delay(12000);Delay400ms(m);/delay(tmp
9、);P40T=(000);/delay(12000);/delay(tmp);Delay400ms(m);P50T=(0x3F);/delay(12000);/delay(tmp);Delay400ms(m);P5OT=(000);/delay(12000);/delay(tmp);Delay400ms(m);P6OT=(Ox3F);/delay(12000);/delay(tmp);Delay400ms(m);P60T=(000);/delay(12000);Delay400ms(m);/delay(tmp);/)/elseif(flag=1)/P2OT=(OxOlD8/)/if(dir)灯
10、的点亮顺序D8-D1,D1-D8循环绕圈/P20T=(080(i+);/)/else/P2OT=(0x01(i+);/)/)/f(i=8)/i=0;/dir=-dir;/)/j+;/i=0;/j=O;/flag+;/if(flag=4)flag=O;/switch(speed)/caseO:/TAeTL&=(IDO+IDl);/TACTL=ID_3;/break;/case1:/TAeTL&=(IDO+IDl);/TACTL=ID_2;/break;/case2:/TAeTL&=(IDO+IDl);/TACTL=ID_1;/break;/case3:/TACTL&=(IDO+IDl);/TACTL=ID_O;/br