Back

[Embeded] AM-128 ATmega SevenSegment (7세그먼트 임베디드 디바이스 개발)

학생 때 AM-128을 이용하여 임베디드 디바이스를 제어하는 프로그램 만들던 내용입니다. 오랜만에 보니깐 새롭네요.

PC USB포트와 AM-128을 연결하고 나서부터는 AVR Studio를 이용하여 보드에 프로그래밍을 할 수 있습니다.

간단한 타이머입니다.

Code - AVRGCC1.c

/*

  • AVRGCC1.c

  • Created: 2013-06-13 오전 9:14:30

*/

#define F_CPU 16000000UL

#define SEVEN_LED_A 0

#define SEVEN_LED_B 1

#define SEVEN_LED_C 2

#define SEVEN_LED_D 3

#define SEVEN_LED_E 4

#define SEVEN_LED_F 5

#define SEVEN_LED_G 6

#define SEVEN_LED_DIP 7

#define SEVEN_LED_1 (1«SEVEN_LED_B | 1«SEVEN_LED_C)

#define SEVEN_LED_2 (1«SEVEN_LED_A | 1«SEVEN_LED_B | 1«SEVEN_LED_G | 1«SEVEN_LED_E | 1«SEVEN_LED_D )

#define SEVEN_LED_3 (1«SEVEN_LED_A | 1«SEVEN_LED_B | 1«SEVEN_LED_G | 1«SEVEN_LED_C | 1«SEVEN_LED_D )

#define SEVEN_LED_4 (1«SEVEN_LED_F | 1«SEVEN_LED_B | 1«SEVEN_LED_G | 1«SEVEN_LED_C )

#define SEVEN_LED_5 (1«SEVEN_LED_A | 1«SEVEN_LED_F | 1«SEVEN_LED_G | 1«SEVEN_LED_C | 1«SEVEN_LED_D )

#define SEVEN_LED_6 (1«SEVEN_LED_A | 1«SEVEN_LED_F | 1«SEVEN_LED_G | 1«SEVEN_LED_E | 1«SEVEN_LED_D | 1«SEVEN_LED_C)

#define SEVEN_LED_7 (1«SEVEN_LED_A | 1«SEVEN_LED_B | 1«SEVEN_LED_C )

#define SEVEN_LED_8 (1«SEVEN_LED_A | 1«SEVEN_LED_B | 1«SEVEN_LED_C | 1«SEVEN_LED_D | 1«SEVEN_LED_E | 1«SEVEN_LED_F | 1«SEVEN_LED_G )

#define SEVEN_LED_9 (1«SEVEN_LED_A | 1«SEVEN_LED_B | 1«SEVEN_LED_G | 1«SEVEN_LED_F | 1«SEVEN_LED_C | 1«SEVEN_LED_D )

#define SEVEN_LED_0 (1«SEVEN_LED_A | 1«SEVEN_LED_B | 1«SEVEN_LED_C | 1«SEVEN_LED_D | 1«SEVEN_LED_E | 1«SEVEN_LED_F )

#define SEVEN_DIG1 PF7

#define SEVEN_DIG2 PF6

#define SEVEN_DIG3 PF5

#define SEVEN_DIG4 PF4

#define MAXNUMBER 9999

#include <util/delay.h>

#include <avr/io.h>

#include <avr/interrupt.h>

#include <avr/io.h>

unsignedshort count =0;

volatileunsignedlong timer0;

int SevenSegment_Manager();

int main(void)

{

    // Initializing PORTs

    DDRF =1<<PF0 |1<<PF1 |1<<PF2 |1<< PF3 |1<<PF4 |1<<PF5 |1<<PF6|1<< PF7;

    DDRC =0xFF;



    TCCR0 |=1<<CS01 |1<<CS02;//256 Prescale



    TIMSK |=1<<TOIE0;// Timer0 Interrupt Enable



    sei();



    while(1)

    {

            SevenSegment_Manager();

    }



    return0;

}

ISR(TIMER0_OVF_vect)

{

    timer0++;// the value increases by 256 in a second.





    //if( timer0 % 256 == 0){

    if( timer0 %25==0){

            count++;



            if( count>MAXNUMBER)

                    count=0;

    }



    if( timer0 %256==0)

    {

            PORTF ^=0x01;

    }

}

int SevenSegment_Manager()

{

    int i;

    int count_value;

    int compare;



    count_value=count;



    for(i=0; i<4; i++)

    {

            switch(i)

                    {

                    case0:

                    PORTF = ~(1<<SEVEN_DIG1);

                    compare = count_value%10;

                    break;

                    case1:

                    PORTF = ~(1<<SEVEN_DIG2);

                    compare =(count_value%100)/10;

                    break;

                    case2:

                    PORTF = ~(1<<SEVEN_DIG3);

                    compare =(count_value%1000)/100;

                    break;

                    case3:

                    PORTF = ~(1<<SEVEN_DIG4);

                    compare =(count_value/1000);

                    break;

                    }

            switch(compare)

            {

                    case0:

                            PORTC = SEVEN_LED_0;

                            break;

                    case1:

                            PORTC = SEVEN_LED_1;

                            break;

                    case2:

                            PORTC = SEVEN_LED_2;

                            break;

                    case3:

                            PORTC = SEVEN_LED_3;

                            break;

                    case4:

                            PORTC = SEVEN_LED_4;

                            break;

                    case5:

                            PORTC = SEVEN_LED_5;

                            break;

                    case6:

                            PORTC = SEVEN_LED_6;

                            break;

                    case7:

                            PORTC = SEVEN_LED_7;

                            break;

                    case8:

                            PORTC = SEVEN_LED_8;

                            break;

                    case9:

                    PORTC = SEVEN_LED_9;

                            break;

            }



            _delay_ms(3);

    }

return0;

}

Licensed under CC BY-NC-SA 4.0
Last updated on Jul 10, 2021 01:05 +0900