欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

STM32单片机CAN通信与DMA方式ADC等常用调试记录

程序员文章站 2022-06-23 08:44:14
STM32单片机CAN通信与DMA方式ADC调试记录1.Can的滤波器1.1.滤波器组合11.2.滤波器组合21.3.滤波器组合32.静默和环回模式2.1.正常模式2.2.静默模式2.3环回模式2.4 静默环回模式3.DMA方式ADC3.1. 8分频2级深度7通道半完成中断时间3.2. DMA方式AD采集配置3.3. 中断服务程序4. 中断服务程序1.Can的滤波器1.1.滤波器组合1// CAN_FilterInitStructure.CAN_FilterNumber=0;//滤波器0CA...

1.Can的滤波器

1.1.滤波器组合1

// 
	CAN_FilterInitStructure.CAN_FilterNumber=0;	//滤波器0
	CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask; 	//
	CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit; 	//32位
	

	CAN_FilterInitStructure.CAN_FilterIdHigh=0x2000;	//32??ID 
	CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;
	CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0xE000;//只关注3位
	CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;
	CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_Filter_FIFO0;//1y???÷01?áaμ?FIFO0
	CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;//?¤??1y???÷0
	CAN_FilterInit(&CAN_FilterInitStructure);			//??2¨?÷3?ê??ˉ
// 
	CAN_FilterInitStructure.CAN_FilterNumber=1;	//滤波器1
	CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask; 	//?á±????£ê?
	CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit; 	//32???í 
	
//ID 0x0000 00A0 //LK
	CAN_FilterInitStructure.CAN_FilterIdHigh=0x2000;	//
	CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;
	CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x1FE0;//只关注8位
	CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;
	CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_Filter_FIFO0;//1y???÷01?áaμ?
	CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;//?¤??1y???÷0
	CAN_FilterInit(&CAN_FilterInitStructure);			//??2¨?÷3?ê??ˉ

收到RxMessage.StdId=100,108,0x110,0x140,0x148,0x150;0x200,0x400,
收不到的ID 0x408,0x208
结论:满足任何一个滤波器规则,就可以被中断收到。

1.2.滤波器组合2

// A code block
	CAN_FilterInitStructure.CAN_FilterNumber=1;	//1y???÷1
	CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask; 	//?á±????£ê?
	CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit; 	//32???í 
	
//ID 0x0000 00A0 //LK
	CAN_FilterInitStructure.CAN_FilterIdHigh=0x2000;	//32??ID 
	CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;
	CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x1FE0;//32??MASK   2?1?D?±ê×????1ê?à??1??£?2?1?D?êy?
	CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;
	CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_Filter_FIFO0;//1y???÷01?áaμ?FIFO0
	CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;//?¤??1y???÷0
	CAN_FilterInit(&CAN_FilterInitStructure);			//??2¨?÷3?ê??ˉ

收到RxMessage.StdId=100,0x200,0x400,
收不到108,0x110,0x140,0x148,0x150,0x408,0x208,

1.3.滤波器组合3

	CAN_FilterInitStructure.CAN_FilterNumber=0;	//1y???÷0
	CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask; 	//?á±????£ê?
	CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit; 	//32???í 

	CAN_FilterInitStructure.CAN_FilterIdHigh=0x2000;	//32??ID 
	CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;
	CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0xE000;//32??MASK   2?1?D?±ê×????1ê?à??1??£?2?1?D?êy?
	CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;
	CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_Filter_FIFO0;//1y???÷01?áaμ?FIFO0
	CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;//?¤??1y???÷0
	CAN_FilterInit(&CAN_FilterInitStructure);			//??2¨?÷3?ê??ˉ

收到RxMessage.StdId=100,108,0x110,0x140,0x148,0x150,
收不到 0x200,0x400,0x408,0x208,

2.静默和环回模式

2.1.正常模式

STM32单片机CAN通信与DMA方式ADC等常用调试记录
可收总线,收不到自己发到总线。

2.2.静默模式

STM32单片机CAN通信与DMA方式ADC等常用调试记录
能收到总线,发不出去,也收不到自己发的

2.3环回模式

STM32单片机CAN通信与DMA方式ADC等常用调试记录
环回模式自己不发送直接不进USB_LP_CAN1_RX0_IRQHandler;只能收到自己的发送,收不到别人发送,可以发送到总线。

2.4 静默环回模式

STM32单片机CAN通信与DMA方式ADC等常用调试记录
静默环回模式,发不到总线,只能收到自己的;自发自收。

3.DMA方式ADC

3.1. 8分频2级深度7通道半完成中断时间

STM32单片机CAN通信与DMA方式ADC等常用调试记录

3.2. DMA方式AD采集配置

void ADCx_Init(void)
{

	GPIO_InitTypeDef GPIO_InitStructure;
	DMA_InitTypeDef DMA_InitStructure;
	ADC_InitTypeDef ADC_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
	
	// �� ADC IO�˿�ʱ��
	RCC_APB2PeriphClockCmd ( RCC_APB2Periph_GPIOA, ENABLE );  //RCC_APB2Periph_GPIOA
	// ��DMAʱ��
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
	// ��ADCʱ��
	RCC_APB2PeriphClockCmd ( RCC_APB2Periph_ADC1, ENABLE );

	
	// ���� ADC IO ����ģʽ
	GPIO_InitStructure.GPIO_Pin = 	  ADC_PIN1|ADC_PIN2|ADC_PIN3|ADC_PIN4;

	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
	
	// ��ʼ�� ADC IO
	GPIO_Init(GPIOA, &GPIO_InitStructure);				
	
	// �� ADC IO�˿�ʱ��
	RCC_APB2PeriphClockCmd ( RCC_APB2Periph_GPIOC, ENABLE );
	
	// ���� ADC IO ����ģʽ
	GPIO_InitStructure.GPIO_Pin = 	  ADC_PIN5|ADC_PIN6;

	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
	
	// ��ʼ�� ADC IO
	GPIO_Init(GPIOC, &GPIO_InitStructure);	


    NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);	

	
	// ��λDMA������
	DMA_DeInit(DMA1_Channel1);
	
	// ���� DMA ��ʼ���ṹ��
	// �����ַΪ��ADC ���ݼĴ�����ַ
	DMA_InitStructure.DMA_PeripheralBaseAddr = ( u32 ) ( & ( ADC1->DR ) );
	
	// �洢����ַ
	DMA_InitStructure.DMA_MemoryBaseAddr = (u32)ADC_ConvertedValue;
	
	// ����Դ��������
	DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
	
	// ��������С��Ӧ�õ�������Ŀ�ĵصĴ�С
	DMA_InitStructure.DMA_BufferSize = NOFCHANEL*2;
	
	// ����Ĵ���ֻ��һ������ַ���õ���
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;

	// �洢����ַ����
	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; 
	
	// �������ݴ�СΪ���֣��������ֽ�
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
	
	// �ڴ����ݴ�СҲΪ���֣����������ݴ�С��ͬ
	DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
	
	// ѭ������ģʽ
	DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;	

	// DMA ����ͨ�����ȼ�Ϊ�ߣ���ʹ��һ��DMAͨ��ʱ�����ȼ����ò�Ӱ��
	DMA_InitStructure.DMA_Priority = DMA_Priority_High;
	
	// ��ֹ�洢�����洢��ģʽ����Ϊ�Ǵ����赽�洢��
	DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
	
	// ��ʼ��DMA
	DMA_Init(DMA1_Channel1, &DMA_InitStructure);

    DMA_ITConfig(DMA1_Channel1, DMA_IT_HT, ENABLE);   //debug


	
	// ʹ�� DMA ͨ��
	DMA_Cmd(DMA1_Channel1 , ENABLE);
	
	// ADC ģʽ����
	// ֻʹ��һ��ADC�����ڵ�ģʽ
	ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
	
	// ɨ��ģʽ
	ADC_InitStructure.ADC_ScanConvMode = ENABLE ; 

	// ����ת��ģʽ
	ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;

	// �����ⲿ����ת��������������
	ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;

	// ת������Ҷ���
	ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
	
	// ת��ͨ������
	ADC_InitStructure.ADC_NbrOfChannel = NOFCHANEL;	
		
	// ��ʼ��ADC
	ADC_Init(ADC1, &ADC_InitStructure);
	
	// ����ADCʱ�ӣΪPCLK2��8��Ƶ����9MHz
	RCC_ADCCLKConfig(RCC_PCLK2_Div8); 
	
	// ����ADC ͨ����ת��˳��Ͳ���ʱ��
	ADC_RegularChannelConfig(ADC1, ADC_CHANNEL1, 1, ADC_SampleTime_239Cycles5);
	ADC_RegularChannelConfig(ADC1, ADC_CHANNEL2, 2, ADC_SampleTime_239Cycles5);
	ADC_RegularChannelConfig(ADC1, ADC_CHANNEL3, 3, ADC_SampleTime_239Cycles5);
	ADC_RegularChannelConfig(ADC1, ADC_CHANNEL4, 4, ADC_SampleTime_239Cycles5);
	ADC_RegularChannelConfig(ADC1, ADC_CHANNEL5, 5, ADC_SampleTime_239Cycles5);
	ADC_RegularChannelConfig(ADC1, ADC_CHANNEL6, 6, ADC_SampleTime_239Cycles5);
	ADC_RegularChannelConfig(ADC1, ADC_CHANNEL7, 7, ADC_SampleTime_239Cycles5);
	
	// ʹ��ADC DMA ����
	ADC_DMACmd(ADC1, ENABLE);
	
	// ����ADC ������ʼת��
	ADC_Cmd(ADC1, ENABLE);
	
	// ��ʼ��ADC У׼�Ĵ���  
	ADC_ResetCalibration(ADC1);
	// �ȴ�У׼�Ĵ�����ʼ�����
	while(ADC_GetResetCalibrationStatus(ADC1));
	
	// ADC��ʼУ׼
	ADC_StartCalibration(ADC1);
	// �ȴ�У׼���
	while(ADC_GetCalibrationStatus(ADC1));
	
	// ����û�в����ⲿ����������ʹ���������ADCת�� 
	ADC_SoftwareStartConvCmd(ADC1, ENABLE);

	
}

3.3. 中断服务程序

void DMA1_Channel1_IRQHandler(void)
{
	static u8 reverse=0;


		if(DMA_GetFlagStatus(DMA1_FLAG_HT1))
		{
			DMA_ClearITPendingBit(DMA1_FLAG_HT1);
			ADC_Average();

		}
		else
		{
			DMA_ClearITPendingBit(DMA1_FLAG_TE1);	
		}


}

4. 常用代码

开关总中断
__disable_irq();
__enable_irq();

空运转
__nop();

软件复位
void soft_reset(void)
{
__set_FAULTMASK(1);
NVIC_SystemReset();
}

本文地址:https://blog.csdn.net/no1xizhong/article/details/109000661