12年北京市电子设计大赛的考题,最近重做了一下
单片机源程序如下:
- /*5月9日
- *
- *
- * */
- #include <msp430.h>
- #include "DAC7811.h"
- #include "TA_PWM.h"
- #include "oled.h"
- #include <stdio.h>
- void CSL_init(void);
- void GPIO_graceInit(void);
- void BCSplus_graceInit(void);
- void USCI_B0_graceInit(void);
- void ADC10_graceInit();
- /*
- * main.c
- */
- int main(void)
- {
- unsigned int max = 0, min = 8192;
- long Vpp=0;
- int gain;
- WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
- _disable_interrupts();
- CSL_init();
- ADC10_graceInit();
- TA0_PWM_Init('A',1,'F',0);
- TA0_PWM_SetPeriod(30);
- _enable_interrupts();
- int j,i;
- long temp;
- char s[16];
- ADC10CTL0 |= ADC10SC;
- OLED_Init();
- OLED_Clear();
- OLED_ShowString(0,0,"Gain:");
- OLED_ShowString(0,2,"Vpp :");
- while(1)
- {
- TA0_PWM_SetPermill(1,500); //PWM波输出,vpp不能超过100mv
- sprintf(s,"%d ",max);
- OLED_ShowString(0,6,s);
- for(j=0;j<500;j++)
- {
- ADC10CTL0 |= ADC10SC+ENC;
- while ((ADC10CTL0 & ADC10IFG) == 0);
- temp=ADC10MEM;
- if(temp > max)
- max = temp;
- if(temp < min)
- min = temp;
- }
- Vpp=(max-min);
- for(i=0;i<4;i++) OLED_ShowString(40,i*2," ");
- sprintf(s,"%dV",Vpp);
- OLED_ShowString(40,2,s);
- Vpp=Vpp*3550/1023; // 测量电压
- sprintf(s,"%dV",Vpp);
- OLED_ShowString(0,4,s);
- Vpp=Vpp/20;
- sprintf(s,"%dV",Vpp);
- OLED_ShowString(70,4,s);
- TA0_PWM_SetPermill(1,Vpp*10); //PWM波输出,vpp不能超过100mv
- if (Vpp<40)
- {
- spiwrite(41,1);
- gain=100;
- }
- else if (Vpp<70)
- {
- spiwrite(129,1);
- gain = 35;
- }
- else if (Vpp<100)
- {
- spiwrite(73,1);
- gain=30;
- }
- sprintf(s,"%d ",max);
- OLED_ShowString(0,6,s);
- sprintf(s,"%d ",min);
- OLED_ShowString(80,6,s);
- sprintf(s,"%d dB",gain);
- OLED_ShowString(40,0,s);
- max=0;
- min=8192;
- __delay_cycles(30000000);
- }
- }
- /*
- * 系统初始化集中调用函数
- */
- void CSL_init(void)
- {
- GPIO_graceInit(); //IO口初始化
- BCSplus_graceInit(); //时钟源初始化
- USCI_B0_graceInit(); //USCI_B初始化-3线SPI
- //ADC初始化 P1.3输入
- }
- /*
- * IO口初始化
- */
- void GPIO_graceInit(void)
- {
- //-----设定P1.0和P1.6的输出初始值-----
- P1DIR |= BIT0; //设定P1.0为输出
- P1OUT |= BIT0; //设定P1.0初值(初始化完成后两灯均亮)
- //-----配合机械按键,启用内部上拉电阻-----
- P1REN |= BIT3; //启用P1.3内部上下拉电阻
- P1OUT |= BIT3; //将电阻设置为上拉
- //-----配置P1.3中断参数-----
- P1DIR &= ~BIT3; // P1.3设为输入(可省略)
- P1IES |= BIT3; // P1.3设为下降沿中断
- P1IE |= BIT3 ; // 允许P1.3中断
- /* Port 1 Port Select 2 Register */
- P1SEL2 = BIT5 | BIT7;
- /* Port 1 Port Select Register */
- P1SEL = BIT5 | BIT7;
- /* Port 2 Output Register */
- P2OUT = BIT0 | BIT1 | BIT2| BIT3 | BIT4 | BIT5;
- /* Port 2 Direction Register */
- P2DIR = BIT0 | BIT1 | BIT2| BIT3 | BIT4 | BIT5;
- }
- /*
- * 设定主时钟为16MHz,低速时钟为外部手表晶振
- */
- void BCSplus_graceInit(void)
- {
- BCSCTL2 = SELM_0 | DIVM_0 | DIVS_0;
- if (CALBC1_16MHZ != 0xFF) {
- /* Adjust this accordingly to your VCC rise time */
- __delay_cycles(10000);
- DCOCTL = 0x00;
- BCSCTL1 = CALBC1_16MHZ; /* Set DCO to 16MHz */
- DCOCTL = CALDCO_16MHZ;
- }
- BCSCTL1 |= XT2OFF | DIVA_0;
- BCSCTL3 = XT2S_0 | LFXT1S_0 | XCAP_1;
- }
- /*
- * 通用串行通信口设置为3线SPI模式,P1.5-SCL/P1.7-SDL
- */
- void USCI_B0_graceInit(void)
- {
- /* Disable USCI */
- UCB0CTL1 |= UCSWRST;
- UCB0CTL0 = UCCKPL |UCMSB | UCMST | UCMODE_0 | UCSYNC;
- UCB0CTL1 = UCSSEL_2 | UCSWRST;
- /* Bit Rate Control Register 0 */
- UCB0BR0 = 32;
- /* Enable USCI */
- UCB0CTL1 &= ~UCSWRST;
- }
- void ADC10_graceInit(void)
- { //p1.2输入,基准电压为3.55v
- // ADC10CTL0 &= ~ENC;
- // ADC10CTL0 = ADC10IE | ADC10ON | ADC10SHT_3 | SREF_0;
- // ADC10CTL1 = CONSEQ_0 | ADC10SSEL_0 | ADC10DIV_0 | SHS_0 | INCH_2;
- // __delay_cycles(30000);
- // ADC10CTL0 |= ENC;
- ADC10CTL0 &= ~ENC;
- ADC10CTL0 =ADC10ON + REFON + ADC10SHT_3 + SREF_0;
- ADC10CTL1 = CONSEQ_0 + ADC10SSEL_0 + ADC10DIV_3 + SHS_0 + INCH_3;
- ADC10AE0 = 0x8;
- __delay_cycles(30000);
- ADC10CTL0 |= ENC;
- }
复制代码
所有资料51hei提供下载:
lian5.9.zip
(103.88 KB, 下载次数: 8)
|