|
发表于 2013-9-24 09:00:04
|
显示全部楼层
本帖最后由 histamine 于 2013-9-24 09:36 编辑
- #include <stdint.h>
- #include <stm32f10x.h>
- #include <stm32f10x_rcc.h>
- #include <stm32f10x_gpio.h>
- #include <stm32f10x_adc.h>
- #define HIGH 0x01
- #define LOW 0x00
- #define OUT_PP 0b00000011
- #define OUT_OD 0b00000111
- #define OUT OUT_PP
- #define IN_FT 0b00000100
- #define IN_PD 0b00001000
- #define IN_PU 0b00011000
- #define IN IN_FT
- #define __enable_port_rcc(P) do{RCC->APB2ENR |= RCC_APB2Periph_GPIO##P;}while(0)
- #define __disable_port_rcc(P) do{RCC->APB2ENR &= ~RCC_APB2Periph_GPIO##P;}while(0)
- #define pin_set(P, N) do{GPIO##P->BSRR = GPIO_Pin_##N;}while(0)
- #define pin_reset(P, N) do{GPIO##P->BRR = GPIO_Pin_##N;}while(0)
- #define pin_read(P, N) (GPIO##P->IDR & GPIO_Pin_##N)
- #define port_set(P, V) do{GPIO##P->ODR = V;}while(0)
- #define port_read(P) (GPIO##P->IDR)
- #define pin_mode(P, N, M) do{__enable_port_rcc(P); __pin_mode(GPIO##P, N, GPIO_Pin_##N, M);}while(0)
- __inline void __pin_mode(GPIO_TypeDef *reg_base, const uint16_t pin_num, const uint16_t gpio_pin, const uint8_t mode)
- {
- __IO uint32_t *reg_addr = 0;
- uint16_t pos = 0;
- if (pin_num < 8)
- {
- reg_addr = ®_base->CRL;
- pos = pin_num;
- }
- else
- {
- reg_addr = ®_base->CRH;
- pos = pin_num - 8;
- }
- const uint16_t lshift = pos << 2;
- *reg_addr &= ~(((uint32_t)0x0F) << lshift);
- *reg_addr |= ((uint32_t)(mode & 0x0F)) << lshift;
- if (mode == IN_PD)
- reg_base->BRR = gpio_pin;
- else if (mode == IN_PU)
- reg_base->BSRR = gpio_pin;
- }
- #define ADC_PA0 0
- #define ADC_PA1 1
- #define ADC_PA2 2
- #define ADC_PA3 3
- #define ADC_PA4 4
- #define ADC_PA5 5
- #define ADC_PA6 6
- #define ADC_PA7 7
- #define ADC_PB0 8
- #define ADC_PB1 9
- #define ADC_PC0 10
- #define ADC_PC1 11
- #define ADC_PC2 12
- #define ADC_PC3 13
- #define ADC_PC4 14
- #define ADC_PC5 15
- #define pin_analog_read(P, N) __pin_analog_read(ADC_P##P##N)
- __inline uint16_t __pin_analog_read(uint8_t adc_channel)
- {
- ADC1->SQR3 &= 0XFFFFFFE0;
- ADC1->SQR3 |= adc_channel;
- ADC1->CR2 |= 1 << 22;
- while(!(ADC1->SR & (1 << 1)));
- return ADC1->DR;
- }
- #define pin_mode_ain(P, N) do{__enable_port_rcc(P); __pin_mode_ain(ADC_P##P##N);}while(0)
- __inline void __pin_mode_ain(const uint16_t adc_channel)
- {
- RCC->APB2ENR |= 1 << 9;
- RCC->APB2RSTR |= 1 << 9;
- RCC->APB2RSTR &= ~(1 << 9);
- RCC->CFGR &= ~(3 << 14);
- RCC->CFGR |= 2 << 14;
- ADC1->CR1 &= 0XF0FFFF;
- ADC1->CR1 |= 0 << 16;
- ADC1->CR1 &= ~(1 << 8);
- ADC1->CR2 &= ~(1 << 1);
- ADC1->CR2 &= ~(7 << 17);
- ADC1->CR2 |= 7 << 17;
- ADC1->CR2 |= 1 << 20;
- ADC1->CR2 &= ~(1 << 11);
- ADC1->SQR1 &= ~(0XF << 20);
- ADC1->SQR1 &= 0 << 20;
- if (adc_channel > 9)
- {
- const uint8_t adc_lshift = (adc_channel - 10) * 3;
- ADC1->SMPR1 &= ~(((uint32_t)0x7) << adc_lshift);
- ADC1->SMPR1 |= ((uint32_t)0x7) << adc_lshift;
- }
- else
- {
- const uint8_t adc_lshift = adc_channel * 3;
- ADC1->SMPR2 &= ~(((uint32_t)0x7) << adc_lshift);
- ADC1->SMPR2 |= ((uint32_t)0x7) << adc_lshift;
- }
- ADC1->CR2 |= 1 << 0;
- ADC1->CR2 |= 1 << 3;
- while(ADC1->CR2 & 1 << 3);
- ADC1->CR2 |= 1 << 2;
- while(ADC1->CR2 & 1 << 2);
- }
复制代码 未测试- pin_mode_ain(A, 0);
- uint16_t adc_value = pin_analog_read(A, 0);
- pin_mode(B, 1, OUT);
- pin_set(B, 1);
- pin_reset(B, 1);
复制代码 |
|