SHARE
TWEET

Untitled

a guest Sep 20th, 2019 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. void startup(void) __attribute__((naked)) __attribute__((section (".start_section")) );
  3.  
  4. void startup ( void )
  5. {
  6. __asm volatile(
  7.     " LDR R0,=0x2001C000\n"     /* set stack */
  8.     " MOV SP,R0\n"
  9.     " BL main\n"                /* call main */
  10.     "_exit: B .\n"              /* never return */
  11.     ) ;
  12. }
  13.  
  14. #define GPIOD_BASE 0x40020C00
  15. #define GPIOE_BASE 0x40021000
  16.  
  17. #define SYSTICK_BASE 0xE000E010
  18. #define STK_CTRL ((volatile unsigned long *) (SYSTICK_BASE))
  19. #define STK_LOAD ((volatile unsigned long *) (SYSTICK_BASE + 0x04))
  20. #define STK_VAL ((volatile unsigned long *) (SYSTICK_BASE + 0x08))
  21.  
  22. #define GPIOD_MODER     * ((unsigned long *)    (GPIOD_BASE))
  23. #define GPIOD_OTYPER    * ((unsigned short *)   (GPIOD_BASE + 0x04))
  24. #define GPIOD_PUPDR     * ((unsigned long *)    (GPIOD_BASE + 0x0C))
  25. #define GPIOD_IDR       * ((unsigned short *)   (GPIOD_BASE + 0x10))
  26. #define GPIOD_IDR_HIGH  * ((unsigned char *)    (GPIOD_BASE + 0x11))
  27. #define GPIOD_ODR       * ((unsigned short *)   (GPIOD_BASE + 0x14))
  28. #define GPIOD_ODR_LOW   * ((unsigned char *)    (GPIOD_BASE + 0x14))
  29. #define GPIOD_ODR_HIGH  * ((unsigned char *)    (GPIOD_BASE + 0x15))
  30.  
  31. #define GPIOE_MODER     * ((unsigned long *)    (GPIOE_BASE))
  32. #define GPIOE_OTYPER    * ((unsigned short *)   (GPIOE_BASE + 0x04))
  33. #define GPIOE_PUPDR     * ((unsigned long *)    (GPIOE_BASE + 0x0C))
  34. #define GPIOE_IDR       * ((unsigned short *)   (GPIOE_BASE + 0x10))
  35. #define GPIOE_IDR_LOW   * ((unsigned char *)    (GPIOE_BASE + 0x10))
  36. #define GPIOE_IDR_HIGH  * ((unsigned char *)    (GPIOE_BASE + 0x11))
  37. #define GPIOE_ODR       * ((unsigned short *)   (GPIOE_BASE + 0x14))
  38. #define GPIOE_ODR_LOW   * ((unsigned char *)    (GPIOE_BASE + 0x14))
  39. #define GPIOE_ODR_HIGH  * ((unsigned char *)    (GPIOE_BASE + 0x15))
  40.  
  41. void app_init (void);
  42. unsigned char keyb(void);
  43. void ActivateRow(int);
  44. unsigned int ReadColumn(void);
  45. void out7seg(unsigned char c);
  46. void irq_handler(void);
  47. int measure_distance(void);
  48.  
  49. static volatile int unit            = 58*10;        // 58*centimeters
  50. static volatile int measure_delay   = 500;          // denotes how long delay in ms between distance measurement
  51.  
  52.  
  53.  
  54. void main(void){
  55.     app_init();
  56.     unsigned char distance;
  57.     while(1){
  58.         distance = measure_distance();
  59.         out7seg(distance);
  60.         delay_x_ms(measure_delay);
  61.     }
  62.    
  63.        
  64. }
  65.  
  66. int measure_distance(void){
  67.     int counter = 0;
  68.     int distance = 0;
  69.     GPIOE_ODR_LOW = 0;
  70.     delay_10mikro();
  71.     GPIOE_ODR_LOW = 1;
  72.     delay_10mikro();
  73.     GPIOE_ODR_LOW = 0;
  74.    
  75.     while(~GPIOE_IDR_LOW & 2){
  76.     }
  77.     while(GPIOE_IDR_LOW & 2){
  78.         delay_1mikro();
  79.         counter++;
  80.     }
  81.     distance = counter/unit;
  82.    
  83.     return distance;
  84. }
  85.  
  86. void delay_1mikro(void){
  87.     *STK_CTRL = 0;
  88.     *STK_LOAD = (168-1);
  89.     *STK_VAL = 0;
  90.     *STK_CTRL = 5;
  91.     while((*STK_CTRL & 0x10000) == 0);
  92.     *STK_CTRL = 0;
  93. }
  94.  
  95. void delay_10mikro(void){
  96.     for(int i = 0; i < 10; i++){   
  97.     delay_1mikro();
  98.     }
  99. }
  100.  
  101. void delay_x_ms(int x){
  102.     for(int i = 0; i < (x*1000); i++){
  103.         delay_1mikro();
  104.     }
  105. }
  106.  
  107. void app_init(void) {
  108.     GPIOD_MODER = 0x5555;
  109.     GPIOE_MODER = 0x00000001; //set PE0 to output, PE1 to input
  110.     GPIOD_OTYPER &= 0x00FF;
  111.     GPIOD_PUPDR &= 0x0000FFFF;
  112.     GPIOD_PUPDR |= 0x00AA0000;
  113. }
  114.  
  115. void out7seg(unsigned char c) {
  116.     GPIOD_ODR_LOW = c;
  117. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top