Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.70 KB | None | 0 0
  1. /*
  2. * startup.c
  3. *
  4. */
  5.  
  6. #import "delay.h"
  7.  
  8. #define GPIO_E 0x40021000
  9. #define GPIO_E_MODER ((volatile unsigned int *) GPIO_E)
  10. #define GPIO_E_OTYPER ((volatile unsigned short *) GPIO_E + 0x4)
  11. #define GPIO_E_OSPEEDR ((volatile unsigned int *) GPIO_E + 0x8)
  12. #define GPIO_E_PUPDR ((volatile unsigned int *) GPIO_E + 0xC)
  13. #define GPIO_E_IDR_LOW ((volatile unsigned char*) GPIO_E + 0x10)
  14. #define GPIO_E_IDR_HIGH ((volatile unsigned char*) GPIO_E + 0x11)
  15. #define GPIO_E_ODR_LOW ((volatile unsigned char*) GPIO_E + 0x14)
  16. #define GPIO_E_ODR_HIGH ((volatile unsigned char*) GPIO_E + 0x15)
  17. #define B_E 0b01000000 //Flagga för att indikera arbetscykel (t.ex. läsa / skriva), 1 om den inleds, 0 om den är klar
  18. #define B_SELECT 0b00000100
  19. #define B_RW 0b00000010
  20. #define B_RS 0b00000001
  21. #define B_CS1 0b00001000
  22. #define B_CS2 0b00010000
  23. #define B_RESET 0b00100000
  24. #define LCD_ON 0b00111111
  25. #define LCD_OFF 0b00111110
  26. #define LCD_SET_ADD 0b01000000
  27. #define LCD_SET_PAGE 0b10111000
  28. #define LCD_DISP_START 0b11000000
  29. #define LCD_BUSY 0b10000000
  30.  
  31. typedef unsigned char uint_8t;
  32.  
  33. /* Välj grafik-display och ettställ de bitar som är 1 i x */
  34. void graphic_ctrl_bit_set( uint_8t x )
  35. {
  36. *GPIO_E_ODR_LOW |= ( (~B_SELECT) & x);
  37. }
  38.  
  39. /* Välj grafik-display och nollställ de bitar som är 1 i x */
  40. void graphic_ctrl_bit_clear( uint_8t x )
  41. {
  42. *GPIO_E_ODR_LOW &= ( (~B_SELECT) & ~x);
  43. }
  44.  
  45. void select_controller( uint_8t controller )
  46. {
  47. switch( controller )
  48. {
  49. case 0:
  50. graphic_ctrl_bit_clear( B_CS1|B_CS2 ); //Clear both CS1 and CS2
  51. break;
  52. case B_CS1:
  53. graphic_ctrl_bit_set( B_CS1 );
  54. graphic_ctrl_bit_clear( B_CS2 );
  55. break;
  56. case B_CS2:
  57. graphic_ctrl_bit_set( B_CS2 );
  58. graphic_ctrl_bit_clear( B_CS1 );
  59. break;
  60. case (B_CS1 | B_CS2):
  61. graphic_ctrl_bit_set( B_CS1|B_CS2 ); //Set both CS1 and CS2 flag-bits
  62. break;
  63. }
  64. }
  65. void graphic_wait_ready( void )
  66. {
  67. graphic_ctrl_bit_clear( B_E );
  68. *GPIO_E_MODER = 0x00005555;
  69. graphic_ctrl_bit_clear( B_RS );
  70. graphic_ctrl_bit_set( B_RW );
  71. delay_500ns();
  72. while(1) //while LCD is busy
  73. {
  74. graphic_ctrl_bit_set( B_E );
  75. delay_500ns();
  76. graphic_ctrl_bit_clear( B_E );
  77. delay_500ns();
  78.  
  79. if( (*GPIO_E_IDR_HIGH & LCD_BUSY) == 0 ) //Break out of loop (to continue with program) when LCD_BUSY busy-flag is set to false (not busy)
  80. {
  81. break;
  82. }
  83. }
  84. graphic_ctrl_bit_set( B_E );
  85. *GPIO_E_MODER = 0x55555555;
  86. }
  87.  
  88. uint_8t graphic_read( uint_8t controller)
  89. {
  90. graphic_ctrl_bit_clear( B_E );
  91. *GPIO_E_MODER = 0x00005555;
  92. graphic_ctrl_bit_set( B_RS|B_RW ); //Set both RS and RW flags
  93. select_controller( controller );
  94. delay_500ns();
  95. graphic_ctrl_bit_set( B_E );
  96. delay_500ns();
  97. uint_8t rv = *GPIO_E_IDR_HIGH;
  98. graphic_ctrl_bit_clear( B_E );
  99. *GPIO_E_MODER = 0x55555555;
  100. if( controller == B_CS1 )
  101. {
  102. select_controller( B_CS1 );
  103. graphic_wait_ready();
  104. }
  105. if( controller == B_CS2 )
  106. {
  107. select_controller( B_CS2 );
  108. graphic_wait_ready();
  109. }
  110. return rv;
  111. }
  112.  
  113. void graphic_write(uint_8t value, uint_8t controller)
  114. {
  115. *GPIO_E_ODR_HIGH = value;
  116. select_controller( controller );
  117. delay_500ns();
  118. graphic_ctrl_bit_set( B_E );
  119. delay_500ns();
  120. graphic_ctrl_bit_clear( B_E );
  121.  
  122. if( controller & B_CS1)
  123. {
  124. select_controller( B_CS1 );
  125. graphic_wait_ready();
  126. }
  127. if( controller & B_CS2 )
  128. {
  129. select_controller( B_CS2 );
  130. graphic_wait_ready();
  131. }
  132.  
  133. *GPIO_E_ODR_HIGH = 0;
  134. graphic_ctrl_bit_set( B_E );
  135. select_controller( 0 );
  136. }
  137.  
  138. void graphic_write_command(uint_8t command, uint_8t controller)
  139. {
  140. graphic_ctrl_bit_clear( B_E );
  141. select_controller( controller ); //Väljer CS_1, CS_2, båda eller ingen
  142. graphic_ctrl_bit_clear( B_RS|B_RW );
  143. graphic_write( command, controller );
  144. }
  145.  
  146. void graphic_write_data(uint_8t data, uint_8t controller)
  147. {
  148. graphic_ctrl_bit_clear( B_E );
  149. select_controller( controller ); //Väljer CS_1, CS_2, båda eller igen
  150. graphic_ctrl_bit_set( B_RS );
  151. graphic_ctrl_bit_clear( B_RW );
  152. graphic_write( data, controller );
  153. }
  154.  
  155. uint_8t graphic_read_data(uint_8t controller)
  156. {
  157. (void) graphic_read( controller ); //Returnerar nonsens
  158. return graphic_read( controller ); //Returnerar korrekt data
  159. }
  160.  
  161.  
  162. void graphic_clear_screen( void )
  163. {
  164. for( char page = 0; page <= 7; page++ )
  165. {
  166. graphic_write_command( LCD_SET_PAGE|page, B_CS1|B_CS2 );
  167. graphic_write_command( LCD_SET_ADD |0 , B_CS1|B_CS2 );
  168. for( char addr = 0; addr <= 63; addr++ )
  169. {
  170. graphic_write_data( 0, B_CS1|B_CS2 );
  171. }
  172. }
  173. }
  174.  
  175. void pixel( unsigned x, unsigned y, unsigned set )
  176. {
  177. unsigned x_fysisk = 0;
  178. unsigned index = (y - 1) / 8; //Calculate modulo 8 for the pixel
  179. uint_8t controller = 0;
  180. uint_8t bitMask = 0;
  181.  
  182. if(x > 128 || y > 64)
  183. {
  184. return; //Invalid pixel coords
  185. }
  186.  
  187. //Skapa bitmask för pixeln (man kan tända / släcka 8 st pixlar på följd, måste ställa in så endast en tänds/släcks på rätt koordinater.
  188. switch( (y - 1) % 8)
  189. {
  190. case 0:
  191. bitMask = 1;
  192. break;
  193. case 1:
  194. bitMask = 2;
  195. break;
  196. case 2:
  197. bitMask = 4;
  198. break;
  199. case 3:
  200. bitMask = 8;
  201. break;
  202. case 4:
  203. bitMask = 0x10;
  204. break;
  205. case 5:
  206. bitMask = 0x20;
  207. break;
  208. case 6:
  209. bitMask = 0x40;
  210. break;
  211. case 7:
  212. bitMask = 0x80;
  213. break;
  214. }
  215.  
  216. if( set == 0)
  217. {
  218. bitMask = ~bitMask;
  219. }
  220.  
  221. //Bestäm fysiska koordinater och välj styrkrets
  222. if( x > 64)
  223. {
  224. controller = B_CS2;
  225. x_fysisk = x - 65;
  226. }
  227. else
  228. {
  229. controller = B_CS1;
  230. x_fysisk = x - 1;
  231. }
  232.  
  233. graphic_write_command( LCD_SET_ADD | x_fysisk, controller );
  234. graphic_write_command( LCD_SET_PAGE | index, controller );
  235. uint_8t temp = graphic_read_data( controller );
  236. graphic_write_command( LCD_SET_ADD | x_fysisk, controller );
  237.  
  238. if( set == 1 )
  239. {
  240. bitMask |= temp;
  241. }
  242. else
  243. {
  244. bitMask &= temp;
  245. }
  246.  
  247. graphic_write_data( bitMask, controller );
  248. }
  249.  
  250. void init_app( void )
  251. {
  252. *GPIO_E_MODER = 0x55555555; //Utgång
  253. }
  254.  
  255. void graphic_initialize( void )
  256. {
  257. graphic_ctrl_bit_set( B_E );
  258. delay_mikro(10);
  259. graphic_ctrl_bit_clear( B_CS1|B_CS2|B_RS|B_E );
  260. delay_milli(30);
  261. graphic_ctrl_bit_set( B_RS );
  262. graphic_write_command( LCD_OFF, B_CS1|B_CS2 );
  263. graphic_write_command( LCD_ON, B_CS1|B_CS2 );
  264. graphic_write_command( LCD_DISP_START, B_CS1|B_CS2 ); //Toggle display
  265. graphic_write_command( LCD_SET_ADD, B_CS1|B_CS2 ); //Address = 0 (Y)
  266. graphic_write_command( LCD_SET_PAGE, B_CS1|B_CS2 ); //Page = 0 (X)
  267. select_controller(0); //deaktivera båda CS-signalerna
  268. }
  269.  
  270. void startup(void) __attribute__((naked)) __attribute__((section (".start_section")) );
  271.  
  272. void startup ( void )
  273. {
  274. __asm volatile(
  275. " LDR R0,=0x2001C000\n" /* set stack */
  276. " MOV SP,R0\n"
  277. " BL main\n" /* call main */
  278. "_exit: B .\n" /* never return */
  279. ) ;
  280. }
  281.  
  282. void main( void )
  283. {
  284. unsigned i;
  285. init_app();
  286. graphic_initialize();
  287. #ifndef SIMULATOR
  288. graphic_clear_screen();
  289. #endif
  290. for( i = 0; i < 128; i++ ) //Rita horisontell linje
  291. {
  292. pixel( i, 10, 1 );
  293. }
  294. for( i = 0; i < 64; i++ ) //Rita vertikal linje
  295. {
  296. pixel( 10, i, 1 );
  297. }
  298. delay_milli( 500 ); //Vänta 0,5 sekunder
  299. for( i = 0; i < 128; i++ ) //Sudda horisontella linjen
  300. {
  301. pixel( i, 10, 0 );
  302. }
  303. for( i = 0; i < 64; i++ ) //Sudda vertikala linjen
  304. {
  305. pixel ( 10, i , 0 );
  306. }
  307. /*
  308. graphic_write_command( LCD_SET_ADD | 40, B_CS1|B_CS2 ); //Address = x coord
  309. graphic_write_command( LCD_SET_PAGE | 40, B_CS1|B_CS2 ); //Page = y coord
  310. graphic_write_data( 0b11111111, B_CS1|B_CS2 ); //Varje bit i mönstret indikerar om motsvarande bit på skärmen ska tändas eller inte.
  311. //Bit 0 kommer tända pixel (40,40), bit 1 kommer släcka pixel (40,41) osv (inställd på att öka i y-led)
  312. */
  313. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement