找回密码
 注册会员
更新自动建库工具PCB Footprint Expert 2024.04 Pro / Library Expert 破解版

那位 大侠 给看看 这个 采样 程序 对不对?

[复制链接]
admin 发表于 2012-9-2 22:26:44 | 显示全部楼层 |阅读模式

本文包含原理图、PCB、源代码、封装库、中英文PDF等资源

您需要 登录 才可以下载或查看,没有账号?注册会员

×
#include <msp430x24x.h>
#define NUM_OF_RESULTS 8
#define num_of_adc12 24
unsigned int Uart_data[NUM_OF_RESULTS]={0xff,0xff};
unsigned int ADC12_index = 0;
static unsigned int ADC12_CON_Flag=1;
static unsigned int A0_results[num_of_adc12]={0}; // Needs to be global in this example
static unsigned int A1_results[num_of_adc12]={0};
//static unsigned int Uart_data01[8]={0x12,0x00,0x01,0x02,0x03,0x04,0x05,0x06};
static unsigned int UART_flag=0;
//延时函数
void delay_nms(unsigned int n)
{
int i,k;
for(i=0;i<n;i++)
for(k=0;k<1024;k++)
_NOP();
}




void Uart1_init()
{
P3SEL|=BIT6|BIT7;
UCA1CTL1 |= UCSSEL_1; // CLK = ACLK
UCA1BR0 = 0x03; // 32kHz/9600 = 3.41
UCA1BR1 = 0x00;
UCA1MCTL = UCBRS1 + UCBRS0; // Modulation UCBRSx = 3
UCA1CTL1 &= ~UCSWRST; // **Initialize USCI state
UC1IE |= UCA1TXIE;
}

void ADC12_ITSTOP()
{
ADC12CTL0&=~ENC;
ADC12CTL0&=~ADC12SC;
}

void ADC12_ITRSTAR()
{
ADC12CTL0|=ENC;
ADC12CTL0|=ADC12SC;
}
void ADC12_init()
{
P6SEL|=BIT0|BIT6;
ADC12CTL0 = ADC12ON+MSC+SHT0_3; // Turn on ADC12, set sampling time
ADC12CTL1|= SHP|CONSEQ_1|ADC12SSEL_1|ADC12DIV_0; // Use sampling timer, single sequence
ADC12MCTL0 = SREF_1+INCH_0; // ref+=AVcc, channel = A0
ADC12MCTL6 = SREF_1+INCH_6+EOS; // ref+=AVcc, channel = A1, end seq.
ADC12IE = 0x40; // Enable ADC12IFG.1
ADC12CTL0 |= ENC; // Enable conversions
ADC12CTL0 |= ADC12SC;
}


void Get_uartdata()
{
char a , b,c,d;
unsigned int temp,i,j;
unsigned int temp1,h,k;
for(i=0;i<num_of_adc12;i++)
for(j=i+1;j<num_of_adc12;j++)
{
if(A0_results[i]<A0_results[j])
{
temp=A0_results[i];
A0_results[i]=A0_results[j];
A0_results[j]=temp;
}

}
a=A0_results[0];
b=(A0_results[0]>>8);
b&=0x0f;
Uart_data[2] = a;
Uart_data[3] = b;

for(h=0;h<num_of_adc12;h++)
for(k=h+1;k<num_of_adc12;k++)

{
if(A1_results[h]<A1_results[k])
{
temp1=A1_results[h];
A1_results[h]=A1_results[k];
A1_results[k]=temp1;
}

}
c=A1_results[0];
d=(A1_results[0]>>8);
d&=0x0f;
Uart_data[4] = c;
Uart_data[5] = d;
}

//******************发送采用查询方式**************************
void UART1_Transmit(unsigned int ch_Data)
{
while (!(UC1IFG & UCA1TXIFG))
;
UCA1TXBUF = ch_Data;
}

//***************数组发送函数********************************
void UART_Sent_Array(unsigned int* p_Array, unsigned char ch_Sum)
{
unsigned char i=0;
for(i=0; i<ch_Sum; i++)
{
UART1_Transmit(*p_Array);
p_Array++;
}
}
void main()
{
WDTCTL = WDTPW+WDTHOLD; // Stop watchdog timer
ADC12_init();
Uart1_init();
_EINT();

while(1)

{
if(ADC12_CON_Flag==0x00)
{
ADC12_ITSTOP();

Get_uartdata();
UC1IE |= UCA1TXIE;
if(UART_flag==0x01)
{
UART_Sent_Array(Uart_data,NUM_OF_RESULTS);
UART_flag=0;
}
//ADC12_index=0;
ADC12_ITRSTAR();

ADC12_CON_Flag=1;
}
else
{
ADC12_ITRSTAR();
_NOP();
}

}
}

#pragma vector=ADC12_VECTOR
__interrupt void ADC12ISR (void)
{


A0_results[ADC12_index] = ADC12MEM0; // Move A0 results, IFG is cleared
A1_results[ADC12_index] = ADC12MEM6;
ADC12_index++;
if(ADC12_index==num_of_adc12) //一轮转换完成,数据将被读出
{
ADC12_index=0;
//清除忙标志
ADC12_ITSTOP();
//转换结束
}
else
{
ADC12_CON_Flag=0;
}

}
#pragma vector=USCIAB1TX_VECTOR
__interrupt void usart1_tx (void)
{
//UART_Sent_Array(Uart_data,NUM_OF_RESULTS);
UART_flag=1;
UC1IE &=~UCA1TXIE;
}
*滑块验证:
您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

QQ|手机版|MCU资讯论坛 ( 京ICP备18035221号-2 )|网站地图

GMT+8, 2024-11-29 12:55 , Processed in 0.049469 second(s), 9 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表