Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define SIMULATOR
- /*
- * startup.c
- *
- */
- #include <stdio.h>
- #define STK_CTRL ((volatile unsigned int *)(0xE000E010))
- #define STK_LOAD ((volatile unsigned int *)(0xE000E014))
- // Bargraph
- #define STK_VAL ((volatile unsigned int *)(0xE000E018))
- /*#define PORT_BARGRAPH_BASE 0x40021000 //MD407 port E
- #define portBargraphModer ((volatile unsigned int *) (PORT_BARGRAPH_BASE))
- #define portBargraphOtyper ((volatile unsigned short *) (PORT_BARGRAPH_BASE+0x4))
- #define portBargraphOspeedr ((volatile unsigned int *) (PORT_BARGRAPH_BASE+0x8))
- #define portBargraphOdrLow ((volatile unsigned char *) (PORT_BARGRAPH_BASE+0x14))
- */
- // Display
- #define PORT_DISPLAY_BASE 0x40021000 //MD407 port E
- #define portModer ((volatile unsigned int *) (PORT_DISPLAY_BASE))
- #define portOtyper ((volatile unsigned short *)(PORT_DISPLAY_BASE+0x4))
- #define portOspeedr ((volatile unsigned int *) (PORT_DISPLAY_BASE+0x8))
- #define portPupdr ((volatile unsigned int *)(PORT_DISPLAY_BASE+0xC))
- #define portIdr ((volatile unsigned short *)(PORT_DISPLAY_BASE+0x10))
- #define portIdrLow ((volatile unsigned char *)(PORT_DISPLAY_BASE+0x10))
- #define portIdrHigh ((volatile unsigned char *)(PORT_DISPLAY_BASE+0x10+1))
- #define portOdrLow ((volatile unsigned char *)(PORT_DISPLAY_BASE+0x14))
- #define portOdrHigh ((volatile unsigned char *)(PORT_DISPLAY_BASE+0x14+1))
- #define B_E 0x40 // Enable
- #define B_RST 0x20 // Reset
- #define B_CS2 0x10 // Controller Select 2
- #define B_CS1 8 // Controller Select 1
- #define B_SELECT 4 // 0 Graphics, 1 ASCII
- #define B_RW 2 // 0 Write, 1 Read
- #define B_RS 1 // 0 Command, 1 Data
- #define LCD_ON 0x3F // Display on
- #define LCD_OFF 0x3E // Display off
- #define LCD_SET_ADD 0x40 // Set horizontal coordinate
- #define LCD_SET_PAGE 0xB8 // Set vertical coordinate
- #define LCD_DISP_START 0xC0 // Start address
- #define LCD_BUSY 0x80 // Read busy status
- #define MAX_POINTS 20
- 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 */
- "_exit: B .\n" /* never return */
- ) ;
- }
- void app_init(void){
- * ((unsigned int *) 0x40021000) = 0x55555555;
- }
- void delay_250ns (void){
- /* SystemCoreClock = 168000000 */
- *STK_CTRL = 0;
- *STK_LOAD = ( (168/4) -1 );
- *STK_VAL = 0;
- *STK_CTRL = 5;
- while( (*STK_CTRL & 0x10000 )== 0 ){
- }
- *STK_CTRL = 0;
- }
- void delay_500ns (void){
- delay_250ns();
- delay_250ns();
- }
- void delay_mikro (unsigned int us){
- while( us-- ){
- delay_250ns();
- delay_250ns();
- delay_250ns();
- delay_250ns();
- }
- }
- void delay_milli( unsigned int ms ){
- #ifdef SIMULATOR
- while( ms-- )
- delay_mikro(1);
- #else
- while( ms-- )
- delay_mikro(1000);
- #endif
- }
- void ascii_ctrl_bit_set( unsigned char x ){
- unsigned char c;
- c = *portOdrLow;
- c |= ( B_SELECT | x );
- *portOdrLow = c;
- }
- void ascii_ctrl_bit_clear( unsigned char x ){
- unsigned char c;
- c = *portOdrLow;
- c &= ( B_SELECT | ~x );
- *portOdrLow = c;
- }
- typedef unsigned char uint8_t;
- void graphic_ctrl_bit_set(uint8_t x){
- uint8_t c;
- c = *portOdrLow;
- c &= ~B_SELECT;
- c |= (~B_SELECT & x);
- *portOdrLow = c;
- }
- void graphic_ctrl_bit_clear(uint8_t x){
- uint8_t c;
- c = *portOdrLow;
- c &= ~B_SELECT;
- c &= ~x;
- *portOdrLow = c;
- }
- static void select_controller(uint8_t controller){
- switch(controller){
- case 0:
- graphic_ctrl_bit_clear(B_CS1|B_CS2);
- break;
- case B_CS1 :
- graphic_ctrl_bit_set(B_CS1);
- graphic_ctrl_bit_clear(B_CS2);
- break;
- case B_CS2 :
- graphic_ctrl_bit_set(B_CS2);
- graphic_ctrl_bit_clear(B_CS1);
- break;
- case B_CS1|B_CS2 :
- graphic_ctrl_bit_set(B_CS1|B_CS2);
- break;
- }
- }
- static void graphic_wait_ready(void) {
- uint8_t c;
- graphic_ctrl_bit_clear(B_E);
- *portModer = 0x00005555; // 15-8 inputs, 7-0 outputs
- graphic_ctrl_bit_clear(B_RS);
- graphic_ctrl_bit_set(B_RW);
- delay_500ns();
- while(1) {
- graphic_ctrl_bit_set(B_E);
- delay_500ns();
- c = *portIdrHigh & LCD_BUSY;
- graphic_ctrl_bit_clear(B_E);
- delay_500ns();
- if( c == 0 ) break;
- }
- *portModer = 0x55555555; // 15-0 outputs
- }
- static uint8_t graphic_read(uint8_t controller) {
- uint8_t c;
- graphic_ctrl_bit_clear(B_E);
- *portModer = 0x00005555; // 15-8 inputs, 7-0 outputs
- graphic_ctrl_bit_set(B_RS|B_RW);
- select_controller(controller);
- delay_500ns();
- graphic_ctrl_bit_set(B_E);
- delay_500ns();
- c = *portIdrHigh;
- graphic_ctrl_bit_clear(B_E);
- *portModer = 0x55555555; // 15-0 outputs
- if( controller & B_CS1 ) {
- select_controller(B_CS1);
- graphic_wait_ready();
- }
- if( controller & B_CS2 ) {
- select_controller(B_CS2);
- graphic_wait_ready();
- }
- return c;
- }
- static void graphic_write(uint8_t value, uint8_t controller){
- *portOdrHigh = value;
- delay_500ns();
- graphic_ctrl_bit_set(B_E);
- delay_500ns();
- graphic_ctrl_bit_clear(B_E);
- if( controller & B_CS1 ) {
- select_controller(B_CS1);
- graphic_wait_ready();
- }
- if( controller & B_CS2 ) {
- select_controller(B_CS2);
- graphic_wait_ready();
- }
- *portOdrHigh = 0;
- graphic_ctrl_bit_set(B_E);
- select_controller(0);
- }
- static void graphic_write_command(uint8_t command, uint8_t controller){
- graphic_ctrl_bit_set(B_E);
- select_controller(controller);
- graphic_ctrl_bit_clear(B_RS);
- graphic_ctrl_bit_clear(B_RW);
- graphic_write(command, controller);
- }
- static void graphic_write_data(uint8_t data, uint8_t controller){
- graphic_ctrl_bit_clear(B_E);
- select_controller(controller);
- graphic_ctrl_bit_set(B_RS);
- graphic_ctrl_bit_clear(B_RW);
- graphic_write(data, controller);
- }
- void graphic_initialize(void){
- graphic_ctrl_bit_set(B_E);
- delay_mikro(10);
- graphic_ctrl_bit_clear(B_CS1);
- graphic_ctrl_bit_clear(B_CS2);
- graphic_ctrl_bit_clear(B_RST);
- graphic_ctrl_bit_clear(B_E);
- delay_mikro(30);
- graphic_ctrl_bit_set(B_RST);
- graphic_write_command(LCD_OFF,B_CS1|B_CS2);
- graphic_write_command(LCD_ON,B_CS1|B_CS2);
- graphic_write_command(LCD_DISP_START,B_CS1|B_CS2);
- graphic_write_command(LCD_SET_ADD,B_CS1|B_CS2);
- graphic_write_command(LCD_SET_PAGE,B_CS1|B_CS2);
- select_controller(0);
- }
- void graphic_clear_screen(void) {
- uint8_t i, j;
- for(j = 0; j < 8; j++) {
- graphic_write_command(LCD_SET_PAGE | j, B_CS1|B_CS2);
- graphic_write_command(LCD_SET_ADD | 0, B_CS1|B_CS2);
- for(i = 0; i <= 63; i++){
- graphic_write_data(0, B_CS1|B_CS2);
- }
- }
- }
- unsigned char graphic_read_data(unsigned char controller) {
- (void) graphic_read(controller);
- return graphic_read(controller);
- }
- void pixel(int x, int y, int set) {
- uint8_t mask, c, controller;
- int index;
- if((x < 1) || (y < 1) || (x > 128) || (y > 64)) return;
- index = (y-1)/8;
- switch( (y-1)%8 ) {
- case 0: mask = 1; break;
- case 1: mask = 2; break;
- case 2: mask = 4; break;
- case 3: mask = 8; break;
- case 4: mask = 0x10; break;
- case 5: mask = 0x20; break;
- case 6: mask = 0x40; break;
- case 7: mask = 0x80; break;
- }
- if(set == 0)
- mask = ~mask;
- if(x > 64){
- controller = B_CS2;
- x = x - 65;
- }
- else {
- controller = B_CS1;
- x = x-1;
- }
- graphic_write_command(LCD_SET_ADD | x, controller );
- graphic_write_command(LCD_SET_PAGE | index, controller );
- c = graphic_read_data(controller);
- graphic_write_command(LCD_SET_ADD | x, controller);
- if(set)
- mask = mask | c;
- else
- mask = mask & c;
- graphic_write_data(mask, controller);
- }
- //5.14
- /*
- void main(void){
- app_init();
- graphic_initialize();
- #ifdef SIMULATOR
- graphic_clear_screen();
- #endif
- graphic_write_command (LCD_SET_ADD | 10, B_CS1 | B_CS2);
- graphic_write_command (LCD_SET_PAGE | 1, B_CS1 | B_CS2);
- graphic_write_data (0xFF, B_CS1 | B_CS2);
- }*/
- //5.15
- /*void main(void){
- unsigned i;
- app_init();
- graphic_initialize();
- #ifdef SIMULATOR
- graphic_clear_screen();
- #endif
- for(i = 0; i<128;i++){
- pixel(i,10,1);
- }
- for(i=0;i<64;i++){
- pixel(10,i,1);
- }
- delay_milli(500);
- for(i = 0; i<128;i++){
- pixel(i,10,0);
- }
- for(i=0;i<64;i++){
- pixel(10,i,0);
- }
- }*/
- typedef struct tPoint{
- unsigned char x;
- unsigned char y;
- }POINT;
- typedef struct tGeometry{
- int numpoints;
- int sizex;
- int sizey;
- POINT px[MAX_POINTS];
- } GEOMETRY, *PGEOMETRY;
- GEOMETRY ball_geometry ={
- 12, /*numpoints*/
- 4,4, /*sizex,sizey*/
- {
- /*px[0,1,2...]*/
- {0,1},{0,2},{1,0},{1,1},{1,2},
- {1,3},{2,0},{2,1},{2,2},{2,3},
- {3,1},
- {3,2},
- }
- };
- typedef struct tObj{
- PGEOMETRY geo;
- int dirx, diry;
- int posx, posy;
- void(*draw) (struct tObj *);
- void(*clear)(struct tObj *);
- void(*move) (struct tObj *);
- void(*set_speed) (struct tObj *, int, int);
- }OBJECT,*POBJECT;
- void set_object_speed(POBJECT o, int speedx, int speedy)
- {
- o -> dirx = speedx;
- o -> diry = speedy;
- }
- void draw_object(POBJECT o){
- for ( int i = 0; i < (o -> geo -> numpoints); i++)
- pixel(o -> posx + (o -> geo -> px[i].x), o -> posy + (o -> geo -> px[i].y), 1);
- }
- void clear_object(POBJECT o){
- for ( int i = 0; i < (o -> geo -> numpoints); i++)
- pixel(o -> posx + (o -> geo -> px[i].x), o -> posy + (o -> geo -> px[i].y), 0);
- }
- void move_object(POBJECT o){
- clear_object(o);
- o -> posx += o -> dirx;
- o -> posy += o -> diry;
- if (o -> posx < 1)
- {
- o -> dirx = -o -> dirx;
- }
- else if (o -> posx > 128)
- {
- o -> dirx = -o -> dirx;
- }
- else if (o -> diry < 1)
- {
- o -> diry = -o -> diry;
- }
- else if (o -> diry > 64)
- {
- o -> diry = -o -> diry;
- }
- draw_object(o);
- }
- static OBJECT ball = {
- &ball_geometry,
- 0,0,
- 1,1,
- draw_object,
- clear_object,
- move_object,
- set_object_speed
- };
- int main(int argc, char **argv){
- POBJECT p = &ball;
- app_init();
- graphic_initialize();
- #ifndef SIMULATOR
- graphic_clear_screen();
- #endif
- p->set_speed(p,4,1);
- while(1){
- p->move(p);
- delay_milli(40);
- }
- }
- /*void ascii_write_controller(unsigned char byte){
- ascii_ctrl_bit_set(B_E);
- *portOdrHigh = byte;
- delay_250ns();
- ascii_ctrl_bit_clear(B_E);
- }
- unsigned char ascii_read_controller(void){
- ascii_ctrl_bit_set(B_E);
- delay_250ns();
- delay_250ns();
- unsigned char rv = *portIdrHigh;
- ascii_ctrl_bit_clear(B_E);
- return rv;
- }
- 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 int *) 0x40021000) = 0x00005555;
- ascii_ctrl_bit_clear(B_RS);
- ascii_ctrl_bit_set(B_RW);
- unsigned char rv = ascii_read_controller();
- * ((unsigned int *) 0x40021000) |= 0x55550000;
- return rv;
- }
- unsigned char ascii_read_data (void){
- * ((unsigned int *) 0x40021000) = 0x00005555;
- ascii_ctrl_bit_set(B_RS);
- ascii_ctrl_bit_set(B_RW);
- unsigned char rv = ascii_read_controller();
- * ((unsigned int *) 0x40021000) |= 0x55550000;
- return rv;
- }
- void ascii_command(unsigned char command){
- while((ascii_read_status() & 0x80) == 0x80){}
- delay_mikro(8);
- ascii_write_cmd(command);
- delay_milli(2);
- }
- void ascii_init(void){
- ascii_command(0x38);
- ascii_command(0x0C);
- ascii_command(0x01);
- ascii_command(0x06);
- }
- void ascii_write_char(unsigned char c){
- while((ascii_read_status() & 0x80) == 0x80){}
- delay_mikro(8);
- ascii_write_data(c);
- delay_milli(2);
- }
- void ascii_gotoxy( unsigned char x, unsigned char y) {
- unsigned char address;
- if(y!=1){
- address=0x40 | (x-1);
- }
- else{
- address=(x-1);
- }
- ascii_write_cmd( 0x80 | address);
- delay_mikro(45);
- }
- int main(int argc, char **argv){
- char *s;
- char test1[] = "Alfanumerisk ";
- 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