Hiển thị các bài đăng có nhãn đếm sản phẩm. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn đếm sản phẩm. Hiển thị tất cả bài đăng
Đếm sản phẩm dùng phương pháp Poilling với button

Đếm sản phẩm dùng phương pháp Poilling với button

/*
 * File:   counter_SP.c
 * Author: PhanHoangThinh
 *
 * Created on Ngày 29 tháng 6 nam 2017, 21:21
 */


#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "lcd.h"
#define _XTAL_FREQ 4000000
// CONFIG1
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator: High-speed crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON       // RE3/MCLR pin function select bit (RE3/MCLR pin function is MCLR)
#pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = OFF      // Brown Out Reset Selection bits (BOR disabled)
#pragma config IESO = OFF       // Internal External Switchover bit (Internal/External Switchover mode is disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled)
#pragma config LVP = OFF        // Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming)

// CONFIG2
#pragma config BOR4V = BOR40V   // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
#pragma config WRT = OFF        // Flash Program Memory Self Write Enable bits (Write protection off)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
unsigned int dem=0;
void main(void) {
 
    ANSEL=0;
    ANSELH=0;
    TRISBbits.TRISB0=TRISBbits.TRISB1=1;
    nRBPU=0;
    WPUB0=WPUB1=1;
    lcd_init();
    lcd_gotoxy(1,0);
    printf("PRODUCT COUNTER");
    lcd_gotoxy(7,1);
    printf("%02d",dem);
    while(1)
    {
        if(RB0==0)
        {
            __delay_ms(100);
            while(RB0==0);
                __delay_ms(100);
                dem++;
                lcd_gotoxy(7,1);
                printf("%02d",dem);
                if(dem==99)
                {
                    dem=0;
                    lcd_gotoxy(7,1);
                    printf("%02d",dem);
                }
            }
        if(RB1==0)
        {
            __delay_ms(100);
            while(RB1==0);
            dem=0;
            lcd_gotoxy(7,1);
            printf("%02d",dem);
        }

        }

    }

Đếm sản phẩm TIMER 0



/*
 * File:   Sp_Timer0.c
 * Author: PhanHoangThinh
 *
 * Created on Ngày 08 tháng 7 n?m 2017, 18:30
 */


#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include "lcd.h"
#define _XTAL_FREQ 4000000
// CONFIG1
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator: High-speed crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON       // RE3/MCLR pin function select bit (RE3/MCLR pin function is MCLR)
#pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = OFF      // Brown Out Reset Selection bits (BOR disabled)
#pragma config IESO = OFF       // Internal External Switchover bit (Internal/External Switchover mode is disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled)
#pragma config LVP = OFF        // Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming)

// CONFIG2
#pragma config BOR4V = BOR40V   // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
#pragma config WRT = OFF        // Flash Program Memory Self Write Enable bits (Write protection off)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
unsigned char dem=0;
void TMR0_dem();
void main(void) {
    ANSEL=0;
    ANSELH=0;
   
    TRISA4=1;
    lcd_init();
    lcd_gotoxy(3,0);
    printf("Dem san pham");
    TMR0_dem();
//    while(1)
//    {
        dem=TMR0;
        if(TMR0IF==1)
        {
            TMR0IF=0;
            lcd_gotoxy(8,1);
            printf("%02d",dem);
        }
       
       
//    }
}
void TMR0_dem(){
    TMR0=0;
    TMR0IF=0;
    T0CS=1;
    GIE=PEIE=TMR0IE=0;
    PSA=1;
    T0SE=0;
}

Đếm sản phẩm dùng ngắt PORTB



/*
 * File:   interPortB.c
 * Author: PhanHoangThinh
 *
 * Created on Ngày 06 tháng 7 n?m 2017, 20:35
 */


#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include "lcd.h"
#define _XTAL_FREQ 4000000
// CONFIG1
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator: High-speed crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON       // RE3/MCLR pin function select bit (RE3/MCLR pin function is MCLR)
#pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = OFF      // Brown Out Reset Selection bits (BOR disabled)
#pragma config IESO = OFF       // Internal External Switchover bit (Internal/External Switchover mode is disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled)
#pragma config LVP = OFF        // Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming)

// CONFIG2
#pragma config BOR4V = BOR40V   // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
#pragma config WRT = OFF        // Flash Program Memory Self Write Enable bits (Write protection off)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
unsigned char dem1=0;
unsigned char dem2=0;
unsigned char dem3=0;
int lol=0;
void main(void) {
   
    ANSEL=0;
    ANSELH=0;
   
    TRISBbits.TRISB0=1;
    TRISBbits.TRISB1=1;
    TRISBbits.TRISB2=1;
   
    nRBPU=0;
    WPUB=0x07;
    RBIF=0;GIE=1;RBIE=1;
    INTEDG=1;
    IOCB=0x07;
    PORTB;
    lcd_init();
   
    lcd_gotoxy(2,0);
    printf("S1");
    lcd_gotoxy(2,1);
    printf("%02d",dem1);
    lcd_gotoxy(7,0);
    printf("S2");
    lcd_gotoxy(7,1);
    printf("%02d",dem2);
    lcd_gotoxy(12,0);
    printf("S3");
    lcd_gotoxy(12,1);
    printf("%02d",dem3);
    while(1)
    {
       
    }      
}

void interrupt NgatPortB()
{
    PORTB;
    RBIF=0;
        if(PORTBbits.RB0==1)
        {
            dem1++;
            lcd_gotoxy(2,1);
            printf("%02d",dem1);
            if(dem1==99) dem1=0;
        }
       
        if(PORTBbits.RB1==1)
        {
            dem2++;
            lcd_gotoxy(7,1);
            printf("%02d",dem2);
            if(dem2==99) dem2=0;
        }
        if(PORTBbits.RB2==1)
        {
            dem3++;
            lcd_gotoxy(12,1);
            printf("%02d",dem3);
            if(dem3==99) dem3=0;
        }
}

Đếm sản phẩm hiển tị lcd dùng ngắt ngoài INT



/*
 * File:   CounterSP_interrupt.c
 * Author: PhanHoangThinh
 *
 * Created on Ngày 06 tháng 7 n?m 2017, 19:17
 */
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include "lcd.h"
#define _XTAL_FREQ 4000000
// CONFIG1
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator: High-speed crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON       // RE3/MCLR pin function select bit (RE3/MCLR pin function is MCLR)
#pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = OFF      // Brown Out Reset Selection bits (BOR disabled)
#pragma config IESO = OFF       // Internal External Switchover bit (Internal/External Switchover mode is disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled)
#pragma config LVP = OFF        // Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming)

// CONFIG2
#pragma config BOR4V = BOR40V   // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
#pragma config WRT = OFF        // Flash Program Memory Self Write Enable bits (Write protection off)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
void ctrinhdem();
void xuathienthi();
void interrupt ngat();
int dem=0;
const char cgram_data[]=
                  {0x1E,0x11,0x11,0x1D,0x11,0x11,0x1E,0x00,
                  0x04,0x0C,0x0A,0x0E,0x11,0x1E,0x0E,0x00,      
                  0x06,0x04,0x0E,0x01,0x0F,0x11,0x0F,0x00,
                  0x03,0x06,0x04,0x0E,0x01,0x0F,0x11,0x0F,
                  0x99
                  };
void main(void) {
   
   
    ANSEL=0;
    ANSELH=0;
    TRISB0=1;
    nRBPU=0;
    WPUB0=1;
 
    GIE=1;
    INTE=1;
    INTEDG=1;
    INTF=0;
   
    unsigned char i;
     lcd_init();
    /*Kiem tra chuong trinh ket thuc*/
    /**********************************/
    i=0;
   lcd_put_byte(0,0x40);         // Lenh = 40H - Dat CGRAM co dia chi bat dau la 00H.
   while(lcd_busy());            // Kiem tra LCD bao ban.
   while(cgram_data[i]!=0x99)
   {
       lcd_put_byte(1,cgram_data[i]);
       while(lcd_busy());            // Kiem tra LCD bao ban.
       i++;
   }
   /*****************************************/
//   while(1)
//   {
       for(int j=0;j<50;j++)
        {
            lcd_gotoxy(3,0);
            lcd_puts("\1m s\2n ph\3m \n");
            lcd_gotoxy(2,0);
            lcd_putc(0);
        }
//   }
    lcd_gotoxy(8,1);
    printf("00",dem);
    while(1);
   
}
void interrupt ngat()
{
   
    if((INTF==1)&&(RB0==1)){
        __delay_ms(100);
        while(RB0==1);
        dem++;
        lcd_gotoxy(8,1);
        printf("%02d",dem);
        if(dem==99)
        {
          dem=0;
        }
       
    }
    INTF=0;
}