#include "msp430g2553.h"
#define IRIN (P1IN&BIT0) //P1.0红外接收头输入值
#define CS1 P1OUT|=0x08 //P1.3 CS LCD4脚
#define CS0 P1OUT&=~0x08
#define SID1 P1OUT|=0x10 //P1.4 SID LCD5脚
#define SID0 P1OUT&=~0x10
#define SCLK1 P1OUT|=0x20 //P1.5 SCLK LCD6脚
#define SCLK0 P1OUT&=~0x20
__no_init unsigned int ad[3] @ 0x0200;//定义接收缓存的首地址为0x0200
unsigned int redvalue, changnumflag;//ad 存放ADC数据 RED为红外接收值 changnumflag为红外开始设定值的标志位
long T = 0, D = 0, D1 = 0, D2 = 0, add; //T为当前温度 D D1 D2为当前温度 上次当前温度 上上次与预设温度偏差 做PID用
unsigned char dat[4];
int flag = 1, shan = 1;
char chushihua[] = {"控制系统 "};
char sheding[] = {"占空比: %"};
char dangqian[] = {"当前电压: V"};
char num[] = {"0123456789"};
//函数声明
void InitSys();
void Initadc();
void Initpwm();
void delay(unsigned char);
void delay_1ms(unsigned int);
void delay_5us(unsigned int);
void display(int, int, int, int);
void display_frame(void);
void lcdinit(void);
void sendbyte(unsigned char);
void write_com(unsigned char);
void write_data(unsigned char);
int main( void )
{
int T0 , sethigh = 0, setlow = 0, n1 = 0 ;
unsigned char n = 0;
unsigned int count;
WDTCTL = WDTPW + WDTHOLD; //关闭看门狗
InitSys(); //初始化
Initadc();
Initpwm();
display_frame(); //写入液晶中静态显示部分数据
while (ADC10CTL1 & BUSY);
while(1)
{
if(redvalue == 0) //设定
{
shan = 1;
n = 0;
changnumflag = 0;
}
if(redvalue == 4) //确定
{
T0 = sethigh * 10 + setlow;
shan = 0;
changnumflag = 0;
}
if(shan == 1 && changnumflag == 1) //设定值
{
n++;
if(n == 1)
{
changnumflag = 0;
switch (redvalue)
{
case 8:
sethigh = 0;
break;
case 12:
sethigh = 1;
break;
case 13:
sethigh = 2;
break;
case 14:
sethigh = 3;
break;
case 16 :
sethigh = 4;
break;
case 17:
sethigh = 5;
break;
case 18:
sethigh = 6;
break;
case 20:
sethigh = 7;
break;
case 21:
sethigh = 8;
break;
case 22:
sethigh = 9;
break;
default:
changnumflag = 1;
break;
}
}
if(n == 2)
{
n = 0;
changnumflag = 0;
switch (redvalue)
{
case 8:
setlow = 0;
break;
case 12:
setlow = 1;
break;
case 13:
setlow = 2;
break;
case 14:
setlow = 3;
break;
case 16 :
setlow = 4;
break;
case 17:
setlow = 5;
break;
case 18:
setlow = 6;
break;
case 20:
setlow = 7;
break;
case 21:
setlow = 8;
break;
case 22:
setlow = 9;
break;
default:
changnumflag = 1;
break;
}
}
}
n1++;
if(n1 == 500)
{
ADC10CTL0 &= ~ENC;
while (ADC10CTL1 & BUSY); // Wait if ADC10 core is active
ADC10SA = 0x200; // Data buffer start
add = ad[0] + ad[1] + ad[2] ;
T = add / 5;
ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start
}
n1 = 0;
CCR1 = T0;
count++;
if(count == 30000) //刷新液晶动态显示内容 因要一数字闪烁刷新频率很低
{
count = 0;
display(shan, sethigh, setlow, T);
}
}
}
/*****************************************************************************
系统初始化
******************************************************************************/
void InitSys()
{
if (CALBC1_1MHZ == 0xFF || CALDCO_1MHZ == 0xFF)
{
while(1); // If calibration constants erased
// do not load, trap CPU!!
}
BCSCTL1 = CALBC1_8MHZ; // 设定工作频率为8M
DCOCTL = CALDCO_8MHZ; // Set DCO step + modulation */
P2DIR = 0xff;
P1DIR &= ~0X01; //红外输入端设定输入 接收头要接上拉电阻(或引脚有内部上拉)
P1DIR |= 0x38;
P1IE = BIT0; //P1.0中断功能打开
P1IES |= BIT0; //P1.0下降沿触发中断
P1IFG &= ~0x01; //清P1.0中断标志位
_BIS_SR(GIE); //开中断
lcdinit(); //LCD初始化
}
void Initadc()
{
ADC10CTL0 = ADC10SHT_2 + MSC + ADC10ON + ADC10IE; //reference to AVcc
ADC10CTL1 = INCH_3 + CONSEQ_1; // A3/A2/A1, single sequence
ADC10AE0 |= 0x0E; // P1.3,2,1 ADC10 option select
ADC10DTC1 = 0x03; // 3 conversions
}
void Initpwm()
{
P1DIR |= 0X04; //P1.4输出PWM
P1SEL |= 0X04;
CCR0 = 0x0063; // PWM Period
CCTL1 = OUTMOD_7; // CCR1 reset/set
CCR1 = 0x0063; // CCR1 PWM duty cycle
TACTL = TASSEL_2 + MC_1 ; // SMCLK, up mode 8分频+ ID_3
}
/*****************************************************************************
端口1中断函数
******************************************************************************/
#pragma vector=PORT1_VECTOR
__interrupt void Port_1()
{
char j, k, n = 0;
if((P1IFG & BIT0) == BIT0)
{
P1IFG &= ~BIT0; //清除P1.0中断标志
P1IE &= ~BIT0; //关闭P1.0中断功能
delay_1ms(2);
if (IRIN == 1)
{
P1IE |= BIT0;
return;
}
while (!IRIN) //等IR变为高电平,跳过9ms的前导低电平信号。
{
delay_5us(28);
n++;
if(n > 100)
{
P1IE |= BIT0;
return;
}
}
n = 0;
for (j = 0; j < 4; j++)
{
for (k = 0; k < 8; k++)
{
while (IRIN) //等 IR 变为低电平
{
delay_5us(28);
}
while (!IRIN) //等 IR 变为高电平
{
delay_5us(28);
}
while (IRIN) //计算IR高电平时长
{
delay_5us(28);
n++;
if (n >= 30)
{
P1IE |= BIT0;
return;
}
}
dat[j] = dat[j] >> 1;
if (n >= 11)
{
dat[j] = dat[j] | 0x80;
}
n = 0;
}
}
if ((dat[2] + dat[3]) != 0xff)
{
评论0