使用ATmega32微控制器读取红外遥控信号

时间:2018-03-10 11:33:43

标签: arduino embedded avr atmega atmelstudio

我是嵌入式系统的初学者,我从arduino开始,然后转向AVR微控制器和嵌入式C。

我想做EVM项目(电子投票机)。我使用Atmega32A作为控制器,LCD(2 * 16)显示投票结果和4个按钮(YES,NO,Abstain,Show results)。

我想把它变成无线的,所以在经过一些搜索之后,我决定使用IR接收器(TSOP-1736)和一个适用于NEC协议的遥控器来制作EVM无线。 然后,我使用Arduino UNO和“IRremote”库将远程信号解码为HEXA代码,我可以在项目中使用,而不是稍后使用按钮!

当我尝试使用这个库使用“ATmel Studio IDE”对Atmega32进行编程时出现问题。实际上,“IRremote”库仅适用于ARDUINO,我无法将其用于ATmel IDE上的Atmega,所以我搜索了很多可以用于avr的替代库,我找到了这个 https://github.com/MalteP/nec-decoder 但是,当我尝试在ATmel工作室IDE中使用它来将最近发送的远程信号存储到变量中时,然后使用它在我的程序中做出决定我不能!我努力寻找解决这个问题的方法,但我很少找到有关它的主题。我阅读了一些关于如何使用中断,PWM和定时器手动读取信号的文章,但我没有说明如何完成工作!

总之,我需要做的就是知道如何使用这个库,如Arduino的“IRremote”,将远程信号的HEXA代码存储到一个数组中,然后使用它们与最近发送的数据进行比较远程信号用远程信号替换按钮的if条件!

以下是我项目的代码:

#include <stdlib.h>

#ifndef F_CPU
#define F_CPU 1000000UL
#endif
#include <avr/io.h>
#include <util/delay.h>
#define EN PD7
#define RW PD5
#define RS PD2
void LCD_INIT( void );
void Lcd_cmd( unsigned char Command );
void Lcd_data( unsigned char data );
void Lcd_write_str( char *str );
void Lcd_set_cursor( unsigned char row, unsigned char col );
void Lcd_out( unsigned char row, unsigned char col, char *str );
// LCD Declarations and pins 
char a[10];
char b[10];
char c[10];
// buffers for showing integers on LCD
#define switch1 PA0 // YES option
#define switch2 PA1 // No option
#define switch3 PA2 // Abstain option
#define switch4 PA3 // Show voting results

int main()
{
    int i = 0;
    int j = 0;
    int k = 0; // Counting variables (i="YES" ) &(j="NO") & (k="Abstain" )
    DDRA = 0b00000000;

    LCD_INIT();

    while( 1 )
    {
        for( int l = 0; l <= 16; l++ )
        {
            for( int n = 0; n <= 2; n++ )
            {
                Lcd_out( n, l, "  " );
            }
        }

        Lcd_out( 1, 3, "press key" ); //At the beginning 
        _delay_ms( 2000 );
        if( PINA & (1 << PA0) )
        {
            Lcd_write_str( "                         " );
            i = i + 1;
            Lcd_out( 1, 3, "You voted Yes" );
            while( PINA & (1 << PA0) );
            _delay_ms( 500 );
        } //Voting YES !!!!
          //---------------------------------------------------------           
        if( PINA & (1 << PA1) )
        {
            Lcd_write_str( "                          " );
            j = j + 1;
            Lcd_out( 1, 3, "You voted NO" );
            _delay_ms( 500 );
            while( PINA & (1 << PA1) );
            _delay_ms( 500 );
        }//Voting No !!!
         //-----------------------------------------------------------
        if( PINA & (1 << PA2) )
        {
            Lcd_write_str( "                           " );
            k = k + 1;
            Lcd_out( 1, 1, "You voted Abstain" );
            _delay_ms( 500 );
            while( PINA & (1 << PA2) );
            _delay_ms( 500 );
        }//Voting Abstain !!!
         //------------------------------------------------------------

         //Getting voting results =======

        if( PINA & (1 << PA3) )
        {
            for( int l = 0; l <= 16; l++ )
            {
                for( int n = 0; n <= 2; n++ )
                {
                    Lcd_out( n, l, "  " );
                }
            }

            while( PINA & (1 << PA3) )
            {
                Lcd_out( 1, 1, "YES = " );
                Lcd_out( 1, 6, itoa( i, a, 10 ) );
                Lcd_out( 1, 9, "NO = " );
                Lcd_out( 1, 14, itoa( j, b, 10 ) );
                Lcd_out( 2, 2, "Abstain = " );
                Lcd_out( 2, 11, itoa( k, c, 10 ) );
            }
        }
        // Showing results
    }
}

void LCD_INIT( void )
{
    DDRD = 0xff;
    DDRB = 0xff;
    PORTD &= ~(1 << EN);
    _delay_us( 2000 );
    Lcd_cmd( 0x38 );
    Lcd_cmd( 0x0E );
    Lcd_cmd( 0x01 );
    //_delay_us(2000);
    Lcd_cmd( 0x06 );
}

void Lcd_cmd( unsigned char Command )
{
    PORTB = Command;
    PORTD &= ~(1 << RW);
    PORTD &= ~(1 << RS);
    PORTD |= (1 << EN);
    _delay_us( 1 );
    PORTD &= ~(1 << EN);
    _delay_us( 2000 );
}

void Lcd_data( unsigned char data )
{
    PORTB = data;
    PORTD &= ~(1 << RW);
    PORTD |= (1 << RS);
    PORTD |= (1 << EN);
    _delay_us( 1 );
    PORTD &= ~(1 << EN);
    _delay_us( 2000 );
}

void Lcd_write_str( char *str )
{
    unsigned char i = 0;
    while( str[i] != ('\0') )
    {
        Lcd_data( str[i] );
        i++;
    }
}

void Lcd_set_cursor( unsigned char row, unsigned char col )
{
    row -= 1; col -= 1;
    Lcd_cmd( 0x80 + (row * 0x40) + col );
}

void Lcd_out( unsigned char row, unsigned char col, char *str )
{
    Lcd_set_cursor( row, col );
    Lcd_write_str( str );
}

0 个答案:

没有答案