重大光电MSP430单片机实验代码

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

实验一

#include

int main(void)

#include

int main(void)

{WDTCTL=WDTPW+WDTHOLD;

int flag=0;

P4REN |=BIT0+BIT1+BIT2+BIT3+BIT4;

P4OUT |=BIT0+BIT1+BIT2+BIT3+BIT4;

P1DIR |=BIT0+BIT6+BIT7;

while(1)

{ if((P4IN&BIT0)==0)

{

if ((P4IN&BIT0)==0)

{ flag=7;

}

}

if((P4IN&BIT4)==0)

{

if ((P4IN&BIT4)==0)

{ flag=3;

}

}

if((P4IN&BIT2)==0)

{

if ((P4IN&BIT2)==0)

{ flag=5;

}

}

if(flag==7)

{P1OUT |=BIT0;P1OUT |=BIT6;P1OUT &=~BIT7; }

if(flag==3)

{P1OUT |=BIT0;P1OUT |=BIT7;P1OUT &=~BIT6; }

if(flag==5)

{P1OUT &=~BIT0;

}

}

}

实验二

#include

void initAdc()

{

ADC12CTL0 |= ADC12MSC;//自动循环采样转换

ADC12CTL0 |= ADC12ON;//启动ADC12模块

ADC12CTL1 |= ADC12CONSEQ1 ;//选择单通道循环采样转换

ADC12CTL1 |= ADC12SHP;//采样保持模式

ADC12MCTL0 |= ADC12INCH_7; //选择通道15,连接拨码电位器

ADC12CTL0 |= ADC12ENC;

}

int main(void)

{

WDTCTL = WDTPW +WDTHOLD;

P4DIR |=BIT5+BIT6+BIT7;

P5DIR |=BIT7;

P8DIR |=BIT0;

ADC12CTL0=ADC12SHT0_8;

initAdc();

ADC12IE|=BIT0;

ADC12CTL0|=ADC12SC;

_EINT();// 开中断

}

#pragma vector=ADC12_VECTOR

__interrupt void ADC12ISR(void)

{

if(ADC12MEM0>=3686)

{

P4OUT |=BIT5+BIT6+BIT7;

P5OUT |=BIT7;

P8OUT |=BIT0;

}

if(ADC12MEM0>=2867)

{ P4OUT &=~BIT5;

P4OUT |=BIT6+BIT7;

P5OUT |=BIT7;P8OUT |=BIT0;

}

if(ADC12MEM0>=2048)

{

P4OUT &=~BIT5;P4OUT &=~BIT6;P4OUT |=BIT7;P5OUT |=BIT7;P8OUT |=BIT0;

}

if(ADC12MEM0>=1229)

{

P4OUT &=~BIT5;P4OUT &=~BIT6;P4OUT &=~BIT7;P5OUT |=BIT7;P8OUT |=BIT0;}

if(ADC12MEM0>=410)

{

P4OUT &=~BIT5;P4OUT &=~BIT6;P4OUT &=~BIT7;P5OUT &=~BIT7;P8OUT |=BIT0;

}

else

{

P4OUT=0x00;

P5OUT=0x00;

P8OUT=0x00;

}

}

实验三

#include

volatile unsigned int sin[256]={

0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,0x0A, 0x0B, 0x0D, 0x0E, 0x10, 0x11, 0x13, 0x15, 0x16, 0x18, 0x1A, 0x1C, 0x1E, 0x20, 0x22, 0x25,0x27, 0x29, 0x2B, 0x2E, 0x30, 0x33, 0x35, 0x38, 0x3A, 0x3D, 0x40, 0x43, 0x45, 0x48, 0x4C, 0x4E,0x51, 0x55, 0x57, 0x5A, 0x5D, 0x60, 0x63, 0x66, 0x69, 0x6C, 0x6F, 0x72, 0x76, 0x79, 0x7C,0x80, 0x80, 0x83, 0x86, 0x89, 0x8D, 0x90, 0x93,0x96,0x99,0x9C,0x9F,0x0A2,0x0A5,0x0A 8,0x0AB,0x0AE,0x0B1,0x0B4,0x0B7,0x0BA,0x 0BC,0x0BF,0x0C2,0x0C5,0x0C7,0x0CA,0x0CC,0 x0CF,0x0D1,0x0D4,0x0D6,0x0D8,0x0DA,0x0D D,0x0DF,0x0E1,0x0E3,0x0E5,0x0E7,0x0E9,0x0 EA,0x0EC,0x0EE,0x0EF,0x0F1,0x0F2,0x0F4,0x0 F5,0x0F6,0x0F7,0x0F8,0x0F9,0x0FA,0x0FB,0x0 FC,0x0FD,0x0FD,0x0FE,0x0FF,0x0FF,0x0FF,0x0 FF,0x0FF,0x0FF,0x0FF,0x0FF,0x0FF,0x0FF,0x0FF, 0x0FF,0x0FE,0x0FD,0x0FD,0x0FC,0x0FB,0x0FA, 0x0F9,0x0F8,0x0F7,0x0F6,0x0F5,0x0F4,0x0F2, 0x0F1,0x0EF,0x0EE,0x0EC,0x0EA,0x0E9,0x0E7, 0x0E5,0x0E3,0x0E1,0x0DE,0x0DD,0x0DA,0x0D 8,0x0D6,0x0D4,0x0D1,0x0CF,0x0CC,0x0CA,0x 0C7,0x0C5,0x0C2,0x0BF,0x0BC,0x0BA,0x0B7,0 x0B4,0x0B1,0x0AE,0x0AB,0x0A8,0x0A5,0x0A2 ,0x9F, 0x9C, 0x99, 0x96, 0x93, 0x90, 0x8D, 0x89, 0x86, 0x83, 0x80, 0x80, 0x7C, 0x79, 0x78, 0x72, 0x6F, 0x6C, 0x69, 0x66, 0x63, 0x60, 0x5D, 0x5A, 0x57, 0x55, 0x51,0x4E, 0x4C, 0x48, 0x45, 0x43, 0x40, 0x3D, 0x3A, 0x38, 0x35, 0x33, 0x30, 0x2E, 0x2B, 0x29, 0x27, 0x25, 0x22, 0x20, 0x1E, 0x1C, 0x1A, 0x18, 0x16, 0x15, 0x13, 0x11, 0x10, 0x0E, 0x0D, 0x0B, 0x0A, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

void main(void)

{ volatile unsigned int i,j;

WDTCTL = WDTPW + WDTHOLD; //关闭看门狗

P7DIR |= BIT6;//设置P7.6口为输出口

P7SEL |= BIT6;//使能P7.6口第二功能位

DAC12_0CTL0 |= DAC12IR; //设置参考电压满刻度值,使Vout = Vref×(DAC12_xDAT/4096)

DAC12_0CTL0 |= DAC12SREF_1; //设置参考电压为AVCC

DAC12_0CTL0 |= DAC12AMP_5; //设置运算放大器输入输出缓冲器为中速中电流DAC12_0CTL0 |= DAC12CALON; //启动校验功能

DAC12_0CTL0 |= DAC12OPS;//选择第二通道P7.6

DAC12_0CTL0 |= DAC12ENC; //转化使能while(1)

{ for(j=0;j<5;j++)

{for(i=0;i<256;i++)

DAC12_0DAT =16*sin[i];

}//输入数据

for(j=5;j<10;j++)

{for(i=0;i<256;i++)

DAC12_0DAT =8*sin[i];

}

}

}

相关文档
最新文档