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