#include "msp430g2553.h"
unsigned int admem[4];
unsigned long n;
unsigned int invot=0;
unsigned int inan=0;
unsigned int otan=0;
unsigned int outvot=0;
void sys_Clock_Init() //时钟初始化为 1MHz;
{
BCSCTL1 = CALBC1_1MHZ; // Set range
DCOCTL = CALDCO_1MHZ; // Set DCO step + modulation */
}
void cai()//ad采样
{
int*p=(int*)0x200;
int i;
P1DIR&=~BIT0+~BIT1+~BIT2+~BIT3;
P1SEL|=BIT0+BIT1+BIT2+BIT3;
WDTCTL = WDTPW + WDTHOLD;// Stop WDT
ADC10CTL0 &= ~ENC;
ADC10CTL1 = INCH_3 + CONSEQ_1+ADC10SSEL_2+SHS_0; // A3/A2/A1/A0, single sequence序列 单次
ADC10CTL0 = ADC10SHT_3 +MSC+ ADC10ON+SREF_0;
ADC10DTC1 = 0x04; // 4 conversions
ADC10AE0 |= BIT0+BIT1+BIT2+BIT3; // P1.3,2,1,0 ADC10 option select
while (ADC10CTL1 & BUSY); // Wait if ADC10 core is active
ADC10SA = 0x200; // Data buffer start
ADC10CTL0 |= ENC+ADC10SC; // Sampling and conversion start
_delay_cycles(3000);
for(i=0x00;i<4;i++)
{
admem[i]=*p;
p++;
}
p=(int*)0x200;
n=admem[0]*66;
outvot=n/100;
n=admem[1]*66;
invot=n/102;
n=admem[2]*30;
inan=n/40;
n=admem[3]*30;
otan=n/40;
return;
}
void xianshi ()//数码管按段显示 显示函数
{
P2SEL=0X00;
P2DIR=0XFF;
unsigned int i;
unsigned int j;
unsigned int num;
unsigned int x[3]={0x10,0x20,0x40};
unsigned int wei;
unsigned int seg[3];
int yu=1;
// int c;
seg[0]=outvot/100;
yu=outvot%100;
seg[1]=yu/10;
seg[2]=yu%10;
//for( c=0;c<100;c++)
//{
for(j=0x00;j<3;j++)
{
num=seg[j];
wei=x[j];
switch(num)
{case 0 :
for(i=0x0;i<0x6;i++)
{P2OUT=wei+i;
_delay_cycles(1000); } break;
case 1:
for(i=0x1;i<3;i++)
{
P2OUT=wei+i;
_delay_cycles(1000);} break;
case 2:
for(i=0x0;i<0x2;i++)
{P2OUT=wei+i;
_delay_cycles(1000); }
for(i=0x3;i<5;i++)
{P2OUT=wei+i;
_delay_cycles(1000); }
for(i=0x6;i<0x7;i++)
{P2OUT=wei+i;
_delay_cycles(1000); } break;
case 3:
for(i=0x0;i<0x4;i++)
{P2OUT=wei+i;
_delay_cycles(1000); }
i=6; P2OUT=wei+i;_delay_cycles(1000);
break;
case 4:
for(i=0x1;i<0x3;i++)
{P2OUT=wei+i;
_delay_cycles(1000); }
for(i=0x5;i<0x7;i++)
{P2OUT=wei+i;
_delay_cycles(1000); } break;
case 5:
i=0;P2OUT=wei+i;_delay_cycles(1000);
for(i=0x2;i<0x4;i++)
{P2OUT=wei+i;
_delay_cycles(1000); }
for(i=0x5;i<0x7;i++)
{P2OUT=wei+i;
_delay_cycles(1000); } break;
case 6:
i=0; P2OUT=wei+i;_delay_cycles(1000);
for(i=0x2;i<0x7;i++)
{P2OUT=wei+i;
_delay_cycles(1000); } break;
case 7:
for(i=0x0;i<0x3;i++)
{P2OUT=wei+i;
_delay_cycles(1000); } break;
case 8:
for(i=0x0;i<0x7;i++)
{P2OUT=wei+i;
_delay_cycles(1000); } break;
case 9:
for(i=0x0;i<0x4;i++)
{P2OUT=wei+i;
_delay_cycles(1000); }
for(i=0x5;i<0x7;i++)
{P2OUT=wei+i;
_delay_cycles(1000); } break;}
}
for(i=0x1;i<0x6;i++)
{P2OUT=0x80+i;
_delay_cycles(1000); }
P2OUT=0X17;
_delay_cycles(1000);
}
void main()
{
sys_Clock_Init();
WDTCTL = WDTPW + WDTHOLD;
//BCSCTL1 = CALBC1_1MHZ; // Set range
//DCOCTL = CALDCO_1MHZ; // Set DCO step + modulation */
P1DIR |= BIT6; // p1.6 口
P1SEL |= BIT6;
TA0CTL |= TASSEL_1+MC_3;
TA0CCTL1 |= OUTMOD_7; //选择高电平模式的 PWM 输出
TA0CCR0=60;//TA1CCR0 写入 2000( 1MHZ/2000) 输出达到了 500HZ
TA0CCR1=50;
for(;;)
{
WDTCTL = WDTPW + WDTHOLD;
cai();
xianshi (); //如果注释掉显示函数pwm口会正常工作
if(outvot>310)//outvot 采样电压电压为3.1
{
TA0CCR1++;
}
if(outvot<310)
{
TA0CCR1--;
}
}
}
此帖出自
小平头技术问答
一周热门 更多>