Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Source code under CC0 1.0
- #include <stdint.h>
- #include <stdio.h>
- #define EVER (;;)
- //Definitions for IO Ports
- #define PA_ODR (*(volatile uint8_t *)0x5000)
- #define PA_IDR (*(volatile uint8_t *)0x5001)
- #define PA_DDR (*(volatile uint8_t *)0x5002)
- #define PA_CR1 (*(volatile uint8_t *)0x5003)
- #define PA_CR2 (*(volatile uint8_t *)0x5004)
- #define PB_ODR (*(volatile uint8_t *)0x5005)
- #define PB_IDR (*(volatile uint8_t *)0x5006)
- #define PB_DDR (*(volatile uint8_t *)0x5007)
- #define PB_CR1 (*(volatile uint8_t *)0x5008)
- #define PB_CR2 (*(volatile uint8_t *)0x5009)
- #define PC_ODR (*(volatile uint8_t *)0x500A)
- #define PC_IDR (*(volatile uint8_t *)0x500B)
- #define PC_DDR (*(volatile uint8_t *)0x500C)
- #define PC_CR1 (*(volatile uint8_t *)0x500D)
- #define PC_CR2 (*(volatile uint8_t *)0x500E)
- #define PD_ODR (*(volatile uint8_t *)0x500F)
- #define PD_IDR (*(volatile uint8_t *)0x5010)
- #define PD_DDR (*(volatile uint8_t *)0x5011)
- #define PD_CR1 (*(volatile uint8_t *)0x5012)
- #define PD_CR2 (*(volatile uint8_t *)0x5013)
- #define PE_ODR (*(volatile uint8_t *)0x5014)
- #define PE_IDR (*(volatile uint8_t *)0x5015)
- #define PE_DDR (*(volatile uint8_t *)0x5016)
- #define PE_CR1 (*(volatile uint8_t *)0x5017)
- #define PE_CR2 (*(volatile uint8_t *)0x5018)
- #define PF_ODR (*(volatile uint8_t *)0x5019)
- #define PF_IDR (*(volatile uint8_t *)0x501A)
- #define PF_DDR (*(volatile uint8_t *)0x501B)
- #define PF_CR1 (*(volatile uint8_t *)0x501C)
- #define PF_CR2 (*(volatile uint8_t *)0x501D)
- //ADC Registers
- #define ADC1_CR1 (*(volatile uint8_t *)0x5340)
- #define ADC1_CR2 (*(volatile uint8_t *)0x5341)
- #define ADC1_CR3 (*(volatile uint8_t *)0x5342)
- #define ADC1_SR (*(volatile uint8_t *)0x5343)
- #define ADC1_DRH (*(volatile uint8_t *)0x5344)
- #define ADC1_DRL (*(volatile uint8_t *)0x5345)
- #define ADC1_HTRH (*(volatile uint8_t *)0x5346)
- #define ADC1_HTRL (*(volatile uint8_t *)0x5347)
- #define ADC1_LTRH (*(volatile uint8_t *)0x5348)
- #define ADC1_LRTR (*(volatile uint8_t *)0x5349)
- #define ADC1_SQR1 (*(volatile uint8_t *)0x534A)
- #define ADC1_SQR2 (*(volatile uint8_t *)0x534B)
- #define ADC1_SQR3 (*(volatile uint8_t *)0x534C)
- #define ADC1_SQR4 (*(volatile uint8_t *)0x534D)
- #define ADC1_TRIGR1 (*(volatile uint8_t *)0x534E)
- #define ADC1_TRIGR2 (*(volatile uint8_t *)0x534F)
- #define ADC1_TRIGR3 (*(volatile uint8_t *)0x5350)
- #define ADC1_TRIGR4 (*(volatile uint8_t *)0x5351)
- //ADC_CR1 Bits
- #define OVERIE (1 << 7) // Overrun interrupt enable
- #define RES_1 (1 << 6) // Configurable resolution
- #define RES_0 (1 << 5) // Configurable resolution
- #define AWDIE (1 << 4) // Analog watchdog interrupt enable
- #define EOCIE (1 << 3) // Interrupt enable for EOC
- #define CONT (1 << 2) // Continuous Conversion
- #define START (1 << 1) // Conversion Start
- #define ADON (1 << 0) // A/D converter ON / OFF
- //ADC_CR3 Bits
- #define SMTP2_2 (1 << 7)
- #define SMTP2_1 (1 << 6)
- #define SMTP2_0 (1 << 5)
- #define CHSEL_4 (1 << 4)
- #define CHSEL_3 (1 << 3)
- #define CHSEL_2 (1 << 2)
- #define CHSEL_1 (1 << 1)
- #define CHSEL_0 (1 << 0)
- //System Configuration
- #define SYSCFG_RMPCR1 (*(volatile uint8_t *)0x509E)
- #define USART1TR_REMAP1 (1 << 5)
- #define USART1TR_REMAP0 (1 << 4)
- //Clock
- #define CLK_DIVR (*(volatile uint8_t *)0x50C0)
- #define CLK_PCKENR1 (*(volatile uint8_t *)0x50C3)
- #define CLK_PCKENR2 (*(volatile uint8_t *)0x50C4)
- //USART
- #define USART1_SR (*(volatile uint8_t *)0x5230)
- #define USART1_DR (*(volatile uint8_t *)0x5231)
- #define USART1_BRR1 (*(volatile uint8_t *)0x5232)
- #define USART1_BRR2 (*(volatile uint8_t *)0x5233)
- #define USART1_CR2 (*(volatile uint8_t *)0x5235)
- #define USART1_CR3 (*(volatile uint8_t *)0x5236)
- #define USART_CR2_TEN (1 << 3)
- #define USART_CR3_STOP2 (1 << 5)
- #define USART_CR3_STOP1 (1 << 4)
- #define USART_SR_TXE (1 << 7)
- //User Specific Definitions
- #define LED3 (1 << 7)
- #define LED4 (1 << 7)
- // we need to provide putchar, as it is controller specific
- void putchar(char c)
- {
- while(!(USART1_SR & USART_SR_TXE));
- USART1_DR = c;
- }
- void uart_init(void)
- {
- // Remap USART1 TX to PA2 and RX to PA4
- SYSCFG_RMPCR1 |= USART1TR_REMAP0;
- SYSCFG_RMPCR1 &= ~USART1TR_REMAP1;
- USART1_CR2 = USART_CR2_TEN; // Allow TX and RX
- USART1_CR3 &= ~(USART_CR3_STOP1 | USART_CR3_STOP2); // 1 stop bit
- USART1_BRR2 = 0x03; USART1_BRR1 = 0x68; // 9600 baud
- }
- void LED_init(void)
- {
- PE_DDR |= LED3;
- PE_CR1 |= LED3;
- PC_DDR |= LED4;
- PC_CR1 |= LED4;
- }
- void ADC1_channel_select(uint8_t n)
- {
- ADC1_CR3 &= ~(0x1F); //reset selection
- ADC1_CR3 |= (n & 0x1F); //set selection
- }
- void main(void)
- {
- unsigned long i = 0;
- CLK_DIVR = 0x00; // Set the frequency to 16 MHz
- CLK_PCKENR1 |= 0xFF; //(1 << 5) & (1 << 7); // Enable peripherals UART & ADC
- CLK_PCKENR2 |= (1 << 4); // enable DMA1
- uart_init();
- LED_init();
- // switch on ADC1
- ADC1_CR1 |= START;
- ADC1_SQR2 = (1 << 2); // select channel 18 (PB0)
- ADC1_SQR3 = 0x00;
- ADC1_SQR4 = 0x00;
- // set ADC1 resolution to 8 bit
- ADC1_CR1 &= ~(1 << 5);
- ADC1_CR1 |= (1 << 6);
- // start measurement for ADC1
- ADC1_CR1 |= (1 << 2); //1 start, 2 cont
- printf("Restarted...\r\n");
- for EVER
- {
- PE_ODR |= LED3;
- printf("PC_IDR: %X\t CR1-3: %X %X %X %X Data: %X\r\n",PC_IDR, ADC1_CR1, ADC1_CR2, ADC1_CR3, ADC1_SR, ADC1_DRL);
- printf("ADC SQR1-4: %X %X %X %X\r\n", ADC1_SQR1, ADC1_SQR2, ADC1_SQR3, ADC1_SQR4);
- printf("CLK_PCKENR: %X %X\r\n", CLK_PCKENR1, CLK_PCKENR2);
- PE_ODR &= ~LED3;
- for(i = 0; i < 0xFFFFF; i++); // Sleep
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement