我计划建造一台 spectral 仪.我的目标是使用3针光电二极管(S9055系列)旋转栅格并捕捉光强度,并使用Python 绘制强度与旋转角的关系图.采用AVR ATMega32a微控制器和MG995伺服电机组成系统,实现了光栅仪的旋转.
该计划的拟议流程如下:
- 我使用一个白色光源和一个栅格,光电二极管捕获通过栅格的衍射光.
- 我通过OC1A针将MG995电机连接到微控制器上,并使其小Angular 旋转,从而产生延迟.
- 我通过运算放大器将光电二极管连接到ADC0引脚(类似于跨阻放大器),并将数据传输到PuTTY端子.
- 将数据收集到数据框中,并绘制图表.
我对马达的转动已经很满意了,但是ADC部分似乎有问题.下面是我的代码,用于捕捉光线并转换值.
#define F_CPU 8000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#include <stdio.h>
//#define USART_BAUDRATE 9600
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
void UART_init(long USART_BAUDRATE)
{
UCSRB |= (1 << RXEN) | (1 << TXEN); /* Turn on the transmission and reception */
UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); /* Use 8-bit character sizes */
UBRRL = BAUD_PRESCALE; /* Load lower 8-bits of the baud rate value */
UBRRH = (BAUD_PRESCALE >> 8); /*Load upper 8-bits*/
}
void UART_TxChar(char ch)
{
while (! (UCSRA & (1<<UDRE))); /*Wait for empty transmit buffer*/
UDR = ch ;
}
void UART_SendString(char *str)
{
unsigned char j=0;
while (str[j]!=0) /*send string up to null */
{
UART_TxChar(str[j]);
j++;
}
}
void adc_init() // Initialize ADC
{
ADMUX = (1<<REFS0); // AVCC as reference voltage
ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0); // Enable ADC and set prescaler to 128
}
// Function to read the ADC value
// Read ADC value from a channel
uint16_t adc_read(uint8_t channel)
{
channel &= 0x07; // Select channel from 0 to 7
ADMUX = (ADMUX & 0xF8) | channel; // Clear the lower 3 bits and set the channel
ADCSRA |= (1<<ADSC); // Start conversion
while(ADCSRA & (1<<ADSC)); // Wait for conversion to complete
return ADC; // Return the ADC value
}
int main()
{
uint16_t adc_value; // Variable to store the ADC value
//DDRB = 0xFF; // Set PORTB as output for LED display
adc_init(); // Initialize ADC
char c="5";
UART_init(9600);
UART_SendString("\nInitializing....\n ");
while(1)
{
adc_value = adc_read(0); // Read ADC value from channel 0 (connected to photodiode)
// Print the ADC value to the Putty terminal
printf("ADC value: %d\n", adc_value);
UART_SendString(c );
UART_SendString(adc_value );
// Wait for a millisecond
_delay_ms(1000);
}
return 0;
}
}
PuTTY终端中的输出显示为: click to view the image个 我拔下串口通信电缆后被夹住了.
因此,正如您所看到的,这不一定是输出,所以有人能帮我找出哪里出错了吗?