📄 motor_operation+lcd.c
字号:
//****************************************************************************
// MSP430-41X Demo - LCD display and Motor operation
//
//
// MSP430F413
// -----------------
// /|\| XIN|-
// | | |
// --|RST XOUT|-
// | | ________
// | | | |
// | P3.0|-->| l |
// | . | . | |
// | P3.7| . | C |
// | P4.0| . | |
// | . . | D |
// | P4.7| . | |
// | P5.0| . | |
// | P5.4|-->|________|
// | |
// Donghai CHeng
// Lierda, Inc
// NOVEMBER 2004
// Built with IAR Embedded Workbench Version: 2.21B
//******************************************************************************
#include "msp430x41x.h"
//#include "motor_define.h"
#define MOTOR_POSITIVE 0x01 //电机正向转动
#define MOTOR_NEGATIVE 0x02 //电机反向转动
#define MOTOR_OUT P6OUT //电机控制端口P6
#define MOTOR_DIR P6DIR
#define MOTOR_OPEN 0x40 //电机开
#define MOTOR_CLOSE 0x80 //电机关
#define BELL_OUT P6OUT //蜂鸣器控制端口P6.3
#define BELL_DIR P6DIR
#define BELL_BIT 0x08
#define COM1 (0x0004)
#define COM2 (0x0008)
#define COM3 (0x0010)
#define R13 (0x0020)
#define R23 (0x0040)
#define R33 (0x0080)
#define SegA 0x01
#define SegB 0x02
#define SegC 0x04
#define SegD 0x80
#define SegE 0x40
#define SegF 0x10
#define SegG 0x20
#define SegH 0x08
#define Digit0 SegA+SegB+SegC+SegD+SegE+SegF
#define Digit1 SegB+SegC
#define Digit2 SegA+SegB+SegD+SegE+SegG
#define Digit3 SegA+SegB+SegC+SegD+SegG
#define Digit4 SegB+SegC+SegF+SegG
#define Digit5 SegA+SegC+SegD+SegF+SegG
#define Digit6 SegA+SegC+SegD+SegE+SegF+SegG
#define Digit7 SegA+SegB+SegC
#define Digit8 SegA+SegB+SegC+SegD+SegE+SegF+SegG
#define Digit9 SegA+SegB+SegC+SegD+SegF+SegG
int Digit[10] = {Digit0,Digit1,Digit2,Digit3,Digit4,Digit5,Digit6,Digit7,Digit8,Digit9};
unsigned int system_state_flag; //状态标志
unsigned char motor_direction; //电机转动方向
unsigned char Control_flag; //阀门开关标志 “1”表示开阀门开 ,“0”表示阀门关
void Motor_Com(void);
void Motor_Run(unsigned char motor_dir); //电机转动控制子程序
void Beep_Alarm(unsigned int cycle,unsigned int high_last,unsigned int low_last); //蜂鸣器报警子程序
void delay(unsigned char l,unsigned int k); //延时子程序
void Motor_Open(void)
{
motor_direction=MOTOR_POSITIVE;
Motor_Run(motor_direction);
}
void Motor_Close(void)
{
motor_direction=MOTOR_NEGATIVE;
Motor_Run(motor_direction);
}
void Motor_Com(void)
{
delay(1,65000); //延时
CACTL1 = CAON+CAEX; //打开比较器
delay(1,25000);
IE2 |= BTIE; //当电机动作时使能BT中断
CACTL1 &= ~CAIFG;
}
void Motor_Run(unsigned char motor_dir)
{
MOTOR_OUT &=~(MOTOR_OPEN+MOTOR_CLOSE);
MOTOR_DIR |= MOTOR_OPEN+MOTOR_CLOSE;
switch(motor_dir)
{
case MOTOR_POSITIVE:{
MOTOR_OUT |= MOTOR_OPEN;
MOTOR_OUT &= ~MOTOR_CLOSE;
Motor_Com();
motor_direction &= ~MOTOR_POSITIVE;
}break;
case MOTOR_NEGATIVE:{
MOTOR_OUT |= MOTOR_CLOSE;
MOTOR_OUT &= ~MOTOR_OPEN;
Motor_Com();
motor_direction &= ~MOTOR_NEGATIVE;
}break;
default:{MOTOR_OUT &=~(MOTOR_OPEN+MOTOR_CLOSE);}break;
}
}
void Beep_Alarm(unsigned int cycle,unsigned int high_last,unsigned int low_last)
{
unsigned int i,j;
BELL_DIR |= BELL_BIT;
for(i=0;i<cycle;i++)
{
BELL_OUT |= BELL_BIT;
for(j=0;j<high_last;j++);
BELL_OUT &= ~BELL_BIT;
for(j=0;j<low_last;j++);
}
BELL_OUT &= ~BELL_BIT;
_NOP();
}
void delay(unsigned char lll,unsigned int kkk)
{
unsigned char ppp;
unsigned int qqq;
for(ppp=0;ppp<lll;ppp++){for(qqq=0;qqq<kkk;qqq++);}
}
/********************初始化***************************/
void Initial(void)
{
int i;
for (i=0; i<10; i++)
{
LCDMEM[i] = 0;
}
P5SEL = R33+R23+R13+COM3+COM2+COM1;
LCDCTL = LCDSG0_2+LCD4MUX+LCDON; // Enable LCD
BTCTL = BTSSEL + BTDIV + BTFRFQ0 + BTIP2 + BTIP1; //定时器定时1s检测电机状态
P1DIR &= ~(BIT6+BIT7);
//CAPD = CAPD7+CAPD6;
CACTL2 = P2CA1+P2CA0+CAF;
}
void main (void)
{
unsigned char i;
WDTCTL = WDTPW + WDTHOLD;
SCFQCTL=31; //MCLK=32*ACLK=1024KHz,DCO+=0
SCFI0=0;
FLL_CTL0 |= XCAP18PF;
Initial(); //初始化
P1DIR&=~BIT5;
P1IE|=BIT5;
P1IES|=BIT5;
P1IFG=0;
for (i=0; i<8; i++)
{
LCDMEM[i] = Digit[i]; //显示“0,1,2,3,4,5,6,7”
}
Control_flag=0; //阀门初始状态为阀关
IE2 &=~ BTIE; //当电机动作时使能BT中断
_EINT();
while(1)
{
if(Control_flag==1)
{
LCDMEM[9]|= 0x02; //显示“阀开”
LCDMEM[9]&=~ 0x20;
Motor_Open(); //打开阀门
}
else
{
LCDMEM[9]&=~ 0x02;
LCDMEM[9]|= 0x20; //显示“阀关”
Motor_Close(); //关闭阀门
}
LPM3;
}
}
/***************按键中断子程序*******************/
#ifdef __IAR_SYSTEMS_ICC__
#if __VER__ >= 200
#pragma vector=PORT1_VECTOR
__interrupt void Port1_Int (void)
#else
interrupt[PORT1_VECTOR] void Port1_Int (void)
#endif
#else
interrupt[PORT1_VECTOR] void Port1_Int (void)
#endif
{
if((P1IFG&BIT5)!=0)
{
P1IFG=0;
if(Control_flag==0)
{
Control_flag=1;
}
else
{
Control_flag=0;
}
LPM3_EXIT;
}
}
/******************BT中断子程序*****************/
/***********定时器定时1s检测电机状态***********/
#ifdef __IAR_SYSTEMS_ICC__
#if __VER__ >= 200
#pragma vector=BASICTIMER_VECTOR
__interrupt void BT_Int (void)
#else
interrupt[BASICTIMER_VECTOR] void BT_Int (void)
#endif
#else
interrupt[BASICTIMER_VECTOR] void BT_Int (void)
#endif
{
if(CACTL2&CAOUT) //如果电机到位,比较器CAOUT输出“1”
{
CACTL1 &= ~CAON; //关闭比较器
MOTOR_OUT &=~(MOTOR_OPEN+MOTOR_CLOSE);
CACTL2=0x0e;
IE2 &=~ BTIE; //关闭BT
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -