dibawah ini adalah contoh sourcecode dari komunikasi serial avr :
001 | /***************************************************** |
002 | This program was produced by the |
003 | CodeWizardAVR V1.25.3 Professional |
004 | Automatic Program Generator |
005 | © Copyright 1998-2007 Pavel Haiduc, HP InfoTech s.r.l. |
006 |
008 |
009 | Project : |
010 | Version : |
011 | Date : 2/25/2009 |
012 | Author : GreenBlack |
013 | Company : WEIP |
014 | Comments: |
015 |
016 | Chip type : ATmega8535 |
017 | Program type : Application |
018 | Clock frequency : 11.059200 MHz |
019 | Memory model : Small |
020 | External SRAM size : 0 |
021 | Data Stack size : 256 |
022 | *****************************************************/ |
023 |
024 | #include <mega8535.h> |
025 | #include <delay.h> |
026 |
027 | // Standard Input/Output functions |
028 | #include <stdio.h> |
029 |
030 | #define ADC_VREF_TYPE 0x00 |
031 |
032 | // Read the AD conversion result |
033 | unsigned int read_adc(unsigned char adc_input) |
034 | { |
035 | ADMUX=adc_input | (ADC_VREF_TYPE & 0xff); |
036 | //delay untuk kestabilan adc(optional) |
037 | delay_us(10); |
038 | // Start the AD conversion |
039 | ADCSRA|=0x40; |
040 | // Wait for the AD conversion to complete |
041 | while ((ADCSRA & 0x10)==0); |
042 | ADCSRA|=0x10; |
043 | return ADCW; |
044 | } |
045 |
046 | // Declare your global variables here |
047 |
048 | void main( void ) |
049 | { |
050 | // Declare your local variables here |
051 | unsigned int dataadc; |
052 | float vin; |
053 |
054 | // Input/Output Ports initialization |
055 | // Port A initialization |
056 | // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In |
057 | // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T |
058 | PORTA=0x00; |
059 | DDRA=0x00; |
060 |
061 | // Port B initialization |
062 | // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In |
063 | // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T |
064 | PORTB=0x00; |
065 | DDRB=0x00; |
066 |
067 | // Port C initialization |
068 | // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In |
069 | // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T |
070 | PORTC=0x00; |
071 | DDRC=0x00; |
072 |
073 | // Port D initialization |
074 | // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In |
075 | // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T |
076 | PORTD=0x00; |
077 | DDRD=0x00; |
078 |
079 | // Timer/Counter 0 initialization |
080 | // Clock source: System Clock |
081 | // Clock value: Timer 0 Stopped |
082 | // Mode: Normal top=FFh |
083 | // OC0 output: Disconnected |
084 | TCCR0=0x00; |
085 | TCNT0=0x00; |
086 | OCR0=0x00; |
087 |
088 | // Timer/Counter 1 initialization |
089 | // Clock source: System Clock |
090 | // Clock value: Timer 1 Stopped |
091 | // Mode: Normal top=FFFFh |
092 | // OC1A output: Discon. |
093 | // OC1B output: Discon. |
094 | // Noise Canceler: Off |
095 | // Input Capture on Falling Edge |
096 | // Timer 1 Overflow Interrupt: Off |
097 | // Input Capture Interrupt: Off |
098 | // Compare A Match Interrupt: Off |
099 | // Compare B Match Interrupt: Off |
100 | TCCR1A=0x00; |
101 | TCCR1B=0x00; |
102 | TCNT1H=0x00; |
103 | TCNT1L=0x00; |
104 | ICR1H=0x00; |
105 | ICR1L=0x00; |
106 | OCR1AH=0x00; |
107 | OCR1AL=0x00; |
108 | OCR1BH=0x00; |
109 | OCR1BL=0x00; |
110 |
111 | // Timer/Counter 2 initialization |
112 | // Clock source: System Clock |
113 | // Clock value: Timer 2 Stopped |
114 | // Mode: Normal top=FFh |
115 | // OC2 output: Disconnected |
116 | ASSR=0x00; |
117 | TCCR2=0x00; |
118 | TCNT2=0x00; |
119 | OCR2=0x00; |
120 |
121 | // External Interrupt(s) initialization |
122 | // INT0: Off |
123 | // INT1: Off |
124 | // INT2: Off |
125 | MCUCR=0x00; |
126 | MCUCSR=0x00; |
127 |
128 | // Timer(s)/Counter(s) Interrupt(s) initialization |
129 | TIMSK=0x00; |
130 |
131 | // USART initialization |
132 | // Communication Parameters: 8 Data, 1 Stop, No Parity |
133 | // USART Receiver: On |
134 | // USART Transmitter: On |
135 | // USART Mode: Asynchronous |
136 | // USART Baud rate: 9600 |
137 | UCSRA=0x00; |
138 | UCSRB=0x18; |
139 | UCSRC=0x86; |
140 | UBRRH=0x00; |
141 | UBRRL=0x47; |
142 |
143 | // Analog Comparator initialization |
144 | // Analog Comparator: Off |
145 | // Analog Comparator Input Capture by Timer/Counter 1: Off |
146 | ACSR=0x80; |
147 | SFIOR=0x00; |
148 |
149 | // ADC initialization |
150 | // ADC Clock frequency: 750.000 kHz |
151 | // ADC Voltage Reference: AREF pin |
152 | // ADC Auto Trigger Source: None |
153 | ADMUX=ADC_VREF_TYPE & 0xff; |
154 | ADCSRA=0x84; |
155 |
156 | while (1) |
157 | { |
158 | // Place your code here |
159 | dataadc = read_adc(0); // input pada pin ADC.0 atau PORTA.0 |
160 | vin = (( float )dataadc*5/1024); // --> 5/1024 adalah Aref/2^10 --> 10 adalah jumlah bit adc |
161 | printf ( "%0.2f" ,vin); |
162 | delay_ms(500); |
163 | }; |
164 | } |
Link untuk download Full Sourcecode : disini
0 comments:
Posting Komentar