[英]AVR photocell not working with Arduino UNO
I was following a tutorial I found to get a photocell to light an LED as a first step to building a morse coder/decoder.我正在学习一个教程,我发现让光电管点亮 LED 作为构建莫尔斯编码器/解码器的第一步。 I only have an arduino UNO available instead of the ATmega328P chip itself to work with.
我只有一个 arduino UNO 可用,而不是 ATmega328P 芯片本身可以使用。 I have connected the photocell to pinout A0 and the LED to pinout D~9 on the UNO.
我已将光电管连接到引脚 A0,将 LED 连接到 UNO 上的引脚 D~9。 I tried to rewrite the code to be used for my current setup but It comes up with three errors and four warnings that I can not figure out how to solve.
我试图重写用于我当前设置的代码,但它出现了三个错误和四个警告,我无法弄清楚如何解决。 Any help or advice would be greatly appreciated.
任何帮助或建议将不胜感激。 I'm using Atmel Studio 7 Gcc C.
我正在使用 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);
}
Errors:错误:
Recipe for target 'main.o' failed - Line 76
expected ';' before numeric constant - Line 44
expected ';' before numeric constant - Line 49
Warnings:警告:
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
The formatting of your delays is incorrect.您的延迟格式不正确。 According to
util/delay.h
(Convenience functions for busy-wait delay loops) here , two functions are available:根据此处的
util/delay.h
(忙等待延迟循环的便利功能),有两个功能可用:
void _delay_ms (double __ms)
void _delay_us (double __us)
You should treat these as function calls, so you need to write:您应该将这些视为 function 调用,因此您需要编写:
_delay_ms(10);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.