adc初始化代码
#include "adc.h"
#include "systick.h"
#include "stdio.h"
#include "stdbool.h"
#include "string.h"
//ADC���ţ�ADC2 IN0~IN15
// ADC1 IN4~IN7
//adc_value_Port1
// PA0 ADC012_IN0 AIN1 12V_1_Vad PoutV/1.5*12 ��ѹֵ
// PA1 ADC012_IN1 AIN2 12V_1_Iad ��2Pout-2.5��/0.4 ����ֵ
// PA2 ADC012_IN2 AIN3 12V_2_Vad PoutV/1.5*12 ��ѹֵ
// PA3 ADC012_IN3 AIN4 12V_2_Iad ��2Pout-2.5��/0.4 ����ֵ
// PF6 ADC2_IN4 AIN5 12V_3_Vad PoutV/1.5*12 ��ѹֵ
// PF7 ADC2_IN5 AIN6 12V_3_Iad ��2Pout-2.5��/0.4 ����ֵ
// PF8 ADC2_IN6 AIN7 12V_4_Vad PoutV/1.5*12 ��ѹֵ
// PF9 ADC2_IN7 AIN8 12V_4_Iad ��2Pout-2.5��/0.1 ����ֵ
// PF10 ADC2_IN8 AIN9 6V_1_Vad PoutV/3.4*12 ��ѹֵ
// PF3 ADC2_IN9 AIN10 6V_1_Iad ��2Pout-2.5��/0.4 ����ֵ
// PC0 ADC012_IN10 AIN11 6V_2_Vad PoutV/3.4*12 ��ѹֵ
// PC1 ADC012_IN11 AIN12 6V_2_Iad ��2Pout-2.5��/0.4 ����ֵ
// PC2 ADC012_IN12 AIN13 6V_3_Vad PoutV/3.4*12 ��ѹֵ
// PC3 ADC012_IN13 AIN14 6V_3_Iad ��2Pout-2.5��/0.4 ����ֵ
// PF4 ADC2_IN14 AIN15 6V_4_Vad PoutV/3.4*12 ��ѹֵ
// PF5 ADC2_IN15 AIN16 6V_4_Iad ��2Pout-2.5��/0.4 ����ֵ
//adc_value_Port2
// PA4 ADC1_IN4 AIN17 6V_5_Vad PoutV/3.4*12 ��ѹֵ
// PA5 ADC1_IN5 AIN18 6V_5_Iad ��2Pout-2.5��/0.4 ����ֵ
// PA6 ADC1_IN6 AIN19 48V_Vad 2.5+48/4810/10*0.625 ��ѹֵ
// PA7 ADC1_IN7 AIN20 48V_Iad ��2Pout-2.5��/0.1 ����ֵ
//
uint16_t adc_value_Port1[16];
uint16_t adc_value_Port2[4];
uint16_t adc_value_Port1CP[16];
uint16_t adc_value_Port2CP[4];
uint8_t adc_value_8bit[40];
bool OverVoltageFlage = false;
bool OverVoltage48Flage = false;
uint8_t OverVoltageChnnelBuff[5] = {0,0,0,0,0};
uint8_t OverVoltage48ChnnelBuff[1] = {0};
uint16_t CurrentCalibrationBuff[10]={0,0,0,0,0,0,0,0,0,0};
void adc_init(void)
{
rcu_periph_clock_enable(RCU_DMA1);
rcu_periph_clock_enable(RCU_GPIOA); //��������ʱ��
rcu_periph_clock_enable(RCU_GPIOC);
rcu_periph_clock_enable(RCU_GPIOF);
rcu_periph_clock_enable(RCU_ADC1);
rcu_periph_clock_enable(RCU_ADC2);
gpio_mode_set(GPIOA,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,GPIO_PIN_0);
gpio_mode_set(GPIOA,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,GPIO_PIN_1);
gpio_mode_set(GPIOA,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,GPIO_PIN_2);
gpio_mode_set(GPIOA,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,GPIO_PIN_3);
gpio_mode_set(GPIOA,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,GPIO_PIN_4);
gpio_mode_set(GPIOA,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,GPIO_PIN_5);
gpio_mode_set(GPIOA,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,GPIO_PIN_6);
gpio_mode_set(GPIOA,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,GPIO_PIN_7);
gpio_mode_set(GPIOC,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,GPIO_PIN_0);
gpio_mode_set(GPIOC,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,GPIO_PIN_1);
gpio_mode_set(GPIOC,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,GPIO_PIN_2);
gpio_mode_set(GPIOC,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,GPIO_PIN_3);
gpio_mode_set(GPIOF,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,GPIO_PIN_3);
gpio_mode_set(GPIOF,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,GPIO_PIN_4);
gpio_mode_set(GPIOF,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,GPIO_PIN_5);
gpio_mode_set(GPIOF,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,GPIO_PIN_6);
gpio_mode_set(GPIOF,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,GPIO_PIN_7);
gpio_mode_set(GPIOF,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,GPIO_PIN_8);
gpio_mode_set(GPIOF,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,GPIO_PIN_9);
gpio_mode_set(GPIOF,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,GPIO_PIN_10);
//adc2
adc_deinit(); //ȱʡ
adc_sync_mode_config(ADC_SYNC_MODE_INDEPENDENT); //��ADC��������
adc_resolution_config(ADC2,ADC_RESOLUTION_12B); //�ֱ���12bit
adc_special_function_config(ADC2, ADC_CONTINUOUS_MODE, ENABLE); //����ģʽ
adc_special_function_config(ADC2, ADC_SCAN_MODE, ENABLE); //ɨ��ģʽ
adc_data_alignment_config(ADC2, ADC_DATAALIGN_RIGHT); //LSB��λ��ǰ
adc_channel_length_config(ADC2, ADC_ROUTINE_CHANNEL,16); //����ͨ���� 10ͨ��
adc_routine_channel_config(ADC2,0,ADC_CHANNEL_0,ADC_SAMPLETIME_144); //ͨ������ 8usһ��ͨ��
adc_routine_channel_config(ADC2,1,ADC_CHANNEL_1,ADC_SAMPLETIME_144); //ͨ������
adc_routine_channel_config(ADC2,2,ADC_CHANNEL_2,ADC_SAMPLETIME_144); //ͨ������
adc_routine_channel_config(ADC2,3,ADC_CHANNEL_3,ADC_SAMPLETIME_144); //ͨ������
adc_routine_channel_config(ADC2,4,ADC_CHANNEL_4,ADC_SAMPLETIME_144); //ͨ������
adc_routine_channel_config(ADC2,5,ADC_CHANNEL_5,ADC_SAMPLETIME_144); //ͨ������
adc_routine_channel_config(ADC2,6,ADC_CHANNEL_6,ADC_SAMPLETIME_144); //ͨ������
adc_routine_channel_config(ADC2,7,ADC_CHANNEL_7,ADC_SAMPLETIME_144); //ͨ������
adc_routine_channel_config(ADC2,8,ADC_CHANNEL_8,ADC_SAMPLETIME_144); //ͨ������
adc_routine_channel_config(ADC2,9,ADC_CHANNEL_9,ADC_SAMPLETIME_144); //ͨ������
adc_routine_channel_config(ADC2,10,ADC_CHANNEL_10,ADC_SAMPLETIME_144); //ͨ������
adc_routine_channel_config(ADC2,11,ADC_CHANNEL_11,ADC_SAMPLETIME_144); //ͨ������
adc_routine_channel_config(ADC2,12,ADC_CHANNEL_12,ADC_SAMPLETIME_144); //ͨ������
adc_routine_channel_config(ADC2,13,ADC_CHANNEL_13,ADC_SAMPLETIME_144); //ͨ������
adc_routine_channel_config(ADC2,14,ADC_CHANNEL_14,ADC_SAMPLETIME_144); //ͨ������
adc_routine_channel_config(ADC2,15,ADC_CHANNEL_15,ADC_SAMPLETIME_144); //ͨ������
adc_dma_request_after_last_enable(ADC2);
adc_dma_mode_enable(ADC2);
adc_enable(ADC2); //ʹ��
delay_1ms(1);
adc_calibration_enable(ADC2); //У
adc_clock_config(ADC_ADCCK_PCLK2_DIV8); //8��Ƶ 5MHZ
//adc1
// adc_deinit(); //ȱʡ
adc_sync_mode_config(ADC_SYNC_MODE_INDEPENDENT); //��ADC��������
adc_resolution_config(ADC1,ADC_RESOLUTION_12B); //�ֱ���12bit
adc_special_function_config(ADC1, ADC_CONTINUOUS_MODE, ENABLE); //����ģʽ
adc_special_function_config(ADC1, ADC_SCAN_MODE, ENABLE); //ɨ��ģʽ
adc_data_alignment_config(ADC1, ADC_DATAALIGN_RIGHT); //LSB��λ��ǰ
adc_channel_length_config(ADC1, ADC_ROUTINE_CHANNEL,4); //����ͨ���� 4ͨ��
adc_routine_channel_config(ADC1,0,ADC_CHANNEL_4,ADC_SAMPLETIME_144); //ͨ������ 8usһ��ͨ��
adc_routine_channel_config(ADC1,1,ADC_CHANNEL_5,ADC_SAMPLETIME_144); //ͨ������
adc_routine_channel_config(ADC1,2,ADC_CHANNEL_6,ADC_SAMPLETIME_144); //ͨ������
adc_routine_channel_config(ADC1,3,ADC_CHANNEL_7,ADC_SAMPLETIME_144); //ͨ������
adc_dma_request_after_last_enable(ADC1);
adc_dma_mode_enable(ADC1);
adc_enable(ADC1); //ʹ��
delay_1ms(1);
adc_calibration_enable(ADC1); //У
dma_single_data_parameter_struct dma_single_data_parameter;
dma_deinit(DMA1, DMA_CH0);
dma_single_data_parameter.periph_addr = (uint32_t)(&ADC_RDATA(ADC2));
dma_single_data_parameter.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
dma_single_data_parameter.memory0_addr = (uint32_t)(adc_value_Port1);
dma_single_data_parameter.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
dma_single_data_parameter.periph_memory_width = DMA_PERIPH_WIDTH_16BIT;
dma_single_data_parameter.direction = DMA_PERIPH_TO_MEMORY;
dma_single_data_parameter.number = 16;
dma_single_data_parameter.priority = DMA_PRIORITY_HIGH;
dma_single_data_mode_init(DMA1, DMA_CH0,&dma_single_data_parameter);
dma_channel_subperipheral_select(DMA1, DMA_CH0, DMA_SUBPERI2);
dma_circulation_enable(DMA1, DMA_CH0);
dma_channel_enable(DMA1, DMA_CH0);
dma_deinit(DMA1, DMA_CH3);
dma_single_data_parameter.periph_addr = (uint32_t)(&ADC_RDATA(ADC1));
dma_single_data_parameter.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
dma_single_data_parameter.memory0_addr = (uint32_t)(adc_value_Port2);
dma_single_data_parameter.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
dma_single_data_parameter.periph_memory_width = DMA_PERIPH_WIDTH_16BIT;
dma_single_data_parameter.direction = DMA_PERIPH_TO_MEMORY;
dma_single_data_parameter.number = 4;
dma_single_data_parameter.priority = DMA_PRIORITY_HIGH;
dma_single_data_mode_init(DMA1, DMA_CH3,&dma_single_data_parameter);
dma_channel_subperipheral_select(DMA1, DMA_CH3, DMA_SUBPERI1);
dma_circulation_enable(DMA1, DMA_CH3);
dma_channel_enable(DMA1, DMA_CH3);
adc_software_trigger_enable(ADC1,ADC_ROUTINE_CHANNEL); //��������
adc_software_trigger_enable(ADC2,ADC_ROUTINE_CHANNEL); //��������
}
uint8_t* ADC_ValueGet(void)
{
uint16_t buff0[20],buff1[20],buff2[20],buff3[20],buff4[20],buff5[20],buff6[20],buff7[20],buff8[20],buff9[20],everBuff[20];
uint16_t buff10[20],buff11[20],buff12[20],buff13[20],buff14[20],buff15[20],buff16[20],buff17[20],buff18[20],buff19[20];
uint16_t *IOBUFF[]={buff0,buff1,buff2,buff3,buff4,buff5,buff6,buff7,buff8,buff9,buff10,
buff11,buff12,buff13,buff14,buff15,buff16,buff17,buff18,buff19};
//����ADC�ɼ�����ʵ�ʵ�ѹֵ��������1000����
// memcpy(adc_value_Port1CP,adc_value_Port1,sizeof(adc_value_Port1));
// memcpy(adc_value_Port2CP,adc_value_Port2,sizeof(adc_value_Port2));
for(int i = 0;i<20;i++)
{
for(int j=0;j<16;j++)
{
IOBUFF[i][j] = adc_value_Port1[j];
}
for(int s = 16;s<20;s++)
{
IOBUFF[i][s] = adc_value_Port2[s-16];
}
delay_1ms(1);
}
for(int i=0;i<20;i++)
{
everBuff[i] =(buff0[i]+buff1[i]+buff2[i]+buff3[i]+buff4[i]+buff5[i]+buff6[i]+buff7[i]+buff8[i]+buff9[i]+
buff10[i]+buff11[i]+buff12[i]+buff13[i]+buff14[i]+buff15[i]+buff16[i]+buff17[i]+buff18[i]+buff19[i])/20;
// everBuff[i] = everBuff[i]/4096*3.3*3/2*1000;
}
for(int i=0;i<16;i++)
{
adc_value_Port1CP[i] = everBuff[i];
}
for(int i=16;i<20;i++)
{
adc_value_Port2CP[i-16]=everBuff[i];
}
for(int i=0;i<16;i++)
{
adc_value_Port1CP[i]&=0x0FFF;
adc_value_Port1CP[i] = (uint16_t)((adc_value_Port1CP[i] * 3.340f * 1000 / 4095));//3.318
}
adc_value_Port1CP[0] = (uint16_t)(adc_value_Port1CP[0]/1.5f*12);
adc_value_Port1CP[1] = (uint16_t)((adc_value_Port1CP[1]*2-CurrentCalibrationBuff[0])/0.4f-130);
adc_value_Port1CP[2] = (uint16_t)(adc_value_Port1CP[2]/1.5f*12);
adc_value_Port1CP[3] = (uint16_t)((adc_value_Port1CP[3]*2-CurrentCalibrationBuff[1])/0.4f-130);
adc_value_Port1CP[4] = (uint16_t)(adc_value_Port1CP[4]/1.5f*12);
adc_value_Port1CP[5] = (uint16_t)((adc_value_Port1CP[5]*2-CurrentCalibrationBuff[2])/0.4f-120);
// adc_value_Port1CP[6] = (uint16_t)(adc_value_Port1CP[6]/1.5f*12); //12V-4
// adc_value_Port1CP[7] = (uint16_t)((adc_value_Port1CP[7]*2-CurrentCalibrationBuff[3])/0.1f); //12V-4
/*2023/9/27 ��12-4��ѹ����ͨ��*/
adc_value_Port1CP[6] = (uint16_t)(adc_value_Port1CP[6]/1.5f*12); //12V-4
adc_value_Port1CP[7] = (uint16_t)(adc_value_Port1CP[7]*2); //12V-4
adc_value_Port1CP[8] = (uint16_t)(adc_value_Port1CP[8]/3.4f*12);
adc_value_Port1CP[9] = (uint16_t)((adc_value_Port1CP[9]*2-CurrentCalibrationBuff[4])/0.4f);
adc_value_Port1CP[10] = (uint16_t)(adc_value_Port1CP[10]/3.4f*12);
adc_value_Port1CP[11] = (uint16_t)((adc_value_Port1CP[11]*2-CurrentCalibrationBuff[5])/0.4f);
adc_value_Port1CP[12] = (uint16_t)(adc_value_Port1CP[12]/3.4f*12);
adc_value_Port1CP[13] = (uint16_t)((adc_value_Port1CP[13]*2-CurrentCalibrationBuff[6])/0.4f);
adc_value_Port1CP[14] = (uint16_t)(adc_value_Port1CP[14]/3.4f*12);
adc_value_Port1CP[15] = (uint16_t)((adc_value_Port1CP[15]*2-CurrentCalibrationBuff[7])/0.4f);
// for(int i=1;i<16;i=i+2)
// {
// if (adc_value_Port1CP[i] < 0x0032)
// {
// adc_value_Port1CP[i] = 0x0000;
// }
// }
for(int i=0;i<16;i++)
{
adc_value_8bit[2*i] = adc_value_Port1CP[i];
adc_value_8bit[2*i+1] = adc_value_Port1CP[i]>>8;
}
for(int i=0;i<4;i++)
{
adc_value_Port2CP[i]&=0x0FFF;
adc_value_Port2CP[i] = (uint16_t)((adc_value_Port2CP[i]* 3.320f * 1000 / 4095));
}
adc_value_Port2CP[0] = (uint16_t)( adc_value_Port2CP[0]/3.4f*12);
adc_value_Port2CP[1] = (uint16_t)((adc_value_Port2CP[1]*2-CurrentCalibrationBuff[8])/0.4f);
adc_value_Port2CP[2] = (uint16_t)((adc_value_Port2CP[2]*2-2491)/0.625f*0.01f*4870);
adc_value_Port2CP[3] = (uint16_t)((adc_value_Port2CP[3]*2-CurrentCalibrationBuff[9])/0.1f);
for(int i=0;i<4;i++)
{
adc_value_8bit[2*i+32] = adc_value_Port2CP[i];
adc_value_8bit[2*i+33] = adc_value_Port2CP[i]>>8;
}
return adc_value_8bit;
}
uint8_t* OverVoltage(void) //6V��ѹ���
{
uint16_t Change6Vbuff[5];
Change6Vbuff[0] = adc_value_Port1[7];
Change6Vbuff[1] = adc_value_Port1[9];
Change6Vbuff[2] = adc_value_Port1[11];
Change6Vbuff[3] = adc_value_Port1[13];
Change6Vbuff[4] = adc_value_Port2[0];
for(int i=0;i<5;i++)
{
Change6Vbuff[i] = Change6Vbuff[i]&0x0FFF;
Change6Vbuff[i] = (uint16_t)(((Change6Vbuff[i]*1000/4095)*3.318f)*12/3.4f);
if(Change6Vbuff[i]>6600)
{
OverVoltageChnnelBuff[i] = 1 ;
OverVoltageFlage = true;
}else
{
OverVoltageChnnelBuff[i] = 0 ;
}
if(OverVoltageChnnelBuff[0]&&OverVoltageChnnelBuff[1]&&OverVoltageChnnelBuff[2]&&OverVoltageChnnelBuff[3]
&&OverVoltageChnnelBuff[4]==0)
{
OverVoltageFlage = false;
}
}
return OverVoltageChnnelBuff;
}
void OverVoltage_48V(void)
{
uint16_t Change48Vbuff[1];
Change48Vbuff[0] = adc_value_Port2[2];
Change48Vbuff[0] &= 0X0FFF;
Change48Vbuff[0] = Change48Vbuff[0]*1000/4095*3.318f*2;
if(Change48Vbuff[0]>52800)
{
OverVoltage48Flage = true;
}else
{
OverVoltage48Flage = false;
}
}
//2023-9-14 ���ӳ�ʼ�� �ɼ�����оƬ��̬�����ѹ
void InitStandard(void) // ����������У
{
uint16_t buff0[20],buff1[20],buff2[20],buff3[20],buff4[20],buff5[20],buff6[20],buff7[20],buff8[20],buff9[20],everBuff[20];
uint16_t buff10[20],buff11[20],buff12[20],buff13[20],buff14[20],buff15[20],buff16[20],buff17[20],buff18[20],buff19[20];
uint16_t *IOBUFF[]={buff0,buff1,buff2,buff3,buff4,buff5,buff6,buff7,buff8,buff9,buff10,
buff11,buff12,buff13,buff14,buff15,buff16,buff17,buff18,buff19};
for(int i = 0;i<20;i++)
{
for(int j=0;j<16;j++)
{
IOBUFF[i][j] = adc_value_Port1[j];
}
for(int s = 16;s<20;s++)
{
IOBUFF[i][s] = adc_value_Port2[s-16];
}
delay_1ms(1);
}
for(int i=0;i<20;i++)
{
everBuff[i] =(buff0[i]+buff1[i]+buff2[i]+buff3[i]+buff4[i]+buff5[i]+buff6[i]+buff7[i]+buff8[i]+buff9[i]+
buff10[i]+buff11[i]+buff12[i]+buff13[i]+buff14[i]+buff15[i]+buff16[i]+buff17[i]+buff18[i]+buff19[i])/20;
everBuff[i] = everBuff[i]*1000*3.318f/4095;
}
for(int i=0;i<10;i++)
{
CurrentCalibrationBuff[i] = everBuff[i*2+1]*2 ;
}
}
Usart+DMA
#include "stdio.h"
#include "stdbool.h"
#include "usart.h"
#include "systick.h"
#define USART0_DATA_ADDRESS ((uint32_t)&USART_DATA(USART0))
bool DMA_Send = false;
uint8_t tx_buffer[64];
uint8_t rx_buffer[64];
/*!
\brief configure COM port
\param[in] COM: COM on the board
\arg COM0: COM on the board
\param[out] none
\retval none
*/
void gd_com_init(uint32_t com,uint32_t baud)
{
/* enable GPIO clock */
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_USART0);
gpio_af_set(GPIOA, GPIO_AF_7, GPIO_PIN_9);
gpio_af_set(GPIOA, GPIO_AF_7, GPIO_PIN_10);
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_9);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
/* configure USART Rx as alternate function push-pull */
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_10);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_10);
/* USART configure */
usart_deinit(com);
usart_baudrate_set(com,baud);
usart_parity_config(com,USART_PM_NONE);
usart_stop_bit_set(com,USART_STB_1BIT);
usart_receive_config(com, USART_RECEIVE_ENABLE);
usart_transmit_config(com, USART_TRANSMIT_ENABLE);
usart_dma_receive_config(com, USART_RECEIVE_DMA_ENABLE);
usart_dma_transmit_config(com,USART_TRANSMIT_DMA_ENABLE);
usart_enable(com);
nvic_irq_enable(USART0_IRQn, 0, 0);
usart_interrupt_enable(com, USART_INT_IDLE); //�����ж�
usart_flag_clear(com,USART_FLAG_IDLE);
}
void usart_dma_config(void)
{
dma_single_data_parameter_struct dma_init_struct;
/* enable DMA1 */
rcu_periph_clock_enable(RCU_DMA1);
/* deinitialize DMA channel7(USART0 tx) */
dma_deinit(DMA1, DMA_CH7);
dma_init_struct.direction = DMA_MEMORY_TO_PERIPH;
dma_init_struct.memory0_addr = (uint32_t)tx_buffer;
dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
dma_init_struct.periph_memory_width = DMA_PERIPH_WIDTH_8BIT;
dma_init_struct.number = 64;
dma_init_struct.periph_addr = USART0_DATA_ADDRESS;
dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
dma_single_data_mode_init(DMA1, DMA_CH7, &dma_init_struct);
/* configure DMA mode */
dma_circulation_disable(DMA1, DMA_CH7);
dma_channel_subperipheral_select(DMA1, DMA_CH7, DMA_SUBPERI4);
//dma_channel_enable(DMA1, DMA_CH7);
nvic_irq_enable(DMA1_Channel7_IRQn,2,0);
dma_interrupt_enable(DMA1,DMA_CH7,DMA_CHXCTL_FTFIE);
dma_deinit(DMA1, DMA_CH2);
dma_init_struct.direction = DMA_PERIPH_TO_MEMORY;
dma_init_struct.memory0_addr = (uint32_t)rx_buffer;
dma_single_data_mode_init(DMA1, DMA_CH2, &dma_init_struct);
/* configure DMA mode */
dma_circulation_disable(DMA1, DMA_CH2);
dma_channel_subperipheral_select(DMA1, DMA_CH2, DMA_SUBPERI4);
dma_channel_enable(DMA1, DMA_CH2);
//nvic_irq_enable(DMA1_Channel2_IRQn,2,0);
//dma_interrupt_enable(DMA1,DMA_CH2,DMA_CHXCTL_FTFIE);
}
/* retarget the C library printf function to the USART */
int fputc(int ch, FILE *f)
{
usart_data_transmit(COM0, (uint8_t)ch);
while(RESET == usart_flag_get(COM0, USART_FLAG_TBE));
return ch;
}
//���ڵ��ַ�����
void com_put_char(uint8_t dat)
{
usart_data_transmit(COM0, dat);
while(RESET == usart_flag_get(COM0, USART_FLAG_TBE));
}
//�����ַ�������
void uart_printf(char *str)
{
unsigned int k=0;
do
{
com_put_char( *(str + k) );
k++;
} while(*(str + k)!='\0');
}
//�����ַ������߽ṹ�巢��
void uart_send_data(uint8_t* data, int len)
{
uint8_t i;
for(i = 0; i < len; i++)
{
com_put_char(data[i]);
}
}
void usart_dma_send(uint8_t len)
{
dma_channel_disable(DMA1,DMA_CH7);
dma_transfer_number_config(DMA1,DMA_CH7,len);
dma_channel_enable(DMA1,DMA_CH7);
// while(usart_flag_get(USART1,USART_FLAG_TC)!=RESET);
}
void usart_dma_en(void)
{
// while(DMA_Send);
// DMA_Send = false;
dma_flag_clear(DMA1, DMA_CH2, DMA_FLAG_FTF);
dma_transfer_number_config(DMA1,DMA_CH2,64);
dma_channel_enable(DMA1,DMA_CH2);
}
void usart_send(uint8_t *buff,uint8_t len)
{
for(int i = 0;i<len;i++)
{
usart_data_transmit(USART0, buff[i]);
delay_1ms(5);
}
}
IIC代码 使用sd5075
#include "IIC.h"
#include "systick.h"
/*һ�β�����ֻ����ɶ�����д �����ֶ���д*/
//#define SDL_H gpio_bit_set(GPIOF,GPIO_PIN_0)
//#define SDL_L gpio_bit_reset(GPIOF,GPIO_PIN_0)
//#define SCL_H gpio_bit_set(GPIOF,GPIO_PIN_1)
//#define SCL_L gpio_bit_reset(GPIOF,GPIO_PIN_1)
#define I2C1_SLAVE_ADDRESS7 0x9A
uint8_t I2CReceBuffer[16];
uint8_t I2CSendBuffer[2] = {0x9A,0x00};
void IICgpio_Init(void)
{
rcu_periph_clock_enable(RCU_GPIOF);
gpio_af_set(GPIOF, GPIO_AF_4, GPIO_PIN_0);
gpio_af_set(GPIOF, GPIO_AF_4, GPIO_PIN_1);
gpio_mode_set(GPIOF, GPIO_MODE_AF, GPIO_PUPD_NONE,GPIO_PIN_0);
gpio_output_options_set(GPIOF, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,GPIO_PIN_0);
gpio_mode_set(GPIOF, GPIO_MODE_AF, GPIO_PUPD_NONE,GPIO_PIN_1);
gpio_output_options_set(GPIOF, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ,GPIO_PIN_1);
}
void IIC_Init(void)
{
rcu_periph_clock_enable(RCU_I2C1);
i2c_clock_config(I2C1,100000,I2C_DTCY_2);
i2c_mode_addr_config(I2C1,I2C_I2CMODE_ENABLE,I2C_ADDFORMAT_7BITS,0xA0);//�ӻ�ģʽ�µĵ�ַ
i2c_enable(I2C1);
i2c_ack_config(I2C1,I2C_ACK_ENABLE);
}
void IIC_Send(uint8_t addr,uint8_t *buff,uint8_t len)
{
while(i2c_flag_get(I2C1, I2C_FLAG_I2CBSY)); //æ
i2c_start_on_bus(I2C1);
while(!i2c_flag_get(I2C1, I2C_FLAG_SBSEND));//��ʼλ����
i2c_master_addressing(I2C1, addr, I2C_TRANSMITTER);
while(!i2c_flag_get(I2C1, I2C_FLAG_ADDSEND));//��ַ����
i2c_flag_clear(I2C1, I2C_FLAG_ADDSEND);
while(!i2c_flag_get(I2C1, I2C_FLAG_TBE));//�����ڼ� I2C_dataΪ��
//for(int i=0;i<len;i++)
// {
i2c_data_transmit(I2C1,buff[1]);
while(!i2c_flag_get(I2C1,I2C_FLAG_TBE));//�����ڼ�I2C_DATAΪ��
// }
i2c_stop_on_bus(I2C1);
while(I2C_CTL0(I2C1)&I2C_CTL0_STOP);
}
void IIC_Receive(uint8_t addr,uint8_t *buff,uint8_t len)
{
while(i2c_flag_get(I2C1,I2C_FLAG_I2CBSY)); //æ��־
i2c_start_on_bus(I2C1);
while(!i2c_flag_get(I2C1,I2C_FLAG_SBSEND)); //�ȴ� ��ʼ�źŷ���
i2c_master_addressing(I2C1,addr,I2C_RECEIVER);
while(!i2c_flag_get(I2C1,I2C_FLAG_ADDSEND)); //�ȴ� ���͵�ַ
i2c_flag_clear(I2C1,I2C_FLAG_ADDSEND);
for(int i=0; i<len; i++){
if((len-2) == i)
{
while(!i2c_flag_get(I2C1, I2C_FLAG_BTC));//�ֽڴ������
i2c_ack_config(I2C1, I2C_ACK_DISABLE);
}
while(!i2c_flag_get(I2C1, I2C_FLAG_RBNE));//�����ڼ� I2C_data�ǿ�
buff[i] = i2c_data_receive(I2C1);
}
i2c_stop_on_bus(I2C1);
while(I2C_CTL0(I2C1)&I2C_CTL0_STOP);
i2c_ack_config(I2C1, I2C_ACK_ENABLE);
}
int16_t Data_Coculeat(uint8_t *buff) //�����¶�ֵ ��12bit ��8bitΪ�������֣���4bitΪС��λ �ֱ���0.0625
{
int16_t Sener = 0;
Sener = (int16_t)(buff[0]<<8)+buff[1];
Sener = Sener>>4;
if(Sener&0x800)//���¶�
{
Sener = (Sener-4096)/16;
}
if(!(Sener&0x800))//���¶�
{
Sener = Sener/16;//*0.0625
}
return Sener;
}
void SD5075Read(void)
{
}
USB_CDC_ACM
#include "cdc_acm_core.h"
#define USBD_VID 0x28E9U
#define USBD_PID 0x018AU
/* note:it should use the C99 standard when compiling the below codes */
/* USB standard device descriptor */
__ALIGN_BEGIN const usb_desc_dev cdc_dev_desc __ALIGN_END =
{
.header =
{
.bLength = USB_DEV_DESC_LEN,
.bDescriptorType = USB_DESCTYPE_DEV,
},
.bcdUSB = 0x0200U,
.bDeviceClass = USB_CLASS_CDC,
.bDeviceSubClass = 0x00U,
.bDeviceProtocol = 0x00U,
.bMaxPacketSize0 = USB_FS_EP0_MAX_LEN,
.idVendor = USBD_VID,
.idProduct = USBD_PID,
.bcdDevice = 0x0100U,
.iManufacturer = STR_IDX_MFC,
.iProduct = STR_IDX_PRODUCT,
.iSerialNumber = STR_IDX_SERIAL,
.bNumberConfigurations = USBD_CFG_MAX_NUM,
};
/* USB device configuration descriptor */
__ALIGN_BEGIN const usb_cdc_desc_config_set cdc_config_desc __ALIGN_END =
{
.config =
{
.header =
{
.bLength = sizeof(usb_desc_config),
.bDescriptorType = USB_DESCTYPE_CONFIG,
},
.wTotalLength = USB_CDC_ACM_CONFIG_DESC_SIZE,
.bNumInterfaces = 0x02U,
.bConfigurationValue = 0x01U,
.iConfiguration = 0x00U,
.bmAttributes = 0x80U,
.bMaxPower = 0x32U
},
.cmd_itf =
{
.header =
{
.bLength = sizeof(usb_desc_itf),
.bDescriptorType = USB_DESCTYPE_ITF
},
.bInterfaceNumber = 0x00U,
.bAlternateSetting = 0x00U,
.bNumEndpoints = 0x01U,
.bInterfaceClass = USB_CLASS_CDC,
.bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
.bInterfaceProtocol = USB_CDC_PROTOCOL_AT,
.iInterface = 0x00U
},
.cdc_header =
{
.header =
{
.bLength = sizeof(usb_desc_header_func),
.bDescriptorType = USB_DESCTYPE_CS_INTERFACE
},
.bDescriptorSubtype = 0x00U,
.bcdCDC = 0x0110U
},
.cdc_call_managment =
{
.header =
{
.bLength = sizeof(usb_desc_call_managment_func),
.bDescriptorType = USB_DESCTYPE_CS_INTERFACE
},
.bDescriptorSubtype = 0x01U,
.bmCapabilities = 0x00U,
.bDataInterface = 0x01U
},
.cdc_acm =
{
.header =
{
.bLength = sizeof(usb_desc_acm_func),
.bDescriptorType = USB_DESCTYPE_CS_INTERFACE
},
.bDescriptorSubtype = 0x02U,
.bmCapabilities = 0x02U,
},
.cdc_union =
{
.header =
{
.bLength = sizeof(usb_desc_union_func),
.bDescriptorType = USB_DESCTYPE_CS_INTERFACE
},
.bDescriptorSubtype = 0x06U,
.bMasterInterface = 0x00U,
.bSlaveInterface0 = 0x01U,
},
.cdc_cmd_endpoint =
{
.header =
{
.bLength = sizeof(usb_desc_ep),
.bDescriptorType = USB_DESCTYPE_EP,
},
.bEndpointAddress = CDC_CMD_EP,
.bmAttributes = USB_EP_ATTR_INT,
.wMaxPacketSize = USB_CDC_CMD_PACKET_SIZE,
.bInterval = 0x0AU
},
.cdc_data_interface =
{
.header =
{
.bLength = sizeof(usb_desc_itf),
.bDescriptorType = USB_DESCTYPE_ITF,
},
.bInterfaceNumber = 0x01U,
.bAlternateSetting = 0x00U,
.bNumEndpoints = 0x02U,
.bInterfaceClass = USB_CLASS_DATA,
.bInterfaceSubClass = 0x00U,
.bInterfaceProtocol = USB_CDC_PROTOCOL_NONE,
.iInterface = 0x00U
},
.cdc_out_endpoint =
{
.header =
{
.bLength = sizeof(usb_desc_ep),
.bDescriptorType = USB_DESCTYPE_EP,
},
.bEndpointAddress = CDC_DATA_OUT_EP,
.bmAttributes = USB_EP_ATTR_BULK,
.wMaxPacketSize = USB_CDC_DATA_PACKET_SIZE,
.bInterval = 0x00U
},
.cdc_in_endpoint =
{
.header =
{
.bLength = sizeof(usb_desc_ep),
.bDescriptorType = USB_DESCTYPE_EP
},
.bEndpointAddress = CDC_DATA_IN_EP,
.bmAttributes = USB_EP_ATTR_BULK,
.wMaxPacketSize = USB_CDC_DATA_PACKET_SIZE,
.bInterval = 0x00U
}
};
/* USB language ID Descriptor */
static __ALIGN_BEGIN const usb_desc_LANGID usbd_language_id_desc __ALIGN_END =
{
.header =
{
.bLength = sizeof(usb_desc_LANGID),
.bDescriptorType = USB_DESCTYPE_STR,
},
.wLANGID = ENG_LANGID
};
/* USB manufacture string */
static __ALIGN_BEGIN const usb_desc_str manufacturer_string __ALIGN_END =
{
.header =
{
.bLength = USB_STRING_LEN(10),
.bDescriptorType = USB_DESCTYPE_STR,
},
.unicode_string = {'G', 'i', 'g', 'a', 'D', 'e', 'v', 'i', 'c', 'e'}
};
/* USB product string */
static __ALIGN_BEGIN const usb_desc_str product_string __ALIGN_END =
{
.header =
{
.bLength = USB_STRING_LEN(12),
.bDescriptorType = USB_DESCTYPE_STR,
},
.unicode_string = {'G', 'D', '3', '2', '-', 'C', 'D', 'C', '_', 'A', 'C', 'M'}
};
/* USBD serial string */
static __ALIGN_BEGIN usb_desc_str serial_string __ALIGN_END =
{
.header =
{
.bLength = USB_STRING_LEN(12),
.bDescriptorType = USB_DESCTYPE_STR,
}
};
/* USB string descriptor set */
void *const usbd_cdc_strings[] =
{
[STR_IDX_LANGID] = (uint8_t *) &usbd_language_id_desc,
[STR_IDX_MFC] = (uint8_t *) &manufacturer_string,
[STR_IDX_PRODUCT] = (uint8_t *) &product_string,
[STR_IDX_SERIAL] = (uint8_t *) &serial_string
};
usb_desc cdc_desc =
{
.dev_desc = (uint8_t *) &cdc_dev_desc,
.config_desc = (uint8_t *) &cdc_config_desc,
.strings = usbd_cdc_strings
};
/* local function prototypes ('static') */
static uint8_t cdc_acm_init (usb_dev *udev, uint8_t config_index);
static uint8_t cdc_acm_deinit (usb_dev *udev, uint8_t config_index);
static uint8_t cdc_acm_req (usb_dev *udev, usb_req *req);
static uint8_t cdc_acm_ctlx_out (usb_dev *udev);
static uint8_t cdc_acm_in (usb_dev *udev, uint8_t ep_num);
static uint8_t cdc_acm_out (usb_dev *udev, uint8_t ep_num);
/* USB CDC device class callbacks structure */
usb_class_core cdc_class =
{
.command = NO_CMD,
.alter_set = 0U,
.init = cdc_acm_init,
.deinit = cdc_acm_deinit,
.req_proc = cdc_acm_req,
.ctlx_out = cdc_acm_ctlx_out,
.data_in = cdc_acm_in,
.data_out = cdc_acm_out
};
/*!
\brief check CDC ACM is ready for data transfer
\param[in] udev: pointer to USB device instance
\param[out] none
\retval 0 if CDC is ready, 5 else
*/
uint8_t cdc_acm_check_ready(usb_dev *udev)
{
if (udev->dev.class_data[CDC_COM_INTERFACE] != NULL) {
usb_cdc_handler *cdc = (usb_cdc_handler *)udev->dev.class_data[CDC_COM_INTERFACE];
if ((1U == cdc->packet_receive) && (1U == cdc->packet_sent)) {
return 0U;
}
}
return 1U;
}
/*!
\brief send CDC ACM data
\param[in] udev: pointer to USB device instance
\param[out] none
\retval USB device operation status
*/
void cdc_acm_data_send (usb_dev *udev)
{
usb_cdc_handler *cdc = (usb_cdc_handler *)udev->dev.class_data[CDC_COM_INTERFACE];
if (0U != cdc->receive_length) {
cdc->packet_sent = 0U;
usbd_ep_send (udev, CDC_DATA_IN_EP, (uint8_t*)(cdc->data), cdc->receive_length);
cdc->receive_length = 0U;
}
}
/*!
\brief receive CDC ACM data
\param[in] udev: pointer to USB device instance
\param[out] none
\retval USB device operation status
*/
void cdc_acm_data_receive (usb_dev *udev)
{
usb_cdc_handler *cdc = (usb_cdc_handler *)udev->dev.class_data[CDC_COM_INTERFACE];
cdc->packet_receive = 0U;
cdc->packet_sent = 0U;
usbd_ep_recev (udev, CDC_DATA_OUT_EP, (uint8_t*)(cdc->data), USB_CDC_DATA_PACKET_SIZE);
}
/*!
\brief initialize the CDC ACM device
\param[in] udev: pointer to USB device instance
\param[in] config_index: configuration index
\param[out] none
\retval USB device operation status
*/
static uint8_t cdc_acm_init (usb_dev *udev, uint8_t config_index)
{
static __ALIGN_BEGIN usb_cdc_handler cdc_handler __ALIGN_END;
/* initialize the data Tx endpoint */
usbd_ep_setup (udev, &(cdc_config_desc.cdc_in_endpoint));
/* initialize the data Rx endpoint */
usbd_ep_setup (udev, &(cdc_config_desc.cdc_out_endpoint));
/* initialize the command Tx endpoint */
usbd_ep_setup (udev, &(cdc_config_desc.cdc_cmd_endpoint));
/* initialize CDC handler structure */
cdc_handler.packet_receive = 1U;
cdc_handler.packet_sent = 1U;
cdc_handler.receive_length = 0U;
cdc_handler.line_coding = (acm_line){
.dwDTERate = 115200U,
.bCharFormat = 0U,
.bParityType = 0U,
.bDataBits = 0x08U
};
udev->dev.class_data[CDC_COM_INTERFACE] = (void *)&cdc_handler;
return USBD_OK;
}
/*!
\brief deinitialize the CDC ACM device
\param[in] udev: pointer to USB device instance
\param[in] config_index: configuration index
\param[out] none
\retval USB device operation status
*/
static uint8_t cdc_acm_deinit (usb_dev *udev, uint8_t config_index)
{
/* deinitialize the data Tx/Rx endpoint */
usbd_ep_clear (udev, CDC_DATA_IN_EP);
usbd_ep_clear (udev, CDC_DATA_OUT_EP);
/* deinitialize the command Tx endpoint */
usbd_ep_clear (udev, CDC_CMD_EP);
return USBD_OK;
}
/*!
\brief handle the CDC ACM class-specific requests
\param[in] udev: pointer to USB device instance
\param[in] req: device class-specific request
\param[out] none
\retval USB device operation status
*/
static uint8_t cdc_acm_req (usb_dev *udev, usb_req *req)
{
usb_cdc_handler *cdc = (usb_cdc_handler *)udev->dev.class_data[CDC_COM_INTERFACE];
usb_transc *transc = NULL;
switch (req->bRequest) {
case SEND_ENCAPSULATED_COMMAND:
/* no operation for this driver */
break;
case GET_ENCAPSULATED_RESPONSE:
/* no operation for this driver */
break;
case SET_COMM_FEATURE:
/* no operation for this driver */
break;
case GET_COMM_FEATURE:
/* no operation for this driver */
break;
case CLEAR_COMM_FEATURE:
/* no operation for this driver */
break;
case SET_LINE_CODING:
transc = &udev->dev.transc_out[0];
/* set the value of the current command to be processed */
udev->dev.class_core->alter_set = req->bRequest;
/* enable EP0 prepare to receive command data packet */
transc->remain_len = req->wLength;
transc->xfer_buf = cdc->cmd;
break;
case GET_LINE_CODING:
transc = &udev->dev.transc_in[0];
cdc->cmd[0] = (uint8_t)(cdc->line_coding.dwDTERate);
cdc->cmd[1] = (uint8_t)(cdc->line_coding.dwDTERate >> 8);
cdc->cmd[2] = (uint8_t)(cdc->line_coding.dwDTERate >> 16);
cdc->cmd[3] = (uint8_t)(cdc->line_coding.dwDTERate >> 24);
cdc->cmd[4] = cdc->line_coding.bCharFormat;
cdc->cmd[5] = cdc->line_coding.bParityType;
cdc->cmd[6] = cdc->line_coding.bDataBits;
transc->xfer_buf = cdc->cmd;
transc->remain_len = 7U;
break;
case SET_CONTROL_LINE_STATE:
/* no operation for this driver */
break;
case SEND_BREAK:
/* no operation for this driver */
break;
default:
break;
}
return USBD_OK;
}
/*!
\brief command data received on control endpoint
\param[in] udev: pointer to USB device instance
\param[out] none
\retval USB device operation status
*/
static uint8_t cdc_acm_ctlx_out (usb_dev *udev)
{
usb_cdc_handler *cdc = (usb_cdc_handler *)udev->dev.class_data[CDC_COM_INTERFACE];
if (udev->dev.class_core->alter_set != NO_CMD) {
/* process the command data */
cdc->line_coding.dwDTERate = (uint32_t)((uint32_t)cdc->cmd[0] |
((uint32_t)cdc->cmd[1] << 8U) |
((uint32_t)cdc->cmd[2] << 16U) |
((uint32_t)cdc->cmd[3] << 24U));
cdc->line_coding.bCharFormat = cdc->cmd[4];
cdc->line_coding.bParityType = cdc->cmd[5];
cdc->line_coding.bDataBits = cdc->cmd[6];
udev->dev.class_core->alter_set = NO_CMD;
}
return USBD_OK;
}
/*!
\brief handle CDC ACM data in
\param[in] udev: pointer to USB device instance
\param[in] ep_num: endpoint identifier
\param[out] none
\retval USB device operation status
*/
static uint8_t cdc_acm_in (usb_dev *udev, uint8_t ep_num)
{
usb_transc *transc = &udev->dev.transc_in[EP_ID(ep_num)];
usb_cdc_handler *cdc = (usb_cdc_handler *)udev->dev.class_data[CDC_COM_INTERFACE];
if ((0U == transc->xfer_len % transc->max_len) && (0U != transc->xfer_len)) {
usbd_ep_send (udev, ep_num, NULL, 0U);
} else {
cdc->packet_sent = 1U;
}
return USBD_OK;
}
/*!
\brief handle CDC ACM data out
\param[in] udev: pointer to USB device instance
\param[in] ep_num: endpoint identifier
\param[out] none
\retval USB device operation status
*/
static uint8_t cdc_acm_out (usb_dev *udev, uint8_t ep_num)
{
usb_cdc_handler *cdc = (usb_cdc_handler *)udev->dev.class_data[CDC_COM_INTERFACE];
cdc->packet_receive = 1U;
cdc->receive_length = ((usb_core_driver *)udev)->dev.transc_out[ep_num].xfer_count;
return USBD_OK;
}