Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Learn programming microcontroller pic via the projects in mikroc language

Status
Not open for further replies.

Trung

New Member
1. Traffic light
I use the pic 16f84 to control three leds with three different color as the system traffic light. The diagram bellow help you see clearly:
dgt+do.jpg

Red light

dgt+vang.jpg

Yellow light

dgt+xanh.jpg

Blue light

* Code for projects
HTML:
/*Author: Huynh Minh Trung*/
unsigned int mask(unsigned short num) {
  switch (num) {
    case 0 : return 0xc0;
    case 1 : return 0xf9;
    case 2 : return 0xa4;
    case 3 : return 0xb0;
    case 4 : return 0x99;
    case 5 : return 0x92;
    case 6 : return 0x82;
    case 7 : return 0xf8;
    case 8 : return 0x80;
    case 9 : return 0x90;
  } //case end
}//~
int digit_no, digit10, digit1, digit, i;

void interrupt() {
  if (digit_no==0) {
    PORTA.F3 = 0;          // Turn off all 7seg displays
    PORTB = digit1;     //  send mask for ones digit to PORTD
    PORTA.F3 = 1;          //  turn on 1st 7 seg., turn off 2nd
    digit_no = 1;
  } else {
    PORTA.F4 = 0;          // Turn off all 7seg displays
    PORTB = digit10;    //  send mask for tens digit to PORTD
    PORTA.F4 = 1;          //  turn on 2nd 7 seg., turn off 1st
    digit_no = 0;
    }
  TMR0 = 0;             //  clear TMRO
  INTCON = 0x20;        //  clear TMR0IF and set TMR0IE
}
 void hienthi(){
      digit   = i % 10u;
      digit1  = mask(digit);           // prepare ones digit
      digit   = (char)(i / 10u) % 10u;
      digit10 = mask(digit);           // prepare tens digit
      }
void main(){
     OPTION_REG  = 0x80;   // Timer0 settings
     TMR0        =   0;
     INTCON      = 0xA0;   // Disable PEIE,INTE,RBIE,T0IE
     TRISA=0X00;
     PORTA=0;
     TRISB=0X00;
     PORTB=0;

do{
     PORTA=0;
     i=10;
     for(i;i>=0;i--){
                       hienthi();
                       PORTA.F0=1;
                       delay_ms(1000);
                     }
     PORTA=0;
     i=3;
     for(i;i>=0;i--){
                     hienthi();
                     PORTA.F1=1;
                     delay_ms(1000);
                     }
     PORTA=0;
     i=20;
     for(i;i>=0;i--){
                     hienthi();
                     PORTA.F2=1;
                     delay_ms(1000);
                     }

         }while(1);
}
 
2. System counting produce, display on 7 segment led
In this project, i use microcontroller pic 16f877A and four digit 7 segment to display result. You see diagram circuit bellow:
mach%2Bdem%2Bsan%2Bpham%2B7seg%2B-%2Beeprom.jpg


The picture above, I use a button "GIA LAP CAM BIEN" as a sensor. This sensor, You can use IR or laser.
Video:

Code for Project:
HTML:
/*
Project: mach dem san pham hien thi tren led 7 doan
Cmu: 16f877a
Description: Trong project ta su dung 1 button tren Rb0 de gia lap cam bien, khi
co mot san pham di qua cam bien tuong ung nhu button duoc an xuong, muc logic o port rb0
o muc thap, gia tri dem tang 1 don vi. Ngoai ra ta con co mot button reset lai gia tri
dem ve 0. Cac gia tri so dem se duoc ghi vao eeprom, khi co khoi dong lai he thong cung
khong so bi mat du lieu cu.
Author: http://dientudieukhien.net
*/
unsigned short mask(unsigned short num) {
  switch (num) {
    case 0 : return 0xC0;
    case 1 : return 0xF9;
    case 2 : return 0xA4;
    case 3 : return 0xB0;
    case 4 : return 0x99;
    case 5 : return 0x92;
    case 6 : return 0x82;
    case 7 : return 0xF8;
    case 8 : return 0x80;
    case 9 : return 0x90;
  } //case end
}
unsigned short digit_no, digit10, digit1, digit;
void interrupt() {
  if (digit_no==0) {
    PORTB.F7=0;
    PORTB.F6 = 0;          // Turn off all 7seg displays
    PORTD = digit1;     //  send mask for ones digit to PORTD
    PORTB.F7 = 1;          //  turn on 1st 7 seg., turn off 2nd
    digit_no = 1;
  } else {
    PORTB.F6=0;
    PORTB.F7 = 0;          // Turn off all 7seg displays
    PORTD = digit10;    //  send mask for tens digit to PORTD
    PORTB.F6 = 1;          //  turn on 2nd 7 seg., turn off 1st
    digit_no = 0;
    }
  TMR0 = 0;             //  clear TMRO
  INTCON = 0x20;        //  clear TMR0IF and set TMR0IE
}

void display(unsigned short i){
unsigned short digit;
digit=i%10;
digit1=mask(digit);           //hien thi hang don vi
digit=i/10;
digit10=mask(digit);          // hien thi hang chuc
delay_ms(500);
}

void main() {
unsigned short i;
  OPTION_REG  = 0x80;   // Timer0 settings
  TMR0        =   0;
  INTCON      = 0xA0;   // Disable PEIE,INTE,RBIE,T0IE
trisb=0x03;
trisd=0x00;
TRISA=0X00;
portb=0;
portd=0;
i=EEPROM_Read(5);   //Doc gia tri tu eeprom
display(i);         //Hien thi gia tri so dem ra led 7 doan
for(;;){            //Vong lap forever
lap:
i=EEPROM_Read(5);   //Doc gia tri moi nhat tu eeprom
if(Button(&portb,0,1,0)){     //Neu port b0 duoc chuyen tu muc logic 1->0
i=i++;                        //Tang gia tri dem 1 don vi
display(i);                   //Hien thi gia tri dem ra led 7 doan
}
if(i>0){
if(Button(&portb,1,1,0)){     //Neu port b1 duoc chuyen tu muc logic 1->0
i=0;                          //Reset gia tri dem
display(i);                   //Hien thi ra led 7 doan
        }
EEPROM_Write(5,i);            //Cap nhat gia tri moi vao eeprom
Delay_ms(100);
      }  goto lap;
     }
}

Hex file:
HTML:
:0A00000008290000000000005A2942
:10000A002628C03083120313F0000800F930F000EC
:10001A000800A430F0000800B030F0000800993061
:10002A00F00008009230F00008008230F00008006A
:10003A00F830F00008008030F00008009030F0003E
:10004A000800831203132F08003A031906282F0801
:10005A00013A03190B282F08023A03190E282F0810
:10006A00033A031911282F08043A031914282F08F0
:10007A00053A031917282F08063A03191A282F08D0
:10008A00073A03191D282F08083A031920282F08B0
:08009A00093A031923280800AC
:0E00A2008312031323088A002208820008003C
:0E00B000A530FD00FD0B5A28000000000800DE
:1000BE0083120313F8010830FC00700DF80D74085C
:1000CE00F80203186C28F8070310F00DFC0B6428D7
:0200DE00080018
:1000E00051208A110A128000840AA20A0319A30A65
:0800F000F003031D7028080055
:1000F800831603178C1C822800007C2883120313A4
:10010800290803178D0003132A0803178C00831688
:100118008C130C150B08F0008B1355308D00AA308A
:100128008D008C14F01B9A288B139B288B170C11AD
:020138000800BD
:10013A0083120313290803178D0083168C130C14DA
:08014A0083120C08F00008000C
:1001520083120313AD012A08F1000130F000710887
:100162000319B728F00D7010FF3EB1287008AE00D9
:10017200290884000008F00570080130031D0030D2
:10018200F0002C08F0060319DE282B08003C0318A7
:10019200CE2858205820AB03C628290884002E08F0
:1001A2000005F00070080130031D0030F0002C083B
:1001B200F0060319DE28FF30AD002D08F00008001C
:1001C2000A3083120313F4002908F0005F20780834
:1001D200F0007008AF0005207008A4000A30F40097
:1001E2002908F0005F207008AF0005207008A50004
:1001F2000330FB008A30FC005530FD00FD0BFF2868
:0E020200FC0BFF28FB0BFF280000000008008B
:10021000803083160313810083128101A0308B008C
:1002200003308316860088018501831286018801C8
:100230000530A9009D2070080313A8007008A900CC
:10024000E120831203130530A9009D2070080313D9
:10025000A8000630A900AA010130AB00AC01A9201A
:10026000700803193729A80A2808A900E1202808DE
:10027000003C031858290630A9000130AA000130BB
:10028000AB00AC01A920700803194929A801A901F4
:10029000E1200530A9002808AA007C208230FC005B
:1002A000DD30FD00FD0B5229FC0B5229000000003F
:0402B000212959297E
:1002B400FF00030E8301A6000A08A7008A01700844
:1002C400A0002108003A031D6F2986130613240891
:1002D400880086170130A1007529061386132508A6
:1002E40088000617A101810120308B002008F0004E
:0E02F40027088A00260E8300FF0E7F0E0900E9
:02400E004A2F37
:00000001FF
 
Last edited:
Status
Not open for further replies.

Latest threads

Back
Top