Anonymous Anonymous - 6 months ago
1017 0
C

fsagsas

/*
 * 	startup.c
 *
 */
 
 
#define GPIOD_BASE 0x40020C00
#define GPIOD_MODER ((volatile unsigned int *) (GPIOD_BASE))
#define GPIOD_OTYPER ((volatile unsigned short *) (GPIOD_BASE+0x4))
#define GPIOD_OSPEEDR ((volatile unsigned int *) (GPIOD_BASE+0x8))
#define GPIOD_PUPDR ((volatile unsigned int *) (GPIOD_BASE+0xC))
#define GPIOD_IDR_LOW ((volatile unsigned char *) (GPIOD_BASE+0x10))
#define GPIOD_IDR_HIGH ((volatile unsigned char *) (GPIOD_BASE+0x11))
#define GPIOD_ODR_LOW ((volatile unsigned char *) (GPIOD_BASE+0x14))
#define GPIOD_ODR_HIGH ((volatile unsigned char *) (GPIOD_BASE+0x15))

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 */
	) ;
}
void app_init(void)
{
	*GPIOD_MODER = 0x55005555;
	*GPIOD_PUPDR = 0x00AA0000;
	*GPIOD_OTYPER = 0x00000000;

}
unsigned char keyb(void)
{
	unsigned char key[] = {1,2,3,0xA,4,5,6,0xB,7,8,9,0xC,0xE,0,0xF,0xD};
	int row, col;
	for(row=1;row <= 4; row++)
	{
		kbdActivate(row);
		if((col = kbdGetCol())){
			kbdActivate(0);
			return key [4*(row-1)+(col-1)];
			}
	}
	kbdActivate(0);
	return 0xFF;
}
void out7seg(unsigned char c)
{
	static unsigned char segCode[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x67,0x77,0x7C,0x39,0x5E,0x79,0x71};
	if(c > 15){
		*GPIOD_ODR_LOW = 0;
		return;
	}
	*GPIOD_ODR_LOW = segCode[c];
}
void main(void)
{
	app_init();
	while(1)
		{
		out7seg(keyb());
		}
}

void kbdActivate(unsigned int row){
	switch(row)
	{
	case 1: *GPIOD_ODR_HIGH = 0x10;
	case 2: *GPIOD_ODR_HIGH = 0x20;
	case 3: *GPIOD_ODR_HIGH = 0x40;
	case 4: *GPIOD_ODR_HIGH = 0x80;
	default: *GPIOD_ODR_HIGH = 0x00;
	}
}

int kbdGetCol(void){
	unsigned short c;
	c = *GPIOD_IDR_HIGH;
	if(c & 0x8 ) return 4; 
	if(c & 0x4 ) return 3; 
	if(c & 0x2 ) return 2; 
	if(c & 0x1 ) return 1; 
	return 0;
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download