Hiển thị các bài đăng có nhãn lap trình C. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn lap trình C. Hiển thị tất cả bài đăng

Điều khiển động cơ DC thông qua module ADC kích từ Biến trở(thay đổi tốc độ động cơ)

Mô phỏng :

CODE : dùng MPLAB XC8 .

#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <htc.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 ADC_init(void)
{
    TRISA3=1;
    ANS3=1;
    ADCS1=0;ADCS0=1;
    VCFG1=0;VCFG0=0;
    CHS3=0;CHS2=0;CHS1=1;CHS0=1;
    ADFM=1;
    ADON=1;
    ADIF=0;
    GIE=PEIE=ADIE=0;
}

unsigned int read_ADC(void)
{
    __delay_us(6);
    GO=1;
    while(GO==1);
    return(((unsigned int )ADRESH)<<8)|ADRESL;
}
unsigned int val_adc, volt=0,a,i,j,k;
void CCP1_init(unsigned char i) // quay nhanh
{
    val_adc=read_ADC();     //doc gia tri adc
    a=round((float)0.09775171*val_adc);
    TRISC2=1; // su dung chan CCP1
    PR2=124;
    CCP1M3=CCP1M2=1; CCP1M1=CCP1M0=0;
    CCPR1L=(a*125)/100;// chon PreT2=4 => Tpwm=500; i la D : chu ki hien tai cua xung PWM
    k=((a*500)/100)%4 ;// k la CCP1CON<5:4>
    if(k==0){ DC1B1=DC1B0=0;}
    else
        if(k==1){ DC1B1=0; DC1B0=1;}
        else
            if(k==2){ DC1B1=1; DC1B0=0;}
            else
            {DC1B1=DC1B0=1;}
    TMR2IF=0;// xoa co bao ngat
    T2CKPS1=0;T2CKPS0=0;
    TMR2ON=1;// cho phep Timer 2 bat dau hoat dong
    while(TMR2IF==0);
    TRISC2=0;
    TRISC1=0;// ?
    RC1=0;
}
void CCP1d_init(unsigned char i)  // quay cham
{
    val_adc=read_ADC();     //doc gia tri adc
    a=round((float)0.09775171*val_adc);
    TRISC2=1;
    PR2=124;
    CCP1M3=CCP1M2=1; CCP1M1=CCP1M0=0;
    CCPR1L=((100-a)*125)/100;
    k=(((100-a)*500)/100)%4;
    if(k==0){ DC1B1=DC1B0=0;}
    else
        if(k==1){ DC1B1=0; DC1B0=1;}
        else
            if(k==2){ DC1B1=1; DC1B0=0;}
            else
            {DC1B1=DC1B0=1;}
    TMR2IF=0;
    T2CKPS1=0;T2CKPS0=0;
    TMR2ON=1;
    while(TMR2IF==0);
    TRISC2=0;
    TRISC1=0;
    RC1=1;
}

void main(void)
{
 
    ANSEL=0x08; ANSELH=0x00;
    TRISA3=1;
    lcd_init();
    ADC_init();
    TRISB0=TRISB1=TRISB2=1;
    nRBPU=0;WPUB0=WPUB1=WPUB2=1;
    lcd_init();
    i=0;
    CCP1_init(i);
    TRISE0=TRISA4=0;
    RE0=1;
    RA4=0;
    while(1)
    {
    val_adc=read_ADC();     //doc gia tri adc
    volt=round((float)0.09775171*val_adc);  
    lcd_gotoxy(1,0);
    printf("Duty Cyc=%3d",volt);
    lcd_gotoxy(13,0);
    lcd_putc(0x25);
    if(RB0==0){
        __delay_ms(40);
        while(RB0==0);
        __delay_ms(40);
        RE0=1;
     
    }
    if(RB1==0){
        __delay_ms(40);
        while(RB1==0);
        __delay_ms(40);
        CCP1CON=0x00;
        CCP2CON=0x00;
        RE0=0;
    }
    if(RB2==0)
    {
        __delay_ms(40);
        while(RB2==0);
        __delay_ms(40);
        RA4=!RA4;
    }
    if(RA4==0){
        lcd_gotoxy(1,1);
        printf("Rotation=FOR");
        CCP1_init(i);
    }
    else{
        lcd_gotoxy(1,1);
        printf("Rotation=REV");
        CCP1d_init(i);
    }
    }
}