Chia sẻ GIAO TIẾP ENCODER ROTARY EC11 + AT328P + OLED I2C

ledacdam

Kỹ sư
#1
Code c giao tiếp với encoder rotary EC11 hiển thị trên Oled 0.96 inch i2c dùng VDK Atmega328P
- Xoay phải tăng , xoay trái giảm giá trị từ 0-99
- Giao tiếp oled với vác hàm cơ bản : Ghi lệnh, data, hiển thị font 21x24, in ảnh ...
- Data font + ảnh được lấy code từ phần mềm PCtoLCD2002
- Code c được dùng cho trình biên dịch CodevisionAVR 3.12 Advanced
Code :

Mã:
// Share code EC11 + Oled I2C + Atmega328P
// Trinh bien dich : CodevisionAVR 3.12
// Fuse&Lock LH : 0xEFD9
// code by damclock https://www.facebook.com/damclockpage/
// Su dung Oled I2C
// + VCC : 3.3V
// + GND : GND
// + SCL : PORTC.5
// + SDA : PORTC.4
// Link EC11 : http://banlinhkien.vn/goods-3256-cong-tac-vo-cap-ec11.html
// + OUTA : PORTD.2
// + OUTB : PORTD.3
// + GND  : GND
#include <mega328p.h>
#include <delay.h> 
              
#define CMD 0               
#define DAT 1

unsigned char dem=0;
unsigned char inc=0;
flash unsigned char Font21[][21] ={  // Font tu 0-9 ( Kich thuoc moi ky tu : 21x24 )
{0x00,0xFE,0xFF,0xFF,0xFF,0xFF,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0xFF,0xFF,0xFF,0xFF,0xFE,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x3C,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0xFE,0xFF,0xFF,0xFF,0xFF,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0xFF,0xFF,0xFF,0xFF,0xFE,0x00},
{0x00,0xFE,0xFF,0xFF,0xFF,0xFF,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0xFF,0xFF,0xFF,0xFF,0xFE,0x00},
{0x00,0x00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,0x0F,0x0F,0x0F,0x0F,0x0F,0xFF,0xFF,0xFF,0xFF,0x0F,0x0F,0x00},
{0x00,0x7F,0xFF,0xFF,0xFF,0xFF,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xFF,0xFF,0xFF,0xFF,0x7F,0x00},
{0x00,0x7F,0xFF,0xFF,0xFF,0xFF,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xFF,0xFF,0xFF,0xFF,0x7F,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xE0,0xF8,0xFE,0xFF,0xFF,0x1F,0x07,0x01,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x7F,0xFF,0xFF,0xFF,0xFF,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xFF,0xFF,0xFF,0xFF,0x7F,0x00},
{0x00,0x7E,0xFE,0xFE,0xFE,0xFE,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xF0,0xFF,0xFF,0xFF,0xFF,0x7F,0x00},
};
flash unsigned char Damclock[] ={ // 96 X 16
0xFF,0xFF,0x03,0x03,0x03,0x03,0x03,0xFF,0xFE,0x00,0x00,0x60,0x60,0x60,0x60,0x60,
0x60,0xE0,0xC0,0x00,0xE0,0xE0,0xC0,0x60,0x60,0xE0,0xE0,0xC0,0x60,0x60,0xE0,0xC0,
0x00,0xC0,0xE0,0x60,0x60,0x60,0x60,0x60,0xE0,0xC0,0x00,0xFF,0xFF,0x00,0x00,0x00,
0x80,0x60,0x10,0x08,0x08,0x04,0xF4,0x04,0x08,0x08,0x10,0x60,0x80,0x00,0xC0,0xE0,
0x60,0x60,0x60,0x60,0x60,0xE0,0xC0,0x00,0xFF,0xFF,0x80,0x00,0x00,0x80,0xC0,0x60,
0x00,0x00,0x00,0x00,0x00,0xF8,0x04,0x02,0xFD,0x25,0x65,0xA5,0x25,0x1A,0x04,0xF8,
0x7F,0x7F,0x60,0x60,0x60,0x60,0x60,0x7F,0x3F,0x00,0x3C,0x7E,0x66,0x66,0x66,0x76,
0x3E,0x7F,0x7F,0x00,0x7F,0x7F,0x00,0x00,0x00,0x7F,0x7F,0x00,0x00,0x00,0x7F,0x7F,
0x00,0x3F,0x7F,0x60,0x60,0x60,0x60,0x60,0x70,0x30,0x00,0x3F,0x7F,0x60,0x60,0x60,
0x63,0x0C,0x10,0x20,0x20,0x40,0x41,0x41,0x21,0x21,0x11,0x0C,0x03,0x00,0x3F,0x7F,
0x60,0x60,0x60,0x60,0x60,0x70,0x30,0x00,0x7F,0x7F,0x01,0x03,0x07,0x0D,0x18,0x30,
0x60,0x00,0x00,0x00,0x00,0x00,0x01,0x02,0x05,0x04,0x04,0x04,0x05,0x02,0x01,0x00,
}; 
/////////////////////////////////////////////////////////////////////////
void IIC(unsigned char byte)
        {
        TWDR=byte;
        TWCR=0x84;
        while(!(TWCR & 0x80));
        }
/////////////////////////////////////////////////////////////////////////
void Oled_write( unsigned char cmd, unsigned char dat )   // Gui du lieu vao oled
        {
        TWCR=0xA4;
        while(!(TWCR&0x80));
        IIC(0x78);
        if(cmd)
                {
                IIC(0b1100000);
                IIC(dat);
                }
        else
                {
                IIC(0x00);
                IIC(dat);
                }
        TWCR=0x94;
        }
/////////////////////////////////////////////////////////////////////////
void Oled_vitri( unsigned char cot,  unsigned char hang )  // Bat dau tu vi tri
        {
        Oled_write(CMD,0xB0|hang); 
        Oled_write(CMD,(0x10|(cot >> 4)));
        Oled_write(CMD,(0x0F&cot)); 
        }
/////////////////////////////////////////////////////////////////////////
void Oled_clear()
        {
        char x, y;
        for(y=0; y<8; y++ )  // 8 dong ( 0-7 )
                {
                Oled_vitri( 0, y );
                for( x=0; x<128; x++ ) // 128 cot  ( 0-127 )
                Oled_write(DAT,0x00);
                }
        }
/////////////////////////////////////////////////////////////////////////
void Oled_init()
        {
        unsigned char i;
        unsigned char cmd1[] =
                {
                0xAE,0xA8,0x3F,0xD3,0x00,0x40,
                0xC8,0xDA,0x12,0x81,0xFF,0xA4,0xA6,0xD5,     
                0x80,0xD9,0x25,0xDB,0x20,0x22,0x00,0x07,
                0xB0,0x10,0x00,0x20,0x02,0x8D,0x14,0xAF,
                };
        for (i=0;i<6;i++)
                {
                Oled_write(CMD,cmd1[i]);
                }                       
        Oled_write(CMD,0xA0 | 1);
        for (i=0;i<24;i++)
                {
                Oled_write(CMD,cmd1[i+6]);
                }
        Oled_clear();
        }                                     
/////////////////////////////////////////////////////////////////////////
void Oled_Font21(unsigned char x, unsigned char y, unsigned char dat, unsigned char amban)
        {
        unsigned char i,j;
        unsigned char So_ki_tu = 10; // Tu 0-9
        unsigned char Rong_chu = 21; // Chieu rong ky tu
        unsigned char Cao_chu = 24;  // Chieu cao ky tu
        dat = dat -48;               // Chi lay so 0-9
        for(j=0;j<Cao_chu/8;j++)     // 3 hang 
                {
                if(amban) // Neu khong su dung am ban
                        {
                        Oled_vitri(x,y+j);
                        for (i=0;i<Rong_chu;i++) Oled_write(DAT,Font21[dat][i+(Rong_chu*So_ki_tu*j)]);
                        // Lay ma font & dich 21 cot 1 ky tu
                        }
                else      // Neu su dung am ban 
                        {
                        Oled_vitri(x,y+j);
                        for (i=0;i<Rong_chu;i++) Oled_write(DAT,~Font21[dat][i+(Rong_chu*So_ki_tu*j)]);
                        }
                }
        // Code chi ap dung khi su dung phan mem PCtoLCD2002 de tao font
        }   
/////////////////////////////////////////////////////////////////////////
void Oled_logo(unsigned char x, unsigned char y, flash unsigned char *map, unsigned char Pix_x, unsigned char Pix_y)
        {
        unsigned int i,n;
        unsigned char row;
        if (Pix_y%8==0) row=Pix_y/8;
        else            row=Pix_y/8+1;
        Oled_vitri(x,y);
        for (n=0;n<row;n++)
                {
                for(i=0;i<Pix_x;i++)
                        {
                        Oled_vitri(x+i,y+n);
                        Oled_write(DAT,map[i+n*Pix_x]);
                        }
                }
        }   
/////////////////////////////////////////////////////////////////////////
interrupt [EXT_INT0] void ext_int0_isr(void)
        {
        EIFR|=(1<<INTF0);
        dem++;
        if(dem>3)dem=0;
        switch(dem)
                {
                case 0: EICRA|= (1<<ISC00); break;
                case 1: EICRA|= (1<<ISC00); break;
                case 2: EICRA&=~(1<<ISC00); break;
                case 3: EICRA&=~(1<<ISC00);
                        inc++;
                        if(inc >99) inc =0;
                        break;       
                }
        }   
/////////////////////////////////////////////////////////////////////////
interrupt [EXT_INT1] void ext_int1_isr(void)
        {
        EIFR|=(1<<INTF1);
        dem++;
        if(dem>3)dem=0;
        switch(dem)
                {
                case 0: EICRA|= (1<<ISC10); break;
                case 1: EICRA|= (1<<ISC10); break;
                case 2: EICRA&=~(1<<ISC10); break;
                case 3: EICRA&=~(1<<ISC10);
                        if(inc>0) inc--;
                        else inc =99;
                        break;
                }
        } 
/////////////////////////////////////////////////////////////////////////
void Ext_init()
        {
        EICRA|=((1<<ISC11)|(1<<ISC01)); // Trigger INT0 & INT1 on falling edge
        EIMSK|=((1<<INT1)|(1<<INT0));   // Enable INT0 & INT1
        }
/////////////////////////////////////////////////////////////////////////
void main(void)
        { 
        DDRB=0xFF;
        DDRC=0xFF;
        DDRD=0xFF;
        PORTB=0xFF;
        PORTC=0xFF;
        PORTD=0xFF;
        MCUCR=0x06;   
        TWBR=12;
        Ext_init();
        Oled_init();
        #asm("sei") 
        while (1)
                { 
                Oled_logo(0,0,Damclock,96,16);
                Oled_Font21( 0,3,inc/10+48,1);
                Oled_Font21(21,3,inc%10+48,1);
                }
        }
 

Đính kèm

Quảng cáo Google