标题:
STM32做的64通道数据采集程序
[打印本页]
作者:
frogsondjj
时间:
2020-7-5 19:19
标题:
STM32做的64通道数据采集程序
STM32做的64通道数据采集电路 采样频率30hz 效果非常
单片机源程序如下:
//4.4 Repeat-sequence mode
//Sequence模式时可以设置多个采样通道。在最后一个通道加上EOS就表明的采样通道结束位置。中断允许只需要设置最后一个通道。
//为了采样速率尽可能快,可设置MSC,此时当SHI上升沿触发第一次采样后,后面的采样在上一次采样结束后自动进行。
#include <msp430x14x.h>
#include "Config.h" //开发板配置头文件,主要配置IO端口信息
//表区
#define ADCOK 0x01 //B0为ADC12完成转换标志.
unsigned char number_table[]={'0','1','2','3','4','5','6','7','8','9','.'};
//unsigned char display_buffer[]={0x00,0x00,0x00,0x00,'\0'};
unsigned char display_buffer[]={0x00,0x00,'\0'};
unsigned char display_buffer2[]={0x00,'\0'};
//*******************波特率***********300 600 1200 2400 4800 9600 19200 38400 76800 115200const
//************************************[0]**[1]**[2]*[3]**[4]**[5]***[6]***[7]****[8]***[9]*
unsigned char BaudrateUBR0[] ={0x6D,0x36,0x1B,0x0D,0x06,0x03, 0xA0, 0xD0, 0x68, 0x45};
unsigned const char BaudrateUBR1[] ={0x00,0x00,0x00,0x00,0x00,0x00, 0x01, 0x00, 0x00, 0x00};
unsigned const char BaudrateUMCTL[]={0x22,0xD5,0x03,0x6B,0x6F,0x4A, 0xC0, 0x40, 0x40, 0x4A};
unsigned int head[] ={0xAA,0x00};
unsigned int tail[] ={0x55,0x00};
unsigned char timp; //全局位变量区
unsigned char gbit; //B0为ADC12完成转换标志.
unsigned int ADC[8] ; //ADC12读出数据数组
unsigned char contralAD;
//子程序声明
void init (void); //初始化
void ADC12setup(void); //ADC12初始化
void BaudrateSetup(unsigned char U0); //UART0初始化
void timer_A_setup(void); //timer_A初始化
void data_converter(unsigned char *p,unsigned int vaule); //数据变换
void data_converter1(unsigned char *p,unsigned int vaule);//数据变换1
void send_data(unsigned char *p); //串行口发送数组
void send_adc12_data(unsigned int *p,unsigned int adcn); //发送ADC12数据
void send_adc12_xieyi(unsigned int *p,unsigned int adcn); //发送ADC12协议
void Adc12Open(unsigned char doit);
//*************************************************************************
// 串口0发送数据函数
//*************************************************************************
/*
void Send_Byte(uchar data)
{
while(!(IFG1&UTXIFG0)); //发送寄存器空的时候发送数据
U0TXBUF=data;
}
//*************************************************************************
// 串口0发送字符串函数
//*************************************************************************
void Print_Str(uchar *s)
{
while(*s != '\0')
{
Send_Byte(*s++);
}
}
*/
//******************************************************************************
//发送ADC12数据
//*p为数据区,adcn为数量
/*
void send_adc12_data(unsigned int *p)
{
unsigned char result[18];
result[0]=0xAA;
result[1] = (unsigned char)(p[0] >> 8); //采样结果,对应板上VR电位器.
result[2] = (unsigned char)p[0]; //对应预留仪表放器电路,此路默认为空.
result[3] = (unsigned char)(p[1]>> 8);
result[4] = (unsigned char)p[1];
result[5] = (unsigned char)(p[2] >> 8);
result[6]= (unsigned char)p[2];
result[7] = (unsigned char)(p[3] >> 8);
result[8] = (unsigned char)p[3];
result[9] = (unsigned char)(p[4] >> 8);
result[10] = (unsigned char)p[4];
result[11] = (unsigned char)(p[5] >> 8);
result[12] = (unsigned char)p[5];
result[13] = (unsigned char)(p[6] >> 8);
result[14] = (unsigned char)p[6];
result[15] = (unsigned char)(p[7] >> 8);
result[16] = (unsigned char)p[7];
result[17] = 0x55;
Print_Str(result); //发送数据
}
*/
//******************************************************************************
//******************************************************************************
//串口接收中断
//usart0 interrupt receive function
#pragma vector=UART1RX_VECTOR
__interrupt void usart1_rx (void)
{
while(RXBUF1 != 0)
{
while(1);
}
;
if(RXBUF1==0x43)//0x43开始,0x44停止采集
{
LPM0_EXIT;
ADC12setup();
//send_adc12_data(ADC,8);
while((gbit&ADCOK)==0); //等待序列单次完成
gbit &= ~ADCOK; //清转换完成标志
}
if( RXBUF1 == 0x44 ){ //收到1时,led灯亮,并发送 "led is off"
Adc12Open(100);
_BIS_SR(LPM0_bits + GIE); // Enter LPM0, Enable interrupts
}
}
//******************************************************************************
//timer_A设置
/*
void timer_A_setup(void)
{
TACTL = TASSEL_2+MC_2; //Timer A 时钟源先为SMCLK,增加方式
// TACTL = TASSEL_1+MC_2; //Timer A 时钟源先为ACLK,增加方式
TACCTL1 = OUTMOD_3; //CCR1输出模式:PWM置位/复位
//TACCR1 = 0x0ffff; //CCR1比较值
TACCR1 = 0xfffff;
}
*/
//******************************************************************************
//UART0初始化 //8Mhz晶体程序不能用.
/*
void BaudrateSetup(unsigned char U0)
{
unsigned int i;
if(U0>5) //当U0>5时,启用XT2
{
BCSCTL1 &= ~XT2OFF; //启动XT2,
do
{IFG1 &= ~OFIFG; //清OSCFault标志
for(i=0xFF;i>0;i--); //延时等待
}
while((IFG1 & OFIFG) != 0); //查OSCFault,为0时转换完成
BCSCTL2 |= SELS; //SMCLK为XT2
}
//UART0
P1OUT=0x00;
if(U0>5){UTCTL0=SSEL1;} // Clock Source:SMCLK
else{UTCTL0=SSEL0;} // Clock Source:ACLK
UCTL0 &= ~SWRST; // SWRST复位, USART允许
UCTL0=CHAR; // 8bit
ME1|=UTXE0 + URXE0; // Enable Tx0,Rx0
IE1|=URXIE0; // RX enable
UBR00=BaudrateUBR0[U0]; // 低位分频器因子
UBR10=BaudrateUBR1[U0]; // 高位分频器因子
UMCTL0=BaudrateUMCTL[U0]; // 波特率调整因子
P3SEL |= 0x30; // 将P3.4,5使用外围模块 = USART0 TXD/RXD
P3DIR |= 0x10; // 将P3.4设为输出(发),P3.5默认为输入(收)
}
*/
//*************************************************************************
// MSP430串口初始化
//*************************************************************************
void UART_Init()
{
U1CTL|=SWRST + CHAR; //复位SWRST,8位数据模式
U1TCTL|=SSEL1; //SMCLK为串口时钟
U1BR1=baud_h; //BRCLK=8MHZ,Baud=BRCLK/N
U1BR0=baud_l; //N=UBR+(UxMCTL)/8
U1MCTL=0x00; //微调寄存器为0,波特率9600bps
ME1|=UTXE1; //UART0发送使能
ME1|=URXE1; //UART0接收使能
U1CTL&=~SWRST;
IE1|=URXIE1; //接收中断使能位
P3SEL|= BIT4 + BIT5; //设置IO口为第二功能模式,启用UART功能
P3DIR|= BIT4; //设置TXD0口方向为输出
}
//*************************************************************************
// ADC Setup函数
//*************************************************************************
//4.4 Repeat-sequence mode
//Sequence模式时可以设置多个采样通道。在最后一个通道加上EOS就表明的采样通道结束位置。中断允许只需要设置最后一个通道。
//为了采样速率尽可能快,可设置MSC,此时当SHI上升沿触发第一次采样后,后面的采样在上一次采样结束后自动进行。
void ADC12setup(void)
{
P6SEL |= 0xff; //使用A/D通道 A0,A1,A2,...,A7
//ADC12CTL0 = ADC12ON+MSC+SHT0_8; // Turn on ADC12, extend sampling time
// to avoid overflow of results
ADC12CTL0 = ADC12ON+MSC+SHT0_0+SHT0_1+SHT0_8+SHT1_8+SHT1_5+SHT1_6+SHT1_7+SHT1_8;
//ADC12CTL1 = SHP+CONSEQ // Use sampling timer, repeated sequence
ADC12CTL1 = ADC12SSEL_0+SHP+CONSEQ_3+ADC12DIV_7 ; //SAMPCON信号选为采样定时器输出
// Use sampling timer, repeated sequence
ADC12MCTL0 = INCH_0; // ref+=AVcc, channel = A0
ADC12MCTL1 = INCH_1; // ref+=AVcc, channel = A1
ADC12MCTL2 = INCH_2; // ref+=AVcc, channel = A2
ADC12MCTL3 = INCH_3; // ref+=AVcc, channel = A3, end seq.
ADC12MCTL4 = INCH_4; // ref+=AVcc, channel = A0
ADC12MCTL5 = INCH_5; // ref+=AVcc, channel = A1
ADC12MCTL6 = INCH_6; // ref+=AVcc, channel = A2
ADC12MCTL7 = INCH_7+EOS; // ref+=AVcc, channel = A3, end seq.
ADC12IE = 0x08; // Enable ADC12IFG.3
ADC12CTL0 |= ENC; // Enable conversions
ADC12CTL0 |= ADC12SC; // Start conversion
_BIS_SR(LPM0_bits + GIE); // Enter LPM0, Enable interrupts
}
//****************************************************************************
//打开或关闭ADC12模块
//doit: 0:打开 100:关闭
//****************************************************************************/
void Adc12Open(unsigned char doit)
{
if(doit==0)
{
ADC12CTL0 |= ADC12ON;
ADC12CTL0 |= ENC; //允许转换
}
else if(doit==100)
{
ADC12CTL0 &= ~ADC12ON; //不允许转换
ADC12CTL0 &= ~ENC;
}
}
//*************************************************************************
// ADC 中断函数
//*************************************************************************
#pragma vector=ADC12_VECTOR
__interrupt void ADC12ISR (void)
{
static unsigned int index = 0;
ADC[0] = ADC12MEM0; // Move A0 results, IFG is cleared
ADC[1] = ADC12MEM1; // Move A1 results, IFG is cleared
ADC[2] = ADC12MEM2; // Move A2 results, IFG is cleared
ADC[3] = ADC12MEM3; // Move A3 results, IFG is cleared
ADC[4] = ADC12MEM4; // Move A0 results, IFG is cleared
ADC[5] = ADC12MEM5; // Move A1 results, IFG is cleared
ADC[6] = ADC12MEM6; // Move A2 results, IFG is cleared
ADC[7] = ADC12MEM7; // Move A3 results, IFG is cleared
gbit |= ADCOK; //置标志,表示ADC采样完成.
send_adc12_xieyi(head,1);
send_adc12_data(ADC,8);
send_adc12_xieyi(tail,1);
//ADC12CTL0 = ~ENC;
//TXBUF0 = 0x55;
//index = (index+1)%Num_of_Results; // Increment results index, modulo; Set Breakpoint here
}
void main(void)
{
init();
// Mainloop
while(1)
{
//LPM0;
ADC12CTL0 |= ADC12SC; // Start conversion
_BIS_SR(LPM0_bits + GIE); // Enter LPM0, Enable interrupts
_NOP();
}
}
//******************************************************************************
//MCU初始化
void init(void)
{
WDTCTL = WDTPW + WDTHOLD; // 停止WDT
// timer_A_setup(); //timer_A初始化
//Clock_Init(); //系统时钟设置
//P1DIR=0x01;P1OUT=0x0f; //LED设置
//BaudrateSetup(5); //设置波特率 9600
//Print_Str("DM430-L Board UART Test...\n"); //发送字符串测试
// 全局中断使能
//ADC12setup();
//Clock_Init(); //系统时钟设置
Clock_Init_Inc;
//LED8 =0xff; //关灭所有LED
UART_Init();
_EINT();
}
void data_converter(unsigned char *p,unsigned int value) //数据变换
{
//unsigned int m,n,j=0;
//p[0]=number_table[value/1000];
p[0]=(unsigned char)(value >> 8);
// m=value%1000;
//p[1]=number_table[m/100];
p[1]=(unsigned char)value ;
//n=m%100;
//p[2]=number_table[n/10];
//j=n%10;
//p[3]=number_table[j/1];
}
//******************************************************************************
void data_converter1(unsigned char *p,unsigned int value) //数据变换
{
p[0]=(unsigned char)value ;
}
//******************************************************************************
//串行口发送数组
void send_data(unsigned char *p)
{unsigned int n;
//timp=RXBUF0;
for(n=0;p[n]!='\0';n++)
{
while ((IFG1 & UTXIFG1) == 0); // USART0发送UTXIFG0=1,表示UTXBUF准备好发送一下字符
TXBUF1 = p[n];
}
}
//******************************************************************************
//发送ADC12数据
//*p为数据区,adcn为数量
void send_adc12_data(unsigned int *p,unsigned int adcn)
{
unsigned int j;
for(j=0;j<adcn;j++)
{
//if(j==0)
//{
data_converter(display_buffer,p[j]); //数据变换
send_data(display_buffer); //发送数据
// }
// else
// {
// data_converter(display_buffer,p[j]); //数据变换
// send_data(display_buffer); //发送数据
// }
}
}
//******************************************************************************
//******************************************************************************
//发送ADC12数据头AA 和 尾55
//*p为数据区
void send_adc12_xieyi(unsigned int *p,unsigned int adcn)
{
unsigned int j;
for(j=0;j<adcn;j++)
{
data_converter1(display_buffer2,p[j]); //数据变换
send_data(display_buffer2); //发送数据
}
}
//******************************************************************************
复制代码
51hei.png
(7.04 KB, 下载次数: 47)
下载附件
2020-7-5 19:43 上传
所有资料51hei提供下载:
AD_Mx_Seq_UART1.rar
(28.72 KB, 下载次数: 17)
2020-7-5 19:18 上传
点击文件名下载附件
下载积分: 黑币 -5
作者:
admin
时间:
2020-7-5 19:46
本帖需要重新编辑补全电路原理图,源码,详细说明与图片即可获得100+黑币(帖子下方有编辑按钮)
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1