A题-报告8-程序清单 | ||||
| ||||
A题-报告8-程序清单 程序清单 main.c #include "CONTROL.h" int main() { SystemCoreClockUpdate(); system_init(); system_control(); return 0; } KEY.c /* * KEY.c * * Created: 2014/11/23 2:16:59 * Author: zzk */
#include "KEY.h" #include "GLOBAL_VARIABLE.h"
volatile u8 key_value=0; //按键数据 static u8 key_data_backup=0; //按键采集数据备份 static u8 press_key_flag=0; //按键按压标志 static u8 release_key_flag=1; //按键释放标志
void key_init(void) { GPIO_InitTypeDef GPIO_InitStructure;//
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
//KEY PB13 PB14 PB15 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12; GPIO_Init(GPIOD, &GPIO_InitStructure); }
u8 get_key_data(void) { u8 key_data1=0,key_data2=0; key_data1=(GPIOB->IDR&0XE000)>>8; //PB13 14 15 KEY1 2 3 key_data2=(GPIOD->IDR&0X1F00)>>8; //PD8 9 10 11 12 KEYA B C D PRESS return (key_data1+key_data2); }
u8 key_test(void) { u8 value=get_key_data(); if(value!=0xFF) return 1; return 0; }
void get_key_value(void) { if(release_key_flag==1) //所有按键松开后release_key_flag值为1,有按键按下时其值为0 { if(press_key_flag==0) { if(key_test()) { key_data_backup=get_key_data(); //捕获当前按键数据 press_key_flag=1; //第一次扫描按键,有按键按下 } } else if(key_test()) //在第一次通过的情况下第二次如果按键扫描依旧通过 { if(key_data_backup==get_key_data()) //两次捕获的数据一致 { switch(key_data_backup) { case KEY1:key_value=key_data_backup;break; case KEY2:key_value=key_data_backup;break; case KEY3:key_value=key_data_backup;break; case KEY4:key_value=key_data_backup;break; case KEY5:key_value=key_data_backup;break; case KEY6:key_value=key_data_backup;break; case KEY7:key_value=key_data_backup;break; case KEY8:key_value=key_data_backup;break; default:break; } release_key_flag=0; } else { press_key_flag=0; } } } else { if(key_test()==0) { release_key_flag=1; press_key_flag=0; } } } PRINTF,c #include "PRINTF.h"
////////////////////////////////////////////////////////////////// //加入以下代码,支持printf函数,而不需要选择use MicroLIB #if 1 #pragma import(__use_no_semihosting) //标准库需要的支持函数 struct __FILE { int handle; /* Whatever you require here. If the only file you are using is */ /* standard output using printf() for debugging, no file handling */ /* is required. */ }; /* FILE is typedef’ d in stdio.h. */ FILE __stdout; //定义_sys_exit()以避免使用半主机模式 _sys_exit(int x) { x = x; } //重定义fputc函数 int fputc(int ch, FILE *f) { while((USART1->SR&0X40)==0);//循环发送,直到发送完毕 USART1->DR = (u8) ch; return ch; } #endif //end //////////////////////////////////////////////////////////////////
void printf_init(void) //输出初始化 { stdout_init(); uasrt_DMA_init(); }
void stdout_init(void) //USART1 init TX:PA9 RX:PA10 { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; /* Enable GPIO clock TX:PA9 RX:PA10*/ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); /* Enable UART clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
/* Connect PXx to USARTx_Tx*/ GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
/* Connect PXx to USARTx_Rx*/ GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
/* Configure USART Tx as alternate function */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz; // GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART Rx as alternate function */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // GPIO_Init(GPIOA, &GPIO_InitStructure);
/* USART configuration */ /* USARTx configured as follows: - BaudRate = 115200 baud - Word Length = 8 Bits - One Stop Bit - No parity - Hardware flow control disabled (RTS and CTS signals) - Receive and transmit enabled */ USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART1, &USART_InitStructure); /* Enable USART interrupts*/ USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); /* Enable USART */ USART_Cmd(USART1, ENABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); NVIC_InitStructure.NVIC_IRQChannel =USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =0; NVIC_InitStructure.NVIC_IRQChannelSubPriority =4; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }
void uasrt_DMA_init(void) //usart DMA channel init { NVIC_InitTypeDef NVIC_InitStructure; DMA_InitTypeDef DMA_InitStructure;
/* Enable DMA clock */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
/* Reset DMA Stream registers (for debug purpose) */ DMA_DeInit(DMA2_Stream7);
/* Configure DMA controller to manage USART TX and RX DMA request ----------*/
/* Configure DMA Initialization Structure */ DMA_InitStructure.DMA_BufferSize = 0 ; //数据量待定 DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable ; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull ; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single ; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; DMA_InitStructure.DMA_PeripheralBaseAddr =(uint32_t)(&(USART1->DR)) ; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_Priority = DMA_Priority_High; /* Configure TX DMA */ DMA_InitStructure.DMA_Channel = DMA_Channel_4 ; DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral ; DMA_InitStructure.DMA_Memory0BaseAddr =(uint32_t)0 ; //地址量待定 DMA_Init(DMA2_Stream7,&DMA_InitStructure); DMA_ITConfig(DMA2_Stream7,DMA_IT_TC,ENABLE); /* Configure RX DMA */ DMA_InitStructure.DMA_Channel = DMA_Channel_4 ; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory ; DMA_InitStructure.DMA_Memory0BaseAddr =(uint32_t)0 ; //地址量待定 DMA_Init(DMA2_Stream2,&DMA_InitStructure); DMA_ITConfig(DMA2_Stream2,DMA_IT_TC,ENABLE);
/* Enable the DMA Stream IRQ Channel */ NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream7_IRQn; //TXD NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 5; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream2_IRQn; //RXD NVIC_Init(&NVIC_InitStructure); }
void usart_send_block_DMA(u32 addr,u16 block_size) //send data using DMA { while((USART1->SR&0X80)==0); DMA2_Stream7->M0AR=addr; DMA2_Stream7->NDTR=block_size; DMA2_Stream7->CR|=0X01; //enable the tx stream /* Enable USART DMA TX Requsts */ USART1->CR3|=(1<<7); }
volatile u8 command=0; void USART1_IRQHandler(void) { command=USART1->DR; if(command==0xFE) NVIC_SystemReset(); //reboot }
void DMA2_Stream7_IRQHandler(void) { DMA2->HIFCR|=(1<<27); //clear the TCIF7 flag }
void DMA2_Stream2_IRQHandler(void) { DMA2->LIFCR|=(1<<21); //clear the TCIF2 flag } TIMER.c #include "TIMER.h" #include "PRINTF.h" #include "KEY.h" #include "GLOBAL_VARIABLE.h"
void systick_init(void) //时基初始化 { uint32_t cnts; RCC_ClocksTypeDef rcc_clocks;
RCC_GetClocksFreq(&rcc_clocks); cnts = rcc_clocks.HCLK_Frequency /50; //20ms
SysTick_Config(cnts); }
void timer2_init(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; NVIC_InitTypeDef NVIC_InitStructure; /* TIM1 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_DeInit(TIM2); //初始化定时器2寄存器 TIM_Cmd(TIM2, DISABLE); //Close the timer2 TIM_InternalClockConfig(TIM2); //选择内部时钟源 TIM_TimeBaseStructure.TIM_Period=19999; // 定义ARR数值 20ms TIM_TimeBaseStructure.TIM_Prescaler=83;//84分频 TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; //采样分频,用来确定多少个触发信号才算一个捕捉 TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式 TIM_TimeBaseStructure.TIM_RepetitionCounter=0; //重复计数器,仅定时器1、8、15、16、17有此功能 TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); TIM_ARRPreloadConfig(TIM2, DISABLE);//禁止ARR预装载缓冲器,这样写入预装载寄存器数值会立即更新到对应的影子寄存器,若将其使能可以用软件产生一个更新事件 TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);//使能溢出中断
NVIC_InitStructure.NVIC_IRQChannel =TIM2_IRQn;//选择中断通道 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;//拥有最低抢断优先级,其实此处为0,因为其范围为0 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;//设置响应优先级,范围为0~15 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能外部中断一通道 NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM2, ENABLE); //ENABLE the timer1 }
/** * @brief This function handles SysTick Handler. * @param None * @retval None */ void SysTick_Handler(void) { get_key_value(); }
static u8 time_count=0; void TIM2_IRQHandler(void) { TIM2->SR&=0xfffe; //clear the interrupt flag time_count++; if(time_count>=12) { time_count=0; System_Single.show_single=TRUE; } get_key_value(); } CONTROL.c #include "CONTROL.h" #include "GLOBAL_VARIABLE.h"
volatile Task_TypeDef Current_Task; //当前任务 bool task_process_permit_flag=FALSE; //任务处理允许标志 volatile Single System_Single; //信号集 //------------------------------------------------ volatile Global_Var global_var; //全局变量
//-------------------------------------------------------------- /* *私有变量 */ typedef struct { u8 run_mode; //运行模式¬0 1 2 u16 set_current_value; //设定电流 u16 target_current_value; //目标电流 u16 now_current_value; //当前电流大小 u16 u1_value; //U1电压 u16 u2_value; //U2电压 u8 adjust_rank; //调整位数(千位,百位,十位,个位) u16 number[4]; u8 *designer; u8 *versinon_nu; u8 *ds_data; }Private_Var; static Private_Var private_var= { 0, //运行模式-0 1 2 0, //设定电流 0, //目标电流 0, //当前电流大小 0, //U1电压 0, //U2电压 3, //调整位数(千位,百位,十位,个位) {1000,100,10,1}, "zhou hao ran", "V2.0", "2015-08-14", };
typedef struct { u8 x; //坐标x u8 y; //坐标y char data[5]; //要显示的数据 u8 length; //显示长度 u8 show_flag; //是否需要显示 }Show_var; static Show_var lcd_show_item[6];
void system_init(void) { delay_init(); printf_init(); printf("system clock value is %lu\n",SystemCoreClock); key_init(); LCD_init(); delay_ms(100); LCD_Show_Frame(); show_init(); adc1_init(); dac1_init(); timer2_init(); printf("system init complete !\n"); Current_Task=TASK_IDLE; System_Single.show_single=TRUE; }
void system_control(void) { while(1) { kernel_schedule(); switch(Current_Task) { case TASK_SHOW:show_thread();break; case TASK_IDLE:idle_thread();break; case TASK_CONTROL:dc_control_thread();break; case TASK_STACK_OF:stack_overflow();break; default:break; } } }
void kernel_schedule(void) //核心调度 { if(System_Single.show_single==TRUE) { Push(Current_Task); Current_Task=TASK_SHOW; System_Single.show_single=FALSE; }
if(System_Single.control_single==TRUE) { Push(Current_Task); Current_Task=TASK_CONTROL; System_Single.control_single=FALSE; } task_process_permit_flag=TRUE; //允许任务调度 }
void dc_control_thread(void) //DC控制核心线程 { u16 *adc_data; u32 data_temp=0; if(task_process_permit_flag!=TRUE) return; adc_data=(u16*)global_var.adc_value_buffer[global_var.valid_buffer_number]; data_temp=adc_data[0]+adc_data[6]+adc_data[12]+adc_data[18]+adc_data[24]+adc_data[30]+adc_data[36]+adc_data[42]+adc_data[48]+adc_data[54]; private_var.now_current_value=(u16)(data_temp/10);
data_temp=adc_data[2]+adc_data[8]+adc_data[14]+adc_data[20]+adc_data[26]+adc_data[32]+adc_data[38]+adc_data[44]+adc_data[50]+adc_data[56]; private_var.u1_value=(u16)(data_temp/10);
data_temp=adc_data[4]+adc_data[10]+adc_data[16]+adc_data[22]+adc_data[28]+adc_data[34]+adc_data[40]+adc_data[46]+adc_data[52]+adc_data[58]; private_var.u2_value=(u16)(data_temp/10);
//dac1_set_value(private_var.u1_value); switch(private_var.run_mode) { case 0: {
}break; case 1: {
}break; case 2: {
}break; } Current_Task=(Task_TypeDef)Pop(); }
void idle_thread(void) //空闲线程,默认线程,处理按键事件,并向其他线程跳转 { u8 i=0; float current_temp; if(task_process_permit_flag!=TRUE) return; switch(key_value) { case KEY_SET1 : //切换模式 { key_value=0; private_var.run_mode++; private_var.run_mode%=3; lcd_show_item[0].data[0]=private_var.run_mode+48; lcd_show_item[0].show_flag=1; System_Single.show_single=TRUE; }break; case KEY_UP : //电流加 { key_value=0; if(private_var.set_current_value<private_var.set_current_value+private_var.number[private_var.adjust_rank]) { private_var.set_current_value+=private_var.number[private_var.adjust_rank]; //current_temp=(private_var.set_current_value*185/1000+2560)*4096/3300; //private_var.target_current_value=(u16)current_temp; //获取目标电流(电压) current_temp=(float)(private_var.set_current_value*4096/2200); dac1_set_value((u16)current_temp); } sprintf(lcd_show_item[1].data,"%04d",private_var.set_current_value); lcd_show_item[1].show_flag=1; System_Single.show_single=TRUE; }break; case KEY_DOWN : //电流减 { key_value=0; if(private_var.set_current_value-private_var.number[private_var.adjust_rank]>=0) { private_var.set_current_value-=private_var.number[private_var.adjust_rank]; //current_temp=(private_var.set_current_value*185/1000+2560)*4096/3300; //private_var.target_current_value=(u16)current_temp; //获取目标电流(电压) current_temp=(float)(private_var.set_current_value*4096/2200); dac1_set_value((u16)current_temp); } sprintf(lcd_show_item[1].data,"%04d",private_var.set_current_value); lcd_show_item[1].show_flag=1; System_Single.show_single=TRUE; }break; case KEY_LEFT : //光标左移 { if(private_var.adjust_rank>0) private_var.adjust_rank--; else private_var.adjust_rank=3; for(i=0;i<4;i++) { if(i!=private_var.adjust_rank) lcd_show_item[2].data[i]=' '; else lcd_show_item[2].data[i]='*'; } lcd_show_item[2].show_flag=1; System_Single.show_single=TRUE; key_value=0; }break; case KEY_RIGHT : //光标右移 { private_var.adjust_rank++; private_var.adjust_rank%=4; for(i=0;i<4;i++) { if(i!=private_var.adjust_rank) lcd_show_item[2].data[i]=' '; else lcd_show_item[2].data[i]='*'; } lcd_show_item[2].show_flag=1; System_Single.show_single=TRUE; key_value=0; }break; } }
void show_thread(void) //显示线程 { u8 i=0; float adc_value=0; adc_value=(float)(private_var.now_current_value*3300/4096-2560)*1000/185; //当前电流显示转换 sprintf(lcd_show_item[3].data,"%04d",(u16)adc_value); lcd_show_item[3].show_flag=1; adc_value=(float)(private_var.u1_value*3.3/4096); //电压U1转换 sprintf(lcd_show_item[4].data,"%4.1f",adc_value); lcd_show_item[4].show_flag=1; adc_value=(float)(private_var.u2_value*3.3/4096); //电压U2转换 sprintf(lcd_show_item[5].data,"%4.1f",adc_value); lcd_show_item[5].show_flag=1;
if(task_process_permit_flag!=TRUE) return; for(i=0;i<6;i++) { if(lcd_show_item[i].show_flag) { lcd_show_item[i].show_flag=0; LCD_show_string(lcd_show_item[i].x*6,lcd_show_item[i].y,lcd_show_item[i].data,lcd_show_item[i].length); } } Current_Task=(Task_TypeDef)Pop(); }
void show_init(void) //显示初始化 { float temp=0;
lcd_show_item[0].x=9; lcd_show_item[0].y=0; lcd_show_item[0].data[0]=private_var.run_mode+48; lcd_show_item[0].length=1; lcd_show_item[0].show_flag=1;
lcd_show_item[1].x=6; lcd_show_item[1].y=1; sprintf(lcd_show_item[1].data,"%04d",private_var.set_current_value); lcd_show_item[1].length=4; lcd_show_item[1].show_flag=1;
lcd_show_item[2].x=6; lcd_show_item[2].y=2; lcd_show_item[2].data[0]=' '; lcd_show_item[2].data[1]=' '; lcd_show_item[2].data[2]=' '; lcd_show_item[2].data[3]='*'; lcd_show_item[2].length=4; lcd_show_item[2].show_flag=1;
lcd_show_item[3].x=6; lcd_show_item[3].y=3; sprintf(lcd_show_item[3].data,"%04d",private_var.now_current_value); lcd_show_item[3].length=4; lcd_show_item[3].show_flag=1;
lcd_show_item[4].x=4; lcd_show_item[4].y=4; temp=(float)(private_var.u1_value*3.3/4096); sprintf(lcd_show_item[4].data,"%4.1f",temp); lcd_show_item[4].length=4; lcd_show_item[4].show_flag=1;
lcd_show_item[5].x=4; lcd_show_item[5].y=5; temp=(float)(private_var.u2_value*3.3/4096); sprintf(lcd_show_item[5].data,"%4.1f",temp); lcd_show_item[5].length=4; lcd_show_item[5].show_flag=1; }
void stack_overflow(void) //堆栈溢出处理 { printf("Stack overflow !\n"); NVIC_SystemReset(); //reboot } DELAY.c #include"DELAY.h"
static u8 fac_us=0;//us 延时倍乘数 static u16 fac_ms=0;//ms 延时倍乘数 //delay init void delay_init(void) { SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);//选择内部时钟 HCLK/8 fac_us=SystemCoreClock/1000000/8; fac_ms=(u16)fac_us*1000; } //延时Nms //注意Nms的范围 //Nms<=0xffffff*8/SYSCLK(MHZ)/1000 //对168M条件下,Nms<=798 void delay_ms(u16 nms) { u32 temp; SysTick->LOAD=(u32)nms*fac_ms;//时间加载 SysTick->VAL =0x00; //清空计数器 SysTick->CTRL=0x01 ; //开始倒数 do { temp=SysTick->CTRL; } while(temp&0x01&&!(temp&(1<<16)));//等待时间到达 SysTick->CTRL=0x00; //关闭计数器 SysTick->VAL =0X00; //清空计数器 } //延时us void delay_us(u32 Nus) { u32 temp; SysTick->LOAD=Nus*fac_us; //时间加载 SysTick->VAL=0x00; //清空计数器 SysTick->CTRL=0x01 ; //开始倒数 do { temp=SysTick->CTRL; } while(temp&0x01&&!(temp&(1<<16)));//等待时间到达 SysTick->CTRL=0x00; //关闭计数器 SysTick->VAL =0X00; //清空计数器 } ADC_GET.c #include "ADC_GET.h" #include "GLOBAL_VARIABLE.h" #define ADC_CDR_ADDRESS ((uint32_t)0x40012308)
void Trig_timer_init(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure;
/* TIM1 clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
TIM_DeInit(TIM1); //初始化定时器1寄存器 TIM_Cmd(TIM1, DISABLE); //Close the timer1 TIM_InternalClockConfig(TIM1); //选择内部时钟源 TIM_TimeBaseStructure.TIM_Period=1680-1; // 定义ARR数值100K TIM_TimeBaseStructure.TIM_Prescaler=0;//1分频 TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; //采样分频,用来确定多少个触发信号才算一个捕捉 TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式 TIM_TimeBaseStructure.TIM_RepetitionCounter=0; //重复计数器,仅定时器1、8、15、16、17有此功能 TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); TIM_ARRPreloadConfig(TIM1, DISABLE);//禁止ARR预装载缓冲器,这样写入预装载寄存器数值会立即更新到对应的影子寄存器,若将其使能可以用软件产生一个更新事件
/* PWM1 Mode configuration: Channel1 */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable; TIM_OCInitStructure.TIM_Pulse = 839; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset; TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset; TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_Cmd(TIM1, ENABLE); //OPEN the timer1 TIM_CtrlPWMOutputs(TIM1, ENABLE); }
void adc1_init(void) //adc1 init channel 0 1 2 { GPIO_InitTypeDef GPIO_InitStructure; DMA_InitTypeDef DMA_InitStructure; ADC_InitTypeDef ADC_InitStructure; ADC_CommonInitTypeDef ADC_CommonInitStructure; NVIC_InitTypeDef NVIC_InitStructure;
/* Enable peripheral clocks *************************************************/ RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA , ENABLE); //PA0 1 2 RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_DMA2 , ENABLE); RCC_APB2PeriphClockCmd( RCC_APB2Periph_ADC1 , ENABLE); RCC_APB2PeriphClockCmd( RCC_APB2Periph_ADC2 , ENABLE); RCC_APB2PeriphClockCmd( RCC_APB2Periph_ADC3 , ENABLE);
/* Configure ADC Channel 12 pin as analog input *****************************/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 |GPIO_Pin_1 |GPIO_Pin_2 ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOA, &GPIO_InitStructure);
/* DMA2 Stream0 channel0 configuration **************************************/ DMA_InitStructure.DMA_Channel = DMA_Channel_0; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC_CDR_ADDRESS; DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&global_var.adc_value_buffer[0][0]; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; DMA_InitStructure.DMA_BufferSize = 30; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA2_Stream0, &DMA_InitStructure);
DMA_ITConfig(DMA2_Stream0,DMA_IT_TC,ENABLE);
DMA_DoubleBufferModeConfig(DMA2_Stream0,(uint32_t)&global_var.adc_value_buffer[1][0],(uint32_t)&global_var.adc_value_buffer[0][0]); DMA_DoubleBufferModeCmd(DMA2_Stream0,ENABLE);
/* Enable the DMA Stream IRQ Channel */ NVIC_InitStructure.NVIC_IRQChannel=DMA2_Stream0_IRQn; //DMA2_Stream0 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);
/* DMA2_Stream0 enable */ DMA_Cmd(DMA2_Stream0, ENABLE);
/* ADC Common configuration *************************************************/ ADC_CommonInitStructure.ADC_Mode = ADC_TripleMode_RegSimult; ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles; ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_1; ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4; //84/4=21 ADC_CommonInit(&ADC_CommonInitStructure);
/* ADC1 regular channel 0 configuration ************************************/ ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //single ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfConversion = 1; ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_3Cycles); /* Enable ADC1 DMA */ ADC_DMACmd(ADC1, ENABLE);
/* ADC2 regular channel 1 configuration ************************************/ ADC_Init(ADC2, &ADC_InitStructure); /* ADC2 regular channel1 configuration */ ADC_RegularChannelConfig(ADC2, ADC_Channel_1, 1, ADC_SampleTime_3Cycles);
/* ADC3 regular channel 2 configuration ************************************/ ADC_Init(ADC3, &ADC_InitStructure); /* ADC3 regular channel2 configuration */ ADC_RegularChannelConfig(ADC3, ADC_Channel_2, 1, ADC_SampleTime_3Cycles);
/* Enable DMA request after last transfer (multi-ADC mode) ******************/ ADC_MultiModeDMARequestAfterLastTransferCmd(ENABLE);
/* Enable ADC1 **************************************************************/ ADC_Cmd(ADC1, ENABLE);
/* Enable ADC2 **************************************************************/ ADC_Cmd(ADC2, ENABLE);
/* Enable ADC3 **************************************************************/ ADC_Cmd(ADC3, ENABLE);
Trig_timer_init(); }
void DMA2_Stream0_IRQHandler(void) { DMA2->LIFCR|=(1<<5); //clear the TCIF0 flag global_var.valid_buffer_number=global_var.adc_buffer_nu; global_var.adc_buffer_nu++; global_var.adc_buffer_nu%=2; //if(System_Single.control_single==TRUE) USART1->DR='D'; System_Single.control_single=TRUE; //系统控制 } DAC_SET.c #include "DAC_SET.h"
void dac1_init(void) { DAC_InitTypeDef DAC_InitStructure; /* Preconfiguration before using DAC----------------------------------------*/ GPIO_InitTypeDef GPIO_InitStructure;
/* GPIOA clock enable (to be used with DAC) */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); /* DAC Periph clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
/* DAC channel 1 & 2 (DAC_OUT1 = PA.4)(DAC_OUT2 = PA.5) configuration */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStructure);
DAC_DeInit();
/* DAC channel1 Configuration */ DAC_InitStructure.DAC_Trigger = DAC_Trigger_Software; DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None; DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable; DAC_Init(DAC_Channel_1, &DAC_InitStructure);
/* Enable DAC Channel1 */ DAC_Cmd(DAC_Channel_1, ENABLE);
DAC_SoftwareTriggerCmd(DAC_Channel_1,ENABLE); }
void dac1_set_value(u16 dac_value) { DAC->DHR12R1=dac_value; DAC->SWTRIGR|=0X01; }
|