logo

English

이곳의 프로그래밍관련 정보와 소스는 마음대로 활용하셔도 좋습니다. 다만 쓰시기 전에 통보 정도는 해주시는 것이 예의 일것 같습니다. 질문이나 오류 수정은 siseong@gmail.com 으로 주세요. 감사합니다.

ATmega8 MCU 간의 TWI 기능을 이용한 I2C 통신

by digipine posted Nov 02, 2017
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print

두개의 ATmega8 MCU 간의 TWI 기능을 사용하여 데이터 전달을 하는 프로그램을 작성합니다.
 

IC1 을 마스터로 지정하고 IC2 를 슬레이브로 지정해서 하이퍼터미널로 입력된 데이터를 시리얼 통신으로 IC1 에 전달하고, IC1 은 다시 TWI 로 IC2 에 데이터를 전달합니다.

IC2 는 전달받은 데이터를 LCD 에 표시하고, 다시 IC1 으로 TWI 를 통해서 전달하면 IC1 은 이 데이터를 시리얼 통신으로 하이퍼터미널에 전달해서 표시하는 프로그램입니다.

(AT24C02 칩은 소켓에서 제거하세요.)

 

코드비젼(CodeVision) 에서 작성한 프로그램은 아래와 같습니다.

 

마스터 (IC1) 의 프로그램입니다.

 

// I/O register definitions for ATmega8
#include <mega8.h>
#include <delay.h>
#include <stdio.h>
#define ESC 0x1b

typedef unsigned char byte;

byte twi_master_read(byte addr)
{
        byte dat;
       
        TWCR = 0xa4;                    // START
        while(((TWCR & 0x80) == 0x00) || ((TWSR & 0xf8) != 0x08));
        TWDR = (addr << 1) | 1;
        TWCR = 0x84;
        while(((TWCR & 0x80) == 0x00) || ((TWSR & 0xf8) != 0x40));
        TWCR = 0x84;
        while(((TWCR & 0x80) == 0x00) || ((TWSR & 0xf8) != 0x58));
        dat = TWDR;
        TWCR = 0x94;
        return dat;
}

void twi_master_write(byte addr, byte dat)
{
        TWCR = 0xa4;                    // START
        while(((TWCR & 0x80) == 0x00) || ((TWSR & 0xf8) != 0x08));
        TWDR = addr << 1;               // ADDRESS
        TWCR = 0x84;
        while(((TWCR & 0x80) == 0x00) || ((TWSR & 0xf8) != 0x18));
        TWDR = dat;                     // WRITE
        TWCR = 0x84;
        while(((TWCR & 0x80) == 0x00) || ((TWSR & 0xf8) != 0x28));
        TWCR = 0x94;
}

void main(void)
{                     
        byte ch;

        UCSRB |= 0x08;
        UCSRB |= 0x10;
        UBRRL = 5;      // X-Tal=11.0592MHz BAUD=115200

        DDRC = 0x0f;

        TWBR = 12;
        TWSR = 0x00;
        printf("\n\rEnter a character....");
        do {
                ch = getchar();
                PORTC = ~ch;
                twi_master_write(0x38, ch);

                delay_us(100);
                ch = twi_master_read(0x38);
                if (ch == ESC) putchar('\f');
                else putchar(ch);
        } while(1);
}

 

슬레이브 (IC2) 의 프로그램입니다.

 

// I/O register definitions for ATmega8
#include <mega8.h>
#include <delay.h>

#define lcd_inst_wr     (PORTC = 0x04)
#define lcd_data_wr     (PORTC = 0x05)
#define lcd_data(i)     (PORTD = i)
#define ESC 0x1b
typedef unsigned char byte;

void lcd_iw(byte inst)
{
        lcd_inst_wr;
        lcd_data(inst);
        delay_us(40);
        PORTC &= 0xfb;
       
        lcd_inst_wr;
        lcd_data(inst<<4);
        delay_us(40);
        PORTC &= 0xfb;
        delay_ms(2);
}

void lcd_dw(byte dw)
{
        lcd_data_wr;
        lcd_data(dw);
        delay_us(40);
        PORTC &= 0xfb;

        lcd_data_wr;
        lcd_data(dw<<4);
        delay_us(40);
        PORTC &= 0xfb;
}

void init_lcd(void)
{
        lcd_iw(0x20);
        lcd_iw(0x28);
        lcd_iw(0x06);
        lcd_iw(0x0e);
        lcd_iw(0x01);
}                  

void lcd_str(char *str)
{
        while(*str){
                lcd_dw(*str++);
        }
}

byte twi_slave_read(byte addr)
{
        byte dat;
       
        TWAR = addr << 1;
        TWCR = 0x44;                    // SLAVE
        while(((TWCR & 0x80) == 0x00) || ((TWSR & 0xf8) != 0x60));
        TWCR = 0xc4;
        while(((TWCR & 0x80) == 0x00) || ((TWSR & 0xf8) != 0x80));
        dat = TWDR;                     // READ
        TWCR = 0xc4;
        while(((TWCR & 0x80) == 0x00) || ((TWSR & 0xf8) != 0xa0));
        TWCR = 0xc4;
        return dat;
}

void twi_slave_write(byte addr, byte dat)
{
        TWAR = addr << 1;
        TWCR = 0x44;                    // SLAVE
        while(((TWCR & 0x80) == 0x00) || ((TWSR & 0xf8) != 0xa8));
        TWDR = dat;
        TWCR = 0xc4;
        while(((TWCR & 0x80) == 0x00) || ((TWSR & 0xf8) != 0xc0));
        TWCR = 0xc4;
}

void main(void)
{                     
        byte ch;
        char *str1 = "<TWI(I2C) - TWI>";
        DDRC = 0x07;
        DDRD = 0xf0;

        init_lcd();

        lcd_str(str1);
        lcd_iw(0xc0);
       
        TWSR = 0x00;
       
        do {
                ch = twi_slave_read(0x38);
                if (ch == ESC) lcd_iw(0xc0);
                else           lcd_dw(ch);

                delay_ms(1);
                twi_slave_write(0x38, ch);
        } while(1);
}

 

AVR-GCC 에서 작성한 프로그램은 아래와 같습니다.

 

마스터 (IC1) 의 프로그램입니다.

 

// I/O register definitions for ATmega8
#include <avr/io.h>
#include <stdio.h>
#define ESC 0x1b

typedef unsigned char byte;

void delay_us(unsigned int time_us)
{
        unsigned int i;
        for(i=0; i<time_us; i++){ // 4 cycle +
                asm("PUSH R0"); // 2 cycle +
                asm("POP R0"); // 2 cycle +
                asm("PUSH R0"); // 2 cycle +
                asm("POP R0"); // 2 cycle + =12 cycle for 11.0592MHZ
//              asm("PUSH R0"); // 2 cycle +
//              asm("POP R0"); // 2 cycle = 16 cycle = 1us for 16MHz
        }
}
void delay_ms(unsigned int time_ms)
{
        unsigned int i;
        for(i=0; i<time_ms;i++)
                delay_us(1000);
}

int uart_putchar(char c)
{
        if (c == '\n')
                uart_putchar('\r');
        loop_until_bit_is_set(UCSRA, UDRE);
        UDR = c;
        return 0;
}

int uart_getchar(void)
{
        char c;
        loop_until_bit_is_set(UCSRA, RXC);
        c = UDR;
        return c;
}

byte twi_master_read(byte addr)
{
        byte dat;
       
        TWCR = 0xa4;                    // START
        while(((TWCR & 0x80) == 0x00) || ((TWSR & 0xf8) != 0x08));
        TWDR = (addr << 1) | 1;
        TWCR = 0x84;
        while(((TWCR & 0x80) == 0x00) || ((TWSR & 0xf8) != 0x40));
        TWCR = 0x84;
        while(((TWCR & 0x80) == 0x00) || ((TWSR & 0xf8) != 0x58));
        dat = TWDR;
        TWCR = 0x94;
        return dat;
}

void twi_master_write(byte addr, byte dat)
{
        TWCR = 0xa4;                    // START
        while(((TWCR & 0x80) == 0x00) || ((TWSR & 0xf8) != 0x08));
        TWDR = addr << 1;               // ADDRESS
        TWCR = 0x84;
        while(((TWCR & 0x80) == 0x00) || ((TWSR & 0xf8) != 0x18));
        TWDR = dat;                     // WRITE
        TWCR = 0x84;
        while(((TWCR & 0x80) == 0x00) || ((TWSR & 0xf8) != 0x28));
        TWCR = 0x94;
}

int main(void)
{                     
        byte ch;
        FILE    *fp;

        fp = fdevopen(uart_putchar, uart_getchar, 0);
        UCSRB = 0x18;
        UBRRL = 5;      // X-Tal=11.0592MHz BAUD=115200

        DDRC = 0x0f;

        TWBR = 12;
        TWSR = 0x00;
        printf("\n\rEnter a character....");
        do {
                ch = getchar();
                PORTC = ~ch;
                twi_master_write(0x38, ch);

                delay_us(100);
                ch = twi_master_read(0x38);
                if (ch == ESC) putchar('\f');
                else putchar(ch);
        } while(1);
}


슬레이브 (IC2) 의 프로그램입니다.

 

// I/O register definitions for ATmega8
#include <avr/io.h>

#define lcd_inst_wr     (PORTC = 0x04)
#define lcd_data_wr     (PORTC = 0x05)
#define lcd_data(i)     (PORTD = i)
#define ESC 0x1b
typedef unsigned char byte;

void delay_us(unsigned int time_us)
{
        unsigned int i;
        for(i=0; i<time_us; i++){ // 4 cycle +
                asm("PUSH R0"); // 2 cycle +
                asm("POP R0"); // 2 cycle +
                asm("PUSH R0"); // 2 cycle +
                asm("POP R0"); // 2 cycle + =12 cycle for 11.0592MHZ
                asm("PUSH R0"); // 2 cycle +
                asm("POP R0"); // 2 cycle = 16 cycle = 1us for 16MHz
        }
}
void delay_ms(unsigned int time_ms)
{
        unsigned int i;
        for(i=0; i<time_ms;i++)
                delay_us(1000);
}

void lcd_iw(byte inst)
{
        lcd_inst_wr;
        lcd_data(inst);
        delay_us(40);
        PORTC &= 0xfb;
       
        lcd_inst_wr;
        lcd_data(inst<<4);
        delay_us(40);
        PORTC &= 0xfb;
        delay_ms(2);
}

void lcd_dw(byte dw)
{
        lcd_data_wr;
        lcd_data(dw);
        delay_us(40);
        PORTC &= 0xfb;

        lcd_data_wr;
        lcd_data(dw<<4);
        delay_us(40);
        PORTC &= 0xfb;
}

void    htoa(byte hh)
{
        byte    temp;

        temp=hh;
        hh &= 0xf0;     hh >>= 4;
        if (hh >= 10)   hh += 7;   
        hh += '0';
        lcd_dw(hh);

        hh=temp;
        hh &= 0x0f;
        if (hh >= 10)   hh += 7; 
        hh += '0';
        lcd_dw(hh);
}

void init_lcd(void)
{
        lcd_iw(0x20);
        lcd_iw(0x28);
        lcd_iw(0x06); //entry mode set
        lcd_iw(0x0e); //display on, cursor on, blink off
        lcd_iw(0x01); //clear display
}                  

void lcd_str(char *str)
{
        while(*str){
                lcd_dw(*str++);
        }
}

byte twi_slave_read(byte addr)
{
        byte dat;
       
        TWAR = addr << 1;
        TWCR = 0x44;                    // SLAVE
        while(((TWCR & 0x80) == 0x00) || ((TWSR & 0xf8) != 0x60));
        TWCR = 0xc4;
        while(((TWCR & 0x80) == 0x00) || ((TWSR & 0xf8) != 0x80));
        dat = TWDR;                     // READ
        TWCR = 0xc4;
        while(((TWCR & 0x80) == 0x00) || ((TWSR & 0xf8) != 0xa0));
        TWCR = 0xc4;
        return dat;
}

void twi_slave_write(byte addr, byte dat)
{
        TWAR = addr << 1;
        TWCR = 0x44;                    // SLAVE
        while(((TWCR & 0x80) == 0x00) || ((TWSR & 0xf8) != 0xa8));
        TWDR = dat;
        TWCR = 0xc4;
        while(((TWCR & 0x80) == 0x00) || ((TWSR & 0xf8) != 0xc0));
        TWCR = 0xc4;
}

int main(void)
{                     
        byte ch;

        DDRC = 0x07;
        DDRD = 0xf0;
       
        init_lcd();

        lcd_str("<TWI(I2C) - TWI>");
        lcd_iw(0xc0);
       
        TWSR = 0x00;
       
        do {
                ch = twi_slave_read(0x38);
                if (ch == ESC) lcd_iw(0xc0);
                else           lcd_dw(ch);

                delay_ms(1);
                twi_slave_write(0x38, ch);
        } while(1);
}

 
TAG •

List of Articles
No. Subject Author Date Views
17 [Linux] ubuntu 16.04에 QT Creator 설치하기 digipine 2017.11.02 24344
16 Phabricator 설치 가이드 우분투 16.04 기준 digipine 2017.11.02 6193
15 Wi-Fi Display Standard Miracast Protocol Log digipine 2017.11.02 850
14 Git Http Backend Upload Size 설정 - Http 500 Error 해결 digipine 2017.11.02 2074
13 [Linux, OSX] pfctl - Packet FIlter Control 사용법 digipine 2017.11.02 3187
12 TCP/IP State Transition - TCP 스택 포팅 시 참조 file digipine 2017.11.02 195188
11 Phabricator 설치 가이드 우분투 12.04 기준 digipine 2017.11.02 1228
» ATmega8 MCU 간의 TWI 기능을 이용한 I2C 통신 digipine 2017.11.02 5998
9 Mac Address 를 String 으로 변환하는 간편한 방법 digipine 2017.11.02 437
8 Wi-Fi display (miracast) FFMpeg MpegTs Supported digipine 2017.11.02 1302
7 Iconv 사용법 소스 digipine 2017.11.01 1262
6 QCAD 1.1 한국 최초의 2D CAD 소프트웨어 file digipine 2017.11.01 830
5 WinPCap과 Ethereal, Wireshark 을 이용한 스니핑(Sniffing) digipine 2017.10.29 5428
4 NMEA-0183을 이용한 GPS 애플리케이션 제작 digipine 2017.10.28 446
3 WIN CE, GPS - NMEA protocol - GPS Virtual Driver digipine 2017.10.28 2829
2 WIN CE C++ 시리얼 제어 방법 digipine 2017.10.28 631
1 CMM / CMMI 란 무엇인가? digipine 2017.10.28 2467
Board Pagination Prev 1 2 Next
/ 2