Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. /*
  2. * startup.c
  3. *
  4. */
  5. #define GPIO_E 0x40021000
  6. #define GPIO_E_MODER ((volatile unsigned int *) (GPIO_E))
  7. #define GPIO_E_IDR_LOW ((volatile unsigned char *) (GPIO_E+0x10))
  8. #define GPIO_E_IDR_HIGH ((volatile unsigned char *) (GPIO_E+0x11))
  9. #define GPIO_E_ODR_LOW ((volatile unsigned char *) (GPIO_E+0x14))
  10. #define GPIO_E_ODR_HIGH ((volatile unsigned char *) (GPIO_E+0x15))
  11.  
  12. #define SysTick 0xE000E010
  13. #define STK_CTRL ((volatile unsigned int *) (SysTick))
  14. #define STK_LOAD ((volatile unsigned int *) (SysTick+0x4))
  15. #define STK_VAL ((volatile unsigned int *) (SysTick+0x8))
  16.  
  17. #define B_E 0x40 // Enable-signal
  18. #define B_SELECT 4 // Select ASCII-display
  19. #define B_RW 2 // 0=Write, 1=Read
  20. #define B_RS 1 // 0=Control, 1=Data
  21.  
  22. void startup(void) __attribute__((naked)) __attribute__((section (".start_section")) );
  23.  
  24. void startup ( void )
  25. {
  26. __asm volatile(
  27. " LDR R0,=0x2001C000\n" /* set stack */
  28. " MOV SP,R0\n"
  29. " BL main\n" /* call main */
  30. ".L1: B .L1\n" /* never return */
  31. ) ;
  32. }
  33.  
  34. void app_init(void) {
  35. *GPIO_E_MODER = 0x55555555;
  36. }
  37.  
  38. void delay_250ns(void){
  39. *STK_CTRL = 0;
  40. *STK_LOAD = ((168/4) -1);
  41. *STK_VAL = 0;
  42. *STK_CTRL = 5;
  43. while ((*STK_CTRL & 0x10000) == 0);
  44. *STK_CTRL = 0;
  45. }
  46.  
  47. void delay_mikro(unsigned int us) {
  48. #ifdef SIMULATOR
  49. us = us / 1000;
  50. us++;
  51. #endif
  52. while (us > 0){
  53. delay_250ns();
  54. delay_250ns();
  55. delay_250ns();
  56. delay_250ns();
  57. us--;
  58. }
  59. }
  60.  
  61. void delay_milli(unsigned int ms) {
  62. #ifdef SIMULATOR
  63. ms = ms / 1000;
  64. ms++;
  65. #endif
  66. while (ms > 0){
  67. delay_mikro(1000);
  68. ms--;
  69. }
  70. }
  71.  
  72. void ascii_ctrl_bit_set(unsigned char x) {
  73. unsigned char c = *GPIO_E_ODR_LOW;
  74. c |= ( B_SELECT | x );
  75. *GPIO_E_ODR_LOW = c;
  76. }
  77.  
  78. void ascii_ctrl_bit_clear(unsigned char x) {
  79. unsigned char c = *GPIO_E_ODR_LOW;
  80. c &= (B_SELECT | ~x);
  81. *GPIO_E_ODR_LOW = c;
  82. }
  83.  
  84. void ascii_write_controller(unsigned char byte) {
  85. ascii_ctrl_bit_set(B_E);
  86. *GPIO_E_ODR_HIGH = byte;
  87. ascii_ctrl_bit_clear(B_E);
  88. delay_250ns();
  89.  
  90. }
  91.  
  92. unsigned char ascii_read_controller(void) {
  93. unsigned char c;
  94. ascii_ctrl_bit_set(B_E);
  95. delay_250ns();
  96. delay_250ns();
  97. c = *GPIO_E_IDR_HIGH;
  98. ascii_ctrl_bit_clear(B_E);
  99. return c;
  100. }
  101.  
  102. void ascii_write_cmd(unsigned char command){
  103. ascii_ctrl_bit_clear(B_RS);
  104. ascii_ctrl_bit_clear(B_RW);
  105. ascii_write_controller(command);
  106. }
  107.  
  108. void ascii_write_data(unsigned char data){
  109. ascii_ctrl_bit_set(B_RS);
  110. ascii_ctrl_bit_clear(B_RW);
  111. ascii_write_controller(data);
  112. }
  113.  
  114. unsigned char ascii_read_status (void){
  115. unsigned char c;
  116. *GPIO_E_MODER = 0x00005555;
  117. ascii_ctrl_bit_clear(B_RS);
  118. ascii_ctrl_bit_set(B_RW);
  119. c = ascii_read_controller();
  120. *GPIO_E_MODER = 0x55555555;
  121. return c;
  122. }
  123.  
  124. unsigned char ascii_read_data (void){
  125. unsigned char c;
  126. *GPIO_E_MODER = 0x00005555;
  127. ascii_ctrl_bit_set(B_RS);
  128. ascii_ctrl_bit_set(B_RW);
  129. c = ascii_read_controller();
  130. *GPIO_E_MODER = 0x55555555;
  131. return c;
  132. }
  133.  
  134. void ascii_Function_Set(unsigned char command){
  135. while ((ascii_read_status() & 0x80) == 0x80){
  136. delay_mikro(8);
  137. ascii_write_cmd(command);
  138. delay_mikro(40);
  139. }
  140. }
  141.  
  142. void ascii_Display_Control(unsigned char command){
  143. while ((ascii_read_status() & 0x80) == 0x80){
  144. delay_mikro(8);
  145. ascii_write_cmd(command);
  146. delay_mikro(40);
  147. }
  148. }
  149. void ascii_Entry_Mode_Set(unsigned char command){
  150. while ((ascii_read_status() & 0x80) == 0x80){
  151. delay_mikro(8);
  152. ascii_write_cmd(command);
  153. delay_mikro(40);
  154. }
  155. }
  156.  
  157. void ascii_init(void){
  158. ascii_Function_Set(0x38);
  159. ascii_Display_Control(0xE);
  160. ascii_Function_Set(0x6);
  161. }
  162.  
  163. void ascii_gotoxy(int x, int y){
  164. unsigned int adress;
  165. adress = x-1;
  166. if (y == 2) {
  167. adress += 0x40;
  168. }
  169. ascii_write_cmd(0x80 | adress);
  170. }
  171.  
  172. void ascii_write_char (unsigned char c){
  173. while((ascii_read_status() & 0x80) == 0x80);
  174. delay_mikro(8);
  175. ascii_write_data(c);
  176. delay_mikro(50);
  177. }
  178.  
  179. int main(int argc, char **argv)
  180. {
  181. char *s;
  182. char test1[] = "Alfanumeriskt ";
  183. char test2[] = "Display - test";
  184.  
  185. app_init();
  186. ascii_init();
  187. ascii_gotoxy(1,1);
  188. s = test1;
  189. while(*s) {
  190. ascii_write_char(*s++);
  191. }
  192. ascii_gotoxy(1,2);
  193. s = test2;
  194. while(*s) {
  195. ascii_write_char(*s++);
  196. }
  197. return 0;
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement