TMR0 as a counter, declaring new variables, enumerated constants, using relay ...

Referring to the previous examples, the microcontroller  executes the program without being affected in any way by its surrounding. Practically, devices operating in this manner are very rare  (for example,  simple  neon sign controller).  Input  pins are also used in this example. There is a schematic  in figure below, while the program  is on the next page. It’s still very simple. Timer TMR0 is used as a counter. The counter input is connected to a push button so that any button  press  causes  timer  TMR0 to count one pulse.  When  the number  of pulses matches the number stored in the TEST register, a logic one (5V) appears on the pin PORTD.3. This  voltage  activates  an  electromechanical   relay,  and  this  bit  is  called  ‘RELAY’  in  the program, therefore.

In this example, the TEST register stores number 5. Of course, it can be any number obtained either by computing or defined as a constant. Besides, the microcontroller  can activate some other device instead of relay, while the sensor can be used instead of the push button. This example  illustrates  one  of  the  most  common  applications  of  the  microcontroller   in  the industry; when something is performed as many times as needed, then something else should
be turned on or off....

Only one enumerated  constant  RELAY  is used in this example.  It is assigned  a value when
declared.

All the  constants,  following  those  with  assigned  values  (RELAY  = 3 and  MOTOR  = 6),  are automatically  assigned  values incremented  by 1 relative  to the previous  constant  value. In this  example,  constants  HEATER  and  PUMP  will  be  assigned  values  4 and  7,  respectively (HEATER = 4 and PUMP = 7).
Read more

Using assembly instructions and internal oscillator LFINTOSC...

This is actually  a sequel  to the previous  example,  but deals  with a bit more  complicated problem...  The idea is to make  LED diodes  on the port B blink slowly.  It can be done by setting large value for delay parameter in the Delay function. But there is also another, more efficient  manner to make this happen.  You remember  that this microcontroller  has built-in oscillator  LFINTOSC  which operates  at the frequency  of 31kHz? Now, it’s time to ‘give it a chance’.

The  program  starts  with  the do-while loop  and  remains  there  for  20  cycles.  After  each iteration,  100mS delay is provided,  which is reflected  as relatively  fast LED blinking of the port  B.  When  the  program  exits  this  loop,  the  microcontroller  starts  using  the  LFINTOSC oscillator as a clock signal source. The LED blinking is considerably  slower now even though the program executes the same do-whileloop with 10 times shorter delay.

For the purpose of making some potentionally dangerous situation more obvious here, control bits are activated by assembly instructions. Simply put, when entering or exiting the assembly instruction  in the program,  the compiler  doesn’t  save data on currently  active  RAM bank, which means that in this program section, bank selection depends on the SFR registers in use. When switching back to the program section written in C, the control bits RP0 and RP1 must return the state they had before ‘assembly language adventure’. In this program, the problem is solved by using the saveBank auxiliary variable which saves the state of these two bits.



You have noticed that the clock signal source is changed ‘on the fly’. If you want to make sure   of   it,   remove   quartz   crystal   prior   to   switching   the   microcontroller    on.   The microcontroller  will not start to operate  because  the Config Word loaded with the program requires the use of crystal on switching on. If you remove the crystal later during operation, nothing will happen, it will not affect the microcontroller at all.

Read more

Writing header, configuring I/O pins, using delay function and switch operator

The only purpose of this program is to turn on a few LED diodes on port B. Anyway, use this example to study what a real program looks like. Figure below shows connection  schematic, while the program is on the next page.


When switching on, every other LED diode on the port B emits light, which indicates that the microcontroller is properly connected and operates normally.

This  example  describes  a  correctly  written  header.  It’s  the  same  for  all  the  programs described  in  this  book.  To  skip  repetitiveness,   it  will  not  be  written  in  the  following examples, but is considered to be at the beginning of every program and marked as ‘Header’.



To make this example more interesting, we will enable LEDs connected to the port B to blink. There are several ways to do it:

1.               As soon as the microcontroller  is turned  on, all LEDs will emit light for a second.
The Delay function is in charge of it in the program. It’s only needed to set delay expressed in milliseconds.
2.               After one second, the program enters the for loop and remains there as long as the variable  k is less  than  20. The  variable  is incremented  by 1 after  each  iteration.  Within the for loop,  the switch operation  monitors  port  B logic  state.  If PORTB=0xFF,  its state  is inverted into 0x00 and vice versa. Any change of these logic states causes all LEDs to blink. Duty Cycle is 5:1 (500mS:100mS).
3.               When the program exits the for loop, the port B logic state changes (0xb 01010101) and the program enters the endless whileloop and remains there as long as 1=1. The port B logic state is inverted each 200mS.



Read more

Modul 4





INTERFACE SENSOR SUHU LM35 MENGGUNAKAN PIC 16F877A



1. Tujuan [kembali]


Mempelajari bagaimana cara merangkai suatu Sensor Suhu (LM35) menggunakan mikrokontroler PIC dengan programnya.


2. Alat [kembali]




2.1 Mikrokontroler PIC16F877A.



2.2 LCD



2.3 Sensor Suhu LM35

3. Teori [kembali]


3.1. Pengertian Mikrokontroler PIC 16F877A

PIC yang merupakan produk dari Microchip Technology, merupakan kepanjangan dari Peripheral Interface Controller memiliki arsitektur Harvard dan merupakan jenis RISC (Reduced Instruction Set Computing). Salah satu jenis mikrokontroler ini adalah PIC16F877A.

3.1.1 Fitur-Fitur PIC16F877A.

a. Memiliki Instruksi sebanyak 35 buah.
b. Masing-masing instruksi dieksekusi dalam satu siklus mesin kecuali untuk instruksi percabangan yaitu dua siklus.
c. Kecepatan operasi masukan clock dari DC hingga 20MHz.
d. Kapasitas memori program berukuran 8k x 14 words.
e. Kapasitas RAM 268 byte.
f. Kapasitas memory EEPROM berukuran 256 byte.

3.1.2 Pin Out PIC16F877A.


-Memiliki 5 PORT I/O (PORTA 6 pin, PORTB 8 pin, PORTC 8 pin, PORTD 8 pin, PORTE 3 pin).
-Memiliki 3 buat timer
-Memiliki 2 buah keluaran PWM.
-Komunikasi serial singkron menggunakan SPI dan I2C.
-Komunikasi USART.

3.1.3 Pemograman PIC16F877A.


Pemograman PIC ini diprogram menggunakan bahasa C dan dapat menggunakan kompiler MikroC for PIC yang dapat didownload di sis resminya.
tu

3.1.4 Rangkaian Dasar Sistem Mikrokontroler PIC16F877A.




Di gambar dapat dilihat bahwa rangkaian dasar dari sistim mikrokontroler biasanya menggunakan rangkaian crystal sebagai clock atau oscilator eksternal sebagai jantung untuk memberikan kecepatan eksekusi dari mikrokontroler tersebut. Pada gambar juga terdapat rangkaian reset yang digunakan untuk memulai dari awal kembali program yang kita jalankan pada mikrokontroler ini.

3.2. Pengertian Sensor Suhu LM35

Sensor suhu IC LM 35 merupkan chip IC produksi Natioanal Semiconductor yang berfungsi untuk mengetahui temperature suatu objek atau ruangan dalam bentuk besaran elektrik, atau dapat juga di definisikan sebagai komponen elektronika yang berfungsi untuk mengubah perubahan temperature yang diterima dalam perubahan besaran elektrik. Sensor suhu IC LM35 dapat mengubah perubahan temperature menjadi perubahan tegangan pada bagian outputnya. Sensor suhu IC LM35 membutuhkan sumber tegangan DC +5 volt dan konsumsi arus DC sebesar 60 µA dalam beroperasi.



Karakteristik Sensor suhu IC LM35 adalah :

1. Memiliki sensitivitas suhu, dengan faktor skala linier antara tegangan dan suhu 10 mVolt/ºC, sehingga dapat dikalibrasi langsung dalam celcius.
2. Memiliki ketepatan atau akurasi kalibrasi yaitu 0,5ºC pada suhu 25 ºC
3. Memiliki jangkauan maksimal operasi suhu antara -55 ºC sampai +150 ºC.
4. Bekerja pada tegangan 4 sampai 30 volt.
5. Memiliki arus rendah yaitu kurang dari 60 µA.
6. Memiliki pemanasan sendiri yang rendah (low-heating) yaitu kurang dari 0,1 ºC pada udara diam.
7. Memiliki impedansi keluaran yang rendah yaitu 0,1 W untuk beban 1 mA.
8. Memiliki ketidaklinieran hanya sekitar ± ¼ ºC.

3.3. Pengertian ADC pada PIC 16F877A

Untuk Mengkonversi data analog yang diinputkan oleh sensor digunakan rangkaian ADC PIC. Rangkaian ADC ini sudah dipelajari di Interface ADC Menggunakan PIC 16F877A

3.3. Pengertian LCD

LCD disini digunakan untuk menampilkan berapa suhu yang diukur oleh sensor. Teori LCD sudah dijelaskan pada Interface LCD Menggunakan PIC 16F877A

4. Rangkaian [kembali]




Rangkaian diatas merupakan rangkaian sederhana dari pengunaan PIC yang dihubungkan dengan sensor suhu. Sensor suhu LM35 dihubungkan dengan sumber tegangan dan ouput dari sensor dimasukkan ke pin Analog dari PIC. kemudian data ini dikonversikan oleh PIC dan kemudian ditampilkan besarapa suhu terukur oleh sensor pada layar LCD.


5. Program [kembali]

// LCD module connections
sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;

sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections

int t;                                  // deklarasi variabel t
char a;                                 // deklarasi variabel a
char lcd[] = "111 Degree";              // deklarasi array lcd dan diisi

void main()
{
ADCON0.ADCS0=0;                                  // ADCS0, ADCS1 & ADCS2 bersama sama menentukan kecepatan clock
ADCON0.ADCS1=1;                                  // jika 0 1 1 maka kecepatannya  Fosc/64
ADCON1.ADCS2=1;
ADCON0.CHS0=0;                                   // CHS0, CHS1 & CHS2 bersama-sama menentukan pin
ADCON0.CHS1=0;                                   // jika 0 0 0  maka pin yang digunakan adalah RA0/AN0
ADCON0.CHS2=0;
ADCON0.ADON=1;                                   // mengaktifkan konverter A/D
ADCON1.ADFM=1;                                   // menyatakan 10 bit terdiri dari 8 bit ADRESL dan 2 bit ADRESH
ADCON1.PCFG0=0;                                  // PCFG0, PCFG1, PCFG2 & PCFG3 bersama sama menetukan
ADCON1.PCFG1=0;                                  // apakah pin digunakan sebagai input analog atau input digital
ADCON1.PCFG2=0;                                  // jika 0000 maka semua pin sebagai input Analog
ADCON1.PCFG3=0;

  Lcd_Init();                                    // inisialisasi LCD
  Lcd_Cmd(_LCD_CURSOR_OFF);                      // matikan cursor LCD

  do
  {
    Lcd_Cmd(_LCD_CLEAR);                         // bersihkan layar LCD
    Lcd_out(1,1, "Temperature:");                // cetak "Temperature" di layar LCD
    t = ADC_Read(0);                             // mendapatkan nilai input analog

    t = t * 0.4887;                              // menyesuaikan nilai derjat dengan input analog
    a = t%10;                                    // mendapatkan nilai satuan suhu
    lcd[2] = a + '0';                            // mengganti nilai satuan array

    t = t/10;
    a = t%10;                                    // mendapatkan nilai puluhan suhu
    lcd[1] = a + '0';                            // mengganti nilai puluhan array

    t = t/10;
    a = t%10;                                    // mendapatkan nilai ratusan suhu
    lcd[0] = a + '0';                            // mengganti nilai ratusan array

    Lcd_out(2,1,lcd);                            // mencetak nilai suhu pada layar LCD
    Delay_ms(100);                               // delay 100 ms
  }while(1);                                     // looping

}

6. Flowchart [kembali]

7. Video Simulasi [kembali]





           


          

Read more