Advertisement
Guest User

graphicdisplay

a guest
Apr 26th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.85 KB | None | 0 0
  1. #define SIMULATOR
  2. /*
  3. * startup.c
  4. *
  5. */
  6. #include <stdio.h>
  7. #define STK_CTRL ((volatile unsigned int *)(0xE000E010))
  8. #define STK_LOAD ((volatile unsigned int *)(0xE000E014))
  9.  
  10. // Bargraph
  11.  
  12. #define STK_VAL ((volatile unsigned int *)(0xE000E018))
  13. /*#define PORT_BARGRAPH_BASE 0x40021000 //MD407 port E
  14. #define portBargraphModer ((volatile unsigned int *) (PORT_BARGRAPH_BASE))
  15. #define portBargraphOtyper ((volatile unsigned short *) (PORT_BARGRAPH_BASE+0x4))
  16. #define portBargraphOspeedr ((volatile unsigned int *) (PORT_BARGRAPH_BASE+0x8))
  17. #define portBargraphOdrLow ((volatile unsigned char *) (PORT_BARGRAPH_BASE+0x14))
  18. */
  19. // Display
  20.  
  21.  
  22. #define PORT_DISPLAY_BASE 0x40021000 //MD407 port E
  23. #define portModer ((volatile unsigned int *) (PORT_DISPLAY_BASE))
  24. #define portOtyper ((volatile unsigned short *)(PORT_DISPLAY_BASE+0x4))
  25. #define portOspeedr ((volatile unsigned int *) (PORT_DISPLAY_BASE+0x8))
  26. #define portPupdr ((volatile unsigned int *)(PORT_DISPLAY_BASE+0xC))
  27. #define portIdr ((volatile unsigned short *)(PORT_DISPLAY_BASE+0x10))
  28. #define portIdrLow ((volatile unsigned char *)(PORT_DISPLAY_BASE+0x10))
  29. #define portIdrHigh ((volatile unsigned char *)(PORT_DISPLAY_BASE+0x10+1))
  30. #define portOdrLow ((volatile unsigned char *)(PORT_DISPLAY_BASE+0x14))
  31. #define portOdrHigh ((volatile unsigned char *)(PORT_DISPLAY_BASE+0x14+1))
  32.  
  33. #define B_E 0x40 // Enable
  34. #define B_RST 0x20 // Reset
  35. #define B_CS2 0x10 // Controller Select 2
  36. #define B_CS1 8 // Controller Select 1
  37. #define B_SELECT 4 // 0 Graphics, 1 ASCII
  38. #define B_RW 2 // 0 Write, 1 Read
  39. #define B_RS 1 // 0 Command, 1 Data
  40.  
  41. #define LCD_ON 0x3F // Display on
  42. #define LCD_OFF 0x3E // Display off
  43. #define LCD_SET_ADD 0x40 // Set horizontal coordinate
  44. #define LCD_SET_PAGE 0xB8 // Set vertical coordinate
  45. #define LCD_DISP_START 0xC0 // Start address
  46. #define LCD_BUSY 0x80 // Read busy status
  47.  
  48.  
  49. #define MAX_POINTS 20
  50.  
  51. void startup(void) __attribute__((naked)) __attribute__((section (".start_section")) );
  52.  
  53. void startup ( void )
  54. {
  55. __asm volatile(
  56. " LDR R0,=0x2001C000\n" /* set stack */
  57. " MOV SP,R0\n"
  58. " BL main\n" /* call main */
  59. "_exit: B .\n" /* never return */
  60. ) ;
  61. }
  62.  
  63. void app_init(void){
  64. * ((unsigned int *) 0x40021000) = 0x55555555;
  65.  
  66. }
  67.  
  68. void delay_250ns (void){
  69. /* SystemCoreClock = 168000000 */
  70. *STK_CTRL = 0;
  71. *STK_LOAD = ( (168/4) -1 );
  72. *STK_VAL = 0;
  73. *STK_CTRL = 5;
  74. while( (*STK_CTRL & 0x10000 )== 0 ){
  75. }
  76. *STK_CTRL = 0;
  77. }
  78.  
  79. void delay_500ns (void){
  80. delay_250ns();
  81. delay_250ns();
  82. }
  83.  
  84.  
  85. void delay_mikro (unsigned int us){
  86. while( us-- ){
  87. delay_250ns();
  88. delay_250ns();
  89. delay_250ns();
  90. delay_250ns();
  91. }
  92. }
  93.  
  94. void delay_milli( unsigned int ms ){
  95. #ifdef SIMULATOR
  96. while( ms-- )
  97. delay_mikro(1);
  98. #else
  99. while( ms-- )
  100. delay_mikro(1000);
  101. #endif
  102. }
  103.  
  104. void ascii_ctrl_bit_set( unsigned char x ){
  105. unsigned char c;
  106. c = *portOdrLow;
  107. c |= ( B_SELECT | x );
  108. *portOdrLow = c;
  109. }
  110.  
  111.  
  112. void ascii_ctrl_bit_clear( unsigned char x ){
  113. unsigned char c;
  114. c = *portOdrLow;
  115. c &= ( B_SELECT | ~x );
  116. *portOdrLow = c;
  117. }
  118.  
  119. typedef unsigned char uint8_t;
  120.  
  121. void graphic_ctrl_bit_set(uint8_t x){
  122.  
  123. uint8_t c;
  124. c = *portOdrLow;
  125. c &= ~B_SELECT;
  126. c |= (~B_SELECT & x);
  127. *portOdrLow = c;
  128.  
  129. }
  130.  
  131. void graphic_ctrl_bit_clear(uint8_t x){
  132.  
  133. uint8_t c;
  134. c = *portOdrLow;
  135. c &= ~B_SELECT;
  136. c &= ~x;
  137. *portOdrLow = c;
  138. }
  139.  
  140. static void select_controller(uint8_t controller){
  141. switch(controller){
  142. case 0:
  143. graphic_ctrl_bit_clear(B_CS1|B_CS2);
  144. break;
  145.  
  146. case B_CS1 :
  147. graphic_ctrl_bit_set(B_CS1);
  148. graphic_ctrl_bit_clear(B_CS2);
  149. break;
  150.  
  151. case B_CS2 :
  152. graphic_ctrl_bit_set(B_CS2);
  153. graphic_ctrl_bit_clear(B_CS1);
  154. break;
  155.  
  156. case B_CS1|B_CS2 :
  157. graphic_ctrl_bit_set(B_CS1|B_CS2);
  158. break;
  159. }
  160. }
  161.  
  162. static void graphic_wait_ready(void) {
  163. uint8_t c;
  164. graphic_ctrl_bit_clear(B_E);
  165. *portModer = 0x00005555; // 15-8 inputs, 7-0 outputs
  166. graphic_ctrl_bit_clear(B_RS);
  167. graphic_ctrl_bit_set(B_RW);
  168. delay_500ns();
  169.  
  170. while(1) {
  171. graphic_ctrl_bit_set(B_E);
  172. delay_500ns();
  173. c = *portIdrHigh & LCD_BUSY;
  174. graphic_ctrl_bit_clear(B_E);
  175. delay_500ns();
  176. if( c == 0 ) break;
  177. }
  178. *portModer = 0x55555555; // 15-0 outputs
  179. }
  180.  
  181. static uint8_t graphic_read(uint8_t controller) {
  182. uint8_t c;
  183. graphic_ctrl_bit_clear(B_E);
  184. *portModer = 0x00005555; // 15-8 inputs, 7-0 outputs
  185. graphic_ctrl_bit_set(B_RS|B_RW);
  186. select_controller(controller);
  187. delay_500ns();
  188. graphic_ctrl_bit_set(B_E);
  189. delay_500ns();
  190. c = *portIdrHigh;
  191. graphic_ctrl_bit_clear(B_E);
  192. *portModer = 0x55555555; // 15-0 outputs
  193.  
  194. if( controller & B_CS1 ) {
  195. select_controller(B_CS1);
  196. graphic_wait_ready();
  197. }
  198.  
  199. if( controller & B_CS2 ) {
  200. select_controller(B_CS2);
  201. graphic_wait_ready();
  202. }
  203. return c;
  204. }
  205.  
  206. static void graphic_write(uint8_t value, uint8_t controller){
  207. *portOdrHigh = value;
  208. delay_500ns();
  209. graphic_ctrl_bit_set(B_E);
  210. delay_500ns();
  211. graphic_ctrl_bit_clear(B_E);
  212.  
  213. if( controller & B_CS1 ) {
  214. select_controller(B_CS1);
  215. graphic_wait_ready();
  216. }
  217. if( controller & B_CS2 ) {
  218. select_controller(B_CS2);
  219. graphic_wait_ready();
  220. }
  221. *portOdrHigh = 0;
  222. graphic_ctrl_bit_set(B_E);
  223. select_controller(0);
  224. }
  225.  
  226. static void graphic_write_command(uint8_t command, uint8_t controller){
  227. graphic_ctrl_bit_set(B_E);
  228. select_controller(controller);
  229. graphic_ctrl_bit_clear(B_RS);
  230. graphic_ctrl_bit_clear(B_RW);
  231. graphic_write(command, controller);
  232. }
  233.  
  234. static void graphic_write_data(uint8_t data, uint8_t controller){
  235. graphic_ctrl_bit_clear(B_E);
  236. select_controller(controller);
  237. graphic_ctrl_bit_set(B_RS);
  238. graphic_ctrl_bit_clear(B_RW);
  239. graphic_write(data, controller);
  240. }
  241.  
  242. void graphic_initialize(void){
  243. graphic_ctrl_bit_set(B_E);
  244. delay_mikro(10);
  245. graphic_ctrl_bit_clear(B_CS1);
  246. graphic_ctrl_bit_clear(B_CS2);
  247. graphic_ctrl_bit_clear(B_RST);
  248. graphic_ctrl_bit_clear(B_E);
  249. delay_mikro(30);
  250. graphic_ctrl_bit_set(B_RST);
  251. graphic_write_command(LCD_OFF,B_CS1|B_CS2);
  252. graphic_write_command(LCD_ON,B_CS1|B_CS2);
  253. graphic_write_command(LCD_DISP_START,B_CS1|B_CS2);
  254. graphic_write_command(LCD_SET_ADD,B_CS1|B_CS2);
  255. graphic_write_command(LCD_SET_PAGE,B_CS1|B_CS2);
  256. select_controller(0);
  257. }
  258. void graphic_clear_screen(void) {
  259. uint8_t i, j;
  260.  
  261. for(j = 0; j < 8; j++) {
  262. graphic_write_command(LCD_SET_PAGE | j, B_CS1|B_CS2);
  263. graphic_write_command(LCD_SET_ADD | 0, B_CS1|B_CS2);
  264. for(i = 0; i <= 63; i++){
  265. graphic_write_data(0, B_CS1|B_CS2);
  266. }
  267. }
  268. }
  269.  
  270. unsigned char graphic_read_data(unsigned char controller) {
  271. (void) graphic_read(controller);
  272. return graphic_read(controller);
  273. }
  274.  
  275. void pixel(int x, int y, int set) {
  276. uint8_t mask, c, controller;
  277. int index;
  278. if((x < 1) || (y < 1) || (x > 128) || (y > 64)) return;
  279. index = (y-1)/8;
  280. switch( (y-1)%8 ) {
  281. case 0: mask = 1; break;
  282. case 1: mask = 2; break;
  283. case 2: mask = 4; break;
  284. case 3: mask = 8; break;
  285. case 4: mask = 0x10; break;
  286. case 5: mask = 0x20; break;
  287. case 6: mask = 0x40; break;
  288. case 7: mask = 0x80; break;
  289. }
  290. if(set == 0)
  291. mask = ~mask;
  292. if(x > 64){
  293. controller = B_CS2;
  294. x = x - 65;
  295. }
  296. else {
  297. controller = B_CS1;
  298. x = x-1;
  299. }
  300. graphic_write_command(LCD_SET_ADD | x, controller );
  301. graphic_write_command(LCD_SET_PAGE | index, controller );
  302. c = graphic_read_data(controller);
  303. graphic_write_command(LCD_SET_ADD | x, controller);
  304. if(set)
  305. mask = mask | c;
  306. else
  307. mask = mask & c;
  308. graphic_write_data(mask, controller);
  309. }
  310.  
  311.  
  312. //5.14
  313. /*
  314. void main(void){
  315. app_init();
  316. graphic_initialize();
  317. #ifdef SIMULATOR
  318. graphic_clear_screen();
  319. #endif
  320. graphic_write_command (LCD_SET_ADD | 10, B_CS1 | B_CS2);
  321. graphic_write_command (LCD_SET_PAGE | 1, B_CS1 | B_CS2);
  322. graphic_write_data (0xFF, B_CS1 | B_CS2);
  323. }*/
  324.  
  325. //5.15
  326. /*void main(void){
  327. unsigned i;
  328. app_init();
  329. graphic_initialize();
  330. #ifdef SIMULATOR
  331. graphic_clear_screen();
  332. #endif
  333. for(i = 0; i<128;i++){
  334. pixel(i,10,1);
  335. }
  336. for(i=0;i<64;i++){
  337. pixel(10,i,1);
  338. }
  339. delay_milli(500);
  340. for(i = 0; i<128;i++){
  341. pixel(i,10,0);
  342. }
  343. for(i=0;i<64;i++){
  344. pixel(10,i,0);
  345. }
  346. }*/
  347.  
  348.  
  349. typedef struct tPoint{
  350. unsigned char x;
  351. unsigned char y;
  352. }POINT;
  353.  
  354. typedef struct tGeometry{
  355. int numpoints;
  356. int sizex;
  357. int sizey;
  358. POINT px[MAX_POINTS];
  359. } GEOMETRY, *PGEOMETRY;
  360.  
  361. GEOMETRY ball_geometry ={
  362. 12, /*numpoints*/
  363. 4,4, /*sizex,sizey*/
  364. {
  365. /*px[0,1,2...]*/
  366. {0,1},{0,2},{1,0},{1,1},{1,2},
  367. {1,3},{2,0},{2,1},{2,2},{2,3},
  368. {3,1},
  369. {3,2},
  370. }
  371. };
  372.  
  373. typedef struct tObj{
  374. PGEOMETRY geo;
  375. int dirx, diry;
  376. int posx, posy;
  377. void(*draw) (struct tObj *);
  378. void(*clear)(struct tObj *);
  379. void(*move) (struct tObj *);
  380. void(*set_speed) (struct tObj *, int, int);
  381. }OBJECT,*POBJECT;
  382.  
  383.  
  384. void set_object_speed(POBJECT o, int speedx, int speedy)
  385. {
  386. o -> dirx = speedx;
  387. o -> diry = speedy;
  388. }
  389.  
  390. void draw_object(POBJECT o){
  391. for ( int i = 0; i < (o -> geo -> numpoints); i++)
  392. pixel(o -> posx + (o -> geo -> px[i].x), o -> posy + (o -> geo -> px[i].y), 1);
  393. }
  394.  
  395.  
  396. void clear_object(POBJECT o){
  397. for ( int i = 0; i < (o -> geo -> numpoints); i++)
  398. pixel(o -> posx + (o -> geo -> px[i].x), o -> posy + (o -> geo -> px[i].y), 0);
  399. }
  400.  
  401.  
  402. void move_object(POBJECT o){
  403. clear_object(o);
  404.  
  405. o -> posx += o -> dirx;
  406. o -> posy += o -> diry;
  407.  
  408. if (o -> posx < 1)
  409. {
  410. o -> dirx = -o -> dirx;
  411. }
  412. else if (o -> posx > 128)
  413. {
  414. o -> dirx = -o -> dirx;
  415. }
  416. else if (o -> diry < 1)
  417. {
  418. o -> diry = -o -> diry;
  419. }
  420. else if (o -> diry > 64)
  421. {
  422. o -> diry = -o -> diry;
  423. }
  424.  
  425. draw_object(o);
  426. }
  427.  
  428. static OBJECT ball = {
  429. &ball_geometry,
  430. 0,0,
  431. 1,1,
  432. draw_object,
  433. clear_object,
  434. move_object,
  435. set_object_speed
  436. };
  437.  
  438. int main(int argc, char **argv){
  439. POBJECT p = &ball;
  440. app_init();
  441. graphic_initialize();
  442. #ifndef SIMULATOR
  443. graphic_clear_screen();
  444. #endif
  445. p->set_speed(p,4,1);
  446. while(1){
  447. p->move(p);
  448. delay_milli(40);
  449. }
  450.  
  451. }
  452.  
  453.  
  454. /*void ascii_write_controller(unsigned char byte){
  455. ascii_ctrl_bit_set(B_E);
  456. *portOdrHigh = byte;
  457. delay_250ns();
  458. ascii_ctrl_bit_clear(B_E);
  459. }
  460.  
  461. unsigned char ascii_read_controller(void){
  462. ascii_ctrl_bit_set(B_E);
  463. delay_250ns();
  464. delay_250ns();
  465. unsigned char rv = *portIdrHigh;
  466. ascii_ctrl_bit_clear(B_E);
  467. return rv;
  468. }
  469.  
  470. void ascii_write_cmd (unsigned char command){
  471. ascii_ctrl_bit_clear(B_RS);
  472. ascii_ctrl_bit_clear(B_RW);
  473. ascii_write_controller(command);
  474. }
  475.  
  476. void ascii_write_data (unsigned char data){
  477. ascii_ctrl_bit_set(B_RS);
  478. ascii_ctrl_bit_clear(B_RW);
  479. ascii_write_controller(data);
  480. }
  481.  
  482. unsigned char ascii_read_status (void){
  483. * ((unsigned int *) 0x40021000) = 0x00005555;
  484. ascii_ctrl_bit_clear(B_RS);
  485. ascii_ctrl_bit_set(B_RW);
  486. unsigned char rv = ascii_read_controller();
  487. * ((unsigned int *) 0x40021000) |= 0x55550000;
  488. return rv;
  489.  
  490. }
  491.  
  492. unsigned char ascii_read_data (void){
  493. * ((unsigned int *) 0x40021000) = 0x00005555;
  494. ascii_ctrl_bit_set(B_RS);
  495. ascii_ctrl_bit_set(B_RW);
  496. unsigned char rv = ascii_read_controller();
  497. * ((unsigned int *) 0x40021000) |= 0x55550000;
  498. return rv;
  499. }
  500.  
  501.  
  502.  
  503. void ascii_command(unsigned char command){
  504. while((ascii_read_status() & 0x80) == 0x80){}
  505. delay_mikro(8);
  506. ascii_write_cmd(command);
  507. delay_milli(2);
  508. }
  509.  
  510. void ascii_init(void){
  511. ascii_command(0x38);
  512. ascii_command(0x0C);
  513. ascii_command(0x01);
  514. ascii_command(0x06);
  515. }
  516.  
  517. void ascii_write_char(unsigned char c){
  518. while((ascii_read_status() & 0x80) == 0x80){}
  519. delay_mikro(8);
  520. ascii_write_data(c);
  521. delay_milli(2);
  522. }
  523.  
  524. void ascii_gotoxy( unsigned char x, unsigned char y) {
  525. unsigned char address;
  526. if(y!=1){
  527. address=0x40 | (x-1);
  528. }
  529. else{
  530. address=(x-1);
  531. }
  532. ascii_write_cmd( 0x80 | address);
  533. delay_mikro(45);
  534. }
  535.  
  536. int main(int argc, char **argv){
  537. char *s;
  538. char test1[] = "Alfanumerisk ";
  539. char test2[] = "Display - test";
  540.  
  541. app_init();
  542. ascii_init();
  543. ascii_gotoxy(1,1);
  544. s = test1;
  545. while(*s)
  546. ascii_write_char(*s++);
  547. ascii_gotoxy(1,2);
  548. s = test2;
  549. while(*s)
  550. ascii_write_char(*s++);
  551. return 0;
  552. }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement