1、LED灯、数码管、蜂鸣器的控制,闪烁的实现,流水灯的实现:
程序2.1.1:
#include "io430.h"
int main( void )
{
unsigned int i;
long int j,k;
unsigned a[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,0x88,0x80,0xc6,0xc0,0x86,0x8e}; //P2输出数组
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
P2DIR=0xFF;P1DIR=0x1F;
while(1)
{
for(j=0;j<200000;j++)
{
P2OUT=a[0];
P1OUT=0x1F;
}
for(j=1;j<=16;j++)
{
for(i=0;i
{
P2OUT=a[j];
for(k=0;k<6000/(j+1);k++)
P1OUT=0xE0;
for(k=0;k<6000/(j+1);k++)
P1OUT=0x1F;
}
}
}
}
此程序包括了LED灯、数码管、蜂鸣器的使用。功能是当数码管显示0~F时,LED灯分别闪几下,蜂鸣器分别响几下。如图2.1
图2.1 数码管示数和LED灯闪烁
程序2.1.2:
#include "msp430G2553.h"
//*********************************************************
//程序功能:心形图案组成的流水灯依次点亮
//*********************************************************
void main( )
{
unsigned int i;
P1DIR=0XFF;
WDTCTL = WDTPW + WDTHOLD;
while(1)
{
for(i=0;i<50000;i++);
P1OUT=0X00;
for(i=0;i<50000;i++);
P1OUT=0XAA;
for(i=0;i<50000;i++);
P1OUT=0X55;
for(i=0;i<50000;i++);
P1OUT=0X7F;
for(i=0;i<50000;i++);
P1OUT=0XBF;
for(i=0;i<50000;i++);
P1OUT=0XDF;
for(i=0;i<20000;i++);
P1OUT=0XEF;
for(i=0;i<50000;i++);
P1OUT=0XF7;
for(i=0;i<50000;i++);
P1OUT=0XFB;
for(i=0;i<50000;i++);
P1OUT=0XFD;
for(i=0;i<50000;i++);
P1OUT=0XFE;
}
}
图2.2 心形图案流水灯
2、4*4按键的使用
程序2.2.1:
#include "msp430g2553.h"
//*****************************************************************
//函数功能:初始化键盘
//*****************************************************************
void Init_Port(void)
{
P1DIR = 0;
P1SEL = 0;
P1DIR |= BIT4;
P1DIR |= BIT5;
P1DIR |= BIT6;
P1DIR |= BIT7;
P1OUT = 0x00;
}
//******************************************************************
//函数功能:延时函数
//******************************************************************
void Delay(void)
{
int i;
for(i=0;i<100;i++) ;
}
//******************************************************************
//函数功能:读取并扫描键值
//******************************************************************
int KeyProcess(void)
{
int nP10,nP11,nP12,nP13;
int nRes = 0; //按键返回值
P1OUT = 0XE0;
nP10 = P1IN & BIT0;
if (nP10 == 0) nRes = 13;
nP11 = P1IN & BIT1;
if (nP11 == 0) nRes = 14;
nP12 = P1IN & BIT2;
if (nP12 == 0) nRes = 15;
nP13 = P1IN & BIT3;
if (nP13 == 0) nRes = 16;
P1OUT = 0XD0;
nP10 = P1IN & BIT0;
if (nP10 == 0) nRes = 9;
nP11 = P1IN & BIT1;
if (nP11 == 0) nRes = 10;
nP12 = P1IN & BIT2;
if (nP12 == 0) nRes = 11;
nP13 = P1IN & BIT3;
if (nP13 == 0) nRes = 12;
P1OUT = 0XB0;
nP10 = P1IN & BIT0;
if (nP10 == 0) nRes = 5;
nP11 = P1IN & BIT1;
if (nP11 == 0) nRes = 6;
nP12 = P1IN & BIT2;
if (nP12 == 0) nRes = 7;
nP13 = P1IN & BIT3;
if (nP13 == 0) nRes = 8;
P1OUT = 0X70;
nP10 = P1IN & BIT0;
if (nP10 == 0) nRes = 1;
nP11 = P1IN & BIT1;
if (nP11 == 0) nRes = 2;
nP12 = P1IN & BIT2;
if (nP12 == 0) nRes = 3;
nP13 = P1IN & BIT3;
if (nP13 == 0) nRes = 4;
P1OUT = 0x00;
for(;;)
{
nP10 = P1IN & BIT0;
nP11 = (P1IN & BIT1)>>1;
nP12 = (P1IN & BIT2)>>2;
nP13 = (P1IN & BIT3)>>3;
if(nP10 == 1 && nP11 == 1 && nP12 == 1 && nP13 == 1)
{
break;
}
}
return nRes; //返回到键盘扫描函数
}
//****************************************************************
//函数功能:扫描键盘
//****************************************************************
int KeyScan(void)
{
int nP10,nP11,nP12,nP13;
int nRes = 0; //按键返回值
while(1)
{
nP10 = P1IN & BIT0;
nP11 = (P1IN & BIT1) >> 1;
nP12 = (P1IN & BIT2) >> 2;
nP13 = (P1IN & BIT3) >> 3;
if(nP10 == 0||nP11 == 0||nP12 == 0||nP13 == 0)
{
Delay(); //消抖
nP10 = P1IN & BIT0;
nP11 = (P1IN & BIT1) >> 1;
nP12 = (P1IN & BIT2) >> 2;
nP13 = (P1IN & BIT3) >> 3;
if(nP10 == 0||nP11 == 0||nP12 == 0||nP13 == 0)
{nRes = KeyProcess();} //调用读取键值的函数
else nRes = -1;
return nRes; //返回到主函数
}
}
}
//**************************************************************
//函数功能:控制P2管脚,数码管示数
//**************************************************************
void shumaguan(int i)
{
int k;
unsigned int a[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,0x88,0x80,0xc6,0xc0,0x86,0x8e}; //P2输出数组
for(k=0;k<30000;k++)
P2OUT=a[i-1];
}
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; //关闭看门狗
int i;
while(1)
{
Init_Port(); //调用初始化键盘函数
i=KeyScan(); //扫描键盘
P2DIR=0XFF; //设置P2为输出
shumaguan(i); //调用数码管显示函数
}
}
图2.3 按键控制数码管
3、LED点阵的控制
程序2.3.1:
#include "msp430G2553.h"
//*********************************************************
//程序功能:用LED点阵显示心形
//*********************************************************
void main( )
{
unsigned int i;
P1DIR=0XFF;P2DIR=0xff;
WDTCTL = WDTPW + WDTHOLD;
while(1)
{
for(i=0;i<100;i++)
{P1OUT=0xfd;P2OUT=0x6c;}
for(i=0;i<100;i++)
{P1OUT=0xfb;P2OUT=0x92;}
for(i=0;i<100;i++)
{P1OUT=0xf7;P2OUT=0x82;}
for(i=0;i<100;i++)
{P1OUT=0xef;P2OUT=0x44;}
for(i=0;i<100;i++)
{P1OUT=0xdf;P2OUT=0x28;}
for(i=0;i<100;i++)
{P1OUT=0xbf;P2OUT=0x10;}
}
}
图2.4 LED点阵的控制
楼主这么给力,赞一个
不过把图片整理一下发上来就好了
嵌入式系统常用到msp430,这个程序接近直接可用的工程了,如果有直接的工程压缩包更好,多谢!
51嵌入式,嵌入式开发的家园!51嵌入式官方QQ群52497844,欢迎爱好者加入。