Advertisement
Guest User

ascii2

a guest
Apr 19th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. /*
  2. * startup.c
  3. *
  4. */
  5. #include <stdio.h>
  6. #define STK_CTRL ((volatile unsigned int *)(0xE000E010))
  7. #define STK_LOAD ((volatile unsigned int *)(0xE000E014))
  8.  
  9. // Bargraph
  10. #define STK_VAL ((volatile unsigned int *)(0xE000E018))
  11. #define PORT_BARGRAPH_BASE 0x40021000 /* MD407 port E */
  12. #define portBargraphModer ((volatile unsigned int *) (PORT_BARGRAPH_BASE))
  13. #define portBargraphOtyper ((volatile unsigned short *) (PORT_BARGRAPH_BASE+0x4))
  14. #define portBargraphOspeedr ((volatile unsigned int *) (PORT_BARGRAPH_BASE+0x8))
  15. #define portBargraphOdrLow ((volatile unsigned char *) (PORT_BARGRAPH_BASE+0x14))
  16.  
  17. // Display
  18. #define PORT_DISPLAY_BASE 0x40021000 /* MD407 port E */
  19. #define portModer ((volatile unsigned int *) (PORT_DISPLAY_BASE))
  20. #define portOtyper ((volatile unsigned short *)(PORT_DISPLAY_BASE+0x4))
  21. #define portOspeedr ((volatile unsigned int *) (PORT_DISPLAY_BASE+0x8))
  22. #define portPupdr ((volatile unsigned int *)(PORT_DISPLAY_BASE+0xC))
  23. #define portIdr ((volatile unsigned short *)(PORT_DISPLAY_BASE+0x10))
  24. #define portIdrLow ((volatile unsigned char *)(PORT_DISPLAY_BASE+0x10))
  25. #define portIdrHigh ((volatile unsigned char *)(PORT_DISPLAY_BASE+0x10+1))
  26. #define portOdrLow ((volatile unsigned char *)(PORT_DISPLAY_BASE+0x14))
  27. #define portOdrHigh ((volatile unsigned char *)(PORT_DISPLAY_BASE+0x14+1))
  28.  
  29. #define B_E 0x40 /* enable */
  30. #define B_SELECT 4 /* välj ascii/lcd */
  31. #define B_RW 2 /* read/write */
  32. #define B_RS 1 /* register select */
  33.  
  34. #define SIMULATOR
  35.  
  36. void startup(void) __attribute__((naked)) __attribute__((section (".start_section")) );
  37.  
  38. void startup ( void )
  39. {
  40. __asm volatile(
  41. " LDR R0,=0x2001C000\n" /* set stack */
  42. " MOV SP,R0\n"
  43. " BL main\n" /* call main */
  44. "_exit: B .\n" /* never return */
  45. ) ;
  46. }
  47.  
  48. void app_init(void){
  49. * ((unsigned int *) 0x40021000) = 0x55555555;
  50. //* ((unsigned int *) 0x40021000) |= 0x55005555;
  51. }
  52.  
  53. void delay_250ns (void){
  54. /* SystemCoreClock = 168000000 */
  55. *STK_CTRL = 0;
  56. *STK_LOAD = ( (168/4) -1 );
  57. *STK_VAL = 0;
  58. *STK_CTRL = 5;
  59. while( (*STK_CTRL & 0x10000 )== 0 ){
  60. }
  61. *STK_CTRL = 0;
  62. }
  63.  
  64. void delay_mikro (unsigned int us){
  65. while( us-- ){
  66. delay_250ns();
  67. delay_250ns();
  68. delay_250ns();
  69. delay_250ns();
  70. }
  71. }
  72.  
  73. void delay_milli( unsigned int ms ){
  74. #ifdef SIMULATOR
  75. while( ms-- )
  76. delay_mikro(1);
  77. #else
  78. while( ms-- )
  79. delay_mikro(1000);
  80. #endif
  81. }
  82.  
  83. /*void main(void){
  84. app_init();
  85. while(1){
  86. *portBargraphOdrLow = 0;
  87. delay_milli(500);
  88. *portBargraphOdrLow = 0xFF;
  89. delay_milli(500);
  90. }
  91. }*/
  92.  
  93. void ascii_ctrl_bit_set( unsigned char x ){
  94. unsigned char c;
  95. c = *portOdrLow;
  96. c |= ( B_SELECT | x );
  97. *portOdrLow = c;
  98. }
  99.  
  100. void ascii_ctrl_bit_clear( unsigned char x ){
  101. unsigned char c;
  102. c = *portOdrLow;
  103. c &= ( B_SELECT | ~x );
  104. *portOdrLow = c;
  105. }
  106.  
  107. void ascii_write_controller(unsigned char byte){
  108. ascii_ctrl_bit_set(B_E);
  109. *portOdrHigh = byte;
  110. delay_250ns();
  111. ascii_ctrl_bit_clear(B_E);
  112. }
  113.  
  114. unsigned char ascii_read_controller(void){
  115. ascii_ctrl_bit_set(B_E);
  116. delay_250ns();
  117. delay_250ns();
  118. unsigned char rv = *portIdrHigh;
  119. ascii_ctrl_bit_clear(B_E);
  120. return rv;
  121. }
  122.  
  123. void ascii_write_cmd (unsigned char command){
  124. ascii_ctrl_bit_clear(B_RS);
  125. ascii_ctrl_bit_clear(B_RW);
  126. ascii_write_controller(command);
  127. }
  128.  
  129. void ascii_write_data (unsigned char data){
  130. ascii_ctrl_bit_set(B_RS);
  131. ascii_ctrl_bit_clear(B_RW);
  132. ascii_write_controller(data);
  133. }
  134.  
  135. unsigned char ascii_read_status (void){
  136. * ((unsigned int *) 0x40021000) = 0x00005555;
  137. ascii_ctrl_bit_clear(B_RS);
  138. ascii_ctrl_bit_set(B_RW);
  139. unsigned char rv = ascii_read_controller();
  140. * ((unsigned int *) 0x40021000) |= 0x55550000;
  141. return rv;
  142.  
  143. }
  144.  
  145. unsigned char ascii_read_data (void){
  146. * ((unsigned int *) 0x40021000) = 0x00005555;
  147. ascii_ctrl_bit_set(B_RS);
  148. ascii_ctrl_bit_set(B_RW);
  149. unsigned char rv = ascii_read_controller();
  150. * ((unsigned int *) 0x40021000) |= 0x55550000;
  151. return rv;
  152. }
  153.  
  154.  
  155.  
  156. void ascii_command(unsigned char command){
  157. while((ascii_read_status() & 0x80) == 0x80){}
  158. delay_mikro(8);
  159. ascii_write_cmd(command);
  160. delay_milli(2);
  161. }/*
  162. if (command & 0x01 || command & 0x02){
  163. delay_mikro(2);
  164. }
  165. else{//(command & 0x04 || command & 0x08 || command & 0x30 || command & 0x80)
  166. //delay_mikro(40);
  167. else if (command & 0x80 && )
  168.  
  169. }*/
  170.  
  171. void ascii_init(void){
  172. ascii_command(0x38);
  173. ascii_command(0x0C);
  174. ascii_command(0x01);
  175. ascii_command(0x06);
  176. }
  177.  
  178. void ascii_write_char(unsigned char c){
  179. while((ascii_read_status() & 0x80) == 0x80){}
  180. delay_mikro(8);
  181. ascii_write_data(c);
  182. delay_milli(2);
  183. }
  184. /*
  185. void gotoxy(unsigned char row, unsigned row column){
  186. unsigned char adress;
  187. where(row=20;row>0;row--){
  188. adress = row-1
  189. if(column==2){
  190. adress = adress + 0x40;
  191. }
  192. }
  193. }
  194. */
  195.  
  196. void ascii_gotoxy( unsigned char x, unsigned char y) {
  197. unsigned char address;
  198. if(y!=1){
  199. address=0x40 | (x-1);
  200. }
  201. else{
  202. address=(x-1);
  203. }
  204. ascii_write_cmd( 0x80 | address);
  205. delay_mikro(45);
  206. }
  207.  
  208. int main(int argc, char **argv){
  209. char *s;
  210. char test1[] = "Alfanumerisk ";
  211. char test2[] = "Display - test";
  212.  
  213. app_init();
  214. ascii_init();
  215. ascii_gotoxy(1,1);
  216. s = test1;
  217. while(*s)
  218. ascii_write_char(*s++);
  219. ascii_gotoxy(1,2);
  220. s = test2;
  221. while(*s)
  222. ascii_write_char(*s++);
  223. return 0;
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement