- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
#include <avr/interrupt.h>
#include <avr/io.h>
#include <stdio.h>
static void avr_init(void);
void uart_putchar(char value);
unsigned char read_adc(unsigned char adc_input);
unsigned char adc_enable;
ISR(SIG_UART_RECV)
{
unsigned char tmp;
tmp=UDR;
if (tmp=='1') adc_enable=1;
else adc_enable=0;
}
int main(void)
{
avr_init();
sei();
uart_putchar('A');
for(;;)
{
if(adc_enable)
{
uart_putchar(read_adc(0x00));
adc_enable = 0;
}
}
return(0);
}
static void avr_init(void)
{
PORTB=0b00000000;
DDRB=0b00001111;
PORTD=0b00111100;
DDRD=0b11000011;
// Baud Rate: 57600
// Character Size: 8-bit
// Mode: Asynchronous
// Parity: Disabled
// Stop Bit: 1-bit
// Multi-processor Communication
UBRRL = 0x0c;
UBRRH = 0x00;
UCSRA = 0x01;
UCSRC = 0x86;
UCSRB = 0xd8;
ADMUX=0x60;
ADCSRA=0xA6;
SFIOR&=0x0F;
SFIOR|=0x10;
return;
}
void uart_putchar(char value)
{
loop_until_bit_is_set(UCSRA, UDRE);
UDR = value;
return;
}
unsigned char read_adc(unsigned char adc_input)
{
ADMUX=adc_input|0x60;
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCH;
}
guest 19.04.2010 21:52 # −1.4
guest 19.04.2010 21:53 # +2.2
guest 19.04.2010 22:52 # +0.4
Кто хотя бы раз программировал для AVR, тот поймёт.