1 | #include <mega88.h>
|
2 |
|
3 | //8MHz
|
4 | // Declare your global variables here
|
5 |
|
6 | #define DATA_REGISTER_EMPTY (1<<UDRE0)
|
7 | #define RX_COMPLETE (1<<RXC0)
|
8 | #define FRAMING_ERROR (1<<FE0)
|
9 | #define PARITY_ERROR (1<<UPE0)
|
10 | #define DATA_OVERRUN (1<<DOR0)
|
11 |
|
12 | // USART Receiver buffer
|
13 | #define RX_BUFFER_SIZE0 8
|
14 | char rx_buffer0[RX_BUFFER_SIZE0];
|
15 |
|
16 | #if RX_BUFFER_SIZE0 <= 256
|
17 | unsigned char rx_wr_index0=0,rx_rd_index0=0;
|
18 | #else
|
19 | unsigned int rx_wr_index0=0,rx_rd_index0=0;
|
20 | #endif
|
21 |
|
22 | #if RX_BUFFER_SIZE0 < 256
|
23 | unsigned char rx_counter0=0;
|
24 | #else
|
25 | unsigned int rx_counter0=0;
|
26 | #endif
|
27 |
|
28 | // This flag is set on USART Receiver buffer overflow
|
29 | bit rx_buffer_overflow0;
|
30 |
|
31 | // USART Receiver interrupt service routine
|
32 | interrupt [USART_RXC] void usart_rx_isr(void)
|
33 | {
|
34 | char status,data;
|
35 | status=UCSR0A;
|
36 | data=UDR0;
|
37 | if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
|
38 | {
|
39 | rx_buffer0[rx_wr_index0++]=data;
|
40 | #if RX_BUFFER_SIZE0 == 256
|
41 | // special case for receiver buffer size=256
|
42 | if (++rx_counter0 == 0) rx_buffer_overflow0=1;
|
43 | #else
|
44 | if (rx_wr_index0 == RX_BUFFER_SIZE0) rx_wr_index0=0;
|
45 | if (++rx_counter0 == RX_BUFFER_SIZE0)
|
46 | {
|
47 | rx_counter0=0;
|
48 | rx_buffer_overflow0=1;
|
49 | }
|
50 | #endif
|
51 | }
|
52 | }
|
53 |
|
54 | #ifndef _DEBUG_TERMINAL_IO_
|
55 | // Get a character from the USART Receiver buffer
|
56 | #define _ALTERNATE_GETCHAR_
|
57 | #pragma used+
|
58 | char getchar(void)
|
59 | {
|
60 | char data;
|
61 | while (rx_counter0==0);
|
62 | data=rx_buffer0[rx_rd_index0++];
|
63 | #if RX_BUFFER_SIZE0 != 256
|
64 | if (rx_rd_index0 == RX_BUFFER_SIZE0) rx_rd_index0=0;
|
65 | #endif
|
66 | #asm("cli")
|
67 | --rx_counter0;
|
68 | #asm("sei")
|
69 | return data;
|
70 | }
|
71 | #pragma used-
|
72 | #endif
|
73 |
|
74 | // Standard Input/Output functions
|
75 | #include <stdio.h>
|
76 |
|
77 | void main(void)
|
78 | {
|
79 | // Declare your local variables here
|
80 |
|
81 | // Crystal Oscillator division factor: 1
|
82 | #pragma optsize-
|
83 | CLKPR=(1<<CLKPCE);
|
84 | CLKPR=(0<<CLKPCE) | (0<<CLKPS3) | (0<<CLKPS2) | (0<<CLKPS1) | (0<<CLKPS0);
|
85 | #ifdef _OPTIMIZE_SIZE_
|
86 | #pragma optsize+
|
87 | #endif
|
88 |
|
89 | // Input/Output Ports initialization
|
90 | // Port B initialization
|
91 | // Function: Bit7=In Bit6=In Bit5=In Bit4=In Bit3=In Bit2=In Bit1=In Bit0=In
|
92 | DDRB=(0<<DDB7) | (0<<DDB6) | (0<<DDB5) | (0<<DDB4) | (0<<DDB3) | (0<<DDB2) | (0<<DDB1) | (0<<DDB0);
|
93 | // State: Bit7=T Bit6=T Bit5=T Bit4=T Bit3=T Bit2=T Bit1=T Bit0=T
|
94 | PORTB=(0<<PORTB7) | (0<<PORTB6) | (0<<PORTB5) | (0<<PORTB4) | (0<<PORTB3) | (0<<PORTB2) | (0<<PORTB1) | (0<<PORTB0);
|
95 |
|
96 | // Port C initialization
|
97 | // Function: Bit6=In Bit5=In Bit4=In Bit3=In Bit2=In Bit1=In Bit0=In
|
98 | DDRC=(0<<DDC6) | (0<<DDC5) | (0<<DDC4) | (0<<DDC3) | (0<<DDC2) | (0<<DDC1) | (0<<DDC0);
|
99 | // State: Bit6=T Bit5=T Bit4=T Bit3=T Bit2=T Bit1=T Bit0=T
|
100 | PORTC=(0<<PORTC6) | (0<<PORTC5) | (0<<PORTC4) | (0<<PORTC3) | (0<<PORTC2) | (0<<PORTC1) | (0<<PORTC0);
|
101 |
|
102 | // Port D initialization
|
103 | // Function: Bit7=In Bit6=In Bit5=In Bit4=In Bit3=In Bit2=In Bit1=In Bit0=In
|
104 | DDRD=(0<<DDD7) | (0<<DDD6) | (0<<DDD5) | (0<<DDD4) | (0<<DDD3) | (0<<DDD2) | (0<<DDD1) | (0<<DDD0);
|
105 | // State: Bit7=T Bit6=T Bit5=T Bit4=T Bit3=T Bit2=T Bit1=T Bit0=T
|
106 | PORTD=(0<<PORTD7) | (0<<PORTD6) | (0<<PORTD5) | (0<<PORTD4) | (0<<PORTD3) | (0<<PORTD2) | (0<<PORTD1) | (0<<PORTD0);
|
107 |
|
108 | // USART initialization
|
109 | // Communication Parameters: 8 Data, 1 Stop, No Parity
|
110 | // USART Receiver: On
|
111 | // USART Transmitter: Off
|
112 | // USART0 Mode: Asynchronous
|
113 | // USART Baud Rate: 9600
|
114 | UCSR0A=(0<<RXC0) | (0<<TXC0) | (0<<UDRE0) | (0<<FE0) | (0<<DOR0) | (0<<UPE0) | (0<<U2X0) | (0<<MPCM0);
|
115 | UCSR0B=(1<<RXCIE0) | (0<<TXCIE0) | (0<<UDRIE0) | (1<<RXEN0) | (0<<TXEN0) | (0<<UCSZ02) | (0<<RXB80) | (0<<TXB80);
|
116 | UCSR0C=(0<<UMSEL01) | (0<<UMSEL00) | (0<<UPM01) | (0<<UPM00) | (0<<USBS0) | (1<<UCSZ01) | (1<<UCSZ00) | (0<<UCPOL0);
|
117 | UBRR0H=0x00;
|
118 | UBRR0L=0x33;
|
119 |
|
120 | // Global enable interrupts
|
121 | #asm("sei")
|
122 |
|
123 | while (1)
|
124 | {
|
125 | // Place your code here
|
126 |
|
127 | }
|
128 | }
|