Advertisement
Guest User

STM8L152C6 ADC minimal

a guest
Oct 12th, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.89 KB | None | 0 0
  1. // Source code under CC0 1.0
  2. #include <stdint.h>
  3. #include <stdio.h>
  4.  
  5. #define EVER (;;)
  6.  
  7. //ADC Registers
  8. #define ADC1_CR1        (*(volatile uint8_t *)0x5340)
  9. #define ADC1_CR2        (*(volatile uint8_t *)0x5341)
  10. #define ADC1_CR3        (*(volatile uint8_t *)0x5342)
  11.  
  12. //System Configuration
  13. #define SYSCFG_RMPCR1   (*(volatile uint8_t *)0x509E)
  14. #define USART1TR_REMAP1 (1 << 5)
  15. #define USART1TR_REMAP0 (1 << 4)
  16.  
  17. //Clock
  18. #define CLK_DIVR        (*(volatile uint8_t *)0x50C0)
  19. #define CLK_PCKENR1     (*(volatile uint8_t *)0x50C3)
  20. #define CLK_PCKENR2     (*(volatile uint8_t *)0x50C4)
  21.  
  22. //USART
  23. #define USART1_SR       (*(volatile uint8_t *)0x5230)
  24. #define USART1_DR       (*(volatile uint8_t *)0x5231)
  25. #define USART1_BRR1     (*(volatile uint8_t *)0x5232)
  26. #define USART1_BRR2     (*(volatile uint8_t *)0x5233)
  27. #define USART1_CR2      (*(volatile uint8_t *)0x5235)
  28. #define USART1_CR3      (*(volatile uint8_t *)0x5236)
  29.  
  30. #define USART_CR2_TEN   (1 << 3)
  31. #define USART_CR3_STOP2 (1 << 5)
  32. #define USART_CR3_STOP1 (1 << 4)
  33. #define USART_SR_TXE    (1 << 7)
  34.  
  35. // we need to provide putchar, as it is controller specific
  36. void putchar(char c)
  37. {
  38.     while(!(USART1_SR & USART_SR_TXE));
  39.  
  40.     USART1_DR = c;
  41. }
  42.  
  43. void uart_init(void)
  44. {
  45.     // Remap USART1 TX to PA2 and RX to PA4
  46.     SYSCFG_RMPCR1 |= USART1TR_REMAP0;
  47.     SYSCFG_RMPCR1 &= ~USART1TR_REMAP1;
  48.  
  49.     USART1_CR2 = USART_CR2_TEN; // Allow TX and RX
  50.     USART1_CR3 &= ~(USART_CR3_STOP1 | USART_CR3_STOP2); // 1 stop bit
  51.     USART1_BRR2 = 0x03; USART1_BRR1 = 0x68; // 9600 baud
  52. }
  53.  
  54. void main(void)
  55. {
  56.     unsigned long i = 0;
  57.  
  58.     CLK_DIVR = 0x00; // Set the frequency to 16 MHz
  59.     CLK_PCKENR1 |= 0xFF; //(1 << 5) & (1 << 7); // Enable peripherals UART & ADC
  60.  
  61.     uart_init();
  62.  
  63.     printf("Restarted...\r\n");
  64.  
  65.     for EVER
  66.     {
  67.         ADC1_CR1 = 0x01;
  68.         printf("ADC_CR1: %X\r\n", ADC1_CR1);
  69.         for(i = 0; i < 0xFFFFF; i++); // Sleep
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement