Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void startup(void) __attribute__((naked)) __attribute__((section (".start_section")) );
- void startup ( void )
- {
- __asm volatile(
- " LDR R0,=0x2001C000\n" /* set stack */
- " MOV SP,R0\n"
- " BL main\n" /* call main */
- "_exit: B .\n" /* never return */
- ) ;
- }
- #define GPIOD_BASE 0x40020C00
- #define GPIOE_BASE 0x40021000
- #define SYSTICK_BASE 0xE000E010
- #define STK_CTRL ((volatile unsigned long *) (SYSTICK_BASE))
- #define STK_LOAD ((volatile unsigned long *) (SYSTICK_BASE + 0x04))
- #define STK_VAL ((volatile unsigned long *) (SYSTICK_BASE + 0x08))
- #define GPIOD_MODER * ((unsigned long *) (GPIOD_BASE))
- #define GPIOD_OTYPER * ((unsigned short *) (GPIOD_BASE + 0x04))
- #define GPIOD_PUPDR * ((unsigned long *) (GPIOD_BASE + 0x0C))
- #define GPIOD_IDR * ((unsigned short *) (GPIOD_BASE + 0x10))
- #define GPIOD_IDR_HIGH * ((unsigned char *) (GPIOD_BASE + 0x11))
- #define GPIOD_ODR * ((unsigned short *) (GPIOD_BASE + 0x14))
- #define GPIOD_ODR_LOW * ((unsigned char *) (GPIOD_BASE + 0x14))
- #define GPIOD_ODR_HIGH * ((unsigned char *) (GPIOD_BASE + 0x15))
- #define GPIOE_MODER * ((unsigned long *) (GPIOE_BASE))
- #define GPIOE_OTYPER * ((unsigned short *) (GPIOE_BASE + 0x04))
- #define GPIOE_PUPDR * ((unsigned long *) (GPIOE_BASE + 0x0C))
- #define GPIOE_IDR * ((unsigned short *) (GPIOE_BASE + 0x10))
- #define GPIOE_IDR_LOW * ((unsigned char *) (GPIOE_BASE + 0x10))
- #define GPIOE_IDR_HIGH * ((unsigned char *) (GPIOE_BASE + 0x11))
- #define GPIOE_ODR * ((unsigned short *) (GPIOE_BASE + 0x14))
- #define GPIOE_ODR_LOW * ((unsigned char *) (GPIOE_BASE + 0x14))
- #define GPIOE_ODR_HIGH * ((unsigned char *) (GPIOE_BASE + 0x15))
- void app_init (void);
- unsigned char keyb(void);
- void ActivateRow(int);
- unsigned int ReadColumn(void);
- void out7seg(unsigned char c);
- void irq_handler(void);
- int measure_distance(void);
- static volatile int unit = 58*10; // 58*centimeters
- static volatile int measure_delay = 500; // denotes how long delay in ms between distance measurement
- void main(void){
- app_init();
- unsigned char distance;
- while(1){
- distance = measure_distance();
- out7seg(distance);
- delay_x_ms(measure_delay);
- }
- }
- int measure_distance(void){
- int counter = 0;
- int distance = 0;
- GPIOE_ODR_LOW = 0;
- delay_10mikro();
- GPIOE_ODR_LOW = 1;
- delay_10mikro();
- GPIOE_ODR_LOW = 0;
- while(~GPIOE_IDR_LOW & 2){
- }
- while(GPIOE_IDR_LOW & 2){
- delay_1mikro();
- counter++;
- }
- distance = counter/unit;
- return distance;
- }
- void delay_1mikro(void){
- *STK_CTRL = 0;
- *STK_LOAD = (168-1);
- *STK_VAL = 0;
- *STK_CTRL = 5;
- while((*STK_CTRL & 0x10000) == 0);
- *STK_CTRL = 0;
- }
- void delay_10mikro(void){
- for(int i = 0; i < 10; i++){
- delay_1mikro();
- }
- }
- void delay_x_ms(int x){
- for(int i = 0; i < (x*1000); i++){
- delay_1mikro();
- }
- }
- void app_init(void) {
- GPIOD_MODER = 0x5555;
- GPIOE_MODER = 0x00000001; //set PE0 to output, PE1 to input
- GPIOD_OTYPER &= 0x00FF;
- GPIOD_PUPDR &= 0x0000FFFF;
- GPIOD_PUPDR |= 0x00AA0000;
- }
- void out7seg(unsigned char c) {
- GPIOD_ODR_LOW = c;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement