Timer/Counter
Many people ask me about this, I think this is the time for me to start writing about timer or counter.
As long as I know, almost every microcontroller has a timer/counter feature. Not only AVR family but also the old micro such as MCS. The basic protocol not change at all, you just have to understand this one an then you will able to work with timer/counter in any other microcontroller.
I take an example ATmega 8535, take a look the datasheet its all there. There are Timer 0, timer 1, and sometimes timer 2. My senior at lab told me that timer 0 was not good enough, but i forgot to ask the reason. Maybe you wanna try to prove his suggestion (let me know if you already have the answer
)
Open page about the operation mode of timer. There are several mode of timer, in MCS usually there are only 3 mode, mode 0 mode 1 and mode 2, in AVR there are mode phase correct, phase&frequency correct, normal mode, etc… (i forget, can`t open datasheet right now). The important thing is, you should read and understan what the register are in timer/counter. there are TCNT, ICR, OCR. The fast way to understand How timer/counter works is by see the picture of timer/counter diagram in datasheet.
Guys….wait a moment please, gotta the job to do…. continue this post next week i have several project to finish…
from sensor to Seven Segment (in Progress)
Kali ini kita belajar tentang Seven Segment yaks!, murah kok (klo yang kecil). Ok seperti biasa display ini akan dikontrol oleh mikrokontroller, untuk project ini saya hanya pakai empat 7 segment.
Komponen dasar 7 segment adalah LED (Light Emitting Diode), jadi aplikasi rangkaiannya pun sama dengan LED. Dirangkaian ada resistor yang berfungsi untuk membatasi arus yang melewati LED (semakin tinggi arus semakin terang nyala LED, tapi resistornya jangan terlalu kecil karena ada batasan arus yang diperbolehkan agar LED tidak rusak).

Kalau beli 7 Segmen pasti ntar ditanya mau yang Anoda atau yang Katoda. Prinsipnya sama aja, komponennya juga sama, cuma nanti letak rangkaiannya agak beda, disini saya pakai 7 segmen common Anoda.

Dari gambar diatas dah kebayangkan kira-kira gimana caranya nampilin angka 0,1,2,atau3, dst… contoh klo mau nampilin angka 0 berarti LED g dan LED dp(desimal point) harus mati. Dari pemahaman ini kita bisa buat rangkaian kontrolnya

NAAAHhhh…… sekarang masalahnya klo saya mau pakai empat 7 segment berarti ada banyak pin I/O yang hrs disediakan (4×8= 32pin) itu namanyya pemborosan…oleh karena itu harus diakalin ….caranya dengan skematik dibawah ini.

Pada rangkaian diatas semua kakai 7 segment disatukan (tentunya kaki yang sama, a dengan a, b dengan b, dst), tapi supply voltage nya dibedakan, dan diatur oleh mikro. Dan untuk menampilkan misal bilangan 2300, PORTD=1010 0100 PORTC=0001 0000 lalu PORTD=1011 0000 PORTC=0010 0000 lalu PORTD=1100 0000 PORTC=0100 0000 lalu PORTD=1100 0000 PORTC=1000 0000 ini dilakukan dengan sangat cepat.
to be continue………
Using Codewizard in CodevisionAVR
PROGRAM : OUTPUT
Tujuan Program : Membuat delapan lampu LED menyala
Hardware : Hubungkan rangkaian LED ke PORTC
Codewizard : setting PORTC sebagai output, set Chip
Langkahnya….
1. Pilih shortcut codevisionAVR dengan tampilan seperti berikut

2. Buat file baru dengan cara : File , NEW

3.Pilih Project

4. Pilih Codewizard

5. Menggunakan CodeWizard : Pengaturan Chip, jenis mikro dan nilai crystal yang digunakan

6.Menggunakan CodeWizard, setting PORTC sebagai Output (klik “In” shg berubah menjadi “out”)

7.Simpan hasil CodeWizard

8.Save setting : save tiga kali dengan nama file yang persis sama

…..SELESAI……

Nah, di program yang dah jadi ini tinggal menambahkan kebutuhan kita, contohnya saya mau menyalakan 8 buah LED (aktif high) yang terhubung di PORT C maka program tambahannya adalah

RELEASE …Guidline Project (REVISI)
it`s finish, if you want to have it, just click this link
Bagi yang ingin memperoleh e-book ini silahkan langsung download saja, sudah saya upload lewat scribd, download disini

Minimum Schematic for micro-controller
This is the minimum schematic for your microcon, if you want to use one of these pin I/O just put another jumper/pin header on the board.

to be continue….
ADC to LCD (ATmega8535)
as my promise to aryo danurwendo
Pertama codewizardnya di setting dulu yah, terutama adc dan lcd
This is the source code, enjoy
========================================================
#include mega8535.h
#include delay.h
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0×15 ;PORTC
#endasm
#include lcd.h
#include stdio.h
#define ADC_VREF_TYPE 0×60
// Read the 8 most significant bits
// of the AD conversion result
unsigned char read_adc(unsigned char adc_input)
{
ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=0×40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0×10)==0);
ADCSRA|=0×10;
return ADCH;
}
// Declare your global variables here
char lcd_buffer[33];
void main(void)
{
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0×80;
SFIOR=0×00;
// ADC initialization
// ADC Clock frequency: 691.200 kHz
// ADC Voltage Reference: AVCC pin
// ADC High Speed Mode: Off
// ADC Auto Trigger Source: Free Running
// Only the 8 most significant bits of
// the AD conversion result are used
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0xA4;
SFIOR&=0x0F;
// LCD module initialization
lcd_init(16);
while (1)
{
lcd_gotoxy(0,0);
sprintf(lcd_buffer,”adc = %c “,read_adc(0));
lcd_puts(lcd_buffer);
};
}
============================================================
ini menampilkan hasil pembacaan adc di PORTA.0 ke LCD yang dipasang di PORTC
kalau mau membaca ADC yang lain, misal PORTA.1 ganti read_adc(0) jadi read_adc(1)
NB:
1. perhatikan yang include, seharusnya da tanda kurung tapi dianggap sintak ni sama webna jdi sy hapus aja
2. ini basicnya, silahkan di modif, kalau mau tampil berupa nilai tegangan seperti multimeter lihat post menampilkan bilangan desimal ke LCD
Project : Stopwatch on ATmega16 (AVR)
Atmega16 and atmega8535 have the same characteristic, so this explanation should work for both of them.
I was helping Mrs Sri, she need this for her Thesis. It is a simply program, she just need to make a stopwatch with microcontroller. I use INT0 as timer ON and INT1 as timer OFF. We use LCD to display the time.
crystal clock = 11.0592 MHz, Timer 0.
This is the program================================================
#include
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0×15 ;PORTC
#endasm
#include lcd.h
#include stdio.h
char lcd_buffer[33];
unsigned int count;
// External Interrupt 0 service routine
interrupt [EXT_INT0] void ext_int0_isr(void)
{
count=0;
}
// External Interrupt 1 service routine
interrupt [EXT_INT1] void ext_int1_isr(void)
{
// Place your code here
//TCCR0=0×00;
lcd_gotoxy(0,0);
sprintf(lcd_buffer,”waktu %u ms”,count);
lcd_puts(lcd_buffer);
}
// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
TCNT0=0×53;
count++;
}
void main(void)
{
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=P State2=P State1=T State0=T
PORTD=0x0C;
DDRD=0×00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock scale: 64
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0×03;
TCNT0=0×53;
OCR0=0×00;
// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Rising Edge
// INT1: On
// INT1 Mode: Rising Edge
// INT2: Off
GICR|=0xC0;
MCUCR=0x0F;
MCUCSR=0×00;
GIFR=0xC0;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0×01;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0×80;
SFIOR=0×00;
// LCD module initialization
lcd_init(16);
// Global enable interrupts
#asm(“sei”)
while (1);
}
=================================================================

How to set up 1 millisecond ?
the key point are : TCCR0=0×03; and TCNT0=0×53;
TCCR0=0×03; that means clock crystal/64, we got 5.78 us for one clock cycle in TCNT0
TCNT0=0×53; that means timer0 will overflow start from 83 (=53 in hexa) to 256, this is the multiplier
timer 0 will count 5.78 us x 173 (256-83) = 999.94 us, it’s almost 1 ms right
after the timer 0 finish the counting, micro will perform the program in the interrupt timer (interrupt [TIM0_OVF] void timer0_ovf_isr(void))
IMPORTANT : don`t open up PIN INT0 & INT1 keep them in low state when there is no signal interrupt.
How to download program into Atmega8535 (AVR)
This post will explain about the way to download program into micro (atmega8535 or AVR) using codevisionAVR as compiler and STK500 (AVR USB ISP) as the programmer-this picture below-
the step by step :
1. connect the cable from the programmer to the mcrocontroller connection, turn on power supply
2. plug in the USB cable from the programmer into your computer
3. Choose : My computer, (right click) properties, Hardware, Device Manager, (double click) Unknown Device
4. Install the driver
5. Back again to device manager and see… Which port is the programmer plug in…for example com 3 …..

Now your programmer is ready to use, its time to set the compiler (codevisionAVR)
1. Go to : Setting, programmer
2. Choose : STK500/AVRISP, com …. (remember step no. 5 above)
3. Go to : Project, Configure, After build.
4. Click : Program the chip, OK

DONE
you are ready to download any program. to compile your program F9, to download CTRL+F9
Good Luck !!
thanks to : Gun-gun (@my office) and Ismail (@lab) very helpful
an Easy way to understand LCD
I hope this explanation would fit the title.
This post will explain about How to control LCD (model LMB162A) using microcontroller such as AVR. Furthermore, I will add the basic information about LCD that will help you to be more familiar with LCD, especially when u used another micro.

LCD : liquid crystal display
Usually I use LCD model LMB162A (16Characters x 2lines) for electronics research purpose. Please googling it on web (u need the datasheet to be more clear about this LCD). This LCD has a driver on it. So, all we have to do is to understand How the driver works. First, we have to know the hardware and How to set up all the components.
Hardware:
There are 16 PIN in the LCD.
PIN 1 = GND
PIN 2 = +5 Volt
PIN 3 = Voltage variable
PIN 4 = RS (control pin)
PIN 5 = R/W (control pin)
PIN 6 = E (control pin)
PIN 7 = DB0 (data pin)
PIN 8 = DB0 (data pin)
PIN 9 = DB0 (data pin)
PIN 10 = DB0 (data pin)
PIN 11 = DB0 (data pin)
PIN 12 = DB0 (data pin)
PIN 13 = DB0 (data pin)
PIN 14 = DB0 (data pin)
PIN 15 = + 5 Volt
PIN 16 = GND
This is the hardware (take attention to pin 3, u need variable resistor to make variable voltage)

We can divide all those PIN into there main point :
1. POWER : PIN no. 1,2,3,15,16.
1 & 2 stand for Logic ; 3 stand for LCD supply; 15 & 16 stand for back light
2. CONTROL : PIN no. 4,5,6.
4 : RS, Register Selection : 1(High) = data byte , 0(Low) = command byte
5 : R/W, Read/Write : 1(High) = Read operation , 0(Low) = Write operation
6 : E, Enable Signal : High = Read data, H=>L (falling edge) = write data
3. DATA : PIN no.7,8,9,10,11,12,13,14.
there r 2 way to put data on LCD, 4-bit and 8-bit
In 8-bit mode : PIN no.7-14 (DB0-DB7)
In 4-bit mode : PIN no.11-14, (DB4-DB7)
and open all the pin no.7-10 (don`t connect them to ground, just open up,do nothing)
to be continue….
Menampilkan bilangan desimal ke LCD
thanks to Acuy….it’s really work
untuk menampilkan suatu tampilan ke LCD sebenarnya sangat mudah, apalagi kalau kita menggunakan codewizard nya codevisionAVR. Hanya saja data yang ditampilkan terbatas dalam bentuk integer. NAh… dalam banyak kasus kita membutuhkan tampilan hasil dari suatu rumus tertentu atau hasil dari pembacaan sensor yang terkadang memiliki angka dibelakang koma alias desimal atau float. kalau dipaksakan (dengan cara biasa) maka mikro akan menolak dan akan muncul peringatan memory tidak mencukupi……… sudah pernah dicoba sampai pake ATMEGA64 tapi teuteup aja ga bisa.
Oleh karena itu harus di akalin…. thanks to Acuy again, I know this solution from him.
Ops tapi sebelumnya mungkin harus saya jelaskan dulu seperti apa sih cara nampilin bilangan yang biasa ke LCD
Ok, this is the step :
1. Di codewizard silahkan pilih LCD, tentukan mau di PORT mana LCD dipasang
2. Catat nomor-nomor pin yang harus dihubungkan antara PORT mikro dan PIN LCD
3. Save dan berikut contoh programnya
=======================================================================
#include
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0×15 ;PORTC
#endasm
#include
#include
#include
char lcd_buffer[33];
void main(void)
{
// Timer(s)/Counter(s) Interrupt(s) initialization
// LCD module initialization
lcd_init(16);
lcd_gotoxy(0,0);
lcd_puts(“HAsil pembacaan”);
lcd_gotoxy(0,1);
lcd_puts(“nampilin apaa gitu”);
// Global enable interrupts
#asm(“sei”)
while (1)
{};
}
========================================================================
jadiii penambahannya :
char lcd_buffer[33];
—————————-
lcd_gotoxy(0,0);
lcd_puts(“Hasil pembacaan”);
lcd_gotoxy(0,1);
lcd_puts(“nampilin apaa gitu”);
—————————–
lcd_gotoxy(0,0); artinya menampilkan di baris pertama, kalau mau dibaris kedua ganti jadi (0,1);
silahkan aja dicoba-coba.
kalau mau nampilin suatu nialai dari variabel semisal
unsigned int data;
—-
sprintf(lcd_buffer,”%u”,data);
lcd_puts(lcd_buffer);
——————————
ok, that’s all, simple kan
Nah masalah akan datang kalau nilai yang mau ditampilkan dari variabel data itu float atau desimal
Solusinya kita pakai fmod , termasuk dalam library math.h
saya dah baca di help-nya codevision tapi sungguh tidak help (he..he…), saya lebih ngerti pas liat contoh penggunaannya
intinya fmod akan menyimpan sisa pembagian, berikut contohnya :
misal saya mau nampilkan hasil perhitungan dari 8/3 ke LCD
a=8;
b=3;
c=a/b;
d=fmod(a,b);
e=d*10/b;
f=fmod(d,b);
g=f/b;
tampilkan ke lcd
sprintf(lcd_buffer,”hasilnya %u,%u%”,c,e,g);
lcd_puts(lcd_buffer);
jadi
c = angka pertama
e = angka pertama dibelakang koma
g = angka kedua dibelakang koma
maka di LCD akan tampil : hasilnya 2,66
Selamat mencoba
-
Archives
- July 2010 (1)
- May 2010 (1)
- March 2010 (4)
- February 2010 (4)
- July 2009 (1)
- March 2009 (4)
- February 2009 (2)
-
Categories
-
RSS
Entries RSS
Comments RSS






