我计划建造一台 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个 我拔下串口通信电缆后被夹住了.

因此,正如您所看到的,这不一定是输出,所以有人能帮我找出哪里出错了吗?

推荐答案

也许这会有帮助

int main()
{
    uint16_t adc_value; // Variable to store the ADC value
    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
        char buf[20];

        sprintf(buf,"ADC value: %d\n", adc_value);
        UART_SendString(c );        
        UART_SendString(buf);

        // Wait for a millisecond
        _delay_ms(1000);

    }   
    return 0;
}

C++相关问答推荐

常数函数指针优化

找出文件是否包含给定的文件签名

C:二进制搜索和二进制插入

为什么内核使用扩展到前后相同的宏定义?

二进制计算器与gmp

如何使用C for Linux和Windows的标准输入与gdb/mi进行通信?

C-使用指针返回修改后的整数数组

为什么memcpy进入缓冲区和指向缓冲区的指针工作相同?

为什么我从CSV文件中进行排序和搜索的代码没有显示数据的所有结果?

如何在VS 2022中正确安装额外的C头文件

S和查尔有什么不同[1]?

无法访问共享目标文件内的共享指针

C程序向服务器发送TCPRST

OSDev--双缓冲重启系统

为什么我在C代码中得到一个不完整的类型?

C++中PUTS函数的返回值

将非连续物理内存映射到用户空间

为什么会出现此错误?二进制表达式的操作数无效

当另一个指向 const 的指针观察到数据时,通过指针更改数据是否安全?

C 语言中 CORDIC 对数的问题