Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * startup.c
- *
- */
- #define GPIO_E 0x40021000
- #define GPIO_E_MODER ((volatile unsigned int *) (GPIO_E))
- #define GPIO_E_IDR_LOW ((volatile unsigned char *) (GPIO_E+0x10))
- #define GPIO_E_IDR_HIGH ((volatile unsigned char *) (GPIO_E+0x11))
- #define GPIO_E_ODR_LOW ((volatile unsigned char *) (GPIO_E+0x14))
- #define GPIO_E_ODR_HIGH ((volatile unsigned char *) (GPIO_E+0x15))
- #define SysTick 0xE000E010
- #define STK_CTRL ((volatile unsigned int *) (SysTick))
- #define STK_LOAD ((volatile unsigned int *) (SysTick+0x4))
- #define STK_VAL ((volatile unsigned int *) (SysTick+0x8))
- #define B_E 0x40 // Enable-signal
- #define B_SELECT 4 // Select ASCII-display
- #define B_RW 2 // 0=Write, 1=Read
- #define B_RS 1 // 0=Control, 1=Data
- 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 */
- ".L1: B .L1\n" /* never return */
- ) ;
- }
- void app_init(void) {
- *GPIO_E_MODER = 0x55555555;
- }
- void delay_250ns(void){
- *STK_CTRL = 0;
- *STK_LOAD = ((168/4) -1);
- *STK_VAL = 0;
- *STK_CTRL = 5;
- while ((*STK_CTRL & 0x10000) == 0);
- *STK_CTRL = 0;
- }
- void delay_mikro(unsigned int us) {
- #ifdef SIMULATOR
- us = us / 1000;
- us++;
- #endif
- while (us > 0){
- delay_250ns();
- delay_250ns();
- delay_250ns();
- delay_250ns();
- us--;
- }
- }
- void delay_milli(unsigned int ms) {
- #ifdef SIMULATOR
- ms = ms / 1000;
- ms++;
- #endif
- while (ms > 0){
- delay_mikro(1000);
- ms--;
- }
- }
- void ascii_ctrl_bit_set(unsigned char x) {
- unsigned char c = *GPIO_E_ODR_LOW;
- c |= ( B_SELECT | x );
- *GPIO_E_ODR_LOW = c;
- }
- void ascii_ctrl_bit_clear(unsigned char x) {
- unsigned char c = *GPIO_E_ODR_LOW;
- c &= (B_SELECT | ~x);
- *GPIO_E_ODR_LOW = c;
- }
- void ascii_write_controller(unsigned char byte) {
- ascii_ctrl_bit_set(B_E);
- *GPIO_E_ODR_HIGH = byte;
- ascii_ctrl_bit_clear(B_E);
- delay_250ns();
- }
- unsigned char ascii_read_controller(void) {
- unsigned char c;
- ascii_ctrl_bit_set(B_E);
- delay_250ns();
- delay_250ns();
- c = *GPIO_E_IDR_HIGH;
- ascii_ctrl_bit_clear(B_E);
- return c;
- }
- void ascii_write_cmd(unsigned char command){
- ascii_ctrl_bit_clear(B_RS);
- ascii_ctrl_bit_clear(B_RW);
- ascii_write_controller(command);
- }
- void ascii_write_data(unsigned char data){
- ascii_ctrl_bit_set(B_RS);
- ascii_ctrl_bit_clear(B_RW);
- ascii_write_controller(data);
- }
- unsigned char ascii_read_status (void){
- unsigned char c;
- *GPIO_E_MODER = 0x00005555;
- ascii_ctrl_bit_clear(B_RS);
- ascii_ctrl_bit_set(B_RW);
- c = ascii_read_controller();
- *GPIO_E_MODER = 0x55555555;
- return c;
- }
- unsigned char ascii_read_data (void){
- unsigned char c;
- *GPIO_E_MODER = 0x00005555;
- ascii_ctrl_bit_set(B_RS);
- ascii_ctrl_bit_set(B_RW);
- c = ascii_read_controller();
- *GPIO_E_MODER = 0x55555555;
- return c;
- }
- void ascii_Function_Set(unsigned char command){
- while ((ascii_read_status() & 0x80) == 0x80){
- delay_mikro(8);
- ascii_write_cmd(command);
- delay_mikro(40);
- }
- }
- void ascii_Display_Control(unsigned char command){
- while ((ascii_read_status() & 0x80) == 0x80){
- delay_mikro(8);
- ascii_write_cmd(command);
- delay_mikro(40);
- }
- }
- void ascii_Entry_Mode_Set(unsigned char command){
- while ((ascii_read_status() & 0x80) == 0x80){
- delay_mikro(8);
- ascii_write_cmd(command);
- delay_mikro(40);
- }
- }
- void ascii_init(void){
- ascii_Function_Set(0x38);
- ascii_Display_Control(0xE);
- ascii_Function_Set(0x6);
- }
- void ascii_gotoxy(int x, int y){
- unsigned int adress;
- adress = x-1;
- if (y == 2) {
- adress += 0x40;
- }
- ascii_write_cmd(0x80 | adress);
- }
- void ascii_write_char (unsigned char c){
- while((ascii_read_status() & 0x80) == 0x80);
- delay_mikro(8);
- ascii_write_data(c);
- delay_mikro(50);
- }
- int main(int argc, char **argv)
- {
- char *s;
- char test1[] = "Alfanumeriskt ";
- char test2[] = "Display - test";
- app_init();
- ascii_init();
- ascii_gotoxy(1,1);
- s = test1;
- while(*s) {
- ascii_write_char(*s++);
- }
- ascii_gotoxy(1,2);
- s = test2;
- while(*s) {
- ascii_write_char(*s++);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement