AVR 光电管不适用于 Arduino UNO

AVR photocell not working with Arduino UNO

我正在按照我发现的一个教程进行操作,作为构建莫尔斯码 coder/decoder 的第一步,我找到了一个光电管来点亮 LED。我只有一个可用的 arduino UNO 而不是 ATmega328P 芯片本身可以使用。我已将光电管连接到 UNO 上的引出线 A0,将 LED 连接到引出线 D~9。我试图重写用于我当前设置的代码,但它出现了三个错误和四个警告,我不知道如何解决。任何帮助或建议将不胜感激。我正在使用 Atmel Studio 7 Gcc C.

#ifndef F_CPU
#define F_CPU 16000000UL
#endif

#include <avr/io.h>
#include <util/delay.h>

#define PHOTO1 0 //sets photocell to PORTC pinout A0
#define LED1 9 //sets LED to PORTB pinout 9



void init_ports_mcu()
{
    DDRB = 0xFFu;  //set all pins at PORTB as output
    PORTB = 0x00u; // sets pins at PORTB as low - LED off
    
    DDRC = 0xFFu; //sets all pins at PORTC as output
    DDRC &= ~(1<<0); //Makes first pin at PORTC input
    PORTC = 0x00u; //sets all pins at PORTC as low-turning off Photocell
}

int map(int x, int in_min, int in_max, int out_min, int out_max)
{
    return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

void ADC_init()
{
    //Enable ADC sampling freq to set prescaler to max value
    ADCSRA |= (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0);
    ADMUX = (1<<REFS0); //select required channel passed as input 
}

uint16_t get_lightLevel()
{
    _delay_ms 10; //wait for line in channel to get selected
    ADCSRA |= (1<<ADSC); //start ADC conversion
    
    while (ADCSRA & (1<<ADSC)); //wait for conversion to complete
    
    _delay_ms 10;
    return (ADC);
    
}



int main(void)
{
    init_ports_mcu(); //setup microcontroller i/o ports
    ADC_init(); //initialize ADC
    
    while (1)
    {
        switch(map(get_lightLevel(), 0, 1023, 0, 3)){ //read and map ADC values
            case 0: //high light intensifies - LED is off
                PORTB &= ~(1<<LED1);
                PORTC &= ~(1<<PHOTO1);
            break;
            case 1: //middle light intensifies - LED is on
                PORTB = (1<<LED1);
                PORTC &= ~(1<<PHOTO1);
            break;
            case 2: //low light intensifies - LED is on
                PORTB = (1<<LED1);
                PORTC = (1<<PHOTO1);
            break;
        
        }
    }
    return (0);
}

错误:

Recipe for target 'main.o' failed - Line 76
expected ';' before numeric constant - Line 44
expected ';' before numeric constant - Line 49

警告:

Statement with no effect [wunused values] - Line 44
Statement with no effect [wunused values] - Line 49
Larger integer implicitly turnicated to unsigned type [wover flow] - Line 69
Larger integer implicitly turnicated to unsigned type [wover flow] - Line 73

您的延迟格式不正确。根据util/delay.h(busy-wait延迟循环的便利函数)here,有两个函数可用:

void    _delay_ms (double __ms)
void    _delay_us (double __us)

你应该把这些当作函数调用,所以你需要这样写:

_delay_ms(10);