07-06-2012, 08:52 PM
|
#2
|
Nhập môn đệ tử
Tham gia ngày: Jun 2012
Bài gửi: 4
:
|
Trích:
Nguyên văn bởi vuong_003
Mọi người cho mình hỏi sao mỗi lần mình bấm nút tăng tốc độ động cơ thì 2 led gắn ở 2 chân RE1 va RE2 sáng loạn cả lên, 2 led đó mình dùng nút nhấn ở chân RA2 va RA3 để điều khiển sáng tắt. mình dùng L298 để điều khiển động cơ, chân Enable của L298 nối với chân RE0 của VDK, động cơ chạy k0 ổn định nữan mỗi lần nhấn nút là mình cho biến tốc độ lên 1, vậy mà khi chạy có khi mình ấn có 1 lần mà tốc độ tự tăng 2 nấc
Đây là code mình viết:
//-----------------------------------------------------------------------------
#include <16f877a.h>
#include <def_877a.h>
#device *=16 ADC=8
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=4000000)
#define tocdo0 0 //toc do bang 0
#define td_macdinh 80 //toc do mac dinh
#define tocdo1 124 //1/5 toc do cuc dai
#define tocdo2 248 //2/5 toc do cuc dai
#define tocdo3 372 //3/5 toc do cuc dai
int16 duty1=0;
int16 duty2=0;
int8 bientocdo1=0;
int8 bientocdo2=0;
int1 bienthuan=0;
int1 biennghich=0;
void main()
{
set_tris_e(0b000); //các chan cu?a porte deu la chan xuat du lieu
set_tris_a(0b00111100);
PORTE=0;
setup_ccp1(CCP_PWM); //khoi tao bo PWM1
setup_ccp2(CCP_PWM); //khoi tao bo PWM2
setup_timer_2(T2_div_by_16,154,1); //tao chu ky xung 1ms
//(1/clock)*4*t2div*(period+1)
//(1/10e6)*4*16*(154+1)=1e-3=1ms
set_pwm1_duty(duty1); //gia tri duty cua PWM duoc tinh
set_pwm2_duty(duty2); //theo cong thuc duty1*t2div/clock
//khoi tao duty=0 ->tat dong co
while(true)
{
if (bit_test(porta,2))
{
Delay_ms(200);
RE1^=1;
}
if (bit_test(porta,3))
{
Delay_ms(200);
RE2^=1;
}
if (bit_test(porta,4)) //neu nhan phim FO : chay thuan
{
RE0=1;
duty1=tocdo0;
duty2=tocdo0;
set_pwm1_duty(duty1); //cho duty cua 2 bo PMW=0 de
set_pwm2_duty(duty2); //tat dong co
delay_ms(500); //delay 1s de giam quan tinh dc
bienthuan=1;
biennghich=0;
bientocdo1=bientocdo1+1;
if(bientocdo1==4)
bientocdo1=0;
goto next;
}
if(bit_test(porta,5)) //neu nhan phim RE : chay nghich
{
RE0=1;
duty1=tocdo0;
duty2=tocdo0;
set_pwm1_duty(duty1); //cho duty cua 2 bo PMW=0 de
set_pwm2_duty(duty2); //tat dong co
delay_ms(500); //delay 1s de giam quan tinh dc
bienthuan=0;
biennghich=1;
bientocdo2=bientocdo2+1;
if(bientocdo2==4)
bientocdo2=0;
goto next;
}
next:
if (bienthuan)
{
duty2 = tocdo0;
switch (bientocdo1)
{
case 0: duty1= tocdo0;
break;
case 1: duty1= tocdo1;
break;
case 2: duty1= tocdo2;
break;
case 3: duty1= tocdo3;
break;
}
set_pwm1_duty(duty1); //cho dco chay thuan
set_pwm2_duty(duty2);
}
if (biennghich)
{
duty1 = tocdo0;
switch (bientocdo2)
{
case 0: duty2= tocdo0;
break;
case 1: duty2= tocdo1;
break;
case 2: duty2= tocdo2;
break;
case 3: duty2= tocdo3;
break;
}
set_pwm1_duty(duty1); //cho dco chay nghich
set_pwm2_duty(duty2);
}
}
}
//================================================== =============================
|
Ban phai Debounce may cai switch buttons day la chuong trinh vi du:
Code:
// configuration bits
#pragma config FOSC = INTIO67
#pragma config WDTEN = OFF, LVP = OFF
#include "switch input.h"
/** V A R I A B L E S *************************************************/
#pragma udata // declare statically allocated uinitialized variables
unsigned char LED_Display; // 8-bit variable
/** D E C L A R A T I O N S *******************************************/
#pragma code// declare executable instructions
void main (void)
{
unsigned char Switch_Count = 0;
LED_Display = 1; // initialize
TRISD = 0b00000000; // PORTD bits 7:0 are all outputs (0)
INTCON2bits.RBPU = 0; // enable PORTB internal pullups
WPUBbits.WPUB0 = 1; // enable pull up on RB0
ANSELH = 0x00; // AN8-12 are digital inputs (AN12 on RB0)
TRISBbits.TRISB0 = 1; // PORTB bit 0 (connected to switch) is input (1)
while (1)
{
LATD = LED_Display; // output LED_Display value to PORTD LEDs
LED_Display <<= 1; // rotate display by 1
if (LED_Display == 0)
LED_Display = 1;
// rotated bit out, so set bit 0
while (Switch_Pin != 1);// wait for switch to be released
Switch_Count = 5;
do
{ // monitor switch input for 5 lows in a row to debounce if
if(Switch_Pin == 0)
{ // pressed state detected
Switch_Count++;
}
else
{
Switch_Count = 0;
}
Delay10TCYx(25); // delay 250 cycles or 1ms.
}while(Switch_Count< DetectsInARow);
};
}
thay đổi nội dung bởi: scorpionfirevn, 07-06-2012 lúc 11:38 PM.
|
|
|