123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647 |
- #include "ad7606.h"
- #define OS0_1() GPIOH->BSRRL = GPIO_Pin_9
- #define OS0_0() GPIOH->BSRRH = GPIO_Pin_9
- #define OS1_1() GPIOH->BSRRL = GPIO_Pin_10
- #define OS1_0() GPIOH->BSRRH = GPIO_Pin_10
- #define OS2_1() GPIOH->BSRRL = GPIO_Pin_11
- #define OS2_0() GPIOH->BSRRH = GPIO_Pin_11
- #define CONVST_1() GPIOH->BSRRL = GPIO_Pin_12
- #define CONVST_0() GPIOH->BSRRH = GPIO_Pin_12
- #define RANGE_1() GPIOH->BSRRL = GPIO_Pin_14
- #define RANGE_0() GPIOH->BSRRH = GPIO_Pin_14
- #define RESET_1() GPIOI->BSRRL = GPIO_Pin_4
- #define RESET_0() GPIOI->BSRRH = GPIO_Pin_4
- #define AD7606_RESULT() *(__IO uint16_t *)0x6C400000
- AD7606_VAR_T g_tAD7606;
- AD7606_FIFO_T g_tAdcFifo;
- static void ad7606_fsmc(void);
- void ad7606_init(void)
- {
- ad7606_fsmc();
- AD7606_SetOS(AD_OS_NO);
- AD7606_SetInputRange(0);
- AD7606_Reset();
- CONVST_1();
- }
- static void ad7606_fsmc(void)
- {
- FSMC_NORSRAMInitTypeDef init;
- FSMC_NORSRAMTimingInitTypeDef timing;
-
-
- timing.FSMC_AddressSetupTime = 3;
- timing.FSMC_AddressHoldTime = 0;
- timing.FSMC_DataSetupTime = 6;
- timing.FSMC_BusTurnAroundDuration = 1;
- timing.FSMC_CLKDivision = 0;
- timing.FSMC_DataLatency = 0;
- timing.FSMC_AccessMode = FSMC_AccessMode_A;
-
- init.FSMC_Bank = FSMC_Bank1_NORSRAM4;
- init.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
- init.FSMC_MemoryType = FSMC_MemoryType_SRAM;
- init.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;
- init.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
- init.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable;
- init.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
- init.FSMC_WrapMode = FSMC_WrapMode_Disable;
- init.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
- init.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
- init.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
- init.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;
- init.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
- init.FSMC_ReadWriteTimingStruct = &timing;
- init.FSMC_WriteTimingStruct = &timing;
- FSMC_NORSRAMInit(&init);
-
- FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM4, ENABLE);
- }
- void AD7606_SetOS(uint8_t _ucOS)
- {
- g_tAD7606.ucOS = _ucOS;
- switch (_ucOS)
- {
- case AD_OS_X2:
- OS2_0();
- OS1_0();
- OS0_1();
- break;
- case AD_OS_X4:
- OS2_0();
- OS1_1();
- OS0_0();
- break;
- case AD_OS_X8:
- OS2_0();
- OS1_1();
- OS0_1();
- break;
- case AD_OS_X16:
- OS2_1();
- OS1_0();
- OS0_0();
- break;
- case AD_OS_X32:
- OS2_1();
- OS1_0();
- OS0_1();
- break;
- case AD_OS_X64:
- OS2_1();
- OS1_1();
- OS0_0();
- break;
- case AD_OS_NO:
- default:
- g_tAD7606.ucOS = AD_OS_NO;
- OS2_0();
- OS1_0();
- OS0_0();
- break;
- }
- }
- void AD7606_SetInputRange(uint8_t _ucRange)
- {
- if (_ucRange == 0)
- {
- g_tAD7606.ucRange = 0;
- RANGE_0();
- }
- else
- {
- g_tAD7606.ucRange = 1;
- RANGE_1();
- }
- }
- void AD7606_Reset(void)
- {
- RESET_0();
- RESET_1();
- RESET_1();
- RESET_1();
- RESET_1();
- RESET_0();
- }
- void AD7606_StartConvst(void)
- {
-
-
- CONVST_0();
- CONVST_0();
- CONVST_0();
- CONVST_1();
- }
- void AD7606_ReadNowAdc(void)
- {
- g_tAD7606.sNowAdc[0] = AD7606_RESULT();
- g_tAD7606.sNowAdc[1] = AD7606_RESULT();
- g_tAD7606.sNowAdc[2] = AD7606_RESULT();
- g_tAD7606.sNowAdc[3] = AD7606_RESULT();
- g_tAD7606.sNowAdc[4] = AD7606_RESULT();
- g_tAD7606.sNowAdc[5] = AD7606_RESULT();
- g_tAD7606.sNowAdc[6] = AD7606_RESULT();
- g_tAD7606.sNowAdc[7] = AD7606_RESULT();
- }
- void AD7606_EnterAutoMode(uint32_t _ulFreq)
- {
-
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
-
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOH, ENABLE);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_Init(GPIOH, &GPIO_InitStructure);
-
- GPIO_PinAFConfig(GPIOH, GPIO_PinSource12, GPIO_AF_TIM5);
- }
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
- uint32_t uiTIMxCLK;
- uint16_t usPrescaler;
- uint16_t usPeriod;
-
-
- uiTIMxCLK = SystemCoreClock / 2;
- if (_ulFreq < 3000)
- {
- usPrescaler = 100 - 1;
- usPeriod = (uiTIMxCLK / 100) / _ulFreq - 1;
- }
- else
- {
- usPrescaler = 0;
- usPeriod = uiTIMxCLK / _ulFreq - 1;
- }
-
- TIM_TimeBaseStructure.TIM_Period = usPeriod;
- TIM_TimeBaseStructure.TIM_Prescaler = usPrescaler;
- TIM_TimeBaseStructure.TIM_ClockDivision = 0;
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure);
-
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse = 4;
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
- TIM_OC3Init(TIM5, &TIM_OCInitStructure);
- TIM_OC3PreloadConfig(TIM5, TIM_OCPreload_Enable);
- TIM_ARRPreloadConfig(TIM5, ENABLE);
- TIM_Cmd(TIM5, ENABLE);
- }
-
- {
- EXTI_InitTypeDef EXTI_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOI, ENABLE);
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
-
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
- GPIO_Init(GPIOI, &GPIO_InitStructure);
-
- SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOI, EXTI_PinSource6);
-
- EXTI_InitStructure.EXTI_Line = EXTI_Line6;
- EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
- EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
-
- EXTI_InitStructure.EXTI_LineCmd = ENABLE;
- EXTI_Init(&EXTI_InitStructure);
-
- NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- }
- uint8_t AD7606_HasNewData(void)
- {
- if (g_tAdcFifo.usCount > 0)
- {
- return 1;
- }
- return 0;
- }
- uint8_t AD7606_FifoFull(void)
- {
- return g_tAdcFifo.ucFull;
- }
- uint8_t AD7606_ReadFifo(uint16_t *_usReadAdc)
- {
- if (AD7606_HasNewData())
- {
- *_usReadAdc = g_tAdcFifo.sBuf[g_tAdcFifo.usRead];
- if (++g_tAdcFifo.usRead >= ADC_FIFO_SIZE)
- {
- g_tAdcFifo.usRead = 0;
- }
- DISABLE_INT();
- if (g_tAdcFifo.usCount > 0)
- {
- g_tAdcFifo.usCount--;
- }
- ENABLE_INT();
- return 1;
- }
- return 0;
- }
- void AD7606_StartRecord(uint32_t _ulFreq)
- {
- AD7606_StopRecord();
- AD7606_Reset();
- AD7606_StartConvst();
- g_tAdcFifo.usRead = 0;
- g_tAdcFifo.usWrite = 0;
- g_tAdcFifo.usCount = 0;
- g_tAdcFifo.ucFull = 0;
- AD7606_EnterAutoMode(_ulFreq);
- }
- void AD7606_StopRecord(void)
- {
- TIM_Cmd(TIM5, DISABLE);
-
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOH, ENABLE);
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
- GPIO_Init(GPIOH, &GPIO_InitStructure);
- }
-
- {
- EXTI_InitTypeDef EXTI_InitStructure;
-
- EXTI_InitStructure.EXTI_Line = EXTI_Line6;
- EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
- EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
- EXTI_InitStructure.EXTI_LineCmd = DISABLE;
- EXTI_Init(&EXTI_InitStructure);
- }
- CONVST_1();
- }
- void AD7606_ISR(void)
- {
- uint8_t i;
- AD7606_ReadNowAdc();
- for (i = 0; i < 8; i++)
- {
- g_tAdcFifo.sBuf[g_tAdcFifo.usWrite] = g_tAD7606.sNowAdc[i];
- if (++g_tAdcFifo.usWrite >= ADC_FIFO_SIZE)
- {
- g_tAdcFifo.usWrite = 0;
- }
- if (g_tAdcFifo.usCount < ADC_FIFO_SIZE)
- {
- g_tAdcFifo.usCount++;
- }
- else
- {
- g_tAdcFifo.ucFull = 1;
- }
- }
- }
- #ifndef EXTI9_5_ISR_MOVE_OUT
- void EXTI9_5_IRQHandler(void)
- {
- if (EXTI_GetITStatus(EXTI_Line6) != RESET)
- {
- AD7606_ISR();
-
- EXTI_ClearITPendingBit(EXTI_Line6);
- }
- }
- #endif
|