Contoh Low Cost Counter Sederhana 4 Digit 7 Segment

Rangkaian counter sederhana yg kita buat menggunakan komponen minimal sehingga bisa menghemant cost.

– 1 pcs board AVR

–  1 pcs 4 digit 7 segment

–  12 pcs  female to female cable

counterminimal

7 segment yang saya gunakan adalah Common Catoda 4 segment untuk clock dengan konfigurasi pin dibawah ini

 

Data Konfigurasi kaki/pin 7 segment berguna untuk menghubungkan dengan pin pada  port microcontroller sebagai berikut ini :

counter4digit

Kode program

/*****************************************************

Chip type : ATmega8535
Clock frequency : 11.059200 MHz

*****************************************************/

#include <mega8535.h>
#include <delay.h>

void kirimbyte7segment(unsigned char data,unsigned char digit);
void writeData(int dat);

int dataglobal=0;

interrupt [EXT_INT0] void ext_int0_isr(void)
{
delay_ms(90);
// Place your code here
dataglobal=dataglobal +1;;
delay_ms(90);
}

// External Interrupt 1 service routine
interrupt [EXT_INT1] void ext_int1_isr(void)
{
delay_ms(60);
// Place your code here
dataglobal=dataglobal -1;;
delay_ms(60);
}
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
writeData(dataglobal);
}
void writeData(int dat);
flash unsigned char text7segmentClock[]=
{  // dp-g-f-e-d-c-b-a
0b00111111,   //0
0b10000110,  / /1
0b01011011,  //2
0b11001111,  //3
0b01100110,  //4
0b11101101,  //5
0b01111101,  //6
0b10000111,  //7
0b01111111,  //8
0b11101111,  //9
0b00000000,  //  blank
0b01000000  // tanda  minus
};

void main(void)
{

char i=0;

// Port A initialization
PORTA=0xff;
DDRA=0xff;

// Port B initialization
PORTB=0xff;
DDRB=0xff;

// Port C initialization
PORTC=0xff;
DDRC=0xff;

// Port D initialization
PORTD=0xff;
DDRD=0xff;

// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Low level
// INT1: On
// INT1 Mode: Low level
// INT2: Off
GICR|=0xC0;
MCUCR=0x00;
MCUCSR=0x00;
GIFR=0xC0;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 10.800 kHz
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x05;
TCNT0=0xC0;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x01;
// Global enable interrupts
#asm(“sei”)

for(i=1;i<5;i++)
{
kirimbyte7segment(text7segmentClock[0],i);
delay_ms(100);
}

dataglobal=0;
while (1)
{

};
}

void kirimbyte7segment(unsigned char data, char digit)
{

if(digit==1)
{
PORTA.0=0;
PORTA.1=1 ;
PORTA.2=1 ;
PORTA.3=1 ;
}
if(digit==2)
{
PORTA.0=1;
PORTA.1=0 ;
PORTA.2=1 ;
PORTA.3=1 ;
}
if(digit==3)
{
PORTA.0=1;
PORTA.1=1 ;
PORTA.2=0 ;
PORTA.3=1 ;
}
if(digit==4)
{
PORTA.0=1;
PORTA.1=1 ;
PORTA.2=1 ;
PORTA.3=0 ;
}

PORTC =data ;
delay_ms(5);
}

void writeData(int dat) // dat max 4 digit
{
unsigned char j,sign=1;
unsigned char angka;

if(dat<0)
{
dat= dat * -1;
sign=0;
}

for(j=0;j<4;j++)
{

angka=dat%10;
dat /= 10; //6/10

if(angka==0)
{
if(dat==0)
{
if(sign==1)angka=10; // blank

if(sign==0)
{
angka=11; //tanda minus
sign=1;
}
}
}
kirimbyte7segment(text7segmentClock[angka], 4-j);

}
}

Advertisements

About pccontrol

Berisi Tutorial Menggunakan PC untuk mengontrol Peralatan dengan cara mudah & praktis.

Posted on 09/02/2015, in Pemrograman C untuk AVR dgn CodeVision. Bookmark the permalink. Leave a comment.

Komentar ,Saran atau Pertanyaan

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: