URL
https://opencores.org/ocsvn/cpu_lecture/cpu_lecture/trunk
Subversion Repositories cpu_lecture
[/] [cpu_lecture/] [trunk/] [html/] [32_Listing_of_hello.c.html] - Rev 22
Go to most recent revision | Compare with Previous | Blame | View Log
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <HTML> <HEAD> <TITLE>html/Listing_of_hello.c</TITLE> <META NAME="generator" CONTENT="HTML::TextToHTML v2.46"> <LINK REL="stylesheet" TYPE="text/css" HREF="lecture.css"> </HEAD> <BODY> <P><table class="ttop"><th class="tpre"><a href="31_Listing_of_Makefile.html">Previous Lesson</a></th><th class="ttop"><a href="toc.html">Table of Content</a></th><th class="tnxt"><a href="33_Listing_of_make_mem.cc.html">Next Lesson</a></th></table> <hr> <H1><A NAME="section_1">32 LISTING OF hello.c</A></H1> <pre class="vhdl"> 1 #include "stdint.h" 2 #include "avr/io.h" 3 #include "avr/pgmspace.h" 4 5 #undef F_CPU 6 #define F_CPU 25000000UL 7 #include "util/delay.h" 8 9 10 //----------------------------------------------------------------------// 11 // // 12 // print char cc on UART. // 13 // return number of chars printed (i.e. 1). // 14 // // 15 //----------------------------------------------------------------------// 16 uint8_t 17 uart_putc(uint8_t cc) 18 { 19 while ((UCSRA & (1 << UDRE)) == 0) ; 20 UDR = cc; 21 return 1; 22 } 23 24 //----------------------------------------------------------------------// 25 // // 26 // print char cc on 7 segment display. // 27 // return number of chars printed (i.e. 1). // 28 // // 29 //----------------------------------------------------------------------// 30 // The segments of the display are encoded like this: 31 // 32 // 33 // segment PORT B 34 // name Bit number 35 // ----A---- ----0---- 36 // | | | | 37 // F B 5 1 38 // | | | | 39 // ----G---- ----6---- 40 // | | | | 41 // E C 4 2 42 // | | | | 43 // ----D---- ----3---- 44 // 45 //----------------------------------------------------------------------------- 46 47 #define SEG7(G, F, E, D, C, B, A) (~(G<<6|F<<5|E<<4|D<<3|C<<2|B<<1|A)) 48 49 uint8_t 50 seg7_putc(uint8_t cc) 51 { 52 uint16_t t; 53 54 switch(cc) 55 { // G F E D C B A 56 case ' ': PORTB = SEG7(0,0,0,0,0,0,0); break; 57 case 'E': PORTB = SEG7(1,1,1,1,0,0,1); break; 58 case 'H': PORTB = SEG7(1,1,1,0,1,1,0); break; 59 case 'L': PORTB = SEG7(0,1,1,1,0,0,0); break; 60 case 'O': PORTB = SEG7(0,1,1,1,1,1,1); break; 61 default: PORTB = SEG7(1,0,0,1,0,0,1); break; 62 } 63 64 // wait 800 + 200 ms. This can be quite boring in simulations, 65 // so we wait only if DIP switch 6 is closed. 66 // 67 if (!(PINB & 0x20)) for (t = 0; t < 800; ++t) _delay_ms(1); 68 PORTB = SEG7(0,0,0,0,0,0,0); 69 if (!(PINB & 0x20)) for (t = 0; t < 200; ++t) _delay_ms(1); 70 71 return 1; 72 } 73 74 //----------------------------------------------------------------------// 75 // // 76 // print string s on UART. // 77 // return number of chars printed. // 78 // // 79 //----------------------------------------------------------------------// 80 uint16_t 81 uart_puts(const char * s) 82 { 83 const char * from = s; 84 uint8_t cc; 85 while ((cc = pgm_read_byte(s++))) uart_putc(cc); 86 return s - from - 1; 87 } 88 89 //----------------------------------------------------------------------// 90 // // 91 // print string s on 7 segment display. // 92 // return number of chars printed. // 93 // // 94 //----------------------------------------------------------------------// 95 uint16_t 96 seg7_puts(const char * s) 97 { 98 const char * from = s; 99 uint8_t cc; 100 while ((cc = pgm_read_byte(s++))) seg7_putc(cc); 101 return s - from - 1; 102 } 103 104 //----------------------------------------------------------------------------- 105 int 106 main(int argc, char * argv[]) 107 { 108 for (;;) 109 { 110 if (PINB & 0x40) // DIP switch 7 open. 111 { 112 // print 'Hello world' on UART. 113 uart_puts(PSTR("Hello, World!\r\n")); 114 } 115 else // DIP switch 7 closed. 116 { 117 // print 'HELLO' on 7-segment display 118 seg7_puts(PSTR("HELLO ")); 119 } 120 } 121 } 122 //----------------------------------------------------------------------------- <pre class="filename"> app/hello.c </pre></pre> <P> <P><hr><BR> <table class="ttop"><th class="tpre"><a href="31_Listing_of_Makefile.html">Previous Lesson</a></th><th class="ttop"><a href="toc.html">Table of Content</a></th><th class="tnxt"><a href="33_Listing_of_make_mem.cc.html">Next Lesson</a></th></table> </BODY> </HTML>
Go to most recent revision | Compare with Previous | Blame | View Log